-
Notifications
You must be signed in to change notification settings - Fork 256
/
OpCodeTests.swift
779 lines (703 loc) · 31.5 KB
/
OpCodeTests.swift
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//
// OpCodeTests.swift
//
// Copyright © 2018 BitcoinKit developers
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
@testable import BitcoinKit
class OpCodeTests: XCTestCase {
var context: ScriptExecutionContext!
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
context = ScriptExecutionContext()
}
func testOp1Negate() {
let opcode = OpCode.OP_1NEGATE
do {
try opcode.execute(context)
let num = try context.number(at: -1)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(num, -1)
} catch let error {
fail(with: opcode, error: error)
}
}
// OP_N is not working correctly right now because we didn't implemente bignum
// After implementing bignum, testOpN() should be enabled
func testOpN() {
let vectors: [(OpCodeProtocol, Int32)] = [(OpCode.OP_1NEGATE, -1),
(OpCode.OP_1,1),
(OpCode.OP_2,2),
(OpCode.OP_3,3),
(OpCode.OP_4,4),
(OpCode.OP_5,5),
(OpCode.OP_6,6),
(OpCode.OP_7,7),
(OpCode.OP_8,8),
(OpCode.OP_9,9),
(OpCode.OP_10,10),
(OpCode.OP_11,11),
(OpCode.OP_12,12),
(OpCode.OP_13,13),
(OpCode.OP_14,14),
(OpCode.OP_15,15),
(OpCode.OP_16,16)]
for (i, (opcode, expectedNumber)) in vectors.enumerated() {
do {
try opcode.execute(context)
let num = try context.number(at: -1)
XCTAssertEqual(num, expectedNumber, "\(opcode.name)(\(opcode.value) execution test.")
XCTAssertEqual(context.stack.count, i + 1)
} catch let error {
fail(with: opcode, error: error)
}
}
}
func testOpVerify() {
pushRandomDataOnStack(context)
let stackCountAtFirst: Int = context.stack.count
let opcode = OpCode.OP_VERIFY
// OP_CODE basic specification
XCTAssertEqual(opcode.name, "OP_VERIFY")
XCTAssertEqual(opcode.value, 0x69)
// OP_VERIFY success
do {
context.pushToStack(true)
XCTAssertEqual(context.stack.count, stackCountAtFirst + 1)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, stackCountAtFirst, "\(opcode.name)(\(String(format: "%02x", opcode.value)) execution test.")
} catch let error {
fail(with: opcode, error: error)
}
// OP_VERIFY fail
do {
context.pushToStack(false)
try opcode.execute(context)
XCTFail("\(opcode.name)(\(opcode.value) execution should throw error.")
} catch OpCodeExecutionError.error("OP_VERIFY failed.") {
// success
} catch let error {
XCTFail("Should throw OpCodeExecutionError .error(\"OP_VERIFY failed.\"), but threw \(error)")
}
}
func testOpDuplicate() {
pushRandomDataOnStack(context)
let stackCountAtFirst: Int = context.stack.count
let opcode = OpCode.OP_DUP
// OP_CODE basic specification
XCTAssertEqual(opcode.name, "OP_DUP")
XCTAssertEqual(opcode.value, 0x76)
// OP_DUP success
do {
// Stack has more than 1 item
XCTAssertGreaterThanOrEqual(context.stack.count, 1)
let stackSnapShot: [Data] = context.stack
let dataOnTop: Data = context.stack.last!
try opcode.execute(context)
XCTAssertEqual(context.stack.count, stackCountAtFirst + 1, "\(opcode.name)(\(String(format: "%02x", opcode.value)) test: One data should be added to stack.")
XCTAssertEqual(context.stack.dropLast().map { Data($0) }, stackSnapShot, "\(opcode.name)(\(String(format: "%02x", opcode.value)) test: The data except the top should be the same after the execution.")
XCTAssertEqual(context.stack.last!, dataOnTop, "\(opcode.name)(\(String(format: "%02x", opcode.value)) test: The data on top should be copied and pushed.")
} catch let error {
fail(with: opcode, error: error)
}
// OP_DUP fail
do {
context.resetStack()
XCTAssertEqual(context.stack.count, 0)
try opcode.execute(context)
XCTFail("\(opcode.name)(\(opcode.value) execution should throw error when stack is empty.")
} catch OpCodeExecutionError.opcodeRequiresItemsOnStack(1) {
// success
} catch let error {
XCTFail("Should throw OpCodeExecutionError .opcodeRequiresItemsOnStack(1), but threw \(error)")
}
}
func testOpCat() {
let opcode = OpCode.OP_CAT
// maxlen_x y OP_CAT -> failure
// Concatenating any operand except an empty vector, including a single byte value (e.g. OP_1),
// onto a maximum sized array causes failure
do {
try context.pushToStack(Data(count: BTC_MAX_SCRIPT_ELEMENT_SIZE))
try context.pushToStack(1)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTFail("\(opcode.name)(\(opcode.value) execution should throw error when push value size limit exceeded.")
} catch OpCodeExecutionError.error("Push value size limit exceeded") {
// success
} catch let error {
XCTFail("Should throw OpCodeExecutionError .error(\"Push value size limit exceeded\"), but threw \(error)")
}
// large_x large_y OP_CAT -> failure
// Concatenating two operands, where the total length is greater than MAX_SCRIPT_ELEMENT_SIZE, causes failure
do {
context.resetStack()
try context.pushToStack(Data(count: BTC_MAX_SCRIPT_ELEMENT_SIZE / 2 + 1))
try context.pushToStack(Data(count: BTC_MAX_SCRIPT_ELEMENT_SIZE / 2))
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTFail("\(opcode.name)(\(opcode.value) execution should throw error when push value size limit exceeded.")
} catch OpCodeExecutionError.error("Push value size limit exceeded") {
// success
} catch let error {
XCTFail("Should throw OpCodeExecutionError .error(\"Push value size limit exceeded\"), but threw \(error)")
}
// OP_0 OP_0 OP_CAT -> OP_0
// Concatenating two empty arrays results in an empty array
do {
context.resetStack()
try context.pushToStack(Data())
try context.pushToStack(Data())
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.data(at: -1), Data())
} catch let error {
fail(with: opcode, error: error)
}
// x OP_0 OP_CAT -> x
// Concatenating an empty array onto any operand results in the operand, including when len(x) = MAX_SCRIPT_ELEMENT_SIZE
do {
context.resetStack()
try context.pushToStack(Data(count: BTC_MAX_SCRIPT_ELEMENT_SIZE))
try context.pushToStack(Data())
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.data(at: -1), Data(count: BTC_MAX_SCRIPT_ELEMENT_SIZE))
} catch let error {
fail(with: opcode, error: error)
}
// OP_0 x OP_CAT -> x
// Concatenating any operand onto an empty array results in the operand, including when len(x) = MAX_SCRIPT_ELEMENT_SIZE
do {
context.resetStack()
try context.pushToStack(Data())
try context.pushToStack(Data(count: BTC_MAX_SCRIPT_ELEMENT_SIZE))
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.data(at: -1), Data(count: BTC_MAX_SCRIPT_ELEMENT_SIZE))
} catch let error {
fail(with: opcode, error: error)
}
// {Ox11} {0x22, 0x33} OP_CAT -> 0x112233
// Concatenating two operands generates the correct result
do {
context.resetStack()
try context.pushToStack(Data([0x11]))
try context.pushToStack(Data([0x22, 0x33]))
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.data(at: -1), Data([0x11, 0x22, 0x33]))
} catch let error {
fail(with: opcode, error: error)
}
}
func testOpSize() {
let opcode = OpCode.OP_SIZE
// OP_SIZE succeeds
do {
try context.pushToStack(Data([0x01]))
XCTAssertEqual(context.stack.count, 1)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 2)
XCTAssertEqual(try context.number(at: -1), 1)
XCTAssertEqual(context.data(at: -2), Data([0x01]))
} catch let error {
fail(with: opcode, error: error)
}
// OP_SIZE succeeds with empty array
do {
context.resetStack()
try context.pushToStack(Data())
XCTAssertEqual(context.stack.count, 1)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 2)
XCTAssertEqual(try context.number(at: -1), 0)
XCTAssertEqual(context.data(at: -2), Data())
} catch let error {
fail(with: opcode, error: error)
}
// OP_SIZE succeeds with maximum sized array case
do {
context.resetStack()
try context.pushToStack(Data(count: BTC_MAX_SCRIPT_ELEMENT_SIZE))
XCTAssertEqual(context.stack.count, 1)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 2)
XCTAssertEqual(try context.number(at: -1), Int32(BTC_MAX_SCRIPT_ELEMENT_SIZE))
XCTAssertEqual(context.data(at: -2), Data(count: BTC_MAX_SCRIPT_ELEMENT_SIZE))
} catch let error {
fail(with: opcode, error: error)
}
}
func testOpSplit() {
let opcode = OpCode.OP_SPLIT
// OP_0 0 OP_SPLIT -> OP_0 OP_0
// Execution of OP_SPLIT on empty array results in two empty arrays.
do {
try context.pushToStack(Data())
try context.pushToStack(0)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 2)
XCTAssertEqual(context.data(at: -1), Data())
XCTAssertEqual(context.data(at: -2), Data())
} catch let error {
fail(with: opcode, error: error)
}
// x 0 OP_SPLIT -> OP_0 x
do {
context.resetStack()
try context.pushToStack(Data([0x01]))
try context.pushToStack(0)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 2)
XCTAssertEqual(context.data(at: -1), Data([0x01]))
XCTAssertEqual(context.data(at: -2), Data())
} catch let error {
fail(with: opcode, error: error)
}
// x len(x) OP_SPLIT -> x OP_0
do {
context.resetStack()
try context.pushToStack(Data([0x01]))
try context.pushToStack(Int32(Data([0x01]).count))
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 2)
XCTAssertEqual(context.data(at: -1), Data())
XCTAssertEqual(context.data(at: -2), Data([0x01]))
} catch let error {
fail(with: opcode, error: error)
}
// x (len(x) + 1) OP_SPLIT -> FAIL
do {
context.resetStack()
try context.pushToStack(Data([0x01]))
try context.pushToStack(Int32(Data([0x01]).count + 1))
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTFail("\(opcode.name)(\(opcode.value) execution should throw error with Invalid OP_SPLIT range.")
} catch OpCodeExecutionError.error("Invalid OP_SPLIT range") {
// success
} catch let error {
XCTFail("Should throw OpCodeExecutionError .error(\"Invalid OP_SPLIT range\"), but threw \(error)")
}
// successful cases
// {0x00, 0x11, 0x22} 0 OP_SPLIT -> OP_0 {0x00, 0x11, 0x22}
do {
context.resetStack()
try context.pushToStack(Data([0x00, 0x11, 0x22]))
try context.pushToStack(0)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 2)
XCTAssertEqual(context.data(at: -1), Data([0x00, 0x11, 0x22]))
XCTAssertEqual(context.data(at: -2), Data())
} catch let error {
fail(with: opcode, error: error)
}
// {0x00, 0x11, 0x22} 1 OP_SPLIT -> {0x00} {0x11, 0x22}
do {
context.resetStack()
try context.pushToStack(Data([0x00, 0x11, 0x22]))
try context.pushToStack(1)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 2)
XCTAssertEqual(context.data(at: -1), Data([0x11, 0x22]))
XCTAssertEqual(context.data(at: -2), Data([0x00]))
} catch let error {
fail(with: opcode, error: error)
}
// {0x00, 0x11, 0x22} 2 OP_SPLIT -> {0x00, 0x11} {0x22}
do {
context.resetStack()
try context.pushToStack(Data([0x00, 0x11, 0x22]))
try context.pushToStack(2)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 2)
XCTAssertEqual(context.data(at: -1), Data([0x22]))
XCTAssertEqual(context.data(at: -2), Data([0x00, 0x11]))
} catch let error {
fail(with: opcode, error: error)
}
// {0x00, 0x11, 0x22} 3 OP_SPLIT -> {0x00, 0x11, 0x22} OP_0
do {
context.resetStack()
try context.pushToStack(Data([0x00, 0x11, 0x22]))
try context.pushToStack(3)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 2)
XCTAssertEqual(context.data(at: -1), Data())
XCTAssertEqual(context.data(at: -2), Data([0x00, 0x11, 0x22]))
} catch let error {
fail(with: opcode, error: error)
}
}
func testOpInvert() {
let opcode = OpCode.OP_INVERT
do {
try opcode.execute(context)
} catch OpCodeExecutionError.disabled {
// success
} catch let error {
XCTFail("Shoud throw OpCodeExecutionError.disabled, but threw \(error)")
}
}
func testOpAnd() {
let opcode = OpCode.OP_AND
// x1 x2 OP_AND -> failure, where len(x1) != len(x2). The two operands must be the same size.
do {
try context.pushToStack(Data(count: 1))
try context.pushToStack(Data(count: 3))
try opcode.execute(context)
XCTFail("\(opcode.name)(\(opcode.value) execution should throw error with Invalid operand size.")
} catch OpCodeExecutionError.error("Invalid operand size") {
// success
} catch let error {
XCTFail("Should throw OpCodeExecutionError .error(\"Invalid operand size\"), but threw \(error)")
}
// x1 x2 OP_AND -> x1 & x2. Check valid results.
do {
context.resetStack()
try context.pushToStack(Data(from: 0b11111100))
try context.pushToStack(Data(from: 0b00111111))
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.data(at: -1), Data(from: 0b00111100))
} catch let error {
fail(with: opcode, error: error)
}
}
func testOpOr() {
let opcode = OpCode.OP_OR
// x1 x2 OP_OR -> failure, where len(x1) != len(x2). The two operands must be the same size.
do {
try context.pushToStack(Data(count: 1))
try context.pushToStack(Data(count: 3))
try opcode.execute(context)
XCTFail("\(opcode.name)(\(opcode.value) execution should throw error with Invalid operand size.")
} catch OpCodeExecutionError.error("Invalid operand size") {
// success
} catch let error {
XCTFail("Should throw OpCodeExecutionError .error(\"Invalid operand size\"), but threw \(error)")
}
// x1 x2 OP_OR -> x1 | x2. Check valid results.
do {
context.resetStack()
try context.pushToStack(Data(from: 0b10110010))
try context.pushToStack(Data(from: 0b01011110))
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.data(at: -1), Data(from: 0b11111110))
} catch let error {
fail(with: opcode, error: error)
}
}
func testOpXor() {
let opcode = OpCode.OP_XOR
// x1 x2 OP_XOR -> failure, where len(x1) != len(x2). The two operands must be the same size.
do {
try context.pushToStack(Data(count: 1))
try context.pushToStack(Data(count: 3))
try opcode.execute(context)
XCTFail("\(opcode.name)(\(opcode.value) execution should throw error with Invalid operand size.")
} catch OpCodeExecutionError.error("Invalid operand size") {
// success
} catch let error {
XCTFail("Should throw OpCodeExecutionError .error(\"Invalid operand size\"), but threw \(error)")
}
// x1 x2 OP_XOR -> x1 xor x2. Check valid results.
do {
context.resetStack()
try context.pushToStack(Data(from: 0b00010100))
try context.pushToStack(Data(from: 0b00000101))
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.data(at: -1), Data(from: 0b00010001))
} catch let error {
fail(with: opcode, error: error)
}
}
func testOpEqualVerify() {
let opcode = OpCode.OP_EQUALVERIFY
// OP_EQUALVERIFY success
do {
try context.pushToStack(1)
try context.pushToStack(1)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 0)
} catch let error {
fail(with: opcode, error: error)
}
// OP_EQUALVERIFY fail
do {
context.resetStack()
try context.pushToStack(1)
try context.pushToStack(3)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
} catch OpCodeExecutionError.error("OP_EQUALVERIFY failed.") {
// success
XCTAssertEqual(context.stack.count, 1)
} catch let error {
fail(with: opcode, error: error)
}
}
func testOpEqual() {
let opcode = OpEqual()
// OP_EQUAL success
do {
try context.pushToStack(1)
try context.pushToStack(1)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.bool(at: -1), true)
} catch let error {
fail(with: opcode, error: error)
}
// OP_EQUAL fail
context.resetStack()
do {
try context.pushToStack(1)
try context.pushToStack(2)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.bool(at: -1), false)
} catch let error {
fail(with: opcode, error: error)
}
}
func testOpHash160() {
let opcode = OpCode.OP_HASH160
// OP_HASH160 success
do {
let data = "hello".data(using: .utf8)!
try context.pushToStack(data)
XCTAssertEqual(context.stack.count, 1)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.data(at: -1).hex, "b6a9c8c230722b7c748331a8b450f05566dc7d0f")
} catch let error {
fail(with: opcode, error: error)
}
// OP_HASH160 fail
do {
context.resetStack()
XCTAssertEqual(context.stack.count, 0)
try opcode.execute(context)
} catch OpCodeExecutionError.opcodeRequiresItemsOnStack(1) {
// do nothing equal success
} catch let error {
fail(with: opcode, error: error)
}
}
func testOpCheckSigBTC() {
let opcode = OpCode.OP_CHECKSIG
// BTC Transaction in testnet3
// https://api.blockcypher.com/v1/btc/test3/txs/0189910c263c4d416d5c5c2cf70744f9f6bcd5feaf0b149b02e5d88afbe78992
let prevTxID = "1524ca4eeb9066b4765effd472bc9e869240c4ecb5c1ee0edb40f8b666088231"
let hash = Data(Data(hex: prevTxID)!.reversed())
let index: UInt32 = 1
let outpoint = TransactionOutPoint(hash: hash, index: index)
let balance: UInt64 = 169012961
let privateKey = try! PrivateKey(wif: "92pMamV6jNyEq9pDpY4f6nBy9KpV2cfJT4L5zDUYiGqyQHJfF1K")
let fromPublicKey = privateKey.publicKey()
let subScript = Data(hex: "76a9142a539adfd7aefcc02e0196b4ccf76aea88a1f47088ac")!
let inputForSign = TransactionInput(previousOutput: outpoint, signatureScript: subScript, sequence: UInt32.max)
let unsignedTx = Transaction(version: 1, inputs: [inputForSign], outputs: [], lockTime: 0)
// sign
let hashType: SighashType = SighashType.BTC.ALL
let utxoToSign = TransactionOutput(value: balance, lockingScript: subScript)
let _txHash = unsignedTx.signatureHash(for: utxoToSign, inputIndex: 0, hashType: hashType)
guard let signature: Data = try? Crypto.sign(_txHash, privateKey: privateKey) else {
XCTFail("Failed to sign tx.")
return
}
let sigData: Data = signature + UInt8(hashType)
let pubkeyData: Data = fromPublicKey.data
// OP_CHECKSIG success
do {
context = ScriptExecutionContext(
transaction: unsignedTx,
utxoToVerify: utxoToSign,
inputIndex: 0)
try context.pushToStack(sigData) // sigData
try context.pushToStack(pubkeyData) // pubkeyData
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.bool(at: -1), true)
} catch let error {
fail(with: opcode, error: error)
}
// OP_CHECKSIG success(invalid signature)
do {
context = ScriptExecutionContext(
transaction: Transaction(
version: 1,
inputs: [TransactionInput(
previousOutput: TransactionOutPoint(hash: Data(), index: 0),
signatureScript: Data(),
sequence: 0)],
outputs: [],
lockTime: 0),
utxoToVerify: utxoToSign,
inputIndex: 0)
try context.pushToStack(sigData) // sigData
try context.pushToStack(pubkeyData) // pubkeyData
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.bool(at: -1), false)
} catch let error {
fail(with: opcode, error: error)
}
// OP_CHECKSIG fail
do {
context = ScriptExecutionContext()
XCTAssertEqual(context.stack.count, 0)
try context.pushToStack("".data(using: .utf8)!) // sigData
try context.pushToStack("".data(using: .utf8)!) // pubkeyData
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
} catch OpCodeExecutionError.error("The transaction or the utxo to verify is not set.") {
// do nothing equal success
} catch let error {
XCTFail("Shoud throw OpCodeExecutionError.error(\"The transaction or the utxo to verify is not set.\", but threw \(error)")
}
}
func testOpCheckSigBCH() {
let opcode = OpCode.OP_CHECKSIG
// BCH Transaction
// https://blockchair.com/bitcoin-cash/transaction/a793605eaed2c08c3f4c7906dd1526238ea04e9a16c785d46988c8cbd56f5088
let prevTxID = "3b3ffd3f597cc114c14b3655f61da60258e2ff69388cd2e463c60504a0d98f78"
let hash = Data(Data(hex: prevTxID)!.reversed())
let index: UInt32 = 1
let outpoint = TransactionOutPoint(hash: hash, index: index)
let balance: UInt64 = 2047900000
let privateKey = try! PrivateKey(wif: "92pMamV6jNyEq9pDpY4f6nBy9KpV2cfJT4L5zDUYiGqyQHJfF1K")
let fromPublicKey = privateKey.publicKey()
let subScript = Data(hex: "30440220356c7a8d55d4a63b1eab9cf00886cb66fe114d31f59faf242295e0e6e2aec9e5022052b6c4ee09b6564edf5a6d13f386e43ca6f9b4af19e9fefea413ac28d5b8d2df41036b7b02cc5592256d22e45c2c70c41b34e962cc370bcf672cea6f476e1db318c8")!
let inputForSign = TransactionInput(previousOutput: outpoint, signatureScript: subScript, sequence: UInt32.max)
let unsignedTx = Transaction(version: 1, inputs: [inputForSign], outputs: [], lockTime: 0)
// sign
let hashType: SighashType = SighashType.BCH.ALL
let utxoToSign = TransactionOutput(value: balance, lockingScript: subScript)
let _txHash = unsignedTx.signatureHash(for: utxoToSign, inputIndex: 0, hashType: hashType)
guard let signature: Data = try? Crypto.sign(_txHash, privateKey: privateKey) else {
XCTFail("Failed to sign tx.")
return
}
let sigData: Data = signature + UInt8(hashType)
let pubkeyData: Data = fromPublicKey.data
// OP_CHECKSIG success
do {
context = ScriptExecutionContext(
transaction: unsignedTx,
utxoToVerify: utxoToSign,
inputIndex: 0)
try context.pushToStack(sigData) // sigData
try context.pushToStack(pubkeyData) // pubkeyData
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.bool(at: -1), true)
} catch let error {
fail(with: opcode, error: error)
}
// OP_CHECKSIG success(invalid signature)
do {
context = ScriptExecutionContext(
transaction: Transaction(
version: 1,
inputs: [TransactionInput(
previousOutput: TransactionOutPoint(hash: Data(), index: 0),
signatureScript: Data(),
sequence: 0)],
outputs: [],
lockTime: 0),
utxoToVerify: utxoToSign,
inputIndex: 0)
try context.pushToStack(sigData) // sigData
try context.pushToStack(pubkeyData) // pubkeyData
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(context.bool(at: -1), false)
} catch let error {
fail(with: opcode, error: error)
}
// OP_CHECKSIG fail
do {
context = ScriptExecutionContext()
XCTAssertEqual(context.stack.count, 0)
try context.pushToStack("".data(using: .utf8)!) // sigData
try context.pushToStack("".data(using: .utf8)!) // pubkeyData
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
} catch OpCodeExecutionError.error("The transaction or the utxo to verify is not set.") {
// do nothing equal success
} catch let error {
XCTFail("Shoud throw OpCodeExecutionError.error(\"The transaction or the utxo to verify is not set.\", but threw \(error)")
}
}
func testOpInvalidOpCode() {
let opcode = OpCode.OP_INVALIDOPCODE
XCTAssertEqual(opcode.name, "OP_INVALIDOPCODE")
XCTAssertEqual(opcode.value, 0xff)
do {
try opcode.execute(context)
} catch OpCodeExecutionError.error("OP_INVALIDOPCODE should not be executed.") {
// success
} catch let error {
XCTFail("Shoud throw OpCodeExecutionError.error(\"OP_INVALIDOPCODE should not be executed.\", but threw \(error)")
}
}
}
private func pushRandomDataOnStack(_ context: ScriptExecutionContext) {
context.resetStack()
let rand = arc4random() % 50 + 1
for _ in (0..<rand) {
let nextRand = arc4random() % 32
switch nextRand {
case 0...16:
try! context.pushToStack(Int32(nextRand))
default:
let extraRand = arc4random()
try! context.pushToStack(Data(from: extraRand))
}
}
}
private func fail(with opCode: OpCodeProtocol, error: Error) {
XCTFail("\(opCode.name)(\(opCode.value)) execution should not fail.\nError: \(error)")
}