psbt: remove Creator struct, create New function as entry point into package
The creator struct really didn't do anything before, as a result in this commit we move to get rid of it, and create a `New` method as customarily used in go packages.
This commit is contained in:
parent
3aff531d68
commit
0cda523b34
1 changed files with 29 additions and 36 deletions
|
@ -4,44 +4,38 @@
|
||||||
|
|
||||||
package psbt
|
package psbt
|
||||||
|
|
||||||
// The Creator has only one function:
|
|
||||||
// takes a list of inputs and outputs and converts them to
|
|
||||||
// the simplest Psbt, i.e. one with no data appended to inputs or outputs,
|
|
||||||
// but only the raw unsigned transaction in the global section.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Creator holds a reference to a created Psbt struct.
|
// MinTxVersion is the lowest transaction version that we'll permit.
|
||||||
type Creator struct {
|
const MinTxVersion = 1
|
||||||
Cpsbt *Psbt
|
|
||||||
|
// New on provision of an input and output 'skeleton' for the transaction, a
|
||||||
|
// new partially populated PBST packet. The populated packet will include the
|
||||||
|
// unsigned transaction, and the set of known inputs and outputs contained
|
||||||
|
// within the unsigned transaction. The values of nLockTime, nSequence (per
|
||||||
|
// input) and transaction version (must be 1 of 2) must be specified here. Note
|
||||||
|
// that the default nSequence value is wire.MaxTxInSequenceNum. Referencing
|
||||||
|
// the PSBT BIP, this function serves the roles of teh Creator.
|
||||||
|
func New(inputs []*wire.OutPoint,
|
||||||
|
outputs []*wire.TxOut, version int32, nLockTime uint32,
|
||||||
|
nSequences []uint32) (*Packet, error) {
|
||||||
|
|
||||||
|
// Create the new struct; the input and output lists will be empty, the
|
||||||
|
// unsignedTx object must be constructed and serialized, and that
|
||||||
|
// serialization should be entered as the only entry for the
|
||||||
|
// globalKVPairs list.
|
||||||
|
//
|
||||||
|
// Ensure that the version of the transaction is greater then our
|
||||||
|
// minimum allowed transaction version. There must be one sequence
|
||||||
|
// number per input.
|
||||||
|
if version < MinTxVersion || len(nSequences) != len(inputs) {
|
||||||
|
return nil, ErrInvalidPsbtFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreatePsbt , on provision of an input and output 'skeleton' for
|
unsignedTx := wire.NewMsgTx(version)
|
||||||
// the transaction, returns a Creator struct.
|
|
||||||
// Note that we require OutPoints and not TxIn structs, as we will
|
|
||||||
// only populate the txid:n information, *not* any scriptSig/witness
|
|
||||||
// information. The values of nLockTime, nSequence (per input) and
|
|
||||||
// transaction version (must be 1 of 2) must be specified here. Note
|
|
||||||
// that the default nSequence value is wire.MaxTxInSequenceNum.
|
|
||||||
func (c *Creator) CreatePsbt(inputs []*wire.OutPoint,
|
|
||||||
outputs []*wire.TxOut, Version int32, nLockTime uint32,
|
|
||||||
nSequences []uint32) error {
|
|
||||||
// Create the new struct; the input and output lists will be empty,
|
|
||||||
// the unsignedTx object must be constructed and serialized,
|
|
||||||
// and that serialization should be entered as the only entry for
|
|
||||||
// the globalKVPairs list.
|
|
||||||
|
|
||||||
// Check the version is a valid Bitcoin tx version; the nLockTime
|
|
||||||
// can be any valid uint32. There must be one sequence number per
|
|
||||||
// input.
|
|
||||||
if !(Version == 1 || Version == 2) || len(nSequences) != len(inputs) {
|
|
||||||
return ErrInvalidPsbtFormat
|
|
||||||
}
|
|
||||||
unsignedTx := wire.NewMsgTx(Version)
|
|
||||||
unsignedTx.LockTime = nLockTime
|
unsignedTx.LockTime = nLockTime
|
||||||
|
|
||||||
for i, in := range inputs {
|
for i, in := range inputs {
|
||||||
unsignedTx.AddTxIn(&wire.TxIn{
|
unsignedTx.AddTxIn(&wire.TxIn{
|
||||||
PreviousOutPoint: *in,
|
PreviousOutPoint: *in,
|
||||||
|
@ -57,14 +51,13 @@ func (c *Creator) CreatePsbt(inputs []*wire.OutPoint,
|
||||||
// transaction; the unknown list can be nil.
|
// transaction; the unknown list can be nil.
|
||||||
pInputs := make([]PInput, len(unsignedTx.TxIn))
|
pInputs := make([]PInput, len(unsignedTx.TxIn))
|
||||||
pOutputs := make([]POutput, len(unsignedTx.TxOut))
|
pOutputs := make([]POutput, len(unsignedTx.TxOut))
|
||||||
c.Cpsbt = &Psbt{
|
|
||||||
|
// This new Psbt is "raw" and contains no key-value fields, so sanity
|
||||||
|
// checking with c.Cpsbt.SanityCheck() is not required.
|
||||||
|
return &Packet{
|
||||||
UnsignedTx: unsignedTx,
|
UnsignedTx: unsignedTx,
|
||||||
Inputs: pInputs,
|
Inputs: pInputs,
|
||||||
Outputs: pOutputs,
|
Outputs: pOutputs,
|
||||||
Unknowns: nil,
|
Unknowns: nil,
|
||||||
}
|
}, nil
|
||||||
|
|
||||||
// This new Psbt is "raw" and contains no key-value fields,
|
|
||||||
// so sanity checking with c.Cpsbt.SanityCheck() is not required.
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue