hdkey

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 12 Imported by: 0

README

hdkey

Hdkey Gen for Bitcoin and Ethereum

Generate New HD Wallet

km, err := hdkey.NewKeyManager(mnemonic, password)
if err != nil {
  log.Fatal(err)
}
masterKey, err := km.GetMasterKey()
if err != nil {
  log.Fatal(err)
}
passphrase := km.Passphrase
if passphrase == "" {
  passphrase = "<none>"
}
fmt.Printf("\n%-18s %s\n", "BIP39 Mnemonic:", km.Mnemonic)
fmt.Printf("%-18s %s\n", "BIP39 Passphrase:", passphrase)
fmt.Printf("%-18s %x\n", "BIP39 Seed:", km.GetSeed())
fmt.Printf("%-18s %s\n", "BIP32 Root Key:", masterKey.B58Serialize())
Get Address and WIF PrivKey
// Purpose Version, CoinType
key, err := km.GetKey(hdkey.PurposeBIP44, hdkey.CoinTypeBTC, 0, 0, uint32(i))
if err != nil {
  log.Fatal(err)
}

// Address and WIF
wif, address, _, _, _, err := key.Calculate(compress)
if err != nil {
  log.Fatal(err)
}

Thanks

Source code is from: https://github.com/modood/hdkeygen/blob/master/main.go, but the original version can only used as standalone software.

I separated the core functions and main func

Documentation

Index

Constants

View Source
const (
	Apostrophe uint32 = 0x80000000 // 0'
)

Variables

This section is empty.

Functions

func CalculateFromPrivateKey

func CalculateFromPrivateKey(prvKey *btcec.PrivateKey, compress bool, networkParams *chaincfg.Params) (wif, serializedPubKeyHex, address, segwitBech32, segwitNested, taproot string, err error)

func CalculateFromWif added in v0.0.3

func CalculateFromWif(wifPrivKey string, compress bool, networkParams *chaincfg.Params) (wif, serializedPubKeyHex, address, segwitBech32, segwitNested, taproot string, err error)

func EthereumAddress

func EthereumAddress(privateKeyBytes []byte) (address string)

ethereumAddress generates an ethereum address from a private key. The private key must be 32 bytes. The address is returned with the 0x prefix and in EIP55 checksum format.

Types

type CoinType

type CoinType = uint32

CoinType SLIP-0044 : Registered coin types for BIP-0044 https://github.com/satoshilabs/slips/blob/master/slip-0044.md

const (
	CoinTypeBTC CoinType = 0x80000000
	CoinTypeETH CoinType = 0x8000003c
)

type Key

type Key struct {
	Path     string
	Bip32Key *bip32.Key
	Network  *chaincfg.Params
}

func (*Key) Calculate

func (k *Key) Calculate(compress bool) (wif, serializedPubKeyHex, address, segwitBech32, segwitNested, taproot string, err error)

type KeyManager

type KeyManager struct {
	Mnemonic   string
	Passphrase string
	Keys       map[string]*bip32.Key
	Network    *chaincfg.Params
	// contains filtered or unexported fields
}

func NewKeyManager

func NewKeyManager(mnemonic, passphrase, network string) (*KeyManager, error)

NewKeyManager return new key manager if mnemonic is not provided, it will generate a new mnemonic with 128 bits of entropy, which is 12 words

func (*KeyManager) GetAccountKey

func (km *KeyManager) GetAccountKey(purpose, coinType, account uint32) (*bip32.Key, error)

func (*KeyManager) GetBTCLegacyKey added in v0.0.2

func (km *KeyManager) GetBTCLegacyKey(index uint32) (*Key, error)

func (*KeyManager) GetBTCNativeSegWitKey added in v0.0.2

func (km *KeyManager) GetBTCNativeSegWitKey(index uint32) (*Key, error)

func (*KeyManager) GetBTCNestedSegWitKey added in v0.0.2

func (km *KeyManager) GetBTCNestedSegWitKey(index uint32) (*Key, error)

func (*KeyManager) GetBTCTaprootKey added in v0.0.2

func (km *KeyManager) GetBTCTaprootKey(index uint32) (*Key, error)

func (*KeyManager) GetChangeKey

func (km *KeyManager) GetChangeKey(purpose, coinType, account, change uint32) (*bip32.Key, error)

GetChangeKey ... https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#change change constant 0 is used for external chain change constant 1 is used for internal chain (also known as change addresses)

func (*KeyManager) GetCoinTypeKey

func (km *KeyManager) GetCoinTypeKey(purpose, coinType uint32) (*bip32.Key, error)

func (*KeyManager) GetKey

func (km *KeyManager) GetKey(purpose, coinType, account, change, index uint32) (*Key, error)

func (*KeyManager) GetMasterKey

func (km *KeyManager) GetMasterKey() (*bip32.Key, error)

func (*KeyManager) GetPurposeKey

func (km *KeyManager) GetPurposeKey(purpose uint32) (*bip32.Key, error)

func (*KeyManager) GetSeed

func (km *KeyManager) GetSeed() []byte

func (*KeyManager) SetNetwork

func (km *KeyManager) SetNetwork(network string)

type Purpose

type Purpose = uint32

Purpose BIP43 - Purpose Field for Deterministic Wallets https://github.com/bitcoin/bips/blob/master/bip-0043.mediawiki

Purpose is a constant set to 44' (or 0x8000002C) following the BIP43 recommendation. It indicates that the subtree of this node is used according to this specification.

What does 44' mean in BIP44? https://bitcoin.stackexchange.com/questions/74368/what-does-44-mean-in-bip44

44' means that hardened keys should be used. The distinguisher for whether a key a given index is hardened is that the index is greater than 2^31, which is 2147483648. In hex, that is 0x80000000. That is what the apostrophe (') means. The 44 comes from adding it to 2^31 to get the final hardened key index. In hex, 44 is 2C, so 0x80000000 + 0x2C = 0x8000002C.

const (
	PurposeBIP44 Purpose = 0x8000002C // 44' BIP44
	PurposeBIP49 Purpose = 0x80000031 // 49' BIP49
	PurposeBIP84 Purpose = 0x80000054 // 84' BIP84
	PurposeBIP86 Purpose = 0x80000056 // 86' BIP86 //taprrot
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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