From bdb0b3d8215d69406e10625d45a9acf5af94925e Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 26 Dec 2024 11:13:32 +0100 Subject: [PATCH] psbt: cleanup code --- btcutil/psbt/bip32.go | 27 +++++++++++++++++---------- btcutil/psbt/types.go | 16 ++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/btcutil/psbt/bip32.go b/btcutil/psbt/bip32.go index 96a3f67274..ef1ffac310 100644 --- a/btcutil/psbt/bip32.go +++ b/btcutil/psbt/bip32.go @@ -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. // @@ -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 @@ -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 diff --git a/btcutil/psbt/types.go b/btcutil/psbt/types.go index e833e1af35..ca555101b9 100644 --- a/btcutil/psbt/types.go +++ b/btcutil/psbt/types.go @@ -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