-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbpu.go
188 lines (159 loc) · 4.19 KB
/
bpu.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Package bpu contains the main functionality of the bpu package
package bpu
import (
_ "embed" // This was found and leaving for now
"encoding/hex"
"errors"
"fmt"
"github.com/bitcoin-sv/go-sdk/script"
"github.com/bitcoin-sv/go-sdk/transaction"
)
// Parse is the main transformation function for the bpu package
func Parse(config ParseConfig) (bpuTx *Tx, err error) {
bpuTx = new(Tx)
err = bpuTx.fromConfig(config)
if err != nil {
return nil, err
}
return bpuTx, nil
}
var defaultTransform Transform = func(r Cell, c string) (to *Cell, err error) {
return &r, nil
}
// convert a raw tx to a bpu tx
func (b *Tx) fromConfig(config ParseConfig) (err error) {
var gene *transaction.Transaction
if config.Tx != nil {
gene = config.Tx
} else {
if config.RawTxHex == nil || len(*config.RawTxHex) == 0 {
return errors.New("raw tx must be set")
}
gene, err = transaction.NewTransactionFromHex(*config.RawTxHex)
if err != nil {
return fmt.Errorf("failed to parse tx: %w", err)
}
}
var inXputs []XPut
inXputs, outXputs, err := collect(config, gene.Inputs, gene.Outputs)
if err != nil {
return err
}
// convert all of the xputs to inputs
inputs, err := processInputs(inXputs, gene.Inputs)
if err != nil {
return err
}
outputs, err := processOutputs(outXputs, gene.Outputs)
if err != nil {
return err
}
txid := gene.TxID()
b.Tx = TxInfo{
H: txid.String(),
}
b.In = inputs
b.Out = outputs
b.Lock = gene.LockTime
return
}
func processInputs(inXputs []XPut, geneInputs []*transaction.TransactionInput) ([]Input, error) {
inputs := make([]Input, 0, len(geneInputs))
for idx, inXput := range inXputs {
geneInput := geneInputs[idx]
var address *string
if geneInput.UnlockingScript != nil {
gInScript := *geneInput.UnlockingScript
// TODO: Remove this hack if libsv accepts this pr:
// https://github.com/libsv/go-bt/pull/133
// only a problem for input scripts
parts, err := script.DecodeScript(gInScript)
if err != nil {
return nil, err
}
if len(parts) == 2 || (len(parts) >= 2 && len(parts[1].Data) == 33) {
partHex := hex.EncodeToString(parts[1].Data)
var a *script.Address
a, err = script.NewAddressFromPublicKeyString(partHex, true)
if err != nil {
return nil, err
}
address = &a.AddressString
}
}
prevTxid := geneInput.SourceTXID.String()
inXput.E = E{
A: address,
V: geneInput.SourceTxSatoshis(),
H: &prevTxid,
I: geneInput.SourceTxOutIndex,
}
inputs = append(inputs, Input{
XPut: inXput,
Seq: geneInputs[idx].SequenceNumber,
})
}
return inputs, nil
}
func processOutputs(outXputs []XPut, geneOutputs []*transaction.TransactionOutput) ([]Output, error) {
outputs := make([]Output, 0, len(geneOutputs))
for idx, outXput := range outXputs {
geneOutput := geneOutputs[idx]
var address *string
var addresses []string
addresses, err := geneOutput.LockingScript.Addresses()
if err != nil {
return nil, err
}
if len(addresses) > 0 {
address = &addresses[0]
}
outXput.E = E{
A: address,
V: &geneOutput.Satoshis,
I: uint32(idx),
H: nil,
}
outputs = append(outputs, Output{
XPut: outXput,
})
}
return outputs, nil
}
// splits inputs and outputs into tapes wherever
// delimeters are found (defined by parse config)
func collect(config ParseConfig, inputs []*transaction.TransactionInput, outputs []*transaction.TransactionOutput) (
xputIns []XPut, xputOuts []XPut, err error) {
if config.Transform == nil {
config.Transform = &defaultTransform
}
if inputs == nil {
inputs = make([]*transaction.TransactionInput, 0)
}
if outputs == nil {
outputs = make([]*transaction.TransactionOutput, 0)
}
// preallocate memory
xputIns = make([]XPut, len(inputs))
for idx, input := range inputs {
var xput = new(XPut)
s := input.UnlockingScript
err := xput.fromScript(config, s, uint8(idx))
if err != nil {
return nil, nil, err
}
xputIns[idx] = *xput
}
// preallocate memory
xputOuts = make([]XPut, len(outputs))
for idx, output := range outputs {
var xput = new(XPut)
s := output.LockingScript
err := xput.fromScript(config, s, uint8(idx))
if err != nil {
return nil, nil, err
}
xputOuts[idx] = *xput
}
return xputIns, xputOuts, nil
}