@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,374 @@
1
+ 'use strict';
2
+
3
+ var _ = require('./util/_');
4
+ var Address = require('./address');
5
+ var Base58Check = require('./encoding/base58check');
6
+ var BN = require('./crypto/bn');
7
+ var JSUtil = require('./util/js');
8
+ var Networks = require('./networks');
9
+ var Point = require('./crypto/point');
10
+ var PublicKey = require('./publickey');
11
+ var Random = require('./crypto/random');
12
+ var $ = require('./util/preconditions');
13
+
14
+ /**
15
+ * Instantiate a PrivateKey from a BN, Buffer or WIF string.
16
+ *
17
+ * @param {string} data - The encoded data in various formats
18
+ * @param {Network|string=} network - a {@link Network} object, or a string with the network name
19
+ * @returns {PrivateKey} A new valid instance of an PrivateKey
20
+ * @constructor
21
+ */
22
+ function PrivateKey(data, network) {
23
+ if (!(this instanceof PrivateKey)) {
24
+ return new PrivateKey(data, network);
25
+ }
26
+ if (data instanceof PrivateKey) {
27
+ return data;
28
+ }
29
+
30
+ var info = this._classifyArguments(data, network);
31
+
32
+ // validation
33
+ if (!info.bn || info.bn.cmp(new BN(0)) === 0) {
34
+ throw new TypeError('Number can not be equal to zero, undefined, null or false');
35
+ }
36
+ if (!info.bn.lt(Point.getN())) {
37
+ throw new TypeError('Number must be less than N');
38
+ }
39
+ if (typeof info.network === 'undefined') {
40
+ throw new TypeError('Must specify the network ("livenet" or "testnet")');
41
+ }
42
+
43
+ JSUtil.defineImmutable(this, {
44
+ bn: info.bn,
45
+ compressed: info.compressed,
46
+ network: info.network,
47
+ });
48
+
49
+ Object.defineProperty(this, 'publicKey', {
50
+ configurable: false,
51
+ enumerable: true,
52
+ get: this.toPublicKey.bind(this),
53
+ });
54
+
55
+ return this;
56
+ }
57
+
58
+ /**
59
+ * Internal helper to instantiate PrivateKey internal `info` object from
60
+ * different kinds of arguments passed to the constructor.
61
+ *
62
+ * @param {*} data
63
+ * @param {Network|string=} network - a {@link Network} object, or a string with the network name
64
+ * @return {Object}
65
+ */
66
+ PrivateKey.prototype._classifyArguments = function (data, network) {
67
+ var info = {
68
+ compressed: true,
69
+ network: network ? Networks.get(network) : Networks.defaultNetwork,
70
+ };
71
+
72
+ // detect type of data
73
+ if (_.isUndefined(data) || _.isNull(data)) {
74
+ info.bn = PrivateKey._getRandomBN();
75
+ } else if (data instanceof BN) {
76
+ info.bn = data;
77
+ } else if (data instanceof Buffer || data instanceof Uint8Array) {
78
+ info = PrivateKey._transformBuffer(data, network);
79
+ } else if (data.bn && data.network) {
80
+ info = PrivateKey._transformObject(data);
81
+ } else if (!network && Networks.get(data)) {
82
+ info.bn = PrivateKey._getRandomBN();
83
+ info.network = Networks.get(data);
84
+ } else if (typeof data === 'string') {
85
+ if (JSUtil.isHexa(data)) {
86
+ info.bn = new BN(Buffer.from(data, 'hex'));
87
+ } else {
88
+ info = PrivateKey._transformWIF(data, network);
89
+ }
90
+ } else {
91
+ throw new TypeError('First argument is an unrecognized data type.');
92
+ }
93
+ return info;
94
+ };
95
+
96
+ /**
97
+ * Internal function to get a random Big Number (BN)
98
+ *
99
+ * @returns {BN} A new randomly generated BN
100
+ * @private
101
+ */
102
+ PrivateKey._getRandomBN = function () {
103
+ var condition;
104
+ var bn;
105
+ do {
106
+ var privbuf = Random.getRandomBuffer(32);
107
+ bn = BN.fromBuffer(privbuf);
108
+ condition = bn.lt(Point.getN());
109
+ } while (!condition);
110
+ return bn;
111
+ };
112
+
113
+ /**
114
+ * Internal function to transform a WIF Buffer into a private key
115
+ *
116
+ * @param {Buffer} buf - An WIF string
117
+ * @param {Network|string=} network - a {@link Network} object, or a string with the network name
118
+ * @returns {Object} An object with keys: bn, network and compressed
119
+ * @private
120
+ */
121
+ PrivateKey._transformBuffer = function (buf, network) {
122
+ var info = {};
123
+
124
+ if (buf.length === 32) {
125
+ return PrivateKey._transformBNBuffer(buf, network);
126
+ }
127
+
128
+ info.network = Networks.get(buf[0], 'privatekey');
129
+
130
+ if (!info.network) {
131
+ throw new Error('Invalid network');
132
+ }
133
+
134
+ if (network && info.network !== Networks.get(network)) {
135
+ throw new TypeError('Private key network mismatch');
136
+ }
137
+
138
+ if (buf.length === 1 + 32 + 1 && buf[1 + 32 + 1 - 1] === 1) {
139
+ info.compressed = true;
140
+ } else if (buf.length === 1 + 32) {
141
+ info.compressed = false;
142
+ } else {
143
+ throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)');
144
+ }
145
+
146
+ info.bn = BN.fromBuffer(buf.slice(1, 32 + 1));
147
+
148
+ return info;
149
+ };
150
+
151
+ /**
152
+ * Internal function to transform a BN buffer into a private key
153
+ *
154
+ * @param {Buffer} buf
155
+ * @param {Network|string=} network - a {@link Network} object, or a string with the network name
156
+ * @returns {object} an Object with keys: bn, network, and compressed
157
+ * @private
158
+ */
159
+ PrivateKey._transformBNBuffer = function (buf, network) {
160
+ var info = {};
161
+ info.network = Networks.get(network) || Networks.defaultNetwork;
162
+ info.bn = BN.fromBuffer(buf);
163
+ info.compressed = false;
164
+ return info;
165
+ };
166
+
167
+ /**
168
+ * Internal function to transform a WIF string into a private key
169
+ *
170
+ * @param {string} buf - An WIF string
171
+ * @returns {Object} An object with keys: bn, network and compressed
172
+ * @private
173
+ */
174
+ PrivateKey._transformWIF = function (str, network) {
175
+ return PrivateKey._transformBuffer(Base58Check.decode(str), network);
176
+ };
177
+
178
+ /**
179
+ * Instantiate a PrivateKey from a Buffer with the DER or WIF representation
180
+ *
181
+ * @param {Buffer} buf
182
+ * @param {Network} network
183
+ * @return {PrivateKey}
184
+ */
185
+ PrivateKey.fromBuffer = function (buf, network) {
186
+ return new PrivateKey(buf, network);
187
+ };
188
+
189
+ PrivateKey.fromHex = function (hex, network) {
190
+ return PrivateKey.fromBuffer(Buffer.from(hex, 'hex'), network);
191
+ };
192
+
193
+ /**
194
+ * Internal function to transform a JSON string on plain object into a private key
195
+ * return this.
196
+ *
197
+ * @param {string} json - A JSON string or plain object
198
+ * @returns {Object} An object with keys: bn, network and compressed
199
+ * @private
200
+ */
201
+ PrivateKey._transformObject = function (json) {
202
+ var bn = new BN(json.bn, 'hex');
203
+ var network = Networks.get(json.network);
204
+ return {
205
+ bn: bn,
206
+ network: network,
207
+ compressed: json.compressed,
208
+ };
209
+ };
210
+
211
+ /**
212
+ * Instantiate a PrivateKey from a WIF string
213
+ *
214
+ * @param {string} str - The WIF encoded private key string
215
+ * @returns {PrivateKey} A new valid instance of PrivateKey
216
+ */
217
+ PrivateKey.fromString = PrivateKey.fromWIF = function (str) {
218
+ $.checkArgument(_.isString(str), 'First argument is expected to be a string.');
219
+ return new PrivateKey(str);
220
+ };
221
+
222
+ /**
223
+ * Instantiate a PrivateKey from a plain JavaScript object
224
+ *
225
+ * @param {Object} obj - The output from privateKey.toObject()
226
+ */
227
+ PrivateKey.fromObject = PrivateKey.fromJSON = function (obj) {
228
+ $.checkArgument(_.isObject(obj), 'First argument is expected to be an object.');
229
+ return new PrivateKey(obj);
230
+ };
231
+
232
+ /**
233
+ * Instantiate a PrivateKey from random bytes
234
+ *
235
+ * @param {string=} network - Either "livenet" or "testnet"
236
+ * @returns {PrivateKey} A new valid instance of PrivateKey
237
+ */
238
+ PrivateKey.fromRandom = function (network) {
239
+ var bn = PrivateKey._getRandomBN();
240
+ return new PrivateKey(bn, network);
241
+ };
242
+
243
+ /**
244
+ * Check if there would be any errors when initializing a PrivateKey
245
+ *
246
+ * @param {string} data - The encoded data in various formats
247
+ * @param {string=} network - Either "livenet" or "testnet"
248
+ * @returns {null|Error} An error if exists
249
+ */
250
+
251
+ PrivateKey.getValidationError = function (data, network) {
252
+ var error;
253
+ try {
254
+ new PrivateKey(data, network);
255
+ } catch (e) {
256
+ error = e;
257
+ }
258
+ return error;
259
+ };
260
+
261
+ /**
262
+ * Check if the parameters are valid
263
+ *
264
+ * @param {string} data - The encoded data in various formats
265
+ * @param {string=} network - Either "livenet" or "testnet"
266
+ * @returns {Boolean} If the private key is would be valid
267
+ */
268
+ PrivateKey.isValid = function (data, network) {
269
+ if (!data) {
270
+ return false;
271
+ }
272
+ return !PrivateKey.getValidationError(data, network);
273
+ };
274
+
275
+ /**
276
+ * Will output the PrivateKey in WIF
277
+ *
278
+ * @returns {string}
279
+ */
280
+ PrivateKey.prototype.toString = function () {
281
+ return this.toWIF();
282
+ };
283
+
284
+ /**
285
+ * Will output the PrivateKey to a WIF string
286
+ *
287
+ * @returns {string} A WIP representation of the private key
288
+ */
289
+ PrivateKey.prototype.toWIF = function () {
290
+ var network = this.network;
291
+ var compressed = this.compressed;
292
+
293
+ var buf;
294
+ if (compressed) {
295
+ buf = Buffer.concat([
296
+ Buffer.from([network.privatekey]),
297
+ this.bn.toBuffer({ size: 32 }),
298
+ Buffer.from([0x01]),
299
+ ]);
300
+ } else {
301
+ buf = Buffer.concat([Buffer.from([network.privatekey]), this.bn.toBuffer({ size: 32 })]);
302
+ }
303
+
304
+ return Base58Check.encode(buf);
305
+ };
306
+
307
+ /**
308
+ * Will return the private key as a BN instance
309
+ *
310
+ * @returns {BN} A BN instance of the private key
311
+ */
312
+ PrivateKey.prototype.toBigNumber = function () {
313
+ return this.bn;
314
+ };
315
+
316
+ /**
317
+ * Will return the private key as a BN buffer
318
+ *
319
+ * @returns {Buffer} A buffer of the private key
320
+ */
321
+ PrivateKey.prototype.toBuffer = function () {
322
+ return this.bn.toBuffer({ size: 32 });
323
+ };
324
+
325
+ PrivateKey.prototype.toHex = function () {
326
+ return this.toBuffer().toString('hex');
327
+ };
328
+
329
+ /**
330
+ * Will return the corresponding public key
331
+ *
332
+ * @returns {PublicKey} A public key generated from the private key
333
+ */
334
+ PrivateKey.prototype.toPublicKey = function () {
335
+ if (!this._pubkey) {
336
+ this._pubkey = PublicKey.fromPrivateKey(this);
337
+ }
338
+ return this._pubkey;
339
+ };
340
+
341
+ /**
342
+ * Will return an address for the private key
343
+ * @param {Network=} network - optional parameter specifying
344
+ * the desired network for the address
345
+ *
346
+ * @returns {Address} An address generated from the private key
347
+ */
348
+ PrivateKey.prototype.toAddress = function (network) {
349
+ var pubkey = this.toPublicKey();
350
+ return Address.fromPublicKey(pubkey, network || this.network);
351
+ };
352
+
353
+ /**
354
+ * @returns {Object} A plain object representation
355
+ */
356
+ PrivateKey.prototype.toObject = PrivateKey.prototype.toJSON = function toObject() {
357
+ return {
358
+ bn: this.bn.toString('hex'),
359
+ compressed: this.compressed,
360
+ network: this.network.toString(),
361
+ };
362
+ };
363
+
364
+ /**
365
+ * Will return a string formatted for the console
366
+ *
367
+ * @returns {string} Private key
368
+ */
369
+ PrivateKey.prototype.inspect = function () {
370
+ var uncompressed = !this.compressed ? ', uncompressed' : '';
371
+ return '<PrivateKey: ' + this.toHex() + ', network: ' + this.network + uncompressed + '>';
372
+ };
373
+
374
+ module.exports = PrivateKey;