@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,281 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+ var BlockHeader = require('./blockheader');
5
+ var BN = require('../crypto/bn');
6
+ var BufferUtil = require('../util/buffer');
7
+ var BufferReader = require('../encoding/bufferreader');
8
+ var BufferWriter = require('../encoding/bufferwriter');
9
+ var Hash = require('../crypto/hash');
10
+ var Transaction = require('../transaction');
11
+ var $ = require('../util/preconditions');
12
+
13
+ /**
14
+ * Instantiate a Block from a Buffer, JSON object, or Object with
15
+ * the properties of the Block
16
+ *
17
+ * @param {*} - A Buffer, JSON string, or Object
18
+ * @returns {Block}
19
+ * @constructor
20
+ */
21
+ function Block(arg) {
22
+ if (!(this instanceof Block)) {
23
+ return new Block(arg);
24
+ }
25
+ _.extend(this, Block._from(arg));
26
+ return this;
27
+ }
28
+
29
+ // https://github.com/bitcoin/bitcoin/blob/b5fa132329f0377d787a4a21c1686609c2bfaece/src/primitives/block.h#L14
30
+ Block.MAX_BLOCK_SIZE = 1000000;
31
+
32
+ /**
33
+ * @param {*} - A Buffer, JSON string or Object
34
+ * @returns {Object} - An object representing block data
35
+ * @throws {TypeError} - If the argument was not recognized
36
+ * @private
37
+ */
38
+ Block._from = function _from(arg) {
39
+ var info = {};
40
+ if (BufferUtil.isBuffer(arg)) {
41
+ info = Block._fromBufferReader(BufferReader(arg));
42
+ } else if (_.isObject(arg)) {
43
+ info = Block._fromObject(arg);
44
+ } else {
45
+ throw new TypeError('Unrecognized argument for Block');
46
+ }
47
+ return info;
48
+ };
49
+
50
+ /**
51
+ * @param {Object} - A plain JavaScript object
52
+ * @returns {Object} - An object representing block data
53
+ * @private
54
+ */
55
+ Block._fromObject = function _fromObject(data) {
56
+ var transactions = [];
57
+ data.transactions.forEach(function(tx) {
58
+ if (tx instanceof Transaction) {
59
+ transactions.push(tx);
60
+ } else {
61
+ transactions.push(Transaction().fromObject(tx));
62
+ }
63
+ });
64
+ var info = {
65
+ header: BlockHeader.fromObject(data.header),
66
+ transactions: transactions
67
+ };
68
+ return info;
69
+ };
70
+
71
+ /**
72
+ * @param {Object} - A plain JavaScript object
73
+ * @returns {Block} - An instance of block
74
+ */
75
+ Block.fromObject = function fromObject(obj) {
76
+ var info = Block._fromObject(obj);
77
+ return new Block(info);
78
+ };
79
+
80
+ /**
81
+ * @param {BufferReader} - Block data
82
+ * @returns {Object} - An object representing the block data
83
+ * @private
84
+ */
85
+ Block._fromBufferReader = function _fromBufferReader(br) {
86
+ var info = {};
87
+ $.checkState(!br.finished(), 'No block data received');
88
+ info.header = BlockHeader.fromBufferReader(br);
89
+ var transactions = br.readVarintNum();
90
+ info.transactions = [];
91
+ for (var i = 0; i < transactions; i++) {
92
+ info.transactions.push(Transaction().fromBufferReader(br));
93
+ }
94
+ return info;
95
+ };
96
+
97
+ /**
98
+ * @param {BufferReader} - A buffer reader of the block
99
+ * @returns {Block} - An instance of block
100
+ */
101
+ Block.fromBufferReader = function fromBufferReader(br) {
102
+ $.checkArgument(br, 'br is required');
103
+ var info = Block._fromBufferReader(br);
104
+ return new Block(info);
105
+ };
106
+
107
+ /**
108
+ * @param {Buffer} - A buffer of the block
109
+ * @returns {Block} - An instance of block
110
+ */
111
+ Block.fromBuffer = function fromBuffer(buf) {
112
+ return Block.fromBufferReader(new BufferReader(buf));
113
+ };
114
+
115
+ /**
116
+ * @param {string} - str - A hex encoded string of the block
117
+ * @returns {Block} - A hex encoded string of the block
118
+ */
119
+ Block.fromString = function fromString(str) {
120
+ var buf = Buffer.from(str, 'hex');
121
+ return Block.fromBuffer(buf);
122
+ };
123
+
124
+ /**
125
+ * @param {Binary} - Raw block binary data or buffer
126
+ * @returns {Block} - An instance of block
127
+ */
128
+ Block.fromRawBlock = function fromRawBlock(data) {
129
+ if (!BufferUtil.isBuffer(data)) {
130
+ data = Buffer.from(data, 'binary');
131
+ }
132
+ var br = BufferReader(data);
133
+ br.pos = Block.Values.START_OF_BLOCK;
134
+ var info = Block._fromBufferReader(br);
135
+ return new Block(info);
136
+ };
137
+
138
+ /**
139
+ * @returns {Object} - A plain object with the block properties
140
+ */
141
+ Block.prototype.toObject = Block.prototype.toJSON = function toObject() {
142
+ var transactions = [];
143
+ this.transactions.forEach(function(tx) {
144
+ transactions.push(tx.toObject());
145
+ });
146
+ return {
147
+ header: this.header.toObject(),
148
+ transactions: transactions
149
+ };
150
+ };
151
+
152
+ /**
153
+ * @returns {Buffer} - A buffer of the block
154
+ */
155
+ Block.prototype.toBuffer = function toBuffer() {
156
+ return this.toBufferWriter().concat();
157
+ };
158
+
159
+ /**
160
+ * @returns {string} - A hex encoded string of the block
161
+ */
162
+ Block.prototype.toString = function toString() {
163
+ return this.toBuffer().toString('hex');
164
+ };
165
+
166
+ /**
167
+ * @param {BufferWriter} - An existing instance of BufferWriter
168
+ * @returns {BufferWriter} - An instance of BufferWriter representation of the Block
169
+ */
170
+ Block.prototype.toBufferWriter = function toBufferWriter(bw) {
171
+ if (!bw) {
172
+ bw = new BufferWriter();
173
+ }
174
+ bw.write(this.header.toBuffer());
175
+ bw.writeVarintNum(this.transactions.length);
176
+ for (var i = 0; i < this.transactions.length; i++) {
177
+ this.transactions[i].toBufferWriter(bw);
178
+ }
179
+ return bw;
180
+ };
181
+
182
+ /**
183
+ * Will iterate through each transaction and return an array of hashes
184
+ * @returns {Array} - An array with transaction hashes
185
+ */
186
+ Block.prototype.getTransactionHashes = function getTransactionHashes() {
187
+ var hashes = [];
188
+ if (this.transactions.length === 0) {
189
+ return [Block.Values.NULL_HASH];
190
+ }
191
+ for (var t = 0; t < this.transactions.length; t++) {
192
+ hashes.push(this.transactions[t]._getHash());
193
+ }
194
+ return hashes;
195
+ };
196
+
197
+ /**
198
+ * Will build a merkle tree of all the transactions, ultimately arriving at
199
+ * a single point, the merkle root.
200
+ * @link https://en.bitcoin.it/wiki/Protocol_specification#Merkle_Trees
201
+ * @returns {Array} - An array with each level of the tree after the other.
202
+ */
203
+ Block.prototype.getMerkleTree = function getMerkleTree() {
204
+
205
+ var tree = this.getTransactionHashes();
206
+
207
+ var j = 0;
208
+ for (var size = this.transactions.length; size > 1; size = Math.floor((size + 1) / 2)) {
209
+ for (var i = 0; i < size; i += 2) {
210
+ var i2 = Math.min(i + 1, size - 1);
211
+ var buf = Buffer.concat([tree[j + i], tree[j + i2]]);
212
+ tree.push(Hash.sha256sha256(buf));
213
+ }
214
+ j += size;
215
+ }
216
+
217
+ return tree;
218
+ };
219
+
220
+ /**
221
+ * Calculates the merkleRoot from the transactions.
222
+ * @returns {Buffer} - A buffer of the merkle root hash
223
+ */
224
+ Block.prototype.getMerkleRoot = function getMerkleRoot() {
225
+ var tree = this.getMerkleTree();
226
+ return tree[tree.length - 1];
227
+ };
228
+
229
+ /**
230
+ * Verifies that the transactions in the block match the header merkle root
231
+ * @returns {Boolean} - If the merkle roots match
232
+ */
233
+ Block.prototype.validMerkleRoot = function validMerkleRoot() {
234
+
235
+ var h = new BN(this.header.merkleRoot.toString('hex'), 'hex');
236
+ var c = new BN(this.getMerkleRoot().toString('hex'), 'hex');
237
+
238
+ if (h.cmp(c) !== 0) {
239
+ return false;
240
+ }
241
+
242
+ return true;
243
+ };
244
+
245
+ /**
246
+ * @returns {Buffer} - The little endian hash buffer of the header
247
+ */
248
+ Block.prototype._getHash = function() {
249
+ return this.header._getHash();
250
+ };
251
+
252
+ var idProperty = {
253
+ configurable: false,
254
+ enumerable: true,
255
+ /**
256
+ * @returns {string} - The big endian hash buffer of the header
257
+ */
258
+ get: function() {
259
+ if (!this._id) {
260
+ this._id = this.header.id;
261
+ }
262
+ return this._id;
263
+ },
264
+ set: _.noop
265
+ };
266
+ Object.defineProperty(Block.prototype, 'id', idProperty);
267
+ Object.defineProperty(Block.prototype, 'hash', idProperty);
268
+
269
+ /**
270
+ * @returns {string} - A string formatted for the console
271
+ */
272
+ Block.prototype.inspect = function inspect() {
273
+ return '<Block ' + this.id + '>';
274
+ };
275
+
276
+ Block.Values = {
277
+ START_OF_BLOCK: 8, // Start of block in raw block data
278
+ NULL_HASH: Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
279
+ };
280
+
281
+ module.exports = Block;
@@ -0,0 +1,296 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+ var BN = require('../crypto/bn');
5
+ var BufferUtil = require('../util/buffer');
6
+ var BufferReader = require('../encoding/bufferreader');
7
+ var BufferWriter = require('../encoding/bufferwriter');
8
+ var Hash = require('../crypto/hash');
9
+ var JSUtil = require('../util/js');
10
+ var $ = require('../util/preconditions');
11
+
12
+ var GENESIS_BITS = 0x1d00ffff;
13
+
14
+ /**
15
+ * Instantiate a BlockHeader from a Buffer, JSON object, or Object with
16
+ * the properties of the BlockHeader
17
+ *
18
+ * @param {*} - A Buffer, JSON string, or Object
19
+ * @returns {BlockHeader} - An instance of block header
20
+ * @constructor
21
+ */
22
+ var BlockHeader = function BlockHeader(arg) {
23
+ if (!(this instanceof BlockHeader)) {
24
+ return new BlockHeader(arg);
25
+ }
26
+ var info = BlockHeader._from(arg);
27
+ this.version = info.version;
28
+ this.prevHash = info.prevHash;
29
+ this.merkleRoot = info.merkleRoot;
30
+ this.time = info.time;
31
+ this.timestamp = info.time;
32
+ this.bits = info.bits;
33
+ this.nonce = info.nonce;
34
+
35
+ if (info.hash) {
36
+ $.checkState(
37
+ this.hash === info.hash,
38
+ 'Argument object hash property does not match block hash.'
39
+ );
40
+ }
41
+
42
+ return this;
43
+ };
44
+
45
+ /**
46
+ * @param {*} - A Buffer, JSON string or Object
47
+ * @returns {Object} - An object representing block header data
48
+ * @throws {TypeError} - If the argument was not recognized
49
+ * @private
50
+ */
51
+ BlockHeader._from = function _from(arg) {
52
+ var info = {};
53
+ if (BufferUtil.isBuffer(arg)) {
54
+ info = BlockHeader._fromBufferReader(BufferReader(arg));
55
+ } else if (_.isObject(arg)) {
56
+ info = BlockHeader._fromObject(arg);
57
+ } else {
58
+ throw new TypeError('Unrecognized argument for BlockHeader');
59
+ }
60
+ return info;
61
+ };
62
+
63
+ /**
64
+ * @param {Object} - A JSON string
65
+ * @returns {Object} - An object representing block header data
66
+ * @private
67
+ */
68
+ BlockHeader._fromObject = function _fromObject(data) {
69
+ $.checkArgument(data, 'data is required');
70
+ var prevHash = data.prevHash;
71
+ var merkleRoot = data.merkleRoot;
72
+ if (_.isString(data.prevHash)) {
73
+ prevHash = BufferUtil.reverse(Buffer.from(data.prevHash, 'hex'));
74
+ }
75
+ if (_.isString(data.merkleRoot)) {
76
+ merkleRoot = BufferUtil.reverse(Buffer.from(data.merkleRoot, 'hex'));
77
+ }
78
+ var info = {
79
+ hash: data.hash,
80
+ version: data.version,
81
+ prevHash: prevHash,
82
+ merkleRoot: merkleRoot,
83
+ time: data.time,
84
+ timestamp: data.time,
85
+ bits: data.bits,
86
+ nonce: data.nonce
87
+ };
88
+ return info;
89
+ };
90
+
91
+ /**
92
+ * @param {Object} - A plain JavaScript object
93
+ * @returns {BlockHeader} - An instance of block header
94
+ */
95
+ BlockHeader.fromObject = function fromObject(obj) {
96
+ var info = BlockHeader._fromObject(obj);
97
+ return new BlockHeader(info);
98
+ };
99
+
100
+ /**
101
+ * @param {Binary} - Raw block binary data or buffer
102
+ * @returns {BlockHeader} - An instance of block header
103
+ */
104
+ BlockHeader.fromRawBlock = function fromRawBlock(data) {
105
+ if (!BufferUtil.isBuffer(data)) {
106
+ data = Buffer.from(data, 'binary');
107
+ }
108
+ var br = BufferReader(data);
109
+ br.pos = BlockHeader.Constants.START_OF_HEADER;
110
+ var info = BlockHeader._fromBufferReader(br);
111
+ return new BlockHeader(info);
112
+ };
113
+
114
+ /**
115
+ * @param {Buffer} - A buffer of the block header
116
+ * @returns {BlockHeader} - An instance of block header
117
+ */
118
+ BlockHeader.fromBuffer = function fromBuffer(buf) {
119
+ var info = BlockHeader._fromBufferReader(BufferReader(buf));
120
+ return new BlockHeader(info);
121
+ };
122
+
123
+ /**
124
+ * @param {string} - A hex encoded buffer of the block header
125
+ * @returns {BlockHeader} - An instance of block header
126
+ */
127
+ BlockHeader.fromString = function fromString(str) {
128
+ var buf = Buffer.from(str, 'hex');
129
+ return BlockHeader.fromBuffer(buf);
130
+ };
131
+
132
+ /**
133
+ * @param {BufferReader} - A BufferReader of the block header
134
+ * @returns {Object} - An object representing block header data
135
+ * @private
136
+ */
137
+ BlockHeader._fromBufferReader = function _fromBufferReader(br) {
138
+ var info = {};
139
+ info.version = br.readInt32LE();
140
+ info.prevHash = br.read(32);
141
+ info.merkleRoot = br.read(32);
142
+ info.time = br.readUInt32LE();
143
+ info.bits = br.readUInt32LE();
144
+ info.nonce = br.readUInt32LE();
145
+ return info;
146
+ };
147
+
148
+ /**
149
+ * @param {BufferReader} - A BufferReader of the block header
150
+ * @returns {BlockHeader} - An instance of block header
151
+ */
152
+ BlockHeader.fromBufferReader = function fromBufferReader(br) {
153
+ var info = BlockHeader._fromBufferReader(br);
154
+ return new BlockHeader(info);
155
+ };
156
+
157
+ /**
158
+ * @returns {Object} - A plain object of the BlockHeader
159
+ */
160
+ BlockHeader.prototype.toObject = BlockHeader.prototype.toJSON = function toObject() {
161
+ return {
162
+ hash: this.hash,
163
+ version: this.version,
164
+ prevHash: BufferUtil.reverse(this.prevHash).toString('hex'),
165
+ merkleRoot: BufferUtil.reverse(this.merkleRoot).toString('hex'),
166
+ time: this.time,
167
+ bits: this.bits,
168
+ nonce: this.nonce
169
+ };
170
+ };
171
+
172
+ /**
173
+ * @returns {Buffer} - A Buffer of the BlockHeader
174
+ */
175
+ BlockHeader.prototype.toBuffer = function toBuffer() {
176
+ return this.toBufferWriter().concat();
177
+ };
178
+
179
+ /**
180
+ * @returns {string} - A hex encoded string of the BlockHeader
181
+ */
182
+ BlockHeader.prototype.toString = function toString() {
183
+ return this.toBuffer().toString('hex');
184
+ };
185
+
186
+ /**
187
+ * @param {BufferWriter} - An existing instance BufferWriter
188
+ * @returns {BufferWriter} - An instance of BufferWriter representation of the BlockHeader
189
+ */
190
+ BlockHeader.prototype.toBufferWriter = function toBufferWriter(bw) {
191
+ if (!bw) {
192
+ bw = new BufferWriter();
193
+ }
194
+ bw.writeInt32LE(this.version);
195
+ bw.write(this.prevHash);
196
+ bw.write(this.merkleRoot);
197
+ bw.writeUInt32LE(this.time);
198
+ bw.writeUInt32LE(this.bits);
199
+ bw.writeUInt32LE(this.nonce);
200
+ return bw;
201
+ };
202
+
203
+ /**
204
+ * Returns the target difficulty for this block
205
+ * @param {Number} bits
206
+ * @returns {BN} An instance of BN with the decoded difficulty bits
207
+ */
208
+ BlockHeader.prototype.getTargetDifficulty = function getTargetDifficulty(bits) {
209
+ bits = bits || this.bits;
210
+
211
+ var target = new BN(bits & 0xffffff);
212
+ var mov = 8 * ((bits >>> 24) - 3);
213
+ while (mov-- > 0) {
214
+ target = target.mul(new BN(2));
215
+ }
216
+ return target;
217
+ };
218
+
219
+ /**
220
+ * @link https://en.bitcoin.it/wiki/Difficulty
221
+ * @return {Number}
222
+ */
223
+ BlockHeader.prototype.getDifficulty = function getDifficulty() {
224
+ var difficulty1TargetBN = this.getTargetDifficulty(GENESIS_BITS).mul(new BN(Math.pow(10, 8)));
225
+ var currentTargetBN = this.getTargetDifficulty();
226
+
227
+ var difficultyString = difficulty1TargetBN.div(currentTargetBN).toString(10);
228
+ var decimalPos = difficultyString.length - 8;
229
+ difficultyString = difficultyString.slice(0, decimalPos) + '.' + difficultyString.slice(decimalPos);
230
+
231
+ return parseFloat(difficultyString);
232
+ };
233
+
234
+ /**
235
+ * @returns {Buffer} - The little endian hash buffer of the header
236
+ */
237
+ BlockHeader.prototype._getHash = function hash() {
238
+ var buf = this.toBuffer();
239
+ return Hash.sha256sha256(buf);
240
+ };
241
+
242
+ var idProperty = {
243
+ configurable: false,
244
+ enumerable: true,
245
+ /**
246
+ * @returns {string} - The big endian hash buffer of the header
247
+ */
248
+ get: function() {
249
+ if (!this._id) {
250
+ this._id = BufferReader(this._getHash()).readReverse().toString('hex');
251
+ }
252
+ return this._id;
253
+ },
254
+ set: _.noop
255
+ };
256
+ Object.defineProperty(BlockHeader.prototype, 'id', idProperty);
257
+ Object.defineProperty(BlockHeader.prototype, 'hash', idProperty);
258
+
259
+ /**
260
+ * @returns {Boolean} - If timestamp is not too far in the future
261
+ */
262
+ BlockHeader.prototype.validTimestamp = function validTimestamp() {
263
+ var currentTime = Math.round(new Date().getTime() / 1000);
264
+ if (this.time > currentTime + BlockHeader.Constants.MAX_TIME_OFFSET) {
265
+ return false;
266
+ }
267
+ return true;
268
+ };
269
+
270
+ /**
271
+ * @returns {Boolean} - If the proof-of-work hash satisfies the target difficulty
272
+ */
273
+ BlockHeader.prototype.validProofOfWork = function validProofOfWork() {
274
+ var pow = new BN(this.id, 'hex');
275
+ var target = this.getTargetDifficulty();
276
+
277
+ if (pow.cmp(target) > 0) {
278
+ return false;
279
+ }
280
+ return true;
281
+ };
282
+
283
+ /**
284
+ * @returns {string} - A string formatted for the console
285
+ */
286
+ BlockHeader.prototype.inspect = function inspect() {
287
+ return '<BlockHeader ' + this.id + '>';
288
+ };
289
+
290
+ BlockHeader.Constants = {
291
+ START_OF_HEADER: 8, // Start buffer position in raw block data
292
+ MAX_TIME_OFFSET: 2 * 60 * 60, // The max a timestamp can be in the future
293
+ LARGEST_HASH: new BN('10000000000000000000000000000000000000000000000000000000000000000', 'hex')
294
+ };
295
+
296
+ module.exports = BlockHeader;
@@ -0,0 +1,4 @@
1
+ module.exports = require('./block');
2
+
3
+ module.exports.BlockHeader = require('./blockheader');
4
+ module.exports.MerkleBlock = require('./merkleblock');