-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconstruct.go
More file actions
73 lines (65 loc) · 1.42 KB
/
reconstruct.go
File metadata and controls
73 lines (65 loc) · 1.42 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package psbt
import (
"fmt"
"github.com/Techlateef/psbt/types"
)
func Reconstruct(p *types.PSBT) (*types.Transaction, error) {
version, err := GetTxVersion(*p)
if err != nil {
return nil, err
}
fallbackLocktime, err := GetFallbackLocktime(*p)
if err != nil {
return nil, err
}
tx := &types.Transaction{
Version: version,
Inputs: make([]types.TxInput, 0, len(p.Inputs)),
Outputs: make([]types.TxOutput, 0, len(p.Outputs)),
LockTime: fallbackLocktime,
}
// ----- Inputs ----- //
for _, in := range p.Inputs {
txidBytes, err := GetPrevTxID(in)
if err != nil {
return nil, err
}
if len(txidBytes) != 32 {
return nil, fmt.Errorf("invalid txid length")
}
var hash [32]byte
copy(hash[:], txidBytes)
index, err := GetOutputIndex(in)
if err != nil {
return nil, err
}
sequence, err := GetSequence(in)
if err != nil {
return nil, err
}
tx.Inputs = append(tx.Inputs, types.TxInput{
PreviousOutput: types.OutPoint{
Hash: hash,
Index: index,
},
ScriptSig: nil, // unsigned PSBT doesn't have scriptSig
Sequence: sequence,
})
}
// ----- Outputs ----- //
for _, out := range p.Outputs {
amount, err := GetOutputAmount(out)
if err != nil {
return nil, err
}
script, err := GetOutputScript(out)
if err != nil {
return nil, err
}
tx.Outputs = append(tx.Outputs, types.TxOutput{
Value: amount,
ScriptPubKey: script,
})
}
return tx, nil
}