forked from bitpay/bitcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode.js
165 lines (142 loc) · 4.71 KB
/
opcode.js
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
'use strict';
var _ = require('lodash');
var chai = require('chai');
var should = chai.should();
var expect = chai.expect;
var bitcore = require('..');
var Opcode = bitcore.Opcode;
describe('Opcode', function() {
it('should create a new Opcode', function() {
var opcode = new Opcode(5);
should.exist(opcode);
});
it('should convert to a string with this handy syntax', function() {
Opcode(0).toString().should.equal('OP_0');
Opcode(96).toString().should.equal('OP_16');
Opcode(97).toString().should.equal('OP_NOP');
});
it('should convert to a number with this handy syntax', function() {
Opcode('OP_0').toNumber().should.equal(0);
Opcode('OP_16').toNumber().should.equal(96);
Opcode('OP_NOP').toNumber().should.equal(97);
});
describe('#fromNumber', function() {
it('should work for 0', function() {
Opcode.fromNumber(0).num.should.equal(0);
});
it('should fail for non-number', function() {
Opcode.fromNumber.bind(null, 'a string').should.throw('Invalid Argument');
});
});
describe('#set', function() {
it('should work for object', function() {
Opcode(42).num.should.equal(42);
});
it('should fail for empty-object', function() {
expect(function() {
Opcode();
}).to.throw(TypeError);
});
});
describe('#toNumber', function() {
it('should work for 0', function() {
Opcode.fromNumber(0).toNumber().should.equal(0);
});
});
describe('#buffer', function() {
it('should correctly input/output a buffer', function() {
var buf = Buffer.from('a6', 'hex');
Opcode.fromBuffer(buf).toBuffer().should.deep.equal(buf);
});
});
describe('#fromString', function() {
it('should work for OP_0', function() {
Opcode.fromString('OP_0').num.should.equal(0);
});
it('should fail for invalid string', function() {
Opcode.fromString.bind(null, 'OP_SATOSHI').should.throw('Invalid opcodestr');
Opcode.fromString.bind(null, 'BANANA').should.throw('Invalid opcodestr');
});
it('should fail for non-string', function() {
Opcode.fromString.bind(null, 123).should.throw('Invalid Argument');
});
});
describe('#toString', function() {
it('should work for OP_0', function() {
Opcode.fromString('OP_0').toString().should.equal('OP_0');
});
it('should not work for non-opcode', function() {
expect(function(){
Opcode('OP_NOTACODE').toString();
}).to.throw('Opcode does not have a string representation');
});
});
describe('@map', function() {
it('should have a map containing 118 elements', function() {
_.size(Opcode.map).should.equal(118);
});
});
describe('@reverseMap', function() {
it('should exist and have op 185', function() {
should.exist(Opcode.reverseMap);
Opcode.reverseMap[185].should.equal('OP_NOP10');
});
});
var smallints = [
Opcode('OP_0'),
Opcode('OP_1'),
Opcode('OP_2'),
Opcode('OP_3'),
Opcode('OP_4'),
Opcode('OP_5'),
Opcode('OP_6'),
Opcode('OP_7'),
Opcode('OP_8'),
Opcode('OP_9'),
Opcode('OP_10'),
Opcode('OP_11'),
Opcode('OP_12'),
Opcode('OP_13'),
Opcode('OP_14'),
Opcode('OP_15'),
Opcode('OP_16')
];
describe('@smallInt', function() {
var testSmallInt = function(n, op) {
Opcode.smallInt(n).toString().should.equal(op.toString());
};
for (var i = 0; i < smallints.length; i++) {
var op = smallints[i];
it('should work for small int ' + op, testSmallInt.bind(null, i, op));
}
it('with not number', function () {
Opcode.smallInt.bind(null, '2').should.throw('Invalid Argument');
});
it('with n equal -1', function () {
Opcode.smallInt.bind(null, -1).should.throw('Invalid Argument');
});
it('with n equal 17', function () {
Opcode.smallInt.bind(null, 17).should.throw('Invalid Argument');
});
});
describe('@isSmallIntOp', function() {
var testIsSmallInt = function(op) {
Opcode.isSmallIntOp(op).should.equal(true);
};
for (var i = 0; i < smallints.length; i++) {
var op = smallints[i];
it('should work for small int ' + op, testIsSmallInt.bind(null, op));
}
it('should work for non-small ints', function() {
Opcode.isSmallIntOp(Opcode('OP_RETURN')).should.equal(false);
Opcode.isSmallIntOp(Opcode('OP_CHECKSIG')).should.equal(false);
Opcode.isSmallIntOp(Opcode('OP_IF')).should.equal(false);
Opcode.isSmallIntOp(Opcode('OP_NOP')).should.equal(false);
});
});
describe('#inspect', function() {
it('should output opcode by name, hex, and decimal', function() {
Opcode.fromString('OP_NOP').inspect().should.equal('<Opcode: OP_NOP, hex: 61, decimal: 97>');
});
});
});