-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbob_test.go
419 lines (384 loc) · 14.4 KB
/
bob_test.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package aip
import (
"encoding/hex"
"fmt"
"testing"
"github.com/bitcoin-sv/go-sdk/script"
"github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoinschema/go-bob"
"github.com/bitcoinschema/go-bpu"
)
// TestNewFromTape will test the method NewFromTape()
func TestNewFromTape(t *testing.T) {
t.Parallel()
// Parse from string into BOB
bobValidData, err := bob.NewFromString(sampleValidBobTx)
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
var bobInvalidData *bob.Tx
if bobInvalidData, err = bob.NewFromString(sampleInvalidBobTx); err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
var (
// Testing private methods
tests = []struct {
inputTapes []bpu.Tape
inputIndex int
expectedSignature string
expectedAlgorithm Algorithm
expectedComponent string
expectedNil bool
expectedValidation bool
}{
{
bobValidData.Out[0].Tape,
2,
"H+lubfcz5Z2oG8B7HwmP8Z+tALP+KNOPgedo7UTXwW8LBpMkgCgatCdpvbtf7wZZQSIMz83emmAvVS4S3F5X1wo=",
BitcoinECDSA,
"134a6TXxzgQ9Az3w8BcvgdZyA5UqRL89da",
false,
true,
},
{
bobInvalidData.Out[0].Tape,
2,
"H+lubfcz5Z2oG8B7HwmP8Z+tALP+KNOPgedo7UTXwW8LBpMkgCgatCdpvbtf7wZZQSIMz83emmAvVS4S3F5X1wo=",
BitcoinECDSA,
"invalid-address",
false,
false,
},
{
[]bpu.Tape{*new(bpu.Tape)},
0,
"",
"",
"",
false,
false,
},
}
)
// Run tests
for idx, test := range tests {
if a := NewFromTape(test.inputTapes[test.inputIndex]); a == nil && !test.expectedNil {
t.Errorf("%d %s Failed: [%v] inputted and nil was not expected", idx, t.Name(), test.inputTapes[test.inputIndex])
} else if a != nil && test.expectedNil {
t.Errorf("%d %s Failed: [%v] inputted and nil was expected", idx, t.Name(), test.inputTapes[test.inputIndex])
} else if a != nil && a.Signature != test.expectedSignature {
t.Errorf("%d %s Failed: [%v] inputted and expected [%s] but got [%s]", idx, t.Name(), test.inputTapes[test.inputIndex], test.expectedSignature, a.Signature)
} else if a != nil && a.Algorithm != test.expectedAlgorithm {
t.Errorf("%d %s Failed: [%v] inputted and expected [%s] but got [%s]", idx, t.Name(), test.inputTapes[test.inputIndex], test.expectedAlgorithm, a.Algorithm)
} else if a != nil && a.AlgorithmSigningComponent != test.expectedComponent {
t.Errorf("%d %s Failed: [%v] inputted and expected [%s] but got [%s]", idx, t.Name(), test.inputTapes[test.inputIndex], test.expectedComponent, a.AlgorithmSigningComponent)
} else if a != nil && len(test.inputTapes) > 1 {
valid, err := ValidateTapes(test.inputTapes)
if valid && !test.expectedValidation {
t.Errorf("%d %s Failed: [%v] inputted and validation should have failed", idx, t.Name(), test.inputTapes)
} else if !valid && test.expectedValidation && err != nil {
t.Errorf("%d %s Failed: [%v] inputted and validation should have passed, error: %s", idx, t.Name(), test.inputTapes, err.Error())
}
}
}
}
// ExampleNewFromTape example using NewFromTape()
func ExampleNewFromTape() {
// Get BOB data from a TX
bobValidData, err := bob.NewFromString(sampleValidBobTx)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get from tape given the AIP index
a := NewFromTape(bobValidData.Out[0].Tape[2])
fmt.Printf("address: %s signature: %s", a.AlgorithmSigningComponent, a.Signature)
// Output:address: 134a6TXxzgQ9Az3w8BcvgdZyA5UqRL89da signature: H+lubfcz5Z2oG8B7HwmP8Z+tALP+KNOPgedo7UTXwW8LBpMkgCgatCdpvbtf7wZZQSIMz83emmAvVS4S3F5X1wo=
}
// BenchmarkNewFromTape benchmarks the method NewFromTape()
func BenchmarkNewFromTape(b *testing.B) {
bobValidData, _ := bob.NewFromString(sampleValidBobTx)
for i := 0; i < b.N; i++ {
_ = NewFromTape(bobValidData.Out[0].Tape[2])
}
}
// TestNewFromTapes will test the method NewFromTapes()
func TestNewFromTapes(t *testing.T) {
t.Parallel()
// Parse from string into BOB
bobValidData, err := bob.NewFromString(sampleValidBobTx)
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
var bobInvalidData *bob.Tx
if bobInvalidData, err = bob.NewFromString(sampleInvalidBobTx); err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
var (
// Testing private methods
tests = []struct {
inputTapes []bpu.Tape
expectedSignature string
expectedAlgorithm Algorithm
expectedComponent string
expectedData0 string
expectedData1 string
expectedData2 string
expectedNil bool
expectedValidation bool
}{
{
bobValidData.Out[0].Tape,
"H+lubfcz5Z2oG8B7HwmP8Z+tALP+KNOPgedo7UTXwW8LBpMkgCgatCdpvbtf7wZZQSIMz83emmAvVS4S3F5X1wo=",
BitcoinECDSA,
"134a6TXxzgQ9Az3w8BcvgdZyA5UqRL89da",
opReturn,
"1BAPSuaPnfGnSBM3GLV9yhxUdYe4vGbdMT",
"ATTEST",
false,
true,
},
{
bobInvalidData.Out[0].Tape,
"H+lubfcz5Z2oG8B7HwmP8Z+tALP+KNOPgedo7UTXwW8LBpMkgCgatCdpvbtf7wZZQSIMz83emmAvVS4S3F5X1wo=",
BitcoinECDSA,
"invalid-address",
opReturn,
"1BAPSuaPnfGnSBM3GLV9yhxUdYe4vGbdMT",
"ATTEST",
false,
false,
},
{
[]bpu.Tape{*new(bpu.Tape)},
"",
"",
"",
"",
"",
"",
true,
false,
},
}
)
// Run tests
for _, test := range tests {
if a := NewFromTapes(test.inputTapes); a == nil && !test.expectedNil {
t.Errorf("%s Failed: [%v] inputted and nil was not expected (aip)", t.Name(), test.inputTapes)
} else if a != nil && test.expectedNil {
t.Errorf("%s Failed: [%v] inputted and nil was expected (aip)", t.Name(), test.inputTapes)
} else if a != nil && a.Signature != test.expectedSignature {
t.Errorf("%s Failed: [%v] inputted and expected [%s] but got [%s]", t.Name(), test.inputTapes, test.expectedSignature, a.Signature)
} else if a != nil && a.Algorithm != test.expectedAlgorithm {
t.Errorf("%s Failed: [%v] inputted and expected [%s] but got [%s]", t.Name(), test.inputTapes, test.expectedAlgorithm, a.Algorithm)
} else if a != nil && a.AlgorithmSigningComponent != test.expectedComponent {
t.Errorf("%s Failed: [%v] inputted and expected [%s] but got [%s]", t.Name(), test.inputTapes, test.expectedComponent, a.AlgorithmSigningComponent)
} else if a != nil && len(a.Data) > 0 && a.Data[0] != test.expectedData0 {
t.Errorf("%s Failed: [%v] inputted and expected [%s] but got [%s]", t.Name(), test.inputTapes, test.expectedData0, a.Data[0])
} else if a != nil && len(a.Data) > 0 && a.Data[1] != test.expectedData1 {
t.Errorf("%s Failed: [%v] inputted and expected [%s] but got [%s]", t.Name(), test.inputTapes, test.expectedData1, a.Data[1])
} else if a != nil && len(a.Data) > 0 && a.Data[2] != test.expectedData2 {
t.Errorf("%s Failed: [%v] inputted and expected [%s] but got [%s]", t.Name(), test.inputTapes, test.expectedData2, a.Data[2])
} else if a != nil && len(test.inputTapes) > 1 {
var valid bool
valid, err = ValidateTapes(test.inputTapes)
if valid && !test.expectedValidation {
t.Errorf("%s Failed: [%v] inputted and validation should have failed", t.Name(), test.inputTapes)
} else if !valid && test.expectedValidation && err != nil {
t.Errorf("%s Failed: [%v] inputted and validation should have passed, error: %s", t.Name(), test.inputTapes, err.Error())
}
}
}
}
// ExampleNewFromTapes example using NewFromTapes()
func ExampleNewFromTapes() {
// Get BOB data from a TX
bobValidData, err := bob.NewFromString(sampleValidBobTx)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get from tape given the AIP index
a := NewFromTapes(bobValidData.Out[0].Tape)
fmt.Printf("address: %s signature: %s", a.AlgorithmSigningComponent, a.Signature)
// Output:address: 134a6TXxzgQ9Az3w8BcvgdZyA5UqRL89da signature: H+lubfcz5Z2oG8B7HwmP8Z+tALP+KNOPgedo7UTXwW8LBpMkgCgatCdpvbtf7wZZQSIMz83emmAvVS4S3F5X1wo=
}
// BenchmarkNewFromTapes benchmarks the method NewFromTapes()
func BenchmarkNewFromTapes(b *testing.B) {
bobValidData, _ := bob.NewFromString(sampleValidBobTx)
for i := 0; i < b.N; i++ {
_ = NewFromTapes(bobValidData.Out[0].Tape)
}
}
// TestValidateTapes will test the method ValidateTapes()
func TestValidateTapes(t *testing.T) {
t.Parallel()
// Parse from string into BOB
bobValidData, err := bob.NewFromString(sampleValidBobTx)
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
var bobInvalidData *bob.Tx
if bobInvalidData, err = bob.NewFromString(sampleInvalidBobTx); err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
var (
// Testing private methods
tests = []struct {
inputTapes []bpu.Tape
expectedValidation bool
}{
{
bobValidData.Out[0].Tape,
true,
},
{
bobInvalidData.Out[0].Tape,
false,
},
{
[]bpu.Tape{*new(bpu.Tape)},
false,
},
}
)
// Run tests
for idx, test := range tests {
if valid, err := ValidateTapes(test.inputTapes); valid && !test.expectedValidation {
t.Errorf("%d %s Failed: inputted and validation should have failed", idx, t.Name())
} else if !valid && test.expectedValidation && err != nil {
t.Errorf("%d %s Failed: inputted and validation should have passed, error: %s", idx, t.Name(), err.Error())
}
}
}
// ExampleValidateTapes example using ValidateTapes()
func ExampleValidateTapes() {
// Get BOB data from a TX
bobValidData, err := bob.NewFromString(sampleValidBobTx)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get from tape
if valid, err := ValidateTapes(bobValidData.Out[0].Tape); valid {
fmt.Print("AIP is valid")
} else if err != nil {
fmt.Printf("AIP is invalid: %s", err.Error())
}
// Output:AIP is valid
}
// BenchmarkValidateTapes benchmarks the method ValidateTapes()
func BenchmarkValidateTapes(b *testing.B) {
bobValidData, _ := bob.NewFromString(sampleValidBobTx)
for i := 0; i < b.N; i++ {
_, _ = ValidateTapes(bobValidData.Out[0].Tape)
}
}
// getBobOutput helper to get op_return in BOB format
func getBobOutput() bpu.Output {
// Create a transaction
// privateKey, _ := bitcoin.PrivateKeyFromString(examplePrivateKey)
tx := transaction.NewTransaction()
scr := &script.Script{}
scr.AppendOpcodes(script.OpFALSE, script.OpRETURN)
scr.AppendPushDataArray([][]byte{[]byte("prefix1"), []byte("example data"), {0x13, 0x37}})
tx.AddOutput(&transaction.TransactionOutput{
Satoshis: 0,
LockingScript: scr,
})
// tx, _ := bitcoin.CreateTx(nil, nil, []bitcoin.OpReturnData{opReturn})
// Create the bob tx from hex
bobTx, _ := bob.NewFromRawTxString(hex.EncodeToString(tx.Bytes()))
return bobTx.Out[0]
}
// // TestSignBobOpReturnData tests for nil case in SignBobOpReturnData()
// func TestSignBobOpReturnData(t *testing.T) {
// t.Parallel()
// var (
// // Testing private methods
// tests = []struct {
// inputPrivateKey string
// inputAlgorithm Algorithm
// inputData bpu.Output
// expectedSignature string
// expectedAipNil bool
// expectedOutNil bool
// expectedError bool
// expectedValidation bool
// }{
// {
// "80699541455b59a8a8a33b85892319de8b8e8944eb8b48e9467137825ae192e59f01",
// BitcoinECDSA,
// getBobOutput(),
// "G/tmf2aRfTqri7pCF793/xDZO2COIzcY2BRpb5P0o+zGIG0tCF3JJLadQwCCC+Lu+Xcanv+Fl82lrk3hVlo8bXY=",
// false,
// false,
// false,
// true,
// },
// {
// "",
// BitcoinECDSA,
// getBobOutput(),
// "",
// true,
// true,
// true,
// false,
// },
// {
// "80699541455b59a8a8a33b85892319de8b8e8944eb8b48e9467137825ae192e59f01",
// Paymail,
// getBobOutput(),
// "G/tmf2aRfTqri7pCF793/xDZO2COIzcY2BRpb5P0o+zGIG0tCF3JJLadQwCCC+Lu+Xcanv+Fl82lrk3hVlo8bXY=",
// false,
// false,
// false,
// true,
// },
// }
// )
// // Run tests
// for _, test := range tests {
// if out, a, err := SignBobOpReturnData(test.inputPrivateKey, test.inputAlgorithm, test.inputData); err != nil && !test.expectedError {
// t.Errorf("%s Failed: [%s] [%s] [%v] inputted and error not expected but got: %s", t.Name(), test.inputPrivateKey, test.inputAlgorithm, test.inputData, err.Error())
// } else if err == nil && test.expectedError {
// t.Errorf("%s Failed: [%s] [%s] [%v] inputted and error was expected", t.Name(), test.inputPrivateKey, test.inputAlgorithm, test.inputData)
// } else if a == nil && !test.expectedAipNil {
// t.Errorf("%s Failed: [%s] [%s] [%v] inputted and nil was not expected (aip)", t.Name(), test.inputPrivateKey, test.inputAlgorithm, test.inputData)
// } else if a != nil && test.expectedAipNil {
// t.Errorf("%s Failed: [%s] [%s] [%v] inputted and nil was expected (aip)", t.Name(), test.inputPrivateKey, test.inputAlgorithm, test.inputData)
// } else if out == nil && !test.expectedOutNil {
// t.Errorf("%s Failed: [%s] [%s] [%v] inputted and nil was not expected (out)", t.Name(), test.inputPrivateKey, test.inputAlgorithm, test.inputData)
// } else if out != nil && test.expectedOutNil {
// t.Errorf("%s Failed: [%s] [%s] [%v] inputted and nil was expected (out)", t.Name(), test.inputPrivateKey, test.inputAlgorithm, test.inputData)
// } else if a != nil && a.Signature != test.expectedSignature {
// t.Errorf("%s Failed: [%s] [%s] [%v] inputted and expected signature [%s] but got [%s]", t.Name(), test.inputPrivateKey, test.inputAlgorithm, test.inputData, test.expectedSignature, a.Signature)
// } else if a != nil {
// var valid bool
// if valid, err = a.Validate(); valid && !test.expectedValidation {
// t.Errorf("%s Failed: [%s] [%s] [%v] inputted and validation should have failed", t.Name(), test.inputPrivateKey, test.inputAlgorithm, test.inputData)
// } else if !valid && test.expectedValidation && err != nil {
// t.Errorf("%s Failed: [%s] [%s] [%v] inputted and validation should have passed, error: %s", t.Name(), test.inputPrivateKey, test.inputAlgorithm, test.inputData, err.Error())
// }
// }
// }
// }
// // ExampleSignBobOpReturnData example using SignBobOpReturnData()
// func ExampleSignBobOpReturnData() {
// _, a, err := SignBobOpReturnData(examplePrivateKey, BitcoinECDSA, getBobOutput())
// if err != nil {
// fmt.Printf("error occurred: %s", err.Error())
// return
// }
// fmt.Printf("signature: %s", a.Signature)
// // Output:signature: G1oBzIBhluBmdu2U6xfb75ACIPpDVmjCV4OV1hVAlnovG1IN3jyIOYK/HJQuH5UOjf2rfaI45SZqPxx9llN+Mgs=
// }
// BenchmarkSignBobOpReturnData benchmarks the method SignBobOpReturnData()
func BenchmarkSignBobOpReturnData(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, _ = SignBobOpReturnData(examplePrivateKey, BitcoinECDSA, getBobOutput())
}
}
// todo: test a TX with signature of specific indexes