Documentation ¶
Index ¶
- Constants
- Variables
- func AddChecksumDblSha256ToBytes(data []byte) ([]byte, error)
- func ChecksumDblSha256(data []byte) ([]byte, error)
- func ExpandPublicKey(key []byte) (*big.Int, *big.Int)
- func HashRipeMD160onSha256(data []byte) ([]byte, error)
- func NewSeed() ([]byte, error)
- func ValidatePrivateKey(key PvtKeyBytes) error
- type AddrEncoding
- type ChainCode
- type ChildNumber
- type Depth
- type FingerPrint
- type Key
- func (key *Key) B58Serialize() string
- func (key *Key) GetChainCode() ChainCode
- func (key *Key) GetChildNumber() ChildNumber
- func (key *Key) GetDepth() Depth
- func (key *Key) GetFingerPrint() FingerPrint
- func (key *Key) GetKeyBytes() KeyBytes
- func (key *Key) GetPvtKeyBytes() PvtKeyBytes
- func (key *Key) GetVersion() uint32
- func (key *Key) IsPrivate() bool
- func (key *Key) IsValid() bool
- func (key *Key) NewChildKey(childIdx uint32) (Key, error)
- func (key *Key) PrivateKeyHex() string
- func (key *Key) PublicKeyExtended() Key
- func (key *Key) PublicKeyHex() string
- func (key *Key) Serialize() ([]byte, error)
- func (key *Key) SetVersion(v Version)
- func (key *Key) SetVersionUint32(v uint32)
- func (key *Key) String() string
- type KeyBytes
- type Path
- type PvtKeyBytes
- type Version
- type VersionBytes
- func (h VersionBytes) CoinVal() (uint32, error)
- func (h VersionBytes) CoinValFull() (uint32, error)
- func (h VersionBytes) IsRegistered() bool
- func (h VersionBytes) IsValid() bool
- func (h VersionBytes) PubKeyFlagBytes() [4]byte
- func (h VersionBytes) PurposeVal() (uint32, error)
- func (h VersionBytes) PurposeValFull() (uint32, error)
- func (h VersionBytes) PvtKeyFlagBytes() [4]byte
Constants ¶
const ( VersionStartIndex = 0 VersionEndIndex = VersionStartIndex + 4 DepthStartIndex = VersionEndIndex DepthEndIndex = DepthStartIndex + 1 FingerPrintStartIndex = DepthEndIndex FingerPrintEndIndex = FingerPrintStartIndex + 4 ChildNumberStartIndex = FingerPrintEndIndex ChildNumberEndIndex = ChildNumberStartIndex + 4 ChainCodeStartIndex = ChildNumberEndIndex ChainCodeEndIndex = ChainCodeStartIndex + 32 PubKeyStartIndex = ChainCodeEndIndex PvtKeyStartIndex = PubKeyStartIndex + 1 PubKeyEndIndex = PubKeyStartIndex + 33 PvtKeyEndIndex = PubKeyEndIndex )
const ( // FirstHardenedChild is the index of the first "hardened" child key as per the FirstHardenedChild = uint32(0x80000000) // PublicKeyCompressedLength is the byte count of a compressed public key PublicKeyCompressedLength = 33 )
Variables ¶
var ( // ErrSerializedKeyWrongSize is returned when trying to deserialize a key that // has an incorrect length ErrSerializedKeyWrongSize = errors.New("serialized keys should by exactly 82 bytes") // ErrHardenedChildPublicKey is returned when trying to create a hardened child // of the public key ErrHardenedChildPublicKey = errors.New("can't create hardened child for public key") // ErrInvalidChecksum is returned when deserializing a key with an incorrect // ChecksumDblSha256 ErrInvalidChecksum = errors.New("ChecksumDblSha256 doesn't match") // ErrInvalidPrivateKey is returned when a derived private key is invalid ErrInvalidPrivateKey = errors.New("invalid private key") // ErrInvalidPublicKey is returned when a derived public key is invalid ErrInvalidPublicKey = errors.New("invalid public key") ErrEmptyKey = errors.New("key is empty") )
var ( ErrUnSupportedHDVersionBytes = errors.New("unsupported hd version bytes") ErrInvalidPurpose = errors.New("invalid purpose") ErrInvalidCoin = errors.New("invalid coin") )
var ( Bitcoinxprvxpub = VersionBytes{ Coin: "Bitcoin", PvtKeyFlag: 0x0488ade4, PvKeyPrefix: "xprv", PubKeyFlag: 0x0488b21e, PubKeyPrefix: "xpub", AddrEncodings: []AddrEncoding{P2PKH, P2SH}, Path: "m/44'/0'", } Bitcoinyprvypub = VersionBytes{ Coin: "Bitcoin", PvtKeyFlag: 0x049d7878, PvKeyPrefix: "yprv", PubKeyFlag: 0x049d7cb2, PubKeyPrefix: "ypub", AddrEncodings: []AddrEncoding{P2WPKHInP2SH}, Path: "m/49'/0'", } Bitcoinzprvzpub = VersionBytes{ Coin: "Bitcoin", PvtKeyFlag: 0x04b2430c, PvKeyPrefix: "zprv", PubKeyFlag: 0x04b24746, PubKeyPrefix: "zpub", AddrEncodings: []AddrEncoding{P2WPKH}, Path: "m/84'/0'", } BitcoinYprvYpub = VersionBytes{ Coin: "Bitcoin", PvtKeyFlag: 0x0295b005, PvKeyPrefix: "Yprv", PubKeyFlag: 0x0295b43f, PubKeyPrefix: "Ypub", AddrEncodings: []AddrEncoding{P2WSHInP2SH}, Path: "m/84'/0'", } BitcoinZprvZpub = VersionBytes{ Coin: "Bitcoin", PvtKeyFlag: 0x02aa7a99, PvKeyPrefix: "Zprv", PubKeyFlag: 0x02aa7ed3, PubKeyPrefix: "Zpub", AddrEncodings: []AddrEncoding{P2WSH}, Path: "m/84'/0'", } Bitcointprvtpub = VersionBytes{ Coin: "Bitcoin Testnet", PvtKeyFlag: 0x04358394, PvKeyPrefix: "tprv", PubKeyFlag: 0x043587cf, PubKeyPrefix: "tpub", AddrEncodings: []AddrEncoding{P2PKH, P2SH}, Path: "m/44'/1'", } Bitcoinuprvupub = VersionBytes{ Coin: "Bitcoin Testnet", PvtKeyFlag: 0x044a4e28, PvKeyPrefix: "uprv", PubKeyFlag: 0x044a5262, PubKeyPrefix: "upub", AddrEncodings: []AddrEncoding{P2WPKHInP2SH}, Path: "m/49'/1'", } Bitcoinvprvvpub = VersionBytes{ Coin: "Bitcoin Testnet", PvtKeyFlag: 0x045f18bc, PvKeyPrefix: "vprv", PubKeyFlag: 0x045f1cf6, PubKeyPrefix: "vpub", AddrEncodings: []AddrEncoding{P2WPKH}, Path: "m/84'/1'", } BitcoinUprvUpub = VersionBytes{ Coin: "Bitcoin Testnet", PvtKeyFlag: 0x024285b5, PvKeyPrefix: "Uprv", PubKeyFlag: 0x024289ef, PubKeyPrefix: "Upub", AddrEncodings: []AddrEncoding{P2WSHInP2SH}, Path: "m/84'/1'", } BitcoinVprvVpub = VersionBytes{ Coin: "Bitcoin Testnet", PvtKeyFlag: 0x02575048, PvKeyPrefix: "Vprv", PubKeyFlag: 0x02575483, PubKeyPrefix: "Vpub", AddrEncodings: []AddrEncoding{P2WSH}, Path: "m/84'/1'", } Groestlcoinxprvxpub = VersionBytes{ Coin: "Groestlcoin", PvtKeyFlag: 0x0488ade4, PvKeyPrefix: "xprv", PubKeyFlag: 0x0488b21e, PubKeyPrefix: "xpub", AddrEncodings: []AddrEncoding{P2PKH, P2SH}, Path: "m/44'/17'", } Groestlcoinyprvypub = VersionBytes{ Coin: "Groestlcoin", PvtKeyFlag: 0x049d7878, PvKeyPrefix: "yprv", PubKeyFlag: 0x049d7cb2, PubKeyPrefix: "ypub", AddrEncodings: []AddrEncoding{P2WPKHInP2SH}, Path: "m/49'/17'", } Groestlcoinzprvzpub = VersionBytes{ Coin: "Groestlcoin", PvtKeyFlag: 0x04b2430c, PvKeyPrefix: "zprv", PubKeyFlag: 0x04b24746, PubKeyPrefix: "zpub", AddrEncodings: []AddrEncoding{P2WPKH}, Path: "m/84'/17'", } GroestlcoinYprvYpub = VersionBytes{ Coin: "Groestlcoin", PvtKeyFlag: 0x0295b005, PvKeyPrefix: "Yprv", PubKeyFlag: 0x0295b43f, PubKeyPrefix: "Ypub", AddrEncodings: []AddrEncoding{P2WSHInP2SH}, Path: "m/84'/17'", } GroestlcoinZprvZpub = VersionBytes{ Coin: "Groestlcoin", PvtKeyFlag: 0x02aa7a99, PvKeyPrefix: "Zprv", PubKeyFlag: 0x02aa7ed3, PubKeyPrefix: "Zpub", AddrEncodings: []AddrEncoding{P2WSH}, Path: "m/84'/17'", } Groestlcointprvtpub = VersionBytes{ Coin: "Groestlcoin Testnet", PvtKeyFlag: 0x04358394, PvKeyPrefix: "tprv", PubKeyFlag: 0x043587cf, PubKeyPrefix: "tpub", AddrEncodings: []AddrEncoding{P2PKH, P2SH}, Path: "m/44'/1'", } Groestlcoinuprvupub = VersionBytes{ Coin: "Groestlcoin Testnet", PvtKeyFlag: 0x044a4e28, PvKeyPrefix: "uprv", PubKeyFlag: 0x044a5262, PubKeyPrefix: "upub", AddrEncodings: []AddrEncoding{P2WPKHInP2SH}, Path: "m/49'/1'", } Groestlcoinvprvvpub = VersionBytes{ Coin: "Groestlcoin Testnet", PvtKeyFlag: 0x045f18bc, PvKeyPrefix: "vprv", PubKeyFlag: 0x045f1cf6, PubKeyPrefix: "vpub", AddrEncodings: []AddrEncoding{P2WPKH}, Path: "m/84'/1'", } GroestlcoinUprvUpub = VersionBytes{ Coin: "Groestlcoin Testnet", PvtKeyFlag: 0x024285b5, PvKeyPrefix: "Uprv", PubKeyFlag: 0x024289ef, PubKeyPrefix: "Upub", AddrEncodings: []AddrEncoding{P2WSHInP2SH}, Path: "m/84'/1'", } GroestlcoinVprvVpub = VersionBytes{ Coin: "Groestlcoin Testnet", PvtKeyFlag: 0x02575048, PvKeyPrefix: "Vprv", PubKeyFlag: 0x02575483, PubKeyPrefix: "Vpub", AddrEncodings: []AddrEncoding{P2WSH}, Path: "m/84'/1'", } LitecoinLtpvLtub = VersionBytes{ Coin: "Litecoin", PvtKeyFlag: 0x019d9cfe, PvKeyPrefix: "Ltpv", PubKeyFlag: 0x019da462, PubKeyPrefix: "Ltub", AddrEncodings: []AddrEncoding{P2PKH, P2SH}, Path: "m/44'/2'", } LitecoinMtpvMtub = VersionBytes{ Coin: "Litecoin", PvtKeyFlag: 0x01b26792, PvKeyPrefix: "Mtpv", PubKeyFlag: 0x01b26ef6, PubKeyPrefix: "Mtub", AddrEncodings: []AddrEncoding{P2WPKHInP2SH}, Path: "m/49'/2'", } Litecointtpvttub = VersionBytes{ Coin: "Litecoin Testnet", PvtKeyFlag: 0x0436ef7d, PvKeyPrefix: "ttpv", PubKeyFlag: 0x0436f6e1, PubKeyPrefix: "ttub", AddrEncodings: []AddrEncoding{P2PKH, P2SH}, Path: "m/44'/1'", } Nexaxprvxpub = VersionBytes{ Coin: "Nexa", PvtKeyFlag: 0x426c6b73, PvKeyPrefix: "xprv", PubKeyFlag: 0x42696720, PubKeyPrefix: "xpub", AddrEncodings: []AddrEncoding{P2PKT, P2PKH, P2SH}, Path: "m/44'/29223'", } Nexaxprvxpub2 = VersionBytes{ Coin: "Nexa Testnet", PvtKeyFlag: 0x04358394, PvKeyPrefix: "xprv", PubKeyFlag: 0x043587cf, PubKeyPrefix: "xpub", AddrEncodings: []AddrEncoding{P2PKH, P2SH}, Path: "m/44'/1'", } Polispprvppub = VersionBytes{ Coin: "Polis", PvtKeyFlag: 0x03e25945, PvKeyPrefix: "pprv", PubKeyFlag: 0x03e25d7e, PubKeyPrefix: "ppub", AddrEncodings: []AddrEncoding{P2PKH}, Path: "m/44'/1997'", } Syscoinzprvzpub = VersionBytes{ Coin: "Syscoin", PvtKeyFlag: 0x04b2430c, PvKeyPrefix: "zprv", PubKeyFlag: 0x04b24746, PubKeyPrefix: "zpub", AddrEncodings: []AddrEncoding{P2WPKH}, Path: "m/84'/57'", } SyscoinZprvZpub = VersionBytes{ Coin: "Syscoin", PvtKeyFlag: 0x02aa7a99, PvKeyPrefix: "Zprv", PubKeyFlag: 0x02aa7ed3, PubKeyPrefix: "Zpub", AddrEncodings: []AddrEncoding{P2WSH}, Path: "m/84'/57'", } Vertcoinvtcpvtcv = VersionBytes{ Coin: "Vertcoin", PvtKeyFlag: 0x0488ade4, PvKeyPrefix: "vtcv", PubKeyFlag: 0x0488b21e, PubKeyPrefix: "vtcp", AddrEncodings: []AddrEncoding{P2PKH, P2SH}, Path: "m/44'/28'", } )
var CoinTypeToHDBytesSlice = map[uint32][]VersionBytes{}
CoinTypeToHDBytesSlice uint32 value is VersionBytes.CoinVal and not VersionBytes.CoinValFull
var DefaultMainnetVersion = Bitcoinxprvxpub
var DefaultTestnetVersion = Bitcointprvtpub
var (
ErrUnSupportedOrInvalidPath = errors.New("path is unsupported and/or invalid")
)
var HDVersionBytesSlice []VersionBytes
HDVersionBytesSlice Slice of All Registered VersionBytes
var PathRegex = regexp.MustCompile(`^[mM](/\d+')*(/\d+)*$`)
var PathToHDBytesSlice = map[string][]VersionBytes{ "m/44'/0'": {Bitcoinxprvxpub}, "m/49'/0'": {Bitcoinyprvypub}, "m/84'/0'": {Bitcoinzprvzpub, BitcoinYprvYpub, BitcoinZprvZpub}, "m/44'/1'": {Bitcointprvtpub, Groestlcointprvtpub, Litecointtpvttub, Nexaxprvxpub2}, "m/49'/1'": {Bitcoinuprvupub, Groestlcoinuprvupub}, "m/84'/1'": {Bitcoinvprvvpub, BitcoinUprvUpub, BitcoinVprvVpub, Groestlcoinvprvvpub, GroestlcoinUprvUpub, GroestlcoinVprvVpub}, "m/44'/17'": {Groestlcoinxprvxpub}, "m/49'/17'": {Groestlcoinyprvypub}, "m/84'/17'": {Groestlcoinzprvzpub, GroestlcoinYprvYpub, GroestlcoinZprvZpub}, "m/44'/2'": {LitecoinLtpvLtub}, "m/49'/2'": {LitecoinMtpvMtub}, "m/44'/29223'": {Nexaxprvxpub}, "m/44'/28'": {Vertcoinvtcpvtcv}, "m/44'/1997'": {Polispprvppub}, "m/84'/57'": {Syscoinzprvzpub, SyscoinZprvZpub}, }
PathToHDBytesSlice Registered HD Version Bytes Path (Two Level) To VersionBytes
var PubFlagToHDBytesSlice = map[uint32][]VersionBytes{}
PubFlagToHDBytesSlice VersionBytes.PubKeyFlag To slice of VersionBytes
var PurposeFullToHDBytesSlice = map[uint32][]VersionBytes{}
PurposeFullToHDBytesSlice uint32 value is VersionBytes.PurposeValFull
var PurposeToHDBytesSlice = map[uint32][]VersionBytes{}
PurposeToHDBytesSlice uint32 value is VersionBytes.PurposeVal
var PvtFlagToHDBytesSlice = map[uint32][]VersionBytes{}
PvtFlagToHDBytesSlice VersionBytes.PvtKeyFlag To slice of VersionBytes
Functions ¶
func ChecksumDblSha256 ¶
func ExpandPublicKey ¶
As described at https://crypto.stackexchange.com/a/8916
func HashRipeMD160onSha256 ¶
HashRipeMD160onSha256 hashRipeMD160(sha256.Sum256(data))
func ValidatePrivateKey ¶
func ValidatePrivateKey(key PvtKeyBytes) error
Types ¶
type AddrEncoding ¶
type AddrEncoding string
const ( P2PKH AddrEncoding = "P2PKH" P2WPKH AddrEncoding = "P2WPKH" P2WPKHInP2SH AddrEncoding = "P2WPKHInP2SH" P2SH AddrEncoding = "P2SH" P2WSH AddrEncoding = "P2WSH" P2WSHInP2SH AddrEncoding = "P2WSHInP2SH" P2PKT AddrEncoding = "P2PKT" )
type ChildNumber ¶
type ChildNumber [4]byte
type FingerPrint ¶
type FingerPrint [4]byte
type Key ¶
type Key [78]byte
Key is [ Version + Depth + FingerPrint + ChildNumber + ChainCode + KeyBytes ]Array m/purpose'/coin_type'/account'/change/address_index
func B58Deserialize ¶
B58Deserialize deserializes a Key encoded in base58 encoding
func NewMasterKey ¶
NewMasterKey creates a new master extended key from a seed VersionBytes.PvtKeyFlagBytes used is DefaultMainnetVersion.PvtKeyFlagBytes
func (*Key) B58Serialize ¶
B58Serialize encodes the Key in the standard Bitcoin base58 encoding
func (*Key) GetChainCode ¶
func (*Key) GetChildNumber ¶
func (key *Key) GetChildNumber() ChildNumber
func (*Key) GetFingerPrint ¶
func (key *Key) GetFingerPrint() FingerPrint
func (*Key) GetKeyBytes ¶
func (*Key) GetPvtKeyBytes ¶
func (key *Key) GetPvtKeyBytes() PvtKeyBytes
func (*Key) GetVersion ¶
func (*Key) NewChildKey ¶
NewChildKey derives a child key from a given parent as outlined by bip32
func (*Key) PrivateKeyHex ¶
PrivateKeyHex returns private key in hex string without prefix 0x
func (*Key) PublicKeyExtended ¶
PublicKeyExtended returns the public version of key or return a copy The 'Neuter' function from the bip32 spec If corresponding version is not found then default VersionBytes.PubKeyFlagBytes (DefaultMainnetVersion.PubKeyFlagBytes) is used
func (*Key) PublicKeyHex ¶
PublicKeyHex returns public key in hex string without prefix 0x
func (*Key) SetVersion ¶
func (*Key) SetVersionUint32 ¶
type Path ¶
type Path string
Path is pattern --> m/purpose'/coin_type'/account'/change/address_index Ref https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki and Ref bip32.Key A path component is hardened if it's value is greater than or equal to FirstHardenedChild or if it's value is lesser but contains apostrophe(')
func (Path) ReplaceValueAtDepth ¶
func (Path) ValueAtDepth ¶
ValueAtDepth Example 0 1 2 3 4 5 m/purpose'/coin_type'/account'/change/address_index
func (Path) ValuesAtDepth ¶
ValuesAtDepth (correspondingIndexes)
0 1 2 3 4
m/purpose'/coin_type'/account'/change/address_index Results in []uint32{Purpose,CoinType,Account,Change,AddIndex} Returns real values (i.e. 40 is 40 and not 40 + FirstHardenedChild)
type PvtKeyBytes ¶
type PvtKeyBytes [32]byte // If first byte of KeyBytes is zero
type VersionBytes ¶
type VersionBytes struct { // Coin Name Coin string // PvtKeyFlag(4 bytes) PvtKeyFlag uint32 // PvKeyPrefix is human-readable, but it's a result of serialization // using above Private Key Hex Bytes (the first 4 val of serialized result obtained // using above PvtKeyFlag should always match this PvKeyPrefix) PvKeyPrefix string // PubKeyFlag is similar to PvtKeyFlag but deals with Public Keys PubKeyFlag uint32 // PubKeyPrefix is similar to PvKeyPrefix but deals with Public Keys PubKeyPrefix string AddrEncodings []AddrEncoding // Path Path Path }
func (VersionBytes) CoinVal ¶
func (h VersionBytes) CoinVal() (uint32, error)
CoinVal returns coinVal in (origVal % FirstHardenedChild), hence it's always less than FirstHardenedChild
func (VersionBytes) CoinValFull ¶
func (h VersionBytes) CoinValFull() (uint32, error)
func (VersionBytes) IsRegistered ¶
func (h VersionBytes) IsRegistered() bool
func (VersionBytes) IsValid ¶
func (h VersionBytes) IsValid() bool
func (VersionBytes) PubKeyFlagBytes ¶
func (h VersionBytes) PubKeyFlagBytes() [4]byte
func (VersionBytes) PurposeVal ¶
func (h VersionBytes) PurposeVal() (uint32, error)
PurposeVal returns purposeVal in (origVal % FirstHardenedChild), hence it's always less than FirstHardenedChild
func (VersionBytes) PurposeValFull ¶
func (h VersionBytes) PurposeValFull() (uint32, error)
func (VersionBytes) PvtKeyFlagBytes ¶
func (h VersionBytes) PvtKeyFlagBytes() [4]byte