forked from vulpemventures/go-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creator.go
57 lines (50 loc) · 2.06 KB
/
creator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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 pset
import (
"github.com/btcsuite/btcutil/psbt"
"github.com/yenkuanlee/go-elements/transaction"
)
// New on provision of an input and output 'skeleton' for the transaction, a
// new partially populated PSET. The populated pset will include the
// unsigned transaction, and the set of known inputs and outputs contained
// within the unsigned transaction. The values of nLockTime 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 the Creator.
func New(inputs []*transaction.TxInput,
outputs []*transaction.TxOutput, version int32, nLockTime uint32) (*Pset, 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 < psbt.MinTxVersion {
return nil, psbt.ErrInvalidPsbtFormat
}
unsignedTx := transaction.NewTx(version)
unsignedTx.Locktime = nLockTime
for _, in := range inputs {
unsignedTx.AddInput(transaction.NewTxInput(in.Hash, in.Index))
}
for _, out := range outputs {
unsignedTx.AddOutput(out)
}
// The input and output lists are empty, but there is a list of those
// two lists, and each one must be of length matching the unsigned
// transaction; the unknown list can be nil.
pInputs := make([]PInput, len(unsignedTx.Inputs))
pOutputs := make([]POutput, len(unsignedTx.Outputs))
// This new Psbt is "raw" and contains no key-value fields, so sanity
// checking with c.Cpsbt.SanityCheck() is not required.
return &Pset{
UnsignedTx: unsignedTx,
Inputs: pInputs,
Outputs: pOutputs,
Unknowns: nil,
}, nil
}