@opcat-labs/opcat 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.yaml +3 -0
- package/index.d.ts +1541 -0
- package/index.js +74 -0
- package/lib/address.js +478 -0
- package/lib/block/block.js +277 -0
- package/lib/block/blockheader.js +295 -0
- package/lib/block/index.js +4 -0
- package/lib/block/merkleblock.js +323 -0
- package/lib/bn.js +3423 -0
- package/lib/crypto/bn.js +278 -0
- package/lib/crypto/ecdsa.js +339 -0
- package/lib/crypto/hash.browser.js +171 -0
- package/lib/crypto/hash.js +2 -0
- package/lib/crypto/hash.node.js +171 -0
- package/lib/crypto/point.js +221 -0
- package/lib/crypto/random.browser.js +28 -0
- package/lib/crypto/random.js +2 -0
- package/lib/crypto/random.node.js +11 -0
- package/lib/crypto/signature.js +325 -0
- package/lib/encoding/base58.js +111 -0
- package/lib/encoding/base58check.js +121 -0
- package/lib/encoding/bufferreader.js +212 -0
- package/lib/encoding/bufferwriter.js +140 -0
- package/lib/encoding/decode-asm.js +24 -0
- package/lib/encoding/decode-hex.js +32 -0
- package/lib/encoding/decode-script-chunks.js +43 -0
- package/lib/encoding/encode-hex.js +284 -0
- package/lib/encoding/is-hex.js +7 -0
- package/lib/encoding/varint.js +75 -0
- package/lib/errors/index.js +54 -0
- package/lib/errors/spec.js +314 -0
- package/lib/hash-cache.js +50 -0
- package/lib/hdprivatekey.js +678 -0
- package/lib/hdpublickey.js +525 -0
- package/lib/message/message.js +191 -0
- package/lib/mnemonic/mnemonic.js +303 -0
- package/lib/mnemonic/pbkdf2.browser.js +68 -0
- package/lib/mnemonic/pbkdf2.js +2 -0
- package/lib/mnemonic/pbkdf2.node.js +68 -0
- package/lib/mnemonic/words/chinese.js +2054 -0
- package/lib/mnemonic/words/english.js +2054 -0
- package/lib/mnemonic/words/french.js +2054 -0
- package/lib/mnemonic/words/index.js +8 -0
- package/lib/mnemonic/words/italian.js +2054 -0
- package/lib/mnemonic/words/japanese.js +2054 -0
- package/lib/mnemonic/words/spanish.js +2054 -0
- package/lib/networks.js +379 -0
- package/lib/opcode.js +255 -0
- package/lib/privatekey.js +374 -0
- package/lib/publickey.js +386 -0
- package/lib/script/index.js +5 -0
- package/lib/script/interpreter.js +1834 -0
- package/lib/script/script.js +1074 -0
- package/lib/script/stack.js +109 -0
- package/lib/script/write-i32-le.js +17 -0
- package/lib/script/write-push-data.js +35 -0
- package/lib/script/write-u16-le.js +12 -0
- package/lib/script/write-u32-le.js +16 -0
- package/lib/script/write-u64-le.js +24 -0
- package/lib/script/write-u8-le.js +8 -0
- package/lib/script/write-varint.js +46 -0
- package/lib/transaction/index.js +7 -0
- package/lib/transaction/input/index.js +5 -0
- package/lib/transaction/input/input.js +354 -0
- package/lib/transaction/input/multisig.js +242 -0
- package/lib/transaction/input/publickey.js +100 -0
- package/lib/transaction/input/publickeyhash.js +118 -0
- package/lib/transaction/output.js +231 -0
- package/lib/transaction/sighash.js +167 -0
- package/lib/transaction/signature.js +97 -0
- package/lib/transaction/transaction.js +1639 -0
- package/lib/transaction/unspentoutput.js +113 -0
- package/lib/util/_.js +47 -0
- package/lib/util/js.js +90 -0
- package/lib/util/preconditions.js +33 -0
- package/package.json +26 -0
- package/test/address.js +509 -0
- package/test/block/block.js +251 -0
- package/test/block/blockheader.js +275 -0
- package/test/block/merklebloack.js +211 -0
- package/test/crypto/bn.js +177 -0
- package/test/crypto/ecdsa.js +391 -0
- package/test/crypto/hash.browser.js +135 -0
- package/test/crypto/hash.js +136 -0
- package/test/crypto/point.js +224 -0
- package/test/crypto/random.js +32 -0
- package/test/crypto/signature.js +409 -0
- package/test/data/bip69.json +215 -0
- package/test/data/bitcoind/base58_keys_invalid.json +52 -0
- package/test/data/bitcoind/base58_keys_valid.json +335 -0
- package/test/data/bitcoind/blocks.json +22 -0
- package/test/data/bitcoind/script_tests.json +3822 -0
- package/test/data/bitcoind/sig_canonical.json +7 -0
- package/test/data/bitcoind/sig_noncanonical.json +36 -0
- package/test/data/bitcoind/tx_invalid.json +445 -0
- package/test/data/bitcoind/tx_valid.json +44 -0
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +14 -0
- package/test/data/blk86756-testnet.json +684 -0
- package/test/data/block.hex +1 -0
- package/test/data/ecdsa.json +230 -0
- package/test/data/merkleblocks.js +488 -0
- package/test/data/messages.json +22 -0
- package/test/data/sighash.json +12 -0
- package/test/data/tx_creation.json +95 -0
- package/test/encoding/base58.js +131 -0
- package/test/encoding/base58check.js +136 -0
- package/test/encoding/bufferreader.js +337 -0
- package/test/encoding/bufferwriter.js +172 -0
- package/test/encoding/varint.js +104 -0
- package/test/hashCache.js +67 -0
- package/test/hdkeys.js +445 -0
- package/test/hdprivatekey.js +332 -0
- package/test/hdpublickey.js +304 -0
- package/test/index.js +16 -0
- package/test/message/message.js +204 -0
- package/test/mnemonic/data/fixtures.json +300 -0
- package/test/mnemonic/mnemonic.js +259 -0
- package/test/mnemonic/mocha.opts +1 -0
- package/test/mnemonic/pbkdf2.test.js +59 -0
- package/test/networks.js +159 -0
- package/test/opcode.js +161 -0
- package/test/privatekey.js +439 -0
- package/test/publickey.js +554 -0
- package/test/script/interpreter.js +734 -0
- package/test/script/script.js +1437 -0
- package/test/transaction/deserialize.js +34 -0
- package/test/transaction/input/input.js +90 -0
- package/test/transaction/input/multisig.js +90 -0
- package/test/transaction/input/publickey.js +68 -0
- package/test/transaction/input/publickeyhash.js +51 -0
- package/test/transaction/output.js +185 -0
- package/test/transaction/sighash.js +65 -0
- package/test/transaction/signature.js +114 -0
- package/test/transaction/transaction.js +1109 -0
- package/test/transaction/unspentoutput.js +110 -0
- package/test/util/js.js +76 -0
- package/test/util/preconditions.js +79 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/* global VARIANT */
|
|
2
|
+
|
|
3
|
+
let encodeHex = null;
|
|
4
|
+
|
|
5
|
+
// Prefer our implementation of encodeHex over Buffer when we don't know the VARIANT
|
|
6
|
+
// to avoid accidentally importing the Buffer shim in the browser.
|
|
7
|
+
|
|
8
|
+
if (typeof VARIANT === 'undefined' || VARIANT === 'browser') {
|
|
9
|
+
// In the browser, a lookup table is fastest, and faster than the Buffer toString shim.
|
|
10
|
+
const HEX = [
|
|
11
|
+
'00',
|
|
12
|
+
'01',
|
|
13
|
+
'02',
|
|
14
|
+
'03',
|
|
15
|
+
'04',
|
|
16
|
+
'05',
|
|
17
|
+
'06',
|
|
18
|
+
'07',
|
|
19
|
+
'08',
|
|
20
|
+
'09',
|
|
21
|
+
'0a',
|
|
22
|
+
'0b',
|
|
23
|
+
'0c',
|
|
24
|
+
'0d',
|
|
25
|
+
'0e',
|
|
26
|
+
'0f',
|
|
27
|
+
'10',
|
|
28
|
+
'11',
|
|
29
|
+
'12',
|
|
30
|
+
'13',
|
|
31
|
+
'14',
|
|
32
|
+
'15',
|
|
33
|
+
'16',
|
|
34
|
+
'17',
|
|
35
|
+
'18',
|
|
36
|
+
'19',
|
|
37
|
+
'1a',
|
|
38
|
+
'1b',
|
|
39
|
+
'1c',
|
|
40
|
+
'1d',
|
|
41
|
+
'1e',
|
|
42
|
+
'1f',
|
|
43
|
+
'20',
|
|
44
|
+
'21',
|
|
45
|
+
'22',
|
|
46
|
+
'23',
|
|
47
|
+
'24',
|
|
48
|
+
'25',
|
|
49
|
+
'26',
|
|
50
|
+
'27',
|
|
51
|
+
'28',
|
|
52
|
+
'29',
|
|
53
|
+
'2a',
|
|
54
|
+
'2b',
|
|
55
|
+
'2c',
|
|
56
|
+
'2d',
|
|
57
|
+
'2e',
|
|
58
|
+
'2f',
|
|
59
|
+
'30',
|
|
60
|
+
'31',
|
|
61
|
+
'32',
|
|
62
|
+
'33',
|
|
63
|
+
'34',
|
|
64
|
+
'35',
|
|
65
|
+
'36',
|
|
66
|
+
'37',
|
|
67
|
+
'38',
|
|
68
|
+
'39',
|
|
69
|
+
'3a',
|
|
70
|
+
'3b',
|
|
71
|
+
'3c',
|
|
72
|
+
'3d',
|
|
73
|
+
'3e',
|
|
74
|
+
'3f',
|
|
75
|
+
'40',
|
|
76
|
+
'41',
|
|
77
|
+
'42',
|
|
78
|
+
'43',
|
|
79
|
+
'44',
|
|
80
|
+
'45',
|
|
81
|
+
'46',
|
|
82
|
+
'47',
|
|
83
|
+
'48',
|
|
84
|
+
'49',
|
|
85
|
+
'4a',
|
|
86
|
+
'4b',
|
|
87
|
+
'4c',
|
|
88
|
+
'4d',
|
|
89
|
+
'4e',
|
|
90
|
+
'4f',
|
|
91
|
+
'50',
|
|
92
|
+
'51',
|
|
93
|
+
'52',
|
|
94
|
+
'53',
|
|
95
|
+
'54',
|
|
96
|
+
'55',
|
|
97
|
+
'56',
|
|
98
|
+
'57',
|
|
99
|
+
'58',
|
|
100
|
+
'59',
|
|
101
|
+
'5a',
|
|
102
|
+
'5b',
|
|
103
|
+
'5c',
|
|
104
|
+
'5d',
|
|
105
|
+
'5e',
|
|
106
|
+
'5f',
|
|
107
|
+
'60',
|
|
108
|
+
'61',
|
|
109
|
+
'62',
|
|
110
|
+
'63',
|
|
111
|
+
'64',
|
|
112
|
+
'65',
|
|
113
|
+
'66',
|
|
114
|
+
'67',
|
|
115
|
+
'68',
|
|
116
|
+
'69',
|
|
117
|
+
'6a',
|
|
118
|
+
'6b',
|
|
119
|
+
'6c',
|
|
120
|
+
'6d',
|
|
121
|
+
'6e',
|
|
122
|
+
'6f',
|
|
123
|
+
'70',
|
|
124
|
+
'71',
|
|
125
|
+
'72',
|
|
126
|
+
'73',
|
|
127
|
+
'74',
|
|
128
|
+
'75',
|
|
129
|
+
'76',
|
|
130
|
+
'77',
|
|
131
|
+
'78',
|
|
132
|
+
'79',
|
|
133
|
+
'7a',
|
|
134
|
+
'7b',
|
|
135
|
+
'7c',
|
|
136
|
+
'7d',
|
|
137
|
+
'7e',
|
|
138
|
+
'7f',
|
|
139
|
+
'80',
|
|
140
|
+
'81',
|
|
141
|
+
'82',
|
|
142
|
+
'83',
|
|
143
|
+
'84',
|
|
144
|
+
'85',
|
|
145
|
+
'86',
|
|
146
|
+
'87',
|
|
147
|
+
'88',
|
|
148
|
+
'89',
|
|
149
|
+
'8a',
|
|
150
|
+
'8b',
|
|
151
|
+
'8c',
|
|
152
|
+
'8d',
|
|
153
|
+
'8e',
|
|
154
|
+
'8f',
|
|
155
|
+
'90',
|
|
156
|
+
'91',
|
|
157
|
+
'92',
|
|
158
|
+
'93',
|
|
159
|
+
'94',
|
|
160
|
+
'95',
|
|
161
|
+
'96',
|
|
162
|
+
'97',
|
|
163
|
+
'98',
|
|
164
|
+
'99',
|
|
165
|
+
'9a',
|
|
166
|
+
'9b',
|
|
167
|
+
'9c',
|
|
168
|
+
'9d',
|
|
169
|
+
'9e',
|
|
170
|
+
'9f',
|
|
171
|
+
'a0',
|
|
172
|
+
'a1',
|
|
173
|
+
'a2',
|
|
174
|
+
'a3',
|
|
175
|
+
'a4',
|
|
176
|
+
'a5',
|
|
177
|
+
'a6',
|
|
178
|
+
'a7',
|
|
179
|
+
'a8',
|
|
180
|
+
'a9',
|
|
181
|
+
'aa',
|
|
182
|
+
'ab',
|
|
183
|
+
'ac',
|
|
184
|
+
'ad',
|
|
185
|
+
'ae',
|
|
186
|
+
'af',
|
|
187
|
+
'b0',
|
|
188
|
+
'b1',
|
|
189
|
+
'b2',
|
|
190
|
+
'b3',
|
|
191
|
+
'b4',
|
|
192
|
+
'b5',
|
|
193
|
+
'b6',
|
|
194
|
+
'b7',
|
|
195
|
+
'b8',
|
|
196
|
+
'b9',
|
|
197
|
+
'ba',
|
|
198
|
+
'bb',
|
|
199
|
+
'bc',
|
|
200
|
+
'bd',
|
|
201
|
+
'be',
|
|
202
|
+
'bf',
|
|
203
|
+
'c0',
|
|
204
|
+
'c1',
|
|
205
|
+
'c2',
|
|
206
|
+
'c3',
|
|
207
|
+
'c4',
|
|
208
|
+
'c5',
|
|
209
|
+
'c6',
|
|
210
|
+
'c7',
|
|
211
|
+
'c8',
|
|
212
|
+
'c9',
|
|
213
|
+
'ca',
|
|
214
|
+
'cb',
|
|
215
|
+
'cc',
|
|
216
|
+
'cd',
|
|
217
|
+
'ce',
|
|
218
|
+
'cf',
|
|
219
|
+
'd0',
|
|
220
|
+
'd1',
|
|
221
|
+
'd2',
|
|
222
|
+
'd3',
|
|
223
|
+
'd4',
|
|
224
|
+
'd5',
|
|
225
|
+
'd6',
|
|
226
|
+
'd7',
|
|
227
|
+
'd8',
|
|
228
|
+
'd9',
|
|
229
|
+
'da',
|
|
230
|
+
'db',
|
|
231
|
+
'dc',
|
|
232
|
+
'dd',
|
|
233
|
+
'de',
|
|
234
|
+
'df',
|
|
235
|
+
'e0',
|
|
236
|
+
'e1',
|
|
237
|
+
'e2',
|
|
238
|
+
'e3',
|
|
239
|
+
'e4',
|
|
240
|
+
'e5',
|
|
241
|
+
'e6',
|
|
242
|
+
'e7',
|
|
243
|
+
'e8',
|
|
244
|
+
'e9',
|
|
245
|
+
'ea',
|
|
246
|
+
'eb',
|
|
247
|
+
'ec',
|
|
248
|
+
'ed',
|
|
249
|
+
'ee',
|
|
250
|
+
'ef',
|
|
251
|
+
'f0',
|
|
252
|
+
'f1',
|
|
253
|
+
'f2',
|
|
254
|
+
'f3',
|
|
255
|
+
'f4',
|
|
256
|
+
'f5',
|
|
257
|
+
'f6',
|
|
258
|
+
'f7',
|
|
259
|
+
'f8',
|
|
260
|
+
'f9',
|
|
261
|
+
'fa',
|
|
262
|
+
'fb',
|
|
263
|
+
'fc',
|
|
264
|
+
'fd',
|
|
265
|
+
'fe',
|
|
266
|
+
'ff',
|
|
267
|
+
];
|
|
268
|
+
|
|
269
|
+
encodeHex = function (buffer) {
|
|
270
|
+
const len = buffer.length;
|
|
271
|
+
const out = new Array(len);
|
|
272
|
+
for (let i = 0; i < len; ++i) {
|
|
273
|
+
out[i] = HEX[buffer[i]];
|
|
274
|
+
}
|
|
275
|
+
return out.join('');
|
|
276
|
+
};
|
|
277
|
+
} else {
|
|
278
|
+
// Buffer toString('hex') in Node is faster than any JavaScript we could write
|
|
279
|
+
encodeHex = function (buffer) {
|
|
280
|
+
return buffer instanceof Buffer ? buffer.toString('hex') : Buffer.from(buffer).toString('hex');
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
module.exports = encodeHex;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var BufferWriter = require('./bufferwriter');
|
|
4
|
+
var BufferReader = require('./bufferreader');
|
|
5
|
+
var BN = require('../crypto/bn');
|
|
6
|
+
|
|
7
|
+
var Varint = function Varint(buf) {
|
|
8
|
+
if (!(this instanceof Varint)) {
|
|
9
|
+
return new Varint(buf);
|
|
10
|
+
}
|
|
11
|
+
if (Buffer.isBuffer(buf)) {
|
|
12
|
+
this.buf = buf;
|
|
13
|
+
} else if (typeof buf === 'number') {
|
|
14
|
+
var num = buf;
|
|
15
|
+
this.fromNumber(num);
|
|
16
|
+
} else if (buf instanceof BN) {
|
|
17
|
+
var bn = buf;
|
|
18
|
+
this.fromBN(bn);
|
|
19
|
+
} else if (buf) {
|
|
20
|
+
var obj = buf;
|
|
21
|
+
this.set(obj);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
Varint.prototype.set = function (obj) {
|
|
26
|
+
this.buf = obj.buf || this.buf;
|
|
27
|
+
return this;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
Varint.prototype.fromString = function (str) {
|
|
31
|
+
this.set({
|
|
32
|
+
buf: Buffer.from(str, 'hex'),
|
|
33
|
+
});
|
|
34
|
+
return this;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
Varint.prototype.toString = function () {
|
|
38
|
+
return this.buf.toString('hex');
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
Varint.prototype.fromBuffer = function (buf) {
|
|
42
|
+
this.buf = buf;
|
|
43
|
+
return this;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
Varint.prototype.fromBufferReader = function (br) {
|
|
47
|
+
this.buf = br.readVarintBuf();
|
|
48
|
+
return this;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
Varint.prototype.fromBN = function (bn) {
|
|
52
|
+
var bw = new BufferWriter();
|
|
53
|
+
this.buf = bw.writeVarintBN(bn).toBuffer();
|
|
54
|
+
return this;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
Varint.prototype.fromNumber = function (num) {
|
|
58
|
+
var bw = new BufferWriter();
|
|
59
|
+
this.buf = bw.writeVarintNum(num).toBuffer();
|
|
60
|
+
return this;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
Varint.prototype.toBuffer = function () {
|
|
64
|
+
return this.buf;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
Varint.prototype.toBN = function () {
|
|
68
|
+
return BufferReader(this.buf).readVarintBN();
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
Varint.prototype.toNumber = function () {
|
|
72
|
+
return BufferReader(this.buf).readVarintNum();
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
module.exports = Varint;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('../util/_');
|
|
4
|
+
|
|
5
|
+
function format(message, args) {
|
|
6
|
+
return message.replace('{0}', args[0]).replace('{1}', args[1]).replace('{2}', args[2]);
|
|
7
|
+
}
|
|
8
|
+
var traverseNode = function (parent, errorDefinition) {
|
|
9
|
+
var NodeError = function () {
|
|
10
|
+
if (_.isString(errorDefinition.message)) {
|
|
11
|
+
this.message = format(errorDefinition.message, arguments);
|
|
12
|
+
} else if (_.isFunction(errorDefinition.message)) {
|
|
13
|
+
this.message = errorDefinition.message.apply(null, arguments);
|
|
14
|
+
} else {
|
|
15
|
+
throw new Error('Invalid error definition for ' + errorDefinition.name);
|
|
16
|
+
}
|
|
17
|
+
this.stack = this.message + '\n' + new Error().stack;
|
|
18
|
+
};
|
|
19
|
+
NodeError.prototype = Object.create(parent.prototype);
|
|
20
|
+
NodeError.prototype.name = parent.prototype.name + errorDefinition.name;
|
|
21
|
+
parent[errorDefinition.name] = NodeError;
|
|
22
|
+
if (errorDefinition.errors) {
|
|
23
|
+
childDefinitions(NodeError, errorDefinition.errors);
|
|
24
|
+
}
|
|
25
|
+
return NodeError;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
var childDefinitions = function (parent, childDefinitions) {
|
|
29
|
+
_.each(childDefinitions, function (childDefinition) {
|
|
30
|
+
traverseNode(parent, childDefinition);
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
var traverseRoot = function (parent, errorsDefinition) {
|
|
35
|
+
childDefinitions(parent, errorsDefinition);
|
|
36
|
+
return parent;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
var opcat = {};
|
|
40
|
+
opcat.Error = function () {
|
|
41
|
+
this.message = 'Internal error';
|
|
42
|
+
this.stack = this.message + '\n' + new Error().stack;
|
|
43
|
+
};
|
|
44
|
+
opcat.Error.prototype = Object.create(Error.prototype);
|
|
45
|
+
opcat.Error.prototype.name = 'opcat.Error';
|
|
46
|
+
|
|
47
|
+
var data = require('./spec');
|
|
48
|
+
traverseRoot(opcat.Error, data);
|
|
49
|
+
|
|
50
|
+
module.exports = opcat.Error;
|
|
51
|
+
|
|
52
|
+
module.exports.extend = function (spec) {
|
|
53
|
+
return traverseNode(opcat.Error, spec);
|
|
54
|
+
};
|