@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.
Files changed (138) hide show
  1. package/.mocharc.yaml +3 -0
  2. package/index.d.ts +1541 -0
  3. package/index.js +74 -0
  4. package/lib/address.js +478 -0
  5. package/lib/block/block.js +277 -0
  6. package/lib/block/blockheader.js +295 -0
  7. package/lib/block/index.js +4 -0
  8. package/lib/block/merkleblock.js +323 -0
  9. package/lib/bn.js +3423 -0
  10. package/lib/crypto/bn.js +278 -0
  11. package/lib/crypto/ecdsa.js +339 -0
  12. package/lib/crypto/hash.browser.js +171 -0
  13. package/lib/crypto/hash.js +2 -0
  14. package/lib/crypto/hash.node.js +171 -0
  15. package/lib/crypto/point.js +221 -0
  16. package/lib/crypto/random.browser.js +28 -0
  17. package/lib/crypto/random.js +2 -0
  18. package/lib/crypto/random.node.js +11 -0
  19. package/lib/crypto/signature.js +325 -0
  20. package/lib/encoding/base58.js +111 -0
  21. package/lib/encoding/base58check.js +121 -0
  22. package/lib/encoding/bufferreader.js +212 -0
  23. package/lib/encoding/bufferwriter.js +140 -0
  24. package/lib/encoding/decode-asm.js +24 -0
  25. package/lib/encoding/decode-hex.js +32 -0
  26. package/lib/encoding/decode-script-chunks.js +43 -0
  27. package/lib/encoding/encode-hex.js +284 -0
  28. package/lib/encoding/is-hex.js +7 -0
  29. package/lib/encoding/varint.js +75 -0
  30. package/lib/errors/index.js +54 -0
  31. package/lib/errors/spec.js +314 -0
  32. package/lib/hash-cache.js +50 -0
  33. package/lib/hdprivatekey.js +678 -0
  34. package/lib/hdpublickey.js +525 -0
  35. package/lib/message/message.js +191 -0
  36. package/lib/mnemonic/mnemonic.js +303 -0
  37. package/lib/mnemonic/pbkdf2.browser.js +68 -0
  38. package/lib/mnemonic/pbkdf2.js +2 -0
  39. package/lib/mnemonic/pbkdf2.node.js +68 -0
  40. package/lib/mnemonic/words/chinese.js +2054 -0
  41. package/lib/mnemonic/words/english.js +2054 -0
  42. package/lib/mnemonic/words/french.js +2054 -0
  43. package/lib/mnemonic/words/index.js +8 -0
  44. package/lib/mnemonic/words/italian.js +2054 -0
  45. package/lib/mnemonic/words/japanese.js +2054 -0
  46. package/lib/mnemonic/words/spanish.js +2054 -0
  47. package/lib/networks.js +379 -0
  48. package/lib/opcode.js +255 -0
  49. package/lib/privatekey.js +374 -0
  50. package/lib/publickey.js +386 -0
  51. package/lib/script/index.js +5 -0
  52. package/lib/script/interpreter.js +1834 -0
  53. package/lib/script/script.js +1074 -0
  54. package/lib/script/stack.js +109 -0
  55. package/lib/script/write-i32-le.js +17 -0
  56. package/lib/script/write-push-data.js +35 -0
  57. package/lib/script/write-u16-le.js +12 -0
  58. package/lib/script/write-u32-le.js +16 -0
  59. package/lib/script/write-u64-le.js +24 -0
  60. package/lib/script/write-u8-le.js +8 -0
  61. package/lib/script/write-varint.js +46 -0
  62. package/lib/transaction/index.js +7 -0
  63. package/lib/transaction/input/index.js +5 -0
  64. package/lib/transaction/input/input.js +354 -0
  65. package/lib/transaction/input/multisig.js +242 -0
  66. package/lib/transaction/input/publickey.js +100 -0
  67. package/lib/transaction/input/publickeyhash.js +118 -0
  68. package/lib/transaction/output.js +231 -0
  69. package/lib/transaction/sighash.js +167 -0
  70. package/lib/transaction/signature.js +97 -0
  71. package/lib/transaction/transaction.js +1639 -0
  72. package/lib/transaction/unspentoutput.js +113 -0
  73. package/lib/util/_.js +47 -0
  74. package/lib/util/js.js +90 -0
  75. package/lib/util/preconditions.js +33 -0
  76. package/package.json +26 -0
  77. package/test/address.js +509 -0
  78. package/test/block/block.js +251 -0
  79. package/test/block/blockheader.js +275 -0
  80. package/test/block/merklebloack.js +211 -0
  81. package/test/crypto/bn.js +177 -0
  82. package/test/crypto/ecdsa.js +391 -0
  83. package/test/crypto/hash.browser.js +135 -0
  84. package/test/crypto/hash.js +136 -0
  85. package/test/crypto/point.js +224 -0
  86. package/test/crypto/random.js +32 -0
  87. package/test/crypto/signature.js +409 -0
  88. package/test/data/bip69.json +215 -0
  89. package/test/data/bitcoind/base58_keys_invalid.json +52 -0
  90. package/test/data/bitcoind/base58_keys_valid.json +335 -0
  91. package/test/data/bitcoind/blocks.json +22 -0
  92. package/test/data/bitcoind/script_tests.json +3822 -0
  93. package/test/data/bitcoind/sig_canonical.json +7 -0
  94. package/test/data/bitcoind/sig_noncanonical.json +36 -0
  95. package/test/data/bitcoind/tx_invalid.json +445 -0
  96. package/test/data/bitcoind/tx_valid.json +44 -0
  97. package/test/data/blk86756-testnet.dat +0 -0
  98. package/test/data/blk86756-testnet.js +14 -0
  99. package/test/data/blk86756-testnet.json +684 -0
  100. package/test/data/block.hex +1 -0
  101. package/test/data/ecdsa.json +230 -0
  102. package/test/data/merkleblocks.js +488 -0
  103. package/test/data/messages.json +22 -0
  104. package/test/data/sighash.json +12 -0
  105. package/test/data/tx_creation.json +95 -0
  106. package/test/encoding/base58.js +131 -0
  107. package/test/encoding/base58check.js +136 -0
  108. package/test/encoding/bufferreader.js +337 -0
  109. package/test/encoding/bufferwriter.js +172 -0
  110. package/test/encoding/varint.js +104 -0
  111. package/test/hashCache.js +67 -0
  112. package/test/hdkeys.js +445 -0
  113. package/test/hdprivatekey.js +332 -0
  114. package/test/hdpublickey.js +304 -0
  115. package/test/index.js +16 -0
  116. package/test/message/message.js +204 -0
  117. package/test/mnemonic/data/fixtures.json +300 -0
  118. package/test/mnemonic/mnemonic.js +259 -0
  119. package/test/mnemonic/mocha.opts +1 -0
  120. package/test/mnemonic/pbkdf2.test.js +59 -0
  121. package/test/networks.js +159 -0
  122. package/test/opcode.js +161 -0
  123. package/test/privatekey.js +439 -0
  124. package/test/publickey.js +554 -0
  125. package/test/script/interpreter.js +734 -0
  126. package/test/script/script.js +1437 -0
  127. package/test/transaction/deserialize.js +34 -0
  128. package/test/transaction/input/input.js +90 -0
  129. package/test/transaction/input/multisig.js +90 -0
  130. package/test/transaction/input/publickey.js +68 -0
  131. package/test/transaction/input/publickeyhash.js +51 -0
  132. package/test/transaction/output.js +185 -0
  133. package/test/transaction/sighash.js +65 -0
  134. package/test/transaction/signature.js +114 -0
  135. package/test/transaction/transaction.js +1109 -0
  136. package/test/transaction/unspentoutput.js +110 -0
  137. package/test/util/js.js +76 -0
  138. 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,7 @@
1
+ const HEX_REGEX = /^(?:[a-fA-F0-9][a-fA-F0-9])*$/;
2
+
3
+ function isHex(s) {
4
+ return HEX_REGEX.test(s);
5
+ }
6
+
7
+ module.exports = isHex;
@@ -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
+ };