@onekeyfe/kaspa-core-lib 1.0.0-alpha.1

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