Skip to content

Commit

Permalink
psbt: allow Unknowns in outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Jan 27, 2023
1 parent 2dbc98b commit 19c7c3d
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions btcutil/psbt/partial_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type POutput struct {
TaprootInternalKey []byte
TaprootTapTree []byte
TaprootBip32Derivation []*TaprootBip32Derivation
Unknowns []*Unknown
}

// NewPsbtOutput creates an instance of PsbtOutput; the three parameters
Expand Down Expand Up @@ -144,8 +145,25 @@ func (po *POutput) deserialize(r io.Reader) error {
)

default:
// Unknown type is allowed for inputs but not outputs.
return ErrInvalidPsbtFormat
// A fall through case for any proprietary types.
keyCodeAndData := append(
[]byte{byte(keyCode)}, keyData...,
)
newUnknown := &Unknown{
Key: keyCodeAndData,
Value: value,
}

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

return ErrDuplicateKey
}
}

po.Unknowns = append(po.Unknowns, newUnknown)
}
}

Expand Down Expand Up @@ -228,5 +246,14 @@ func (po *POutput) serialize(w io.Writer) error {
}
}

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

return nil
}

0 comments on commit 19c7c3d

Please sign in to comment.