hd

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: Apache-2.0, Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package hd provides basic functionality Hierarchical Deterministic Wallets.

The user must understand the overall concept of the BIP 32 and the BIP 44 specs:

https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki

In combination with the bip39 package in go-crypto this package provides the functionality for deriving keys using a BIP 44 HD path, or, more general, by passing a BIP 32 path.

In particular, this package (together with bip39) provides all necessary functionality to derive keys from mnemonics generated during the cosmos fundraiser.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeMastersFromSeed

func ComputeMastersFromSeed(seed []byte) (secret [32]byte, chainCode [32]byte)

ComputeMastersFromSeed returns the master public key, master secret, and chain code in hex.

func DerivePrivateKeyForPath

func DerivePrivateKeyForPath(privKeyBytes [32]byte, chainCode [32]byte, path string) ([32]byte, error)

DerivePrivateKeyForPath derives the private key by following the BIP 32/44 path from privKeyBytes, using the given chainCode.

Example
seed := mnemonicToSeed("barrel original fuel morning among eternal " +
	"filter ball stove pluck matrix mechanic")
master, ch := ComputeMastersFromSeed(seed)
fmt.Println("keys from fundraiser test-vector (cosmos, bitcoin, ether)")
fmt.Println()
// cosmos
priv, err := DerivePrivateKeyForPath(master, ch, types.FullFundraiserPath)
if err != nil {
	fmt.Println("INVALID")
} else {
	fmt.Println(hex.EncodeToString(priv[:]))
}
// bitcoin
priv, err = DerivePrivateKeyForPath(master, ch, "44'/0'/0'/0/0")
if err != nil {
	fmt.Println("INVALID")
} else {
	fmt.Println(hex.EncodeToString(priv[:]))
}
// ether
priv, err = DerivePrivateKeyForPath(master, ch, "44'/60'/0'/0/0")
if err != nil {
	fmt.Println("INVALID")
} else {
	fmt.Println(hex.EncodeToString(priv[:]))
}
// INVALID
priv, err = DerivePrivateKeyForPath(master, ch, "X/0'/0'/0/0")
if err != nil {
	fmt.Println("INVALID")
} else {
	fmt.Println(hex.EncodeToString(priv[:]))
}
priv, err = DerivePrivateKeyForPath(master, ch, "-44/0'/0'/0/0")
if err != nil {
	fmt.Println("INVALID")
} else {
	fmt.Println(hex.EncodeToString(priv[:]))
}

fmt.Println()
fmt.Println("keys generated via https://coinomi.com/recovery-phrase-tool.html")
fmt.Println()

seed = mnemonicToSeed(
	"advice process birth april short trust crater change bacon monkey medal garment " +
		"gorilla ranch hour rival razor call lunar mention taste vacant woman sister")
master, ch = ComputeMastersFromSeed(seed)
priv, _ = DerivePrivateKeyForPath(master, ch, "44'/1'/1'/0/4")
fmt.Println(hex.EncodeToString(priv[:]))

seed = mnemonicToSeed("idea naive region square margin day captain habit " +
	"gun second farm pact pulse someone armed")
master, ch = ComputeMastersFromSeed(seed)
priv, _ = DerivePrivateKeyForPath(master, ch, "44'/0'/0'/0/420")
fmt.Println(hex.EncodeToString(priv[:]))

fmt.Println()
fmt.Println("BIP 32 example")
fmt.Println()

// bip32 path: m/0/7
seed = mnemonicToSeed("monitor flock loyal sick object grunt duty ride develop assault harsh history")
master, ch = ComputeMastersFromSeed(seed)
priv, _ = DerivePrivateKeyForPath(master, ch, "0/7")
fmt.Println(hex.EncodeToString(priv[:]))
Output:

keys from fundraiser test-vector (cosmos, bitcoin, ether)

bfcb217c058d8bbafd5e186eae936106ca3e943889b0b4a093ae13822fd3170c
e77c3de76965ad89997451de97b95bb65ede23a6bf185a55d80363d92ee37c3d
7fc4d8a8146dea344ba04c593517d3f377fa6cded36cd55aee0a0bb968e651bc
INVALID
INVALID

keys generated via https://coinomi.com/recovery-phrase-tool.html

a61f10c5fecf40c084c94fa54273b6f5d7989386be4a37669e6d6f7b0169c163
32c4599843de3ef161a629a461d12c60b009b676c35050be5f7ded3a3b23501f

BIP 32 example

c4c11d8c03625515905d7e89d25dfc66126fbc629ecca6db489a1a72fc4bda78

Types

type BIP44Params

type BIP44Params struct {
	Purpose      uint32 `json:"purpose"`
	CoinType     uint32 `json:"coinType"`
	Account      uint32 `json:"account"`
	Change       bool   `json:"change"`
	AddressIndex uint32 `json:"addressIndex"`
}

BIP44Params wraps BIP 44 params (5 level BIP 32 path). To receive a canonical string representation ala m / purpose' / coinType' / account' / change / addressIndex call String() on a BIP44Params instance.

func NewFundraiserParams

func NewFundraiserParams(account, coinType, addressIdx uint32) *BIP44Params

NewFundraiserParams creates a BIP 44 parameter object from the params: m / 44' / coinType' / account' / 0 / address_index The fixed parameters (purpose', coin_type', and change) are determined by what was used in the fundraiser.

func NewParams

func NewParams(purpose, coinType, account uint32, change bool, addressIdx uint32) *BIP44Params

NewParams creates a BIP 44 parameter object from the params: m / purpose' / coinType' / account' / change / addressIndex

Example
path := NewParams(44, 0, 0, false, 0)
fmt.Println(path.String())
path = NewParams(44, 33, 7, true, 9)
fmt.Println(path.String())
Output:

m/44'/0'/0'/0/0
m/44'/33'/7'/1/9

func NewParamsFromPath

func NewParamsFromPath(path string) (*BIP44Params, error)

NewParamsFromPath parses the BIP44 path and unmarshals it into a Bip44Params. It supports both absolute and relative paths.

func (BIP44Params) DerivationPath

func (p BIP44Params) DerivationPath() []uint32

DerivationPath returns the BIP44 fields as an array.

func (BIP44Params) String

func (p BIP44Params) String() string

String returns the full absolute HD path of the BIP44 (https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki) params: m / purpose' / coin_type' / account' / change / address_index

Jump to

Keyboard shortcuts

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