github.com/decred/dcrlnd@v0.7.6/internal/psbt/bip32.go (about) 1 package psbt 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 ) 7 8 // Bip32Derivation encapsulates the data for the input and output 9 // Bip32Derivation key-value fields. 10 // 11 // TODO(roasbeef): use hdkeychain here instead? 12 type Bip32Derivation struct { 13 // PubKey is the raw pubkey serialized in compressed format. 14 PubKey []byte 15 16 // MasterKeyFingerprint is the finger print of the master pubkey. 17 MasterKeyFingerprint uint32 18 19 // Bip32Path is the BIP 32 path with child index as a distinct integer. 20 Bip32Path []uint32 21 } 22 23 // Bip32Sorter implements sort.Interface for the Bip32Derivation struct. 24 type Bip32Sorter []*Bip32Derivation 25 26 func (s Bip32Sorter) Len() int { return len(s) } 27 28 func (s Bip32Sorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 29 30 func (s Bip32Sorter) Less(i, j int) bool { 31 return bytes.Compare(s[i].PubKey, s[j].PubKey) < 0 32 } 33 34 // SerializeBIP32Derivation takes a master key fingerprint as defined in BIP32, 35 // along with a path specified as a list of uint32 values, and returns a 36 // bytestring specifying the derivation in the format required by BIP174: // 37 // master key fingerprint (4) || child index (4) || child index (4) || .... 38 func SerializeBIP32Derivation(masterKeyFingerprint uint32, 39 bip32Path []uint32) []byte { 40 41 var masterKeyBytes [4]byte 42 binary.LittleEndian.PutUint32(masterKeyBytes[:], masterKeyFingerprint) 43 44 derivationPath := make([]byte, 0, 4+4*len(bip32Path)) 45 derivationPath = append(derivationPath, masterKeyBytes[:]...) 46 for _, path := range bip32Path { 47 var pathbytes [4]byte 48 binary.LittleEndian.PutUint32(pathbytes[:], path) 49 derivationPath = append(derivationPath, pathbytes[:]...) 50 } 51 52 return derivationPath 53 }