2019-12-19 19:20:22 +01:00
|
|
|
// Copyright (c) 2018 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package psbt
|
|
|
|
|
|
|
|
// The Finalizer requires provision of a single PSBT input
|
|
|
|
// in which all necessary signatures are encoded, and
|
2020-01-16 02:42:03 +01:00
|
|
|
// uses it to construct valid final sigScript and scriptWitness
|
2019-12-19 19:20:22 +01:00
|
|
|
// fields.
|
|
|
|
// NOTE that p2sh (legacy) and p2wsh currently support only
|
|
|
|
// multisig and no other custom script.
|
|
|
|
|
|
|
|
import (
|
2021-09-10 22:30:39 +02:00
|
|
|
"github.com/lbryio/lbcd/txscript"
|
2019-12-19 19:20:22 +01:00
|
|
|
)
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// isFinalized considers this input finalized if it contains at least one of
|
|
|
|
// the FinalScriptSig or FinalScriptWitness are filled (which only occurs in a
|
|
|
|
// successful call to Finalize*).
|
|
|
|
func isFinalized(p *Packet, inIndex int) bool {
|
|
|
|
input := p.Inputs[inIndex]
|
|
|
|
return input.FinalScriptSig != nil || input.FinalScriptWitness != nil
|
2019-12-19 19:20:22 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// isFinalizableWitnessInput returns true if the target input is a witness UTXO
|
|
|
|
// that can be finalized.
|
|
|
|
func isFinalizableWitnessInput(pInput *PInput) bool {
|
|
|
|
pkScript := pInput.WitnessUtxo.PkScript
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// If this is a native witness output, then we require both
|
|
|
|
// the witness script, but not a redeem script.
|
|
|
|
case txscript.IsWitnessProgram(pkScript):
|
|
|
|
if txscript.IsPayToWitnessScriptHash(pkScript) {
|
|
|
|
if pInput.WitnessScript == nil ||
|
|
|
|
pInput.RedeemScript != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// A P2WKH output on the other hand doesn't need
|
|
|
|
// neither a witnessScript or redeemScript.
|
|
|
|
if pInput.WitnessScript != nil ||
|
|
|
|
pInput.RedeemScript != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 19:20:22 +01:00
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// For nested P2SH inputs, we verify that a witness script is known.
|
|
|
|
case txscript.IsPayToScriptHash(pkScript):
|
|
|
|
if pInput.RedeemScript == nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-19 19:20:22 +01:00
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// If this is a nested P2SH input, then it must also have a
|
|
|
|
// witness script, while we don't need one for P2WKH.
|
|
|
|
if txscript.IsPayToWitnessScriptHash(pInput.RedeemScript) {
|
|
|
|
if pInput.WitnessScript == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if txscript.IsPayToWitnessPubKeyHash(pInput.RedeemScript) {
|
|
|
|
if pInput.WitnessScript != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// unrecognized type
|
|
|
|
return false
|
|
|
|
}
|
2019-12-19 19:20:22 +01:00
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// If this isn't a nested nested P2SH output or a native witness
|
|
|
|
// output, then we can't finalize this input as we don't understand it.
|
|
|
|
default:
|
2019-12-19 19:20:22 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// isFinalizableLegacyInput returns true of the passed input a legacy input
|
|
|
|
// (non-witness) that can be finalized.
|
|
|
|
func isFinalizableLegacyInput(p *Packet, pInput *PInput, inIndex int) bool {
|
|
|
|
// If the input has a witness, then it's invalid.
|
|
|
|
if pInput.WitnessScript != nil {
|
|
|
|
return false
|
2019-12-19 19:20:22 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// Otherwise, we'll verify that we only have a RedeemScript if the prev
|
|
|
|
// output script is P2SH.
|
|
|
|
outIndex := p.UnsignedTx.TxIn[inIndex].PreviousOutPoint.Index
|
|
|
|
if txscript.IsPayToScriptHash(pInput.NonWitnessUtxo.TxOut[outIndex].PkScript) {
|
|
|
|
if pInput.RedeemScript == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if pInput.RedeemScript != nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-19 19:20:22 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
return true
|
2019-12-19 19:20:22 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// isFinalizable checks whether the structure of the entry for the input of the
|
|
|
|
// psbt.Packet at index inIndex contains sufficient information to finalize
|
|
|
|
// this input.
|
|
|
|
func isFinalizable(p *Packet, inIndex int) bool {
|
2019-12-19 19:20:22 +01:00
|
|
|
pInput := p.Inputs[inIndex]
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// The input cannot be finalized without any signatures
|
2019-12-19 19:20:22 +01:00
|
|
|
if pInput.PartialSigs == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// For an input to be finalized, we'll one of two possible top-level
|
|
|
|
// UTXOs present. Each UTXO type has a distinct set of requirements to
|
|
|
|
// be considered finalized.
|
|
|
|
switch {
|
|
|
|
|
|
|
|
// A witness input must be either native P2WSH or nested P2SH with all
|
|
|
|
// relevant sigScript or witness data populated.
|
|
|
|
case pInput.WitnessUtxo != nil:
|
|
|
|
if !isFinalizableWitnessInput(&pInput) {
|
2019-12-19 19:20:22 +01:00
|
|
|
return false
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
case pInput.NonWitnessUtxo != nil:
|
|
|
|
if !isFinalizableLegacyInput(p, &pInput, inIndex) {
|
|
|
|
return false
|
2019-12-19 19:20:22 +01:00
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// If neither a known UTXO type isn't present at all, then we'll
|
|
|
|
// return false as we need one of them.
|
|
|
|
default:
|
2019-12-19 19:20:22 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// MaybeFinalize attempts to finalize the input at index inIndex in the PSBT p,
|
|
|
|
// returning true with no error if it succeeds, OR if the input has already
|
|
|
|
// been finalized.
|
|
|
|
func MaybeFinalize(p *Packet, inIndex int) (bool, error) {
|
2019-12-19 19:20:22 +01:00
|
|
|
if isFinalized(p, inIndex) {
|
|
|
|
return true, nil
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
if !isFinalizable(p, inIndex) {
|
|
|
|
return false, ErrNotFinalizable
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
if err := Finalize(p, inIndex); err != nil {
|
2019-12-19 19:20:22 +01:00
|
|
|
return false, err
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// MaybeFinalizeAll attempts to finalize all inputs of the psbt.Packet that are
|
|
|
|
// not already finalized, and returns an error if it fails to do so.
|
|
|
|
func MaybeFinalizeAll(p *Packet) error {
|
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
for i := range p.UnsignedTx.TxIn {
|
|
|
|
success, err := MaybeFinalize(p, i)
|
|
|
|
if err != nil || !success {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// Finalize assumes that the provided psbt.Packet struct has all partial
|
|
|
|
// signatures and redeem scripts/witness scripts already prepared for the
|
|
|
|
// specified input, and so removes all temporary data and replaces them with
|
|
|
|
// completed sigScript and witness fields, which are stored in key-types 07 and
|
|
|
|
// 08. The witness/non-witness utxo fields in the inputs (key-types 00 and 01)
|
|
|
|
// are left intact as they may be needed for validation (?). If there is any
|
|
|
|
// invalid or incomplete data, an error is returned.
|
|
|
|
func Finalize(p *Packet, inIndex int) error {
|
2019-12-19 19:20:22 +01:00
|
|
|
pInput := p.Inputs[inIndex]
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// Depending on the UTXO type, we either attempt to finalize it as a
|
|
|
|
// witness or legacy UTXO.
|
|
|
|
switch {
|
|
|
|
case pInput.WitnessUtxo != nil:
|
|
|
|
if err := finalizeWitnessInput(p, inIndex); err != nil {
|
2019-12-19 19:20:22 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
case pInput.NonWitnessUtxo != nil:
|
|
|
|
if err := finalizeNonWitnessInput(p, inIndex); err != nil {
|
2019-12-19 19:20:22 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
default:
|
2019-12-19 19:20:22 +01:00
|
|
|
return ErrInvalidPsbtFormat
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// Before returning we sanity check the PSBT to ensure we don't extract
|
|
|
|
// an invalid transaction or produce an invalid intermediate state.
|
|
|
|
if err := p.SanityCheck(); err != nil {
|
2019-12-19 19:20:22 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// checkFinalScriptSigWitness checks whether a given input in the psbt.Packet
|
|
|
|
// struct already has the fields 07 (FinalInScriptSig) or 08 (FinalInWitness).
|
|
|
|
// If so, it returns true. It does not modify the Psbt.
|
|
|
|
func checkFinalScriptSigWitness(p *Packet, inIndex int) bool {
|
2019-12-19 19:20:22 +01:00
|
|
|
pInput := p.Inputs[inIndex]
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
if pInput.FinalScriptSig != nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
if pInput.FinalScriptWitness != nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// finalizeNonWitnessInput attempts to create a PsbtInFinalScriptSig field for
|
|
|
|
// the input at index inIndex, and removes all other fields except for the UTXO
|
|
|
|
// field, for an input of type non-witness, or returns an error.
|
|
|
|
func finalizeNonWitnessInput(p *Packet, inIndex int) error {
|
|
|
|
// If this input has already been finalized, then we'll return an error
|
|
|
|
// as we can't proceed.
|
2019-12-19 19:20:22 +01:00
|
|
|
if checkFinalScriptSigWitness(p, inIndex) {
|
|
|
|
return ErrInputAlreadyFinalized
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// Our goal here is to construct a sigScript given the pubkey,
|
|
|
|
// signature (keytype 02), of which there might be multiple, and the
|
|
|
|
// redeem script field (keytype 04) if present (note, it is not present
|
2019-12-19 19:20:22 +01:00
|
|
|
// for p2pkh type inputs).
|
2020-01-16 02:42:03 +01:00
|
|
|
var sigScript []byte
|
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
pInput := p.Inputs[inIndex]
|
|
|
|
containsRedeemScript := pInput.RedeemScript != nil
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
var (
|
|
|
|
pubKeys [][]byte
|
|
|
|
sigs [][]byte
|
|
|
|
)
|
2019-12-19 19:20:22 +01:00
|
|
|
for _, ps := range pInput.PartialSigs {
|
|
|
|
pubKeys = append(pubKeys, ps.PubKey)
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
sigOK := checkSigHashFlags(ps.Signature, &pInput)
|
|
|
|
if !sigOK {
|
|
|
|
return ErrInvalidSigHashFlags
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
sigs = append(sigs, ps.Signature)
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// We have failed to identify at least 1 (sig, pub) pair in the PSBT,
|
|
|
|
// which indicates it was not ready to be finalized. As a result, we
|
|
|
|
// can't proceed.
|
2019-12-19 19:20:22 +01:00
|
|
|
if len(sigs) < 1 || len(pubKeys) < 1 {
|
|
|
|
return ErrNotFinalizable
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// If this input doesn't need a redeem script (P2PKH), then we'll
|
|
|
|
// construct a simple sigScript that's just the signature then the
|
|
|
|
// pubkey (OP_CHECKSIG).
|
|
|
|
var err error
|
2019-12-19 19:20:22 +01:00
|
|
|
if !containsRedeemScript {
|
2020-01-16 02:42:03 +01:00
|
|
|
// At this point, we should only have a single signature and
|
|
|
|
// pubkey.
|
2019-12-19 19:20:22 +01:00
|
|
|
if len(sigs) != 1 || len(pubKeys) != 1 {
|
|
|
|
return ErrNotFinalizable
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// In this case, our sigScript is just: <sig> <pubkey>.
|
2019-12-19 19:20:22 +01:00
|
|
|
builder := txscript.NewScriptBuilder()
|
|
|
|
builder.AddData(sigs[0]).AddData(pubKeys[0])
|
2020-01-16 02:42:03 +01:00
|
|
|
sigScript, err = builder.Script()
|
2019-12-19 19:20:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-16 02:42:03 +01:00
|
|
|
// This is assumed p2sh multisig Given redeemScript and pubKeys
|
|
|
|
// we can decide in what order signatures must be appended.
|
|
|
|
orderedSigs, err := extractKeyOrderFromScript(
|
|
|
|
pInput.RedeemScript, pubKeys, sigs,
|
|
|
|
)
|
2019-12-19 19:20:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// At this point, we assume that this is a mult-sig input, so
|
|
|
|
// we construct our sigScript which looks something like this
|
|
|
|
// (mind the extra element for the extra multi-sig pop):
|
|
|
|
// * <nil> <sigs...> <redeemScript>
|
|
|
|
//
|
|
|
|
// TODO(waxwing): the below is specific to the multisig case.
|
2019-12-19 19:20:22 +01:00
|
|
|
builder := txscript.NewScriptBuilder()
|
|
|
|
builder.AddOp(txscript.OP_FALSE)
|
|
|
|
for _, os := range orderedSigs {
|
|
|
|
builder.AddData(os)
|
|
|
|
}
|
|
|
|
builder.AddData(pInput.RedeemScript)
|
2020-01-16 02:42:03 +01:00
|
|
|
sigScript, err = builder.Script()
|
2019-12-19 19:20:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// At this point, a sigScript has been constructed. Remove all fields
|
|
|
|
// other than non-witness utxo (00) and finaliscriptsig (07)
|
2019-12-19 19:20:22 +01:00
|
|
|
newInput := NewPsbtInput(pInput.NonWitnessUtxo, nil)
|
2020-01-16 02:42:03 +01:00
|
|
|
newInput.FinalScriptSig = sigScript
|
|
|
|
|
|
|
|
// Overwrite the entry in the input list at the correct index. Note
|
|
|
|
// that this removes all the other entries in the list for this input
|
|
|
|
// index.
|
2019-12-19 19:20:22 +01:00
|
|
|
p.Inputs[inIndex] = *newInput
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:42:03 +01:00
|
|
|
// finalizeWitnessInput attempts to create PsbtInFinalScriptSig field and
|
|
|
|
// PsbtInFinalScriptWitness field for input at index inIndex, and removes all
|
|
|
|
// other fields except for the utxo field, for an input of type witness, or
|
|
|
|
// returns an error.
|
|
|
|
func finalizeWitnessInput(p *Packet, inIndex int) error {
|
|
|
|
// If this input has already been finalized, then we'll return an error
|
|
|
|
// as we can't proceed.
|
2019-12-19 19:20:22 +01:00
|
|
|
if checkFinalScriptSigWitness(p, inIndex) {
|
|
|
|
return ErrInputAlreadyFinalized
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// Depending on the actual output type, we'll either populate a
|
|
|
|
// serializedWitness or a witness as well asa sigScript.
|
|
|
|
var (
|
|
|
|
sigScript []byte
|
|
|
|
serializedWitness []byte
|
|
|
|
)
|
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
pInput := p.Inputs[inIndex]
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// First we'll validate and collect the pubkey+sig pairs from the set
|
|
|
|
// of partial signatures.
|
|
|
|
var (
|
|
|
|
pubKeys [][]byte
|
|
|
|
sigs [][]byte
|
|
|
|
)
|
2019-12-19 19:20:22 +01:00
|
|
|
for _, ps := range pInput.PartialSigs {
|
|
|
|
pubKeys = append(pubKeys, ps.PubKey)
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
sigOK := checkSigHashFlags(ps.Signature, &pInput)
|
|
|
|
if !sigOK {
|
|
|
|
return ErrInvalidSigHashFlags
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
sigs = append(sigs, ps.Signature)
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// If at this point, we don't have any pubkey+sig pairs, then we bail
|
|
|
|
// as we can't proceed.
|
2019-12-19 19:20:22 +01:00
|
|
|
if len(sigs) == 0 || len(pubKeys) == 0 {
|
|
|
|
return ErrNotFinalizable
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
containsRedeemScript := pInput.RedeemScript != nil
|
|
|
|
cointainsWitnessScript := pInput.WitnessScript != nil
|
|
|
|
|
|
|
|
// If there's no redeem script, then we assume that this is native
|
|
|
|
// segwit input.
|
|
|
|
var err error
|
2019-12-19 19:20:22 +01:00
|
|
|
if !containsRedeemScript {
|
2020-01-16 02:42:03 +01:00
|
|
|
// If we have only a sigley pubkey+sig pair, and no witness
|
|
|
|
// script, then we assume this is a P2WKH input.
|
|
|
|
if len(pubKeys) == 1 && len(sigs) == 1 &&
|
|
|
|
!cointainsWitnessScript {
|
|
|
|
|
|
|
|
serializedWitness, err = writePKHWitness(
|
|
|
|
sigs[0], pubKeys[0],
|
|
|
|
)
|
2019-12-19 19:20:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-16 02:42:03 +01:00
|
|
|
// Otherwise, we must have a witnessScript field, so
|
|
|
|
// we'll generate a valid multi-sig witness.
|
|
|
|
//
|
|
|
|
// NOTE: We tacitly assume multisig.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): need to add custom finalize for
|
|
|
|
// non-multisig P2WSH outputs (HTLCs, delay outputs,
|
|
|
|
// etc).
|
2019-12-19 19:20:22 +01:00
|
|
|
if !cointainsWitnessScript {
|
|
|
|
return ErrNotFinalizable
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
serializedWitness, err = getMultisigScriptWitness(
|
|
|
|
pInput.WitnessScript, pubKeys, sigs,
|
|
|
|
)
|
2019-12-19 19:20:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-16 02:42:03 +01:00
|
|
|
// Otherwise, we assume that this is a p2wsh multi-sig output,
|
|
|
|
// which is nested in a p2sh, or a p2wkh nested in a p2sh.
|
|
|
|
//
|
|
|
|
// In this case, we'll take the redeem script (the witness
|
|
|
|
// program in this case), and push it on the stack within the
|
|
|
|
// sigScript.
|
2019-12-19 19:20:22 +01:00
|
|
|
builder := txscript.NewScriptBuilder()
|
|
|
|
builder.AddData(pInput.RedeemScript)
|
2020-01-16 02:42:03 +01:00
|
|
|
sigScript, err = builder.Script()
|
2019-12-19 19:20:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// If don't have a witness script, then we assume this is a
|
|
|
|
// nested p2wkh output.
|
2019-12-19 19:20:22 +01:00
|
|
|
if !cointainsWitnessScript {
|
2020-01-16 02:42:03 +01:00
|
|
|
// Assumed p2sh-p2wkh Here the witness is just (sig,
|
|
|
|
// pub) as for p2pkh case
|
2019-12-19 19:20:22 +01:00
|
|
|
if len(sigs) != 1 || len(pubKeys) != 1 {
|
|
|
|
return ErrNotFinalizable
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
serializedWitness, err = writePKHWitness(sigs[0], pubKeys[0])
|
2019-12-19 19:20:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
2019-12-19 19:20:22 +01:00
|
|
|
} else {
|
2020-01-16 02:42:03 +01:00
|
|
|
// Otherwise, we assume that this is a p2wsh multi-sig,
|
|
|
|
// so we generate the proper witness.
|
|
|
|
serializedWitness, err = getMultisigScriptWitness(
|
|
|
|
pInput.WitnessScript, pubKeys, sigs,
|
|
|
|
)
|
2019-12-19 19:20:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
// At this point, a witness has been constructed, and a sigScript (if
|
|
|
|
// nested; else it's []). Remove all fields other than witness utxo
|
|
|
|
// (01) and finalscriptsig (07), finalscriptwitness (08).
|
2019-12-19 19:20:22 +01:00
|
|
|
newInput := NewPsbtInput(nil, pInput.WitnessUtxo)
|
2020-01-16 02:42:03 +01:00
|
|
|
if len(sigScript) > 0 {
|
|
|
|
newInput.FinalScriptSig = sigScript
|
2019-12-19 19:20:22 +01:00
|
|
|
}
|
2020-01-16 02:42:03 +01:00
|
|
|
|
|
|
|
newInput.FinalScriptWitness = serializedWitness
|
|
|
|
|
|
|
|
// Finally, we overwrite the entry in the input list at the correct
|
|
|
|
// index.
|
2019-12-19 19:20:22 +01:00
|
|
|
p.Inputs[inIndex] = *newInput
|
|
|
|
return nil
|
|
|
|
}
|