-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bob.go
320 lines (281 loc) · 7.49 KB
/
bob.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Package bob is a library for working with BOB formatted transactions
//
// Specs: https://bob.planaria.network/
//
// If you have any suggestions or comments, please feel free to open an issue on
// this GitHub repository!
//
// By BitcoinSchema Organization (https://bitcoinschema.org)
package bob
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/bitcoin-sv/go-sdk/chainhash"
"github.com/bitcoin-sv/go-sdk/script"
"github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/go-sdk/transaction/template/p2pkh"
"github.com/bitcoinschema/go-bpu"
)
// Protocol delimiter constants
// OP_SWAP = 0x7c = 124 = "|"
const (
ProtocolDelimiterAsm = "OP_SWAP"
ProtocolDelimiterInt = 0x7c
ProtocolDelimiterByte = byte(ProtocolDelimiterInt)
ProtocolDelimiter = string(rune(ProtocolDelimiterInt))
)
// Tx is a BOB formatted Bitcoin transaction
//
// DO NOT CHANGE ORDER - aligned for memory optimization (malign)
type Tx struct {
bpu.Tx
}
// used by bpu.Parse to determine if the parsing should be shallow or deep
// always using shallow since it covers 99.99% of cases and eliminates
// bottlenecking on txs with lots of pushdatas (like complex sCrypt contracts)
var shallowMode = bpu.Shallow
// NewFromBytes creates a new BOB Tx from a NDJSON line representing a BOB transaction,
// as returned by the bitbus 2 API
func NewFromBytes(line []byte) (bobTx *Tx, err error) {
bobTx = new(Tx)
err = bobTx.FromBytes(line)
return
}
// NewFromRawTxString creates a new BobTx from a hex encoded raw tx string
func NewFromRawTxString(rawTxString string) (bobTx *Tx, err error) {
bobTx = new(Tx)
err = bobTx.FromRawTxString(rawTxString)
return
}
// NewFromString creates a new BobTx from a BOB formatted string
func NewFromString(line string) (bobTx *Tx, err error) {
bobTx = new(Tx)
err = bobTx.FromString(line)
return
}
// NewFromTx creates a new BobTx from a libsv Transaction
func NewFromTx(tx *transaction.Transaction) (bobTx *Tx, err error) {
bobTx = new(Tx)
err = bobTx.FromTx(tx)
if err != nil {
return nil, err
}
return
}
// FromBytes takes a BOB formatted tx string as bytes
func (t *Tx) FromBytes(line []byte) error {
tu := new(bpu.Tx)
if err := json.Unmarshal(line, &tu); err != nil {
return fmt.Errorf("error parsing line: %v, %w", line, err)
}
// The out.E.A field can be either an address or "false"
fixedOuts := make([]bpu.Output, 0)
for _, out := range tu.Out {
var address string
if out.E.A != nil {
address = *out.E.A
}
fixedOuts = append(fixedOuts, bpu.Output{
XPut: bpu.XPut{
I: out.I,
Tape: out.Tape,
E: bpu.E{
A: &address,
V: out.E.V,
I: out.E.I,
H: out.E.H,
},
},
})
}
t.Blk = tu.Blk
t.ID = tu.ID
t.In = tu.In
t.Lock = tu.Lock
t.Out = fixedOuts
t.Tx.Tx = tu.Tx
// Check for missing hex values and supply them
for outIdx, out := range t.Out {
for tapeIdx, tape := range out.Tape {
for cellIdx, cell := range tape.Cell {
if cell.H == nil && cell.B != nil && len(*cell.B) > 0 {
// base 64 decode cell.B and encode it to hex string
cellBytes, err := base64.StdEncoding.DecodeString(*cell.B)
if err != nil {
return err
}
var hexStr = hex.EncodeToString(cellBytes)
t.Out[outIdx].Tape[tapeIdx].Cell[cellIdx].H = &hexStr
}
}
}
}
for inIdx, in := range t.In {
for tapeIdx, tape := range in.Tape {
for cellIdx, cell := range tape.Cell {
if cell.H == nil && cell.B != nil && len(*cell.B) > 0 {
// base 64 decode cell.B and encode it to hex string
cellBytes, err := base64.StdEncoding.DecodeString(*cell.B)
if err != nil {
return err
}
hexStr := hex.EncodeToString(cellBytes)
t.In[inIdx].Tape[tapeIdx].Cell[cellIdx].H = &hexStr
}
}
}
}
return nil
}
// FromRawTxString takes a hex encoded tx string
func (t *Tx) FromRawTxString(rawTxString string) (err error) {
var separator = "|"
var l = bpu.IncludeL
var opReturn = uint8(106)
var opFalse = uint8(0)
var splitConfig = []bpu.SplitConfig{
{
Token: &bpu.Token{
Op: &opReturn,
},
Include: &l,
},
{
Token: &bpu.Token{
Op: &opFalse,
},
Include: &l,
Require: &opReturn,
},
{
Token: &bpu.Token{
S: &separator,
},
Require: &opReturn,
},
}
bpuTx, err := bpu.Parse(bpu.ParseConfig{RawTxHex: &rawTxString, SplitConfig: splitConfig, Mode: &shallowMode})
if bpuTx != nil {
t.Tx = *bpuTx
}
return
}
// FromString takes a BOB formatted string
func (t *Tx) FromString(line string) (err error) {
err = t.FromBytes([]byte(line))
return
}
// FromTx takes a bt.Tx
func (t *Tx) FromTx(tx *transaction.Transaction) error {
if tx == nil {
return fmt.Errorf("Tx must be set")
}
var separator = "|"
var l = bpu.IncludeL
var opReturn = uint8(106)
var splitConfig = []bpu.SplitConfig{
{
Token: &bpu.Token{
Op: &opReturn,
},
Include: &l,
},
{
Token: &bpu.Token{
S: &separator,
},
Require: &opReturn,
},
}
bpuTx, err := bpu.Parse(bpu.ParseConfig{Tx: tx, SplitConfig: splitConfig, Mode: &shallowMode})
if err != nil {
return err
}
if bpuTx != nil {
t.Tx = *bpuTx
}
return nil
}
// ToRawTxString converts the BOBTx to a libsv.transaction, and outputs the raw hex
func (t *Tx) ToRawTxString() (string, error) {
tx, err := t.ToTx()
if err != nil {
return "", err
}
return tx.String(), nil
}
// ToString returns a json string of bobTx
func (t *Tx) ToString() (string, error) {
// Create JSON from the instance data.
b, err := json.Marshal(t)
return string(b), err
}
// ToTx returns a bt.Tx
func (t *Tx) ToTx() (*transaction.Transaction, error) {
tx := transaction.NewTransaction()
tx.LockTime = t.Lock
for _, in := range t.In {
if len(in.Tape) == 0 || len(in.Tape[0].Cell) == 0 {
return nil, fmt.Errorf("failed to process inputs. More tapes or cells than expected. %+v", in.Tape)
}
add, err := script.NewAddressFromString(*in.E.A)
if err != nil {
return nil, err
}
prevTxScript, _ := p2pkh.Lock(add)
var scriptAsm []string
// TODO: This will break if there is ever a bpu splitter present in inputs
for _, cell := range in.Tape[0].Cell {
cellData := *cell.H
scriptAsm = append(scriptAsm, cellData)
}
builtUnlockScript, err := script.NewFromASM(strings.Join(scriptAsm, " "))
if err != nil {
return nil, fmt.Errorf("failed to get script from asm: %v error: %w", scriptAsm, err)
}
v := uint64(0)
if in.E.V != nil {
v = *in.E.V
}
// add inputs
i := &transaction.TransactionInput{
SourceTxOutIndex: in.E.I, // TODO: This might be getting set incorrectly?
UnlockingScript: builtUnlockScript,
SequenceNumber: in.Seq,
}
i.SourceTXID, _ = chainhash.NewHashFromHex(*in.E.H)
i.SetSourceTxOutput(&transaction.TransactionOutput{
Satoshis: v,
LockingScript: prevTxScript,
})
tx.Inputs = append(tx.Inputs, i) // AddInput(i)
}
// add outputs
for _, out := range t.Out {
// Build the locking script
var lockScriptAsm []string
for tapeIdx, tape := range out.Tape {
for cellIdx, cell := range tape.Cell {
if cellIdx == 0 && tapeIdx > 1 {
// add the separator back in
lockScriptAsm = append(lockScriptAsm, ProtocolDelimiterAsm)
}
if cell.H != nil {
lockScriptAsm = append(lockScriptAsm, *cell.H)
} else if cell.Ops != nil {
lockScriptAsm = append(lockScriptAsm, *cell.Ops)
}
}
}
lockingScript, _ := script.NewFromASM(strings.Join(lockScriptAsm, " "))
o := &transaction.TransactionOutput{
Satoshis: *out.E.V,
LockingScript: lockingScript,
}
tx.AddOutput(o)
}
return tx, nil
}