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:
Olaoluwa Osuntokun 2020-01-15 17:40:21 -08:00
parent a94de55e85
commit 57a6543394
No known key found for this signature in database
GPG key ID: BC13F65E2DC84465

View file

@ -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
}
// CreatePsbt , on provision of an input and output 'skeleton' for // New on provision of an input and output 'skeleton' for the transaction, a
// the transaction, returns a Creator struct. // new partially populated PBST packet. The populated packet will include the
// Note that we require OutPoints and not TxIn structs, as we will // unsigned transaction, and the set of known inputs and outputs contained
// only populate the txid:n information, *not* any scriptSig/witness // within the unsigned transaction. The values of nLockTime, nSequence (per
// information. The values of nLockTime, nSequence (per input) and // input) and transaction version (must be 1 of 2) must be specified here. Note
// transaction version (must be 1 of 2) must be specified here. Note // that the default nSequence value is wire.MaxTxInSequenceNum. Referencing
// that the default nSequence value is wire.MaxTxInSequenceNum. // the PSBT BIP, this function serves the roles of teh Creator.
func (c *Creator) CreatePsbt(inputs []*wire.OutPoint, func New(inputs []*wire.OutPoint,
outputs []*wire.TxOut, Version int32, nLockTime uint32, outputs []*wire.TxOut, version int32, nLockTime uint32,
nSequences []uint32) error { 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.
// Check the version is a valid Bitcoin tx version; the nLockTime // Create the new struct; the input and output lists will be empty, the
// can be any valid uint32. There must be one sequence number per // unsignedTx object must be constructed and serialized, and that
// input. // serialization should be entered as the only entry for the
if !(Version == 1 || Version == 2) || len(nSequences) != len(inputs) { // globalKVPairs list.
return ErrInvalidPsbtFormat //
// 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
} }
unsignedTx := wire.NewMsgTx(Version)
unsignedTx.LockTime = nLockTime
unsignedTx := wire.NewMsgTx(version)
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
} }