hdwallet

package module
v0.0.0-...-d834250 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2025 License: MIT Imports: 7 Imported by: 0

README

Crypto Wallet Generator

Go library for generating cryptocurrency wallets using BIP39 and BIP44 standards

Installation

go get github.com/not-for-prod/hdwallet

Dependencies

"github.com/tyler-smith/go-bip39"
"github.com/tyler-smith/go-bip32"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/btcsuite/btcd/btcutil/base58"
"golang.org/x/hdwallet/sha3"

Usage Example

// Generate a 12-word mnemonic (128 bits entropy)
mnemonic, err := hdwallet.GenerateMnemonic(128)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Mnemonic: %s\n", mnemonic)

// Generate TRON keys from mnemonic
privateKey, publicKey, err := hdwallet.GenerateKeysFromMnemonic(
    mnemonic,
    cointype.Tron, // TRON coin type
    0,             // First account
    0,             // External chain (receiving addresses)
    0,             // First address
)
if err != nil {
    log.Fatal(err)
}

// Generate TRON address
address := hdwallet.GenerateTronAddress(publicKey)
fmt.Printf("TRON Address: %s\n", address)
fmt.Printf("Private Key: %x\n", privateKey.Serialize())
fmt.Printf("Public Key: %x\n", publicKey.SerializeCompressed())

Supported Cryptocurrencies

Currently supported coin types:

Cryptocurrency Coin Type Constant
TRON 195 cointype.Tron

Note: The library will be extended to support additional cryptocurrencies by adding coin type constants and address generation functions.

Acknowledgments

  • BIP39 - Mnemonic code for generating deterministic keys
  • BIP32 - Hierarchical deterministic wallets
  • BIP44 - Multi-Account Hierarchy for Deterministic Wallets
  • SLIP-0044 - Registered coin types for BIP44

Disclaimer

This library is for educational and development purposes. Always conduct thorough security audits before using in production environments with real funds. The authors are not responsible for any loss of funds due to security vulnerabilities or misuse of this library.

Documentation

Index

Constants

View Source
const (
	HardenedOffset uint32 = 0x80000000
	Purpose        uint32 = 44
)

Variables

This section is empty.

Functions

func DeriveKeyFromPath

func DeriveKeyFromPath(masterKey *bip32.Key, coin, account, chain, address uint32) (*bip32.Key, error)

DeriveKeyFromPath derives a private key from a master key using BIP44 hierarchical deterministic derivation BIP44 defines a specific derivation path structure: m/purpose'/coin_type'/account'/change/address_index Where: - purpose: Always 44' (0x8000002C) for BIP44 compliance - coin_type: Registered coin type (e.g., 0' for Bitcoin, 60' for Ethereum) - account: Account index starting from 0' (allows multiple accounts per coin) - change: 0 for external chain (receiving addresses), 1 for internal chain (change addresses) - address_index: Address index starting from 0 (sequential address generation)

The apostrophe (') indicates hardened derivation (adds 0x80000000 to the index) Hardened derivation provides additional security by making it impossible to derive the parent private key from a child private key and parent public key

func GenerateKeysFromMnemonic

func GenerateKeysFromMnemonic(mnemonic string, coin, account, chain, address uint32) (*secp256k1.PrivateKey,
	*secp256k1.PublicKey, error)

GenerateKeysFromMnemonic converts a BIP39 mnemonic phrase into secp256k1 private/public key pair with flexible BIP44 derivation path parameters

This is a more flexible version that allows specifying custom derivation paths rather than hardcoded values, enabling support for: - Multiple cryptocurrencies (Bitcoin, Ethereum, Tron, etc.) - Multiple accounts per coin - Both receiving and change addresses - Any address index in the sequence

Parameters: - mnemonic: BIP39 mnemonic phrase (12, 15, 18, 21, or 24 words) - coin: Coin type from SLIP-0044 registry (e.g., 0 for Bitcoin, 60 for Ethereum, 195 for Tron) - account: Account index (usually 0 for first account) - chain: 0 for external chain (receiving), 1 for internal chain (change) - address: Address index (0, 1, 2, ... for sequential addresses)

func GenerateMnemonic

func GenerateMnemonic(bitSize int) (string, error)

GenerateMnemonic creates a new BIP39 mnemonic phrase for wallet seed generation BIP39 defines a method for generating deterministic wallets using human-readable words The mnemonic acts as a master seed that can regenerate all wallet keys deterministically

Security considerations: - The entropy must be cryptographically secure and truly random - Mnemonic phrases should be stored securely and never transmitted over insecure channels - Loss of the mnemonic means permanent loss of wallet access - The mnemonic can regenerate the entire wallet hierarchy across different applications

func GenerateTronAddress

func GenerateTronAddress(publicKey *secp256k1.PublicKey) string

GenerateTronAddress generates a TRON address from a secp256k1 public key TRON addresses are similar to Ethereum addresses but use a different prefix and encoding The process follows these steps: 1. Extract uncompressed public key coordinates 2. Hash using Keccak-256 (same as Ethereum) 3. Take last 20 bytes and add TRON prefix (0x41) 4. Add double SHA-256 checksum 5. Encode in Base58 format

TRON addresses always start with 'T' when encoded and are 34 characters long Example: TLsV52sRDL79HXGGm9yzwKibb6BeruhUzy

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL