2021-12-25 01:34:26 +01:00
|
|
|
package psbt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// POutput is a struct encapsulating all the data that can be attached
|
|
|
|
// to any specific output of the PSBT.
|
|
|
|
type POutput struct {
|
2022-05-02 16:25:15 +02:00
|
|
|
RedeemScript []byte
|
|
|
|
WitnessScript []byte
|
|
|
|
Bip32Derivation []*Bip32Derivation
|
|
|
|
TaprootInternalKey []byte
|
|
|
|
TaprootTapTree []byte
|
|
|
|
TaprootBip32Derivation []*TaprootBip32Derivation
|
2021-12-25 01:34:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPsbtOutput creates an instance of PsbtOutput; the three parameters
|
|
|
|
// redeemScript, witnessScript and Bip32Derivation are all allowed to be
|
|
|
|
// `nil`.
|
|
|
|
func NewPsbtOutput(redeemScript []byte, witnessScript []byte,
|
|
|
|
bip32Derivation []*Bip32Derivation) *POutput {
|
|
|
|
return &POutput{
|
|
|
|
RedeemScript: redeemScript,
|
|
|
|
WitnessScript: witnessScript,
|
|
|
|
Bip32Derivation: bip32Derivation,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deserialize attempts to recode a new POutput from the passed io.Reader.
|
|
|
|
func (po *POutput) deserialize(r io.Reader) error {
|
|
|
|
for {
|
2023-01-27 15:30:42 +01:00
|
|
|
keyCode, keyData, err := getKey(r)
|
2021-12-25 01:34:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-27 15:30:42 +01:00
|
|
|
if keyCode == -1 {
|
|
|
|
// Reached separator byte, this section is done.
|
2021-12-25 01:34:26 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := wire.ReadVarBytes(
|
|
|
|
r, 0, MaxPsbtValueLength, "PSBT value",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:30:42 +01:00
|
|
|
switch OutputType(keyCode) {
|
2021-12-25 01:34:26 +01:00
|
|
|
|
|
|
|
case RedeemScriptOutputType:
|
|
|
|
if po.RedeemScript != nil {
|
|
|
|
return ErrDuplicateKey
|
|
|
|
}
|
2023-01-27 15:30:42 +01:00
|
|
|
if keyData != nil {
|
|
|
|
return ErrInvalidKeyData
|
2021-12-25 01:34:26 +01:00
|
|
|
}
|
|
|
|
po.RedeemScript = value
|
|
|
|
|
|
|
|
case WitnessScriptOutputType:
|
|
|
|
if po.WitnessScript != nil {
|
|
|
|
return ErrDuplicateKey
|
|
|
|
}
|
2023-01-27 15:30:42 +01:00
|
|
|
if keyData != nil {
|
|
|
|
return ErrInvalidKeyData
|
2021-12-25 01:34:26 +01:00
|
|
|
}
|
|
|
|
po.WitnessScript = value
|
|
|
|
|
|
|
|
case Bip32DerivationOutputType:
|
2023-01-27 15:30:42 +01:00
|
|
|
if !validatePubkey(keyData) {
|
|
|
|
return ErrInvalidKeyData
|
2021-12-25 01:34:26 +01:00
|
|
|
}
|
2023-01-27 15:30:41 +01:00
|
|
|
master, derivationPath, err := ReadBip32Derivation(
|
|
|
|
value,
|
|
|
|
)
|
2021-12-25 01:34:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:30:42 +01:00
|
|
|
// Duplicate keys are not allowed.
|
2021-12-25 01:34:26 +01:00
|
|
|
for _, x := range po.Bip32Derivation {
|
2023-01-27 15:30:42 +01:00
|
|
|
if bytes.Equal(x.PubKey, keyData) {
|
2021-12-25 01:34:26 +01:00
|
|
|
return ErrDuplicateKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
po.Bip32Derivation = append(po.Bip32Derivation,
|
|
|
|
&Bip32Derivation{
|
2023-01-27 15:30:42 +01:00
|
|
|
PubKey: keyData,
|
2021-12-25 01:34:26 +01:00
|
|
|
MasterKeyFingerprint: master,
|
|
|
|
Bip32Path: derivationPath,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-05-02 16:25:15 +02:00
|
|
|
case TaprootInternalKeyOutputType:
|
|
|
|
if po.TaprootInternalKey != nil {
|
|
|
|
return ErrDuplicateKey
|
|
|
|
}
|
2023-01-27 15:30:42 +01:00
|
|
|
if keyData != nil {
|
|
|
|
return ErrInvalidKeyData
|
2022-05-02 16:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !validateXOnlyPubkey(value) {
|
2023-01-27 15:30:42 +01:00
|
|
|
return ErrInvalidKeyData
|
2022-05-02 16:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
po.TaprootInternalKey = value
|
|
|
|
|
|
|
|
case TaprootTapTreeType:
|
|
|
|
if po.TaprootTapTree != nil {
|
|
|
|
return ErrDuplicateKey
|
|
|
|
}
|
2023-01-27 15:30:42 +01:00
|
|
|
if keyData != nil {
|
|
|
|
return ErrInvalidKeyData
|
2022-05-02 16:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
po.TaprootTapTree = value
|
|
|
|
|
|
|
|
case TaprootBip32DerivationOutputType:
|
2023-01-27 15:30:42 +01:00
|
|
|
if !validateXOnlyPubkey(keyData) {
|
|
|
|
return ErrInvalidKeyData
|
2022-05-02 16:25:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 15:30:41 +01:00
|
|
|
taprootDerivation, err := ReadTaprootBip32Derivation(
|
2023-01-27 15:30:42 +01:00
|
|
|
keyData, value,
|
2022-05-02 16:25:15 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duplicate keys are not allowed.
|
|
|
|
for _, x := range po.TaprootBip32Derivation {
|
2023-01-27 15:30:42 +01:00
|
|
|
if bytes.Equal(x.XOnlyPubKey, keyData) {
|
2022-05-02 16:25:15 +02:00
|
|
|
return ErrDuplicateKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
po.TaprootBip32Derivation = append(
|
|
|
|
po.TaprootBip32Derivation, taprootDerivation,
|
|
|
|
)
|
|
|
|
|
2021-12-25 01:34:26 +01:00
|
|
|
default:
|
|
|
|
// Unknown type is allowed for inputs but not outputs.
|
|
|
|
return ErrInvalidPsbtFormat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// serialize attempts to write out the target POutput into the passed
|
|
|
|
// io.Writer.
|
|
|
|
func (po *POutput) serialize(w io.Writer) error {
|
|
|
|
if po.RedeemScript != nil {
|
|
|
|
err := serializeKVPairWithType(
|
|
|
|
w, uint8(RedeemScriptOutputType), nil, po.RedeemScript,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if po.WitnessScript != nil {
|
|
|
|
err := serializeKVPairWithType(
|
|
|
|
w, uint8(WitnessScriptOutputType), nil, po.WitnessScript,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(Bip32Sorter(po.Bip32Derivation))
|
|
|
|
for _, kd := range po.Bip32Derivation {
|
|
|
|
err := serializeKVPairWithType(w,
|
|
|
|
uint8(Bip32DerivationOutputType),
|
|
|
|
kd.PubKey,
|
|
|
|
SerializeBIP32Derivation(
|
|
|
|
kd.MasterKeyFingerprint,
|
|
|
|
kd.Bip32Path,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 16:25:15 +02:00
|
|
|
if po.TaprootInternalKey != nil {
|
|
|
|
err := serializeKVPairWithType(
|
|
|
|
w, uint8(TaprootInternalKeyOutputType), nil,
|
|
|
|
po.TaprootInternalKey,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if po.TaprootTapTree != nil {
|
|
|
|
err := serializeKVPairWithType(
|
|
|
|
w, uint8(TaprootTapTreeType), nil,
|
|
|
|
po.TaprootTapTree,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(po.TaprootBip32Derivation, func(i, j int) bool {
|
|
|
|
return po.TaprootBip32Derivation[i].SortBefore(
|
|
|
|
po.TaprootBip32Derivation[j],
|
|
|
|
)
|
|
|
|
})
|
|
|
|
for _, derivation := range po.TaprootBip32Derivation {
|
2023-01-27 15:30:41 +01:00
|
|
|
value, err := SerializeTaprootBip32Derivation(
|
2022-05-02 16:25:15 +02:00
|
|
|
derivation,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = serializeKVPairWithType(
|
|
|
|
w, uint8(TaprootBip32DerivationOutputType),
|
|
|
|
derivation.XOnlyPubKey, value,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 01:34:26 +01:00
|
|
|
return nil
|
|
|
|
}
|