Skip to content

Commit

Permalink
psbt: fix formatting and typos
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Jan 27, 2023
1 parent 734ab73 commit 2dbc98b
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 135 deletions.
2 changes: 1 addition & 1 deletion btcutil/psbt/bip32.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Bip32Derivation struct {
// PubKey is the raw pubkey serialized in compressed format.
PubKey []byte

// MasterKeyFingerprint is the finger print of the master pubkey.
// MasterKeyFingerprint is the fingerprint of the master pubkey.
MasterKeyFingerprint uint32

// Bip32Path is the BIP 32 path with child index as a distinct integer.
Expand Down
5 changes: 3 additions & 2 deletions btcutil/psbt/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ func Extract(p *Packet) (*wire.MsgTx, error) {
return nil, err
}

// Now that we know how may inputs we'll need, we'll
// Now that we know how many inputs we'll need, we'll
// construct a packing slice, then read out each input
// (with a varint prefix) from the witnessReader.
tin.Witness = make(wire.TxWitness, witCount)
for j := uint64(0); j < witCount; j++ {
wit, err := wire.ReadVarBytes(
witnessReader, 0, txscript.MaxScriptSize, "witness",
witnessReader, 0,
txscript.MaxScriptSize, "witness",
)
if err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion btcutil/psbt/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package psbt
import (
"bytes"
"fmt"

"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
)
Expand Down Expand Up @@ -462,7 +463,9 @@ func finalizeWitnessInput(p *Packet, inIndex int) error {
return ErrNotFinalizable
}

serializedWitness, err = writePKHWitness(sigs[0], pubKeys[0])
serializedWitness, err = writePKHWitness(
sigs[0], pubKeys[0],
)
if err != nil {
return err
}
Expand Down
120 changes: 59 additions & 61 deletions btcutil/psbt/partial_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ type PInput struct {
// NOTE: Only one of the two arguments should be specified, with the other
// being `nil`; otherwise the created PsbtInput object will fail IsSane()
// checks and will not be usable.
func NewPsbtInput(nonWitnessUtxo *wire.MsgTx,
witnessUtxo *wire.TxOut) *PInput {

func NewPsbtInput(nonWitnessUtxo *wire.MsgTx, witnessUtxo *wire.TxOut) *PInput {
return &PInput{
NonWitnessUtxo: nonWitnessUtxo,
WitnessUtxo: witnessUtxo,
Expand All @@ -57,7 +55,6 @@ func NewPsbtInput(nonWitnessUtxo *wire.MsgTx,
// IsSane returns true only if there are no conflicting values in the Psbt
// PInput. For segwit v0 no checks are currently implemented.
func (pi *PInput) IsSane() bool {

// TODO(guggero): Implement sanity checks for segwit v1. For segwit v0
// it is unsafe to only rely on the witness UTXO so we don't check that
// only one is set anymore.
Expand All @@ -69,12 +66,12 @@ func (pi *PInput) IsSane() bool {
// deserialize attempts to deserialize a new PInput from the passed io.Reader.
func (pi *PInput) deserialize(r io.Reader) error {
for {
keyint, keydata, err := getKey(r)
keyCode, keyData, err := getKey(r)
if err != nil {
return err
}
if keyint == -1 {
// Reached separator byte
if keyCode == -1 {
// Reached separator byte, this section is done.
break
}
value, err := wire.ReadVarBytes(
Expand All @@ -84,14 +81,14 @@ func (pi *PInput) deserialize(r io.Reader) error {
return err
}

switch InputType(keyint) {
switch InputType(keyCode) {

case NonWitnessUtxoType:
if pi.NonWitnessUtxo != nil {
return ErrDuplicateKey
}
if keydata != nil {
return ErrInvalidKeydata
if keyData != nil {
return ErrInvalidKeyData
}
tx := wire.NewMsgTx(2)

Expand All @@ -105,8 +102,8 @@ func (pi *PInput) deserialize(r io.Reader) error {
if pi.WitnessUtxo != nil {
return ErrDuplicateKey
}
if keydata != nil {
return ErrInvalidKeydata
if keyData != nil {
return ErrInvalidKeyData
}
txout, err := readTxOut(value)
if err != nil {
Expand All @@ -116,15 +113,15 @@ func (pi *PInput) deserialize(r io.Reader) error {

case PartialSigType:
newPartialSig := PartialSig{
PubKey: keydata,
PubKey: keyData,
Signature: value,
}

if !newPartialSig.checkValid() {
return ErrInvalidPsbtFormat
}

// Duplicate keys are not allowed
// Duplicate keys are not allowed.
for _, x := range pi.PartialSigs {
if bytes.Equal(x.PubKey, newPartialSig.PubKey) {
return ErrDuplicateKey
Expand All @@ -137,41 +134,41 @@ func (pi *PInput) deserialize(r io.Reader) error {
if pi.SighashType != 0 {
return ErrDuplicateKey
}
if keydata != nil {
return ErrInvalidKeydata
if keyData != nil {
return ErrInvalidKeyData
}

// Bounds check on value here since the sighash type must be a
// 32-bit unsigned integer.
// Bounds check on value here since the sighash type
// must be a 32-bit unsigned integer.
if len(value) != 4 {
return ErrInvalidKeydata
return ErrInvalidKeyData
}

shtype := txscript.SigHashType(
sighashType := txscript.SigHashType(
binary.LittleEndian.Uint32(value),
)
pi.SighashType = shtype
pi.SighashType = sighashType

case RedeemScriptInputType:
if pi.RedeemScript != nil {
return ErrDuplicateKey
}
if keydata != nil {
return ErrInvalidKeydata
if keyData != nil {
return ErrInvalidKeyData
}
pi.RedeemScript = value

case WitnessScriptInputType:
if pi.WitnessScript != nil {
return ErrDuplicateKey
}
if keydata != nil {
return ErrInvalidKeydata
if keyData != nil {
return ErrInvalidKeyData
}
pi.WitnessScript = value

case Bip32DerivationInputType:
if !validatePubkey(keydata) {
if !validatePubkey(keyData) {
return ErrInvalidPsbtFormat
}
master, derivationPath, err := ReadBip32Derivation(
Expand All @@ -183,15 +180,15 @@ func (pi *PInput) deserialize(r io.Reader) error {

// Duplicate keys are not allowed
for _, x := range pi.Bip32Derivation {
if bytes.Equal(x.PubKey, keydata) {
if bytes.Equal(x.PubKey, keyData) {
return ErrDuplicateKey
}
}

pi.Bip32Derivation = append(
pi.Bip32Derivation,
&Bip32Derivation{
PubKey: keydata,
PubKey: keyData,
MasterKeyFingerprint: master,
Bip32Path: derivationPath,
},
Expand All @@ -201,8 +198,8 @@ func (pi *PInput) deserialize(r io.Reader) error {
if pi.FinalScriptSig != nil {
return ErrDuplicateKey
}
if keydata != nil {
return ErrInvalidKeydata
if keyData != nil {
return ErrInvalidKeyData
}

pi.FinalScriptSig = value
Expand All @@ -211,8 +208,8 @@ func (pi *PInput) deserialize(r io.Reader) error {
if pi.FinalScriptWitness != nil {
return ErrDuplicateKey
}
if keydata != nil {
return ErrInvalidKeydata
if keyData != nil {
return ErrInvalidKeyData
}

pi.FinalScriptWitness = value
Expand All @@ -221,40 +218,40 @@ func (pi *PInput) deserialize(r io.Reader) error {
if pi.TaprootKeySpendSig != nil {
return ErrDuplicateKey
}
if keydata != nil {
return ErrInvalidKeydata
if keyData != nil {
return ErrInvalidKeyData
}

// The signature can either be 64 or 65 bytes.
switch {
case len(value) == schnorrSigMinLength:
if !validateSchnorrSignature(value) {
return ErrInvalidKeydata
return ErrInvalidKeyData
}

case len(value) == schnorrSigMaxLength:
if !validateSchnorrSignature(
value[0:schnorrSigMinLength],
) {
return ErrInvalidKeydata
return ErrInvalidKeyData
}

default:
return ErrInvalidKeydata
return ErrInvalidKeyData
}

pi.TaprootKeySpendSig = value

case TaprootScriptSpendSignatureType:
// The key data for the script spend signature is:
// <xonlypubkey> <leafhash>
if len(keydata) != 32*2 {
return ErrInvalidKeydata
if len(keyData) != 32*2 {
return ErrInvalidKeyData
}

newPartialSig := TaprootScriptSpendSig{
XOnlyPubKey: keydata[:32],
LeafHash: keydata[32:],
XOnlyPubKey: keyData[:32],
LeafHash: keyData[32:],
}

// The signature can either be 64 or 65 bytes.
Expand All @@ -270,11 +267,11 @@ func (pi *PInput) deserialize(r io.Reader) error {
)

default:
return ErrInvalidKeydata
return ErrInvalidKeyData
}

if !newPartialSig.checkValid() {
return ErrInvalidKeydata
return ErrInvalidKeyData
}

// Duplicate keys are not allowed.
Expand All @@ -290,19 +287,19 @@ func (pi *PInput) deserialize(r io.Reader) error {

case TaprootLeafScriptType:
if len(value) < 1 {
return ErrInvalidKeydata
return ErrInvalidKeyData
}

newLeafScript := TaprootTapLeafScript{
ControlBlock: keydata,
ControlBlock: keyData,
Script: value[:len(value)-1],
LeafVersion: txscript.TapscriptLeafVersion(
value[len(value)-1],
),
}

if !newLeafScript.checkValid() {
return ErrInvalidKeydata
return ErrInvalidKeyData
}

// Duplicate keys are not allowed.
Expand All @@ -320,20 +317,20 @@ func (pi *PInput) deserialize(r io.Reader) error {
)

case TaprootBip32DerivationInputType:
if !validateXOnlyPubkey(keydata) {
return ErrInvalidKeydata
if !validateXOnlyPubkey(keyData) {
return ErrInvalidKeyData
}

taprootDerivation, err := ReadTaprootBip32Derivation(
keydata, value,
keyData, value,
)
if err != nil {
return err
}

// Duplicate keys are not allowed.
for _, x := range pi.TaprootBip32Derivation {
if bytes.Equal(x.XOnlyPubKey, keydata) {
if bytes.Equal(x.XOnlyPubKey, keyData) {
return ErrDuplicateKey
}
}
Expand All @@ -346,12 +343,12 @@ func (pi *PInput) deserialize(r io.Reader) error {
if pi.TaprootInternalKey != nil {
return ErrDuplicateKey
}
if keydata != nil {
return ErrInvalidKeydata
if keyData != nil {
return ErrInvalidKeyData
}

if !validateXOnlyPubkey(value) {
return ErrInvalidKeydata
return ErrInvalidKeyData
}

pi.TaprootInternalKey = value
Expand All @@ -360,25 +357,27 @@ func (pi *PInput) deserialize(r io.Reader) error {
if pi.TaprootMerkleRoot != nil {
return ErrDuplicateKey
}
if keydata != nil {
return ErrInvalidKeydata
if keyData != nil {
return ErrInvalidKeyData
}

pi.TaprootMerkleRoot = value

default:
// A fall through case for any proprietary types.
keyintanddata := []byte{byte(keyint)}
keyintanddata = append(keyintanddata, keydata...)
keyCodeAndData := append(
[]byte{byte(keyCode)}, keyData...,
)
newUnknown := &Unknown{
Key: keyintanddata,
Key: keyCodeAndData,
Value: value,
}

// Duplicate key+keydata are not allowed
// Duplicate key+keyData are not allowed.
for _, x := range pi.Unknowns {
if bytes.Equal(x.Key, newUnknown.Key) &&
bytes.Equal(x.Value, newUnknown.Value) {

return ErrDuplicateKey
}
}
Expand All @@ -392,7 +391,6 @@ func (pi *PInput) deserialize(r io.Reader) error {

// serialize attempts to serialize the target PInput into the passed io.Writer.
func (pi *PInput) serialize(w io.Writer) error {

if !pi.IsSane() {
return ErrInvalidPsbtFormat
}
Expand Down Expand Up @@ -595,7 +593,7 @@ func (pi *PInput) serialize(w io.Writer) error {
}

// Unknown is a special case; we don't have a key type, only a key and
// a value field
// a value field.
for _, kv := range pi.Unknowns {
err := serializeKVpair(w, kv.Key, kv.Value)
if err != nil {
Expand Down
Loading

0 comments on commit 2dbc98b

Please sign in to comment.