Skip to content

Commit

Permalink
psbt: cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Dec 26, 2024
1 parent b1b1e95 commit bdb0b3d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
27 changes: 17 additions & 10 deletions btcutil/psbt/bip32.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"encoding/binary"
)

const (
// uint32Size is the size of a uint32 in bytes.
uint32Size = 4
)

// Bip32Derivation encapsulates the data for the input and output
// Bip32Derivation key-value fields.
//
Expand Down Expand Up @@ -38,21 +43,23 @@ func (s Bip32Sorter) Less(i, j int) bool {

// ReadBip32Derivation deserializes a byte slice containing chunks of 4 byte
// little endian encodings of uint32 values, the first of which is the
// masterkeyfingerprint and the remainder of which are the derivation path.
// MasterKeyFingerprint and the remainder of which are the derivation path.
func ReadBip32Derivation(path []byte) (uint32, []uint32, error) {
// BIP-0174 defines the derivation path being encoded as
// "<32-bit uint> <32-bit uint>*"
// with the asterisk meaning 0 to n times. Which in turn means that an
// empty path is valid, only the key fingerprint is mandatory.
if len(path)%4 != 0 {
if len(path)%uint32Size != 0 {
return 0, nil, ErrInvalidPsbtFormat
}

masterKeyInt := binary.LittleEndian.Uint32(path[:4])
masterKeyInt := binary.LittleEndian.Uint32(path[:uint32Size])

var paths []uint32
for i := 4; i < len(path); i += 4 {
paths = append(paths, binary.LittleEndian.Uint32(path[i:i+4]))
for i := uint32Size; i < len(path); i += uint32Size {
paths = append(paths, binary.LittleEndian.Uint32(
path[i:i+uint32Size],
))
}

return masterKeyInt, paths, nil
Expand All @@ -65,15 +72,15 @@ func ReadBip32Derivation(path []byte) (uint32, []uint32, error) {
func SerializeBIP32Derivation(masterKeyFingerprint uint32,
bip32Path []uint32) []byte {

var masterKeyBytes [4]byte
var masterKeyBytes [uint32Size]byte
binary.LittleEndian.PutUint32(masterKeyBytes[:], masterKeyFingerprint)

derivationPath := make([]byte, 0, 4+4*len(bip32Path))
derivationPath := make([]byte, 0, uint32Size+uint32Size*len(bip32Path))
derivationPath = append(derivationPath, masterKeyBytes[:]...)
for _, path := range bip32Path {
var pathbytes [4]byte
binary.LittleEndian.PutUint32(pathbytes[:], path)
derivationPath = append(derivationPath, pathbytes[:]...)
var pathBytes [uint32Size]byte
binary.LittleEndian.PutUint32(pathBytes[:], path)
derivationPath = append(derivationPath, pathBytes[:]...)
}

return derivationPath
Expand Down
16 changes: 8 additions & 8 deletions btcutil/psbt/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ const (
// invalid.
UnsignedTxType GlobalType = 0

// XpubType houses a global xpub for the entire PSBT packet.
// XPubType houses a global xPub for the entire PSBT packet.
//
// The key ({0x01}|{xpub}) is he 78 byte serialized extended public key
// as defined by BIP 32. Extended public keys are those that can be
// The key ({0x01}|{xpub}) is the 78 byte serialized extended public key
// as defined by BIP-0032. Extended public keys are those that can be
// used to derive public keys used in the inputs and outputs of this
// transaction. It should be the public key at the highest hardened
// derivation index so that
// the unhardened child keys used in the transaction can be derived.
// derivation index so that the unhardened child keys used in the
// transaction can be derived.
//
// The value is the master key fingerprint as defined by BIP 32
// The value is the master key fingerprint as defined by BIP-0032
// concatenated with the derivation path of the public key. The
// derivation path is represented as 32-bit little endian unsigned
// integer indexes concatenated with each other. The number of 32 bit
// integer indexes concatenated with each other. The number of 32-bit
// unsigned integer indexes must match the depth provided in the
// extended public key.
XpubType GlobalType = 1
XPubType GlobalType = 1

// VersionType houses the global version number of this PSBT. There is
// no key (only contains the byte type), then the value if omitted, is
Expand Down

0 comments on commit bdb0b3d

Please sign in to comment.