@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,277 @@
1
+ 'use strict';
2
+ var _ = require('lodash');
3
+
4
+ var BufferUtil = require('./util/buffer');
5
+ var JSUtil = require('./util/js');
6
+ var networks = [];
7
+ var networkMaps = {};
8
+
9
+ /**
10
+ * A network is merely a map containing values that correspond to version
11
+ * numbers for each bitcoin network. Currently only supporting "livenet"
12
+ * (a.k.a. "mainnet") and "testnet".
13
+ * @constructor
14
+ */
15
+ function Network() {}
16
+
17
+ Network.prototype.toString = function toString() {
18
+ return this.name;
19
+ };
20
+
21
+ /**
22
+ * @function
23
+ * @member Networks#get
24
+ * Retrieves the network associated with a magic number or string.
25
+ * @param {string|number|Network} arg
26
+ * @param {string|Array} keys - if set, only check if the magic number associated with this name matches
27
+ * @return Network
28
+ */
29
+ function get(arg, keys) {
30
+ if (~networks.indexOf(arg)) {
31
+ return arg;
32
+ }
33
+ if (keys) {
34
+ if (!_.isArray(keys)) {
35
+ keys = [keys];
36
+ }
37
+ for (var i = 0; i < networks.length; i++) {
38
+ var network = networks[i];
39
+ var filteredNet = _.pick(network, keys);
40
+ var netValues = _.values(filteredNet);
41
+ if(~netValues.indexOf(arg)) {
42
+ return network;
43
+ }
44
+ }
45
+ return undefined;
46
+ }
47
+ return networkMaps[arg];
48
+ }
49
+
50
+ /***
51
+ * Derives an array from the given prefix to be used in the computation
52
+ * of the address' checksum.
53
+ *
54
+ * @param {string} prefix Network prefix. E.g.: 'kaspatest'.
55
+ */
56
+ function prefixToArray(prefix) {
57
+ var result = [];
58
+ for (var i=0; i < prefix.length; i++) {
59
+ result.push(prefix.charCodeAt(i) & 31);
60
+ }
61
+ return result;
62
+ }
63
+
64
+ /**
65
+ * @function
66
+ * @member Networks#add
67
+ * Will add a custom Network
68
+ * @param {Object} data
69
+ * @param {string} data.name - The name of the network
70
+ * @param {string} data.alias - The aliased name of the network
71
+ * @param {Number} data.pubkeyhash - The publickey hash prefix
72
+ * @param {Number} data.privatekey - The privatekey prefix
73
+ * @param {Number} data.scripthash - The scripthash prefix
74
+ * @param {Number} data.xpubkey - The extended public key magic
75
+ * @param {Number} data.xprivkey - The extended private key magic
76
+ * @param {Number} data.networkMagic - The network magic number
77
+ * @param {Number} data.port - The network port
78
+ * @param {Array} data.dnsSeeds - An array of dns seeds
79
+ * @return Network
80
+ */
81
+ function addNetwork(data) {
82
+
83
+ var network = new Network();
84
+
85
+ JSUtil.defineImmutable(network, {
86
+ name: data.name,
87
+ alias: data.alias,
88
+ pubkeyhash: data.pubkeyhash,
89
+ privatekey: data.privatekey,
90
+ scripthash: data.scripthash,
91
+ xpubkey: data.xpubkey,
92
+ xprivkey: data.xprivkey,
93
+ });
94
+
95
+ var indexBy = data.indexBy || Object.keys(data);
96
+
97
+ if (data.prefix) {
98
+ _.extend(network, {
99
+ prefix: data.prefix,
100
+ prefixArray: prefixToArray(data.prefix),
101
+ });
102
+ }
103
+
104
+ if (data.networkMagic) {
105
+ _.extend(network, {
106
+ networkMagic: BufferUtil.integerAsBuffer(data.networkMagic)
107
+ });
108
+ }
109
+
110
+ if (data.port) {
111
+ _.extend(network, {
112
+ port: data.port
113
+ });
114
+ }
115
+
116
+ if (data.dnsSeeds) {
117
+ _.extend(network, {
118
+ dnsSeeds: data.dnsSeeds
119
+ });
120
+ }
121
+ networks.push(network);
122
+ indexNetworkBy(network, indexBy);
123
+ return network;
124
+ }
125
+
126
+ function indexNetworkBy(network, keys) {
127
+ for(var i = 0; i < keys.length; i++) {
128
+ var key = keys[i];
129
+ var networkValue = network[key];
130
+ if(!_.isUndefined(networkValue) && !_.isObject(networkValue)) {
131
+ networkMaps[networkValue] = network;
132
+ }
133
+ }
134
+ }
135
+
136
+ function unindexNetworkBy(network, values) {
137
+ for(var index = 0; index < values.length; index++){
138
+ var value = values[index];
139
+ if(networkMaps[value] === network) {
140
+ delete networkMaps[value];
141
+ }
142
+ }
143
+ }
144
+
145
+ /**
146
+ * @function
147
+ * @member Networks#remove
148
+ * Will remove a custom network
149
+ * @param {Network} network
150
+ */
151
+ function removeNetwork(network) {
152
+ for (var i = 0; i < networks.length; i++) {
153
+ if (networks[i] === network) {
154
+ networks.splice(i, 1);
155
+ }
156
+ }
157
+ unindexNetworkBy(network, Object.keys(networkMaps));
158
+ }
159
+
160
+ // from https://github.com/Bitcoin-ABC/bitcoin-abc/blob/master/src/chainparams.cpp#L212
161
+ var dnsSeeds = [
162
+ 'seed.bitcoinabc.org',
163
+ 'seed-abc.bitcoinforks.org',
164
+ 'btccash-seeder.bitcoinunlimited.info',
165
+ 'seeder.jasonbcox.com',
166
+ 'seed.deadalnix.me',
167
+ 'seed.bchd.cash'
168
+ ];
169
+
170
+ var liveNetwork = {
171
+ name: 'livenet',
172
+ alias: 'mainnet',
173
+ prefix: 'bitcoincash',
174
+ pubkeyhash: 28,
175
+ privatekey: 0x80,
176
+ scripthash: 40,
177
+ xpubkey: 0x0488b21e,
178
+ xprivkey: 0x0488ade4,
179
+ networkMagic: 0xe3e1f3e8,
180
+ port: 8333,
181
+ dnsSeeds: dnsSeeds
182
+ };
183
+
184
+ var testNetwork = {
185
+ name: 'testnet',
186
+ prefix: 'bchtest',
187
+ pubkeyhash: 0x6f,
188
+ privatekey: 0xef,
189
+ scripthash: 0xc4,
190
+ xpubkey: 0x043587cf,
191
+ xprivkey: 0x04358394,
192
+ networkMagic: 0xf4e5f3f4,
193
+ port: 18333,
194
+ dnsSeeds: dnsSeeds
195
+ };
196
+
197
+ var regtestNetwork = {
198
+ name: 'regtest',
199
+ prefix: 'bchreg',
200
+ pubkeyhash: 0x6f,
201
+ privatekey: 0xef,
202
+ scripthash: 0xc4,
203
+ xpubkey: 0x043587cf,
204
+ xprivkey: 0x04358394,
205
+ networkMagic: 0xdab5bffa,
206
+ port: 18444,
207
+ dnsSeeds: [],
208
+ indexBy: [
209
+ 'port',
210
+ 'name',
211
+ 'prefix',
212
+ 'networkMagic'
213
+ ]
214
+ };
215
+
216
+
217
+ // Add configurable values for testnet/regtest
218
+
219
+
220
+ addNetwork(testNetwork);
221
+ addNetwork(regtestNetwork);
222
+ addNetwork(liveNetwork);
223
+
224
+ var livenet = get('livenet');
225
+ var regtest = get('regtest');
226
+ var testnet = get('testnet');
227
+
228
+ const kaspaNetworks = ['kaspa', 'kaspadev', 'kaspareg', 'kaspatest', 'kaspasim'];
229
+
230
+ kaspaNetworks.map((str) => {
231
+ addNetwork({
232
+ name: str,
233
+ prefix: str,
234
+ pubkeyhash: 0x00, // publickey hash prefix
235
+ privatekey: 0x80, // privatekey prefix -- must be 128 or toWIF() result will not match with kaspa
236
+ scripthash: 0x05,
237
+ xpubkey: 0x0488b21e, // extended public key magic
238
+ xprivkey: 0x0488ade4, // extended private key magic
239
+ networkMagic: 0xdab5bffa, // network magic number
240
+ });
241
+ });
242
+
243
+ /**
244
+ * @function
245
+ * @deprecated
246
+ * @member Networks#enableRegtest
247
+ * Will enable regtest features for testnet
248
+ */
249
+ function enableRegtest() {
250
+ testnet.regtestEnabled = true;
251
+ }
252
+
253
+ /**
254
+ * @function
255
+ * @deprecated
256
+ * @member Networks#disableRegtest
257
+ * Will disable regtest features for testnet
258
+ */
259
+ function disableRegtest() {
260
+ testnet.regtestEnabled = false;
261
+ }
262
+
263
+ /**
264
+ * @namespace Networks
265
+ */
266
+ module.exports = {
267
+ add: addNetwork,
268
+ remove: removeNetwork,
269
+ defaultNetwork: livenet,
270
+ livenet: livenet,
271
+ mainnet: livenet,
272
+ testnet: testnet,
273
+ regtest: regtest,
274
+ get: get,
275
+ enableRegtest: enableRegtest,
276
+ disableRegtest: disableRegtest
277
+ };
package/lib/opcode.js ADDED
@@ -0,0 +1,261 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+ var $ = require('./util/preconditions');
5
+ var BufferUtil = require('./util/buffer');
6
+ var JSUtil = require('./util/js');
7
+
8
+ function Opcode(num) {
9
+ if (!(this instanceof Opcode)) {
10
+ return new Opcode(num);
11
+ }
12
+
13
+ var value;
14
+
15
+ if (_.isNumber(num)) {
16
+ value = num;
17
+ } else if (_.isString(num)) {
18
+ value = Opcode.map[num];
19
+ } else {
20
+ throw new TypeError('Unrecognized num type: "' + typeof(num) + '" for Opcode');
21
+ }
22
+
23
+ JSUtil.defineImmutable(this, {
24
+ num: value
25
+ });
26
+
27
+ return this;
28
+ }
29
+
30
+ Opcode.fromBuffer = function(buf) {
31
+ $.checkArgument(BufferUtil.isBuffer(buf));
32
+ return new Opcode(Number('0x' + buf.toString('hex')));
33
+ };
34
+
35
+ Opcode.fromNumber = function(num) {
36
+ $.checkArgument(_.isNumber(num));
37
+ return new Opcode(num);
38
+ };
39
+
40
+ Opcode.fromString = function(str) {
41
+ $.checkArgument(_.isString(str));
42
+ var value = Opcode.map[str];
43
+ if (typeof value === 'undefined') {
44
+ throw new TypeError('Invalid opcodestr');
45
+ }
46
+ return new Opcode(value);
47
+ };
48
+
49
+ Opcode.prototype.toHex = function() {
50
+ return this.num.toString(16);
51
+ };
52
+
53
+ Opcode.prototype.toBuffer = function() {
54
+ return Buffer.from(this.toHex(), 'hex');
55
+ };
56
+
57
+ Opcode.prototype.toNumber = function() {
58
+ return this.num;
59
+ };
60
+
61
+ Opcode.prototype.toString = function() {
62
+ var str = Opcode.reverseMap[this.num];
63
+ if (typeof str === 'undefined') {
64
+ throw new Error('Opcode does not have a string representation');
65
+ }
66
+ return str;
67
+ };
68
+
69
+ Opcode.smallInt = function(n) {
70
+ $.checkArgument(_.isNumber(n), 'Invalid Argument: n should be number');
71
+ $.checkArgument(n >= 0 && n <= 16, 'Invalid Argument: n must be between 0 and 16');
72
+ if (n === 0) {
73
+ return Opcode('OP_0');
74
+ }
75
+ return new Opcode(Opcode.map.OP_1 + n - 1);
76
+ };
77
+
78
+ Opcode.map = {
79
+ // push value
80
+ OP_FALSE: 0,
81
+ OP_0: 0,
82
+ OP_PUSHDATA1: 76,
83
+ OP_PUSHDATA2: 77,
84
+ OP_PUSHDATA4: 78,
85
+ OP_1NEGATE: 79,
86
+ OP_RESERVED: 80,
87
+ OP_TRUE: 81,
88
+ OP_1: 81,
89
+ OP_2: 82,
90
+ OP_3: 83,
91
+ OP_4: 84,
92
+ OP_5: 85,
93
+ OP_6: 86,
94
+ OP_7: 87,
95
+ OP_8: 88,
96
+ OP_9: 89,
97
+ OP_10: 90,
98
+ OP_11: 91,
99
+ OP_12: 92,
100
+ OP_13: 93,
101
+ OP_14: 94,
102
+ OP_15: 95,
103
+ OP_16: 96,
104
+
105
+ // control
106
+ OP_NOP: 97,
107
+ OP_VER: 98,
108
+ OP_IF: 99,
109
+ OP_NOTIF: 100,
110
+ OP_VERIF: 101,
111
+ OP_VERNOTIF: 102,
112
+ OP_ELSE: 103,
113
+ OP_ENDIF: 104,
114
+ OP_VERIFY: 105,
115
+ OP_RETURN: 106,
116
+
117
+ // stack ops
118
+ OP_TOALTSTACK: 107,
119
+ OP_FROMALTSTACK: 108,
120
+ OP_2DROP: 109,
121
+ OP_2DUP: 110,
122
+ OP_3DUP: 111,
123
+ OP_2OVER: 112,
124
+ OP_2ROT: 113,
125
+ OP_2SWAP: 114,
126
+ OP_IFDUP: 115,
127
+ OP_DEPTH: 116,
128
+ OP_DROP: 117,
129
+ OP_DUP: 118,
130
+ OP_NIP: 119,
131
+ OP_OVER: 120,
132
+ OP_PICK: 121,
133
+ OP_ROLL: 122,
134
+ OP_ROT: 123,
135
+ OP_SWAP: 124,
136
+ OP_TUCK: 125,
137
+
138
+ // splice ops
139
+ OP_CAT: 126,
140
+ OP_SPLIT: 127,
141
+ OP_NUM2BIN: 128,
142
+ OP_BIN2NUM: 129,
143
+ OP_SIZE: 130,
144
+
145
+ // bit logic
146
+ OP_INVERT: 131,
147
+ OP_AND: 132,
148
+ OP_OR: 133,
149
+ OP_XOR: 134,
150
+ OP_EQUAL: 135,
151
+ OP_EQUALVERIFY: 136,
152
+ OP_RESERVED1: 137,
153
+ OP_RESERVED2: 138,
154
+
155
+ // numeric
156
+ OP_1ADD: 139,
157
+ OP_1SUB: 140,
158
+ OP_2MUL: 141,
159
+ OP_2DIV: 142,
160
+ OP_NEGATE: 143,
161
+ OP_ABS: 144,
162
+ OP_NOT: 145,
163
+ OP_0NOTEQUAL: 146,
164
+
165
+ OP_ADD: 147,
166
+ OP_SUB: 148,
167
+ OP_MUL: 149,
168
+ OP_DIV: 150,
169
+ OP_MOD: 151,
170
+ OP_LSHIFT: 152,
171
+ OP_RSHIFT: 153,
172
+
173
+ OP_BOOLAND: 154,
174
+ OP_BOOLOR: 155,
175
+ OP_NUMEQUAL: 156,
176
+ OP_NUMEQUALVERIFY: 157,
177
+ OP_NUMNOTEQUAL: 158,
178
+ OP_LESSTHAN: 159,
179
+ OP_GREATERTHAN: 160,
180
+ OP_LESSTHANOREQUAL: 161,
181
+ OP_GREATERTHANOREQUAL: 162,
182
+ OP_MIN: 163,
183
+ OP_MAX: 164,
184
+
185
+ OP_WITHIN: 165,
186
+
187
+ // crypto
188
+ OP_RIPEMD160: 166,
189
+ OP_SHA1: 167,
190
+ OP_SHA256: 168,
191
+ OP_HASH160: 169,
192
+ OP_HASH256: 170,
193
+ OP_BLAKE2B: 170,
194
+ OP_CODESEPARATOR: 171,
195
+ OP_CHECKSIG: 172,
196
+ OP_CHECKSIGVERIFY: 173,
197
+ OP_CHECKMULTISIG: 174,
198
+ OP_CHECKMULTISIGVERIFY: 175,
199
+
200
+ // timelocks
201
+ OP_NOP2: 177,
202
+ OP_CHECKLOCKTIMEVERIFY: 177,
203
+ OP_NOP3: 178,
204
+ OP_CHECKSEQUENCEVERIFY: 178,
205
+
206
+ // expansion
207
+ OP_NOP1: 176,
208
+ OP_NOP4: 179,
209
+ OP_NOP5: 180,
210
+ OP_NOP6: 181,
211
+ OP_NOP7: 182,
212
+ OP_NOP8: 183,
213
+ OP_NOP9: 184,
214
+ OP_NOP10: 185,
215
+
216
+ // More crypto
217
+ OP_CHECKDATASIG: 186,
218
+ OP_CHECKDATASIGVERIFY: 187,
219
+ OP_REVERSEBYTES: 188,
220
+
221
+ OP_PREFIX_BEGIN: 240,
222
+ OP_PREFIX_END: 241,
223
+
224
+ // template matching params
225
+ OP_SMALLINTEGER: 250,
226
+ OP_PUBKEYS: 251,
227
+ OP_PUBKEYHASH: 253,
228
+ OP_PUBKEY: 254,
229
+ OP_INVALIDOPCODE: 255
230
+ };
231
+
232
+ Opcode.reverseMap = [];
233
+
234
+ for (var k in Opcode.map) {
235
+ Opcode.reverseMap[Opcode.map[k]] = k;
236
+ }
237
+
238
+ // Easier access to opcodes
239
+ _.extend(Opcode, Opcode.map);
240
+
241
+ /**
242
+ * @returns true if opcode is one of OP_0, OP_1, ..., OP_16
243
+ */
244
+ Opcode.isSmallIntOp = function(opcode) {
245
+ if (opcode instanceof Opcode) {
246
+ opcode = opcode.toNumber();
247
+ }
248
+ return ((opcode === Opcode.map.OP_0) ||
249
+ ((opcode >= Opcode.map.OP_1) && (opcode <= Opcode.map.OP_16)));
250
+ };
251
+
252
+ /**
253
+ * Will return a string formatted for the console
254
+ *
255
+ * @returns {string} Script opcode
256
+ */
257
+ Opcode.prototype.inspect = function() {
258
+ return '<Opcode: ' + this.toString() + ', hex: '+this.toHex()+', decimal: '+this.num+'>';
259
+ };
260
+
261
+ module.exports = Opcode;