psbt: export Bip32 encode/decode functions

This commit is contained in:
Oliver Gugger 2023-01-27 15:30:41 +01:00
parent be056b0a0b
commit 734ab735b9
No known key found for this signature in database
GPG key ID: 8E4256593F177720
4 changed files with 18 additions and 13 deletions

View file

@ -36,10 +36,10 @@ func (s Bip32Sorter) Less(i, j int) bool {
return bytes.Compare(s[i].PubKey, s[j].PubKey) < 0 return bytes.Compare(s[i].PubKey, s[j].PubKey) < 0
} }
// readBip32Derivation deserializes a byte slice containing chunks of 4 byte // ReadBip32Derivation deserializes a byte slice containing chunks of 4 byte
// little endian encodings of uint32 values, the first of which is the // 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) { func ReadBip32Derivation(path []byte) (uint32, []uint32, error) {
// BIP-0174 defines the derivation path being encoded as // BIP-0174 defines the derivation path being encoded as
// "<32-bit uint> <32-bit uint>*" // "<32-bit uint> <32-bit uint>*"
// with the asterisk meaning 0 to n times. Which in turn means that an // with the asterisk meaning 0 to n times. Which in turn means that an

View file

@ -174,7 +174,9 @@ func (pi *PInput) deserialize(r io.Reader) error {
if !validatePubkey(keydata) { if !validatePubkey(keydata) {
return ErrInvalidPsbtFormat return ErrInvalidPsbtFormat
} }
master, derivationPath, err := readBip32Derivation(value) master, derivationPath, err := ReadBip32Derivation(
value,
)
if err != nil { if err != nil {
return err return err
} }
@ -322,7 +324,7 @@ func (pi *PInput) deserialize(r io.Reader) error {
return ErrInvalidKeydata return ErrInvalidKeydata
} }
taprootDerivation, err := readTaprootBip32Derivation( taprootDerivation, err := ReadTaprootBip32Derivation(
keydata, value, keydata, value,
) )
if err != nil { if err != nil {
@ -538,7 +540,7 @@ func (pi *PInput) serialize(w io.Writer) error {
) )
}) })
for _, derivation := range pi.TaprootBip32Derivation { for _, derivation := range pi.TaprootBip32Derivation {
value, err := serializeTaprootBip32Derivation( value, err := SerializeTaprootBip32Derivation(
derivation, derivation,
) )
if err != nil { if err != nil {

View file

@ -74,7 +74,9 @@ func (po *POutput) deserialize(r io.Reader) error {
if !validatePubkey(keydata) { if !validatePubkey(keydata) {
return ErrInvalidKeydata return ErrInvalidKeydata
} }
master, derivationPath, err := readBip32Derivation(value) master, derivationPath, err := ReadBip32Derivation(
value,
)
if err != nil { if err != nil {
return err return err
} }
@ -123,7 +125,7 @@ func (po *POutput) deserialize(r io.Reader) error {
return ErrInvalidKeydata return ErrInvalidKeydata
} }
taprootDerivation, err := readTaprootBip32Derivation( taprootDerivation, err := ReadTaprootBip32Derivation(
keydata, value, keydata, value,
) )
if err != nil { if err != nil {
@ -211,7 +213,7 @@ func (po *POutput) serialize(w io.Writer) error {
) )
}) })
for _, derivation := range po.TaprootBip32Derivation { for _, derivation := range po.TaprootBip32Derivation {
value, err := serializeTaprootBip32Derivation( value, err := SerializeTaprootBip32Derivation(
derivation, derivation,
) )
if err != nil { if err != nil {

View file

@ -2,6 +2,7 @@ package psbt
import ( import (
"bytes" "bytes"
"github.com/btcsuite/btcd/btcec/v2/schnorr" "github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
@ -92,10 +93,10 @@ func (s *TaprootBip32Derivation) SortBefore(other *TaprootBip32Derivation) bool
return bytes.Compare(s.XOnlyPubKey, other.XOnlyPubKey) < 0 return bytes.Compare(s.XOnlyPubKey, other.XOnlyPubKey) < 0
} }
// readTaprootBip32Derivation deserializes a byte slice containing the Taproot // ReadTaprootBip32Derivation deserializes a byte slice containing the Taproot
// BIP32 derivation info that consists of a list of leaf hashes as well as the // BIP32 derivation info that consists of a list of leaf hashes as well as the
// normal BIP32 derivation info. // normal BIP32 derivation info.
func readTaprootBip32Derivation(xOnlyPubKey, func ReadTaprootBip32Derivation(xOnlyPubKey,
value []byte) (*TaprootBip32Derivation, error) { value []byte) (*TaprootBip32Derivation, error) {
// The taproot key BIP 32 derivation path is defined as: // The taproot key BIP 32 derivation path is defined as:
@ -141,7 +142,7 @@ func readTaprootBip32Derivation(xOnlyPubKey,
} }
// Read the BIP32 derivation info. // Read the BIP32 derivation info.
fingerprint, path, err := readBip32Derivation(leftoverBuf.Bytes()) fingerprint, path, err := ReadBip32Derivation(leftoverBuf.Bytes())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -152,9 +153,9 @@ func readTaprootBip32Derivation(xOnlyPubKey,
return &derivation, nil return &derivation, nil
} }
// serializeTaprootBip32Derivation serializes a TaprootBip32Derivation to its // SerializeTaprootBip32Derivation serializes a TaprootBip32Derivation to its
// raw byte representation. // raw byte representation.
func serializeTaprootBip32Derivation(d *TaprootBip32Derivation) ([]byte, func SerializeTaprootBip32Derivation(d *TaprootBip32Derivation) ([]byte,
error) { error) {
var buf bytes.Buffer var buf bytes.Buffer