@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.
- package/LICENSE +32 -0
- package/README.md +59 -0
- package/index.d.ts +410 -0
- package/index.js +93 -0
- package/lib/address.js +792 -0
- package/lib/block/block.js +281 -0
- package/lib/block/blockheader.js +296 -0
- package/lib/block/index.js +4 -0
- package/lib/block/merkleblock.js +313 -0
- package/lib/crypto/bn.js +218 -0
- package/lib/crypto/ecdsa.js +296 -0
- package/lib/crypto/hash.js +85 -0
- package/lib/crypto/point.js +166 -0
- package/lib/crypto/random.js +57 -0
- package/lib/crypto/schnorr.js +287 -0
- package/lib/crypto/signature.js +441 -0
- package/lib/encoding/base58.js +70 -0
- package/lib/encoding/base58check.js +95 -0
- package/lib/encoding/bufferreader.js +202 -0
- package/lib/encoding/bufferwriter.js +155 -0
- package/lib/encoding/varint.js +72 -0
- package/lib/errors/index.js +61 -0
- package/lib/errors/spec.js +191 -0
- package/lib/hdprivatekey.js +646 -0
- package/lib/hdpublickey.js +496 -0
- package/lib/networks.js +277 -0
- package/lib/opcode.js +261 -0
- package/lib/privatekey.js +431 -0
- package/lib/publickey.js +375 -0
- package/lib/script/index.js +3 -0
- package/lib/script/interpreter.js +2107 -0
- package/lib/script/script.js +1163 -0
- package/lib/transaction/index.js +7 -0
- package/lib/transaction/input/index.js +6 -0
- package/lib/transaction/input/input.js +375 -0
- package/lib/transaction/input/multisig.js +221 -0
- package/lib/transaction/input/multisigscripthash.js +178 -0
- package/lib/transaction/input/publickey.js +91 -0
- package/lib/transaction/input/publickeyhash.js +100 -0
- package/lib/transaction/output.js +188 -0
- package/lib/transaction/sighash.js +534 -0
- package/lib/transaction/signature.js +89 -0
- package/lib/transaction/transaction.js +1384 -0
- package/lib/transaction/unspentoutput.js +109 -0
- package/lib/unit.js +238 -0
- package/lib/uri.js +223 -0
- package/lib/util/base32.js +63 -0
- package/lib/util/buffer.js +162 -0
- package/lib/util/convertBits.js +62 -0
- package/lib/util/js.js +110 -0
- package/lib/util/log.js +10 -0
- package/lib/util/preconditions.js +34 -0
- package/package.json +62 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
var BlockHeader = require('./blockheader');
|
|
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 Transaction = require('../transaction');
|
|
11
|
+
var errors = require('../errors');
|
|
12
|
+
var $ = require('../util/preconditions');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Instantiate a MerkleBlock from a Buffer, JSON object, or Object with
|
|
16
|
+
* the properties of the Block
|
|
17
|
+
*
|
|
18
|
+
* @param {*} - A Buffer, JSON string, or Object representing a MerkleBlock
|
|
19
|
+
* @returns {MerkleBlock}
|
|
20
|
+
* @constructor
|
|
21
|
+
*/
|
|
22
|
+
function MerkleBlock(arg) {
|
|
23
|
+
/* jshint maxstatements: 18 */
|
|
24
|
+
|
|
25
|
+
if (!(this instanceof MerkleBlock)) {
|
|
26
|
+
return new MerkleBlock(arg);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var info = {};
|
|
30
|
+
if (BufferUtil.isBuffer(arg)) {
|
|
31
|
+
info = MerkleBlock._fromBufferReader(BufferReader(arg));
|
|
32
|
+
} else if (_.isObject(arg)) {
|
|
33
|
+
var header;
|
|
34
|
+
if(arg.header instanceof BlockHeader) {
|
|
35
|
+
header = arg.header;
|
|
36
|
+
} else {
|
|
37
|
+
header = BlockHeader.fromObject(arg.header);
|
|
38
|
+
}
|
|
39
|
+
info = {
|
|
40
|
+
/**
|
|
41
|
+
* @name MerkleBlock#header
|
|
42
|
+
* @type {BlockHeader}
|
|
43
|
+
*/
|
|
44
|
+
header: header,
|
|
45
|
+
/**
|
|
46
|
+
* @name MerkleBlock#numTransactions
|
|
47
|
+
* @type {Number}
|
|
48
|
+
*/
|
|
49
|
+
numTransactions: arg.numTransactions,
|
|
50
|
+
/**
|
|
51
|
+
* @name MerkleBlock#hashes
|
|
52
|
+
* @type {String[]}
|
|
53
|
+
*/
|
|
54
|
+
hashes: arg.hashes,
|
|
55
|
+
/**
|
|
56
|
+
* @name MerkleBlock#flags
|
|
57
|
+
* @type {Number[]}
|
|
58
|
+
*/
|
|
59
|
+
flags: arg.flags
|
|
60
|
+
};
|
|
61
|
+
} else {
|
|
62
|
+
throw new TypeError('Unrecognized argument for MerkleBlock');
|
|
63
|
+
}
|
|
64
|
+
_.extend(this,info);
|
|
65
|
+
this._flagBitsUsed = 0;
|
|
66
|
+
this._hashesUsed = 0;
|
|
67
|
+
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {Buffer} - MerkleBlock data in a Buffer object
|
|
73
|
+
* @returns {MerkleBlock} - A MerkleBlock object
|
|
74
|
+
*/
|
|
75
|
+
MerkleBlock.fromBuffer = function fromBuffer(buf) {
|
|
76
|
+
return MerkleBlock.fromBufferReader(BufferReader(buf));
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {BufferReader} - MerkleBlock data in a BufferReader object
|
|
81
|
+
* @returns {MerkleBlock} - A MerkleBlock object
|
|
82
|
+
*/
|
|
83
|
+
MerkleBlock.fromBufferReader = function fromBufferReader(br) {
|
|
84
|
+
return new MerkleBlock(MerkleBlock._fromBufferReader(br));
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @returns {Buffer} - A buffer of the block
|
|
89
|
+
*/
|
|
90
|
+
MerkleBlock.prototype.toBuffer = function toBuffer() {
|
|
91
|
+
return this.toBufferWriter().concat();
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param {BufferWriter} - An existing instance of BufferWriter
|
|
96
|
+
* @returns {BufferWriter} - An instance of BufferWriter representation of the MerkleBlock
|
|
97
|
+
*/
|
|
98
|
+
MerkleBlock.prototype.toBufferWriter = function toBufferWriter(bw) {
|
|
99
|
+
if (!bw) {
|
|
100
|
+
bw = new BufferWriter();
|
|
101
|
+
}
|
|
102
|
+
bw.write(this.header.toBuffer());
|
|
103
|
+
bw.writeUInt32LE(this.numTransactions);
|
|
104
|
+
bw.writeVarintNum(this.hashes.length);
|
|
105
|
+
for (var i = 0; i < this.hashes.length; i++) {
|
|
106
|
+
bw.write(Buffer.from(this.hashes[i], 'hex'));
|
|
107
|
+
}
|
|
108
|
+
bw.writeVarintNum(this.flags.length);
|
|
109
|
+
for (i = 0; i < this.flags.length; i++) {
|
|
110
|
+
bw.writeUInt8(this.flags[i]);
|
|
111
|
+
}
|
|
112
|
+
return bw;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @returns {Object} - A plain object with the MerkleBlock properties
|
|
117
|
+
*/
|
|
118
|
+
MerkleBlock.prototype.toObject = MerkleBlock.prototype.toJSON = function toObject() {
|
|
119
|
+
return {
|
|
120
|
+
header: this.header.toObject(),
|
|
121
|
+
numTransactions: this.numTransactions,
|
|
122
|
+
hashes: this.hashes,
|
|
123
|
+
flags: this.flags
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Verify that the MerkleBlock is valid
|
|
129
|
+
* @returns {Boolean} - True/False whether this MerkleBlock is Valid
|
|
130
|
+
*/
|
|
131
|
+
MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {
|
|
132
|
+
$.checkState(_.isArray(this.flags), 'MerkleBlock flags is not an array');
|
|
133
|
+
$.checkState(_.isArray(this.hashes), 'MerkleBlock hashes is not an array');
|
|
134
|
+
|
|
135
|
+
// Can't have more hashes than numTransactions
|
|
136
|
+
if(this.hashes.length > this.numTransactions) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Can't have more flag bits than num hashes
|
|
141
|
+
if(this.flags.length * 8 < this.hashes.length) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
var height = this._calcTreeHeight();
|
|
146
|
+
var opts = { hashesUsed: 0, flagBitsUsed: 0 };
|
|
147
|
+
var root = this._traverseMerkleTree(height, 0, opts);
|
|
148
|
+
if(opts.hashesUsed !== this.hashes.length) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
return BufferUtil.equals(root, this.header.merkleRoot);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Return a list of all the txs hash that match the filter
|
|
156
|
+
* @returns {Array} - txs hash that match the filter
|
|
157
|
+
*/
|
|
158
|
+
MerkleBlock.prototype.filterdTxsHash = function filterdTxsHash() {
|
|
159
|
+
$.checkState(_.isArray(this.flags), 'MerkleBlock flags is not an array');
|
|
160
|
+
$.checkState(_.isArray(this.hashes), 'MerkleBlock hashes is not an array');
|
|
161
|
+
|
|
162
|
+
// Can't have more hashes than numTransactions
|
|
163
|
+
if(this.hashes.length > this.numTransactions) {
|
|
164
|
+
throw new errors.MerkleBlock.InvalidMerkleTree();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Can't have more flag bits than num hashes
|
|
168
|
+
if(this.flags.length * 8 < this.hashes.length) {
|
|
169
|
+
throw new errors.MerkleBlock.InvalidMerkleTree();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// If there is only one hash the filter do not match any txs in the block
|
|
173
|
+
if(this.hashes.length === 1) {
|
|
174
|
+
return [];
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
var height = this._calcTreeHeight();
|
|
178
|
+
var opts = { hashesUsed: 0, flagBitsUsed: 0 };
|
|
179
|
+
var txs = this._traverseMerkleTree(height, 0, opts, true);
|
|
180
|
+
if(opts.hashesUsed !== this.hashes.length) {
|
|
181
|
+
throw new errors.MerkleBlock.InvalidMerkleTree();
|
|
182
|
+
}
|
|
183
|
+
return txs;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Traverse a the tree in this MerkleBlock, validating it along the way
|
|
188
|
+
* Modeled after Bitcoin Core merkleblock.cpp TraverseAndExtract()
|
|
189
|
+
* @param {Number} - depth - Current height
|
|
190
|
+
* @param {Number} - pos - Current position in the tree
|
|
191
|
+
* @param {Object} - opts - Object with values that need to be mutated throughout the traversal
|
|
192
|
+
* @param {Boolean} - checkForTxs - if true return opts.txs else return the Merkle Hash
|
|
193
|
+
* @param {Number} - opts.flagBitsUsed - Number of flag bits used, should start at 0
|
|
194
|
+
* @param {Number} - opts.hashesUsed - Number of hashes used, should start at 0
|
|
195
|
+
* @param {Array} - opts.txs - Will finish populated by transactions found during traversal that match the filter
|
|
196
|
+
* @returns {Buffer|null} - Buffer containing the Merkle Hash for that height
|
|
197
|
+
* @returns {Array} - transactions found during traversal that match the filter
|
|
198
|
+
* @private
|
|
199
|
+
*/
|
|
200
|
+
MerkleBlock.prototype._traverseMerkleTree = function traverseMerkleTree(depth, pos, opts, checkForTxs) {
|
|
201
|
+
/* jshint maxcomplexity: 12*/
|
|
202
|
+
/* jshint maxstatements: 20 */
|
|
203
|
+
|
|
204
|
+
opts = opts || {};
|
|
205
|
+
opts.txs = opts.txs || [];
|
|
206
|
+
opts.flagBitsUsed = opts.flagBitsUsed || 0;
|
|
207
|
+
opts.hashesUsed = opts.hashesUsed || 0;
|
|
208
|
+
var checkForTxs = checkForTxs || false;
|
|
209
|
+
|
|
210
|
+
if(opts.flagBitsUsed > this.flags.length * 8) {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
var isParentOfMatch = (this.flags[opts.flagBitsUsed >> 3] >>> (opts.flagBitsUsed++ & 7)) & 1;
|
|
214
|
+
if(depth === 0 || !isParentOfMatch) {
|
|
215
|
+
if(opts.hashesUsed >= this.hashes.length) {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
var hash = this.hashes[opts.hashesUsed++];
|
|
219
|
+
if(depth === 0 && isParentOfMatch) {
|
|
220
|
+
opts.txs.push(hash);
|
|
221
|
+
}
|
|
222
|
+
return Buffer.from(hash, 'hex');
|
|
223
|
+
} else {
|
|
224
|
+
var left = this._traverseMerkleTree(depth-1, pos*2, opts);
|
|
225
|
+
var right = left;
|
|
226
|
+
if(pos*2+1 < this._calcTreeWidth(depth-1)) {
|
|
227
|
+
right = this._traverseMerkleTree(depth-1, pos*2+1, opts);
|
|
228
|
+
}
|
|
229
|
+
if (checkForTxs){
|
|
230
|
+
return opts.txs;
|
|
231
|
+
} else {
|
|
232
|
+
return Hash.sha256sha256(new Buffer.concat([left, right]));
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/** Calculates the width of a merkle tree at a given height.
|
|
238
|
+
* Modeled after Bitcoin Core merkleblock.h CalcTreeWidth()
|
|
239
|
+
* @param {Number} - Height at which we want the tree width
|
|
240
|
+
* @returns {Number} - Width of the tree at a given height
|
|
241
|
+
* @private
|
|
242
|
+
*/
|
|
243
|
+
MerkleBlock.prototype._calcTreeWidth = function calcTreeWidth(height) {
|
|
244
|
+
return (this.numTransactions + (1 << height) - 1) >> height;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
/** Calculates the height of the merkle tree in this MerkleBlock
|
|
248
|
+
* @param {Number} - Height at which we want the tree width
|
|
249
|
+
* @returns {Number} - Height of the merkle tree in this MerkleBlock
|
|
250
|
+
* @private
|
|
251
|
+
*/
|
|
252
|
+
MerkleBlock.prototype._calcTreeHeight = function calcTreeHeight() {
|
|
253
|
+
var height = 0;
|
|
254
|
+
while (this._calcTreeWidth(height) > 1) {
|
|
255
|
+
height++;
|
|
256
|
+
}
|
|
257
|
+
return height;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* @param {Transaction|String} - Transaction or Transaction ID Hash
|
|
262
|
+
* @returns {Boolean} - return true/false if this MerkleBlock has the TX or not
|
|
263
|
+
* @private
|
|
264
|
+
*/
|
|
265
|
+
MerkleBlock.prototype.hasTransaction = function hasTransaction(tx) {
|
|
266
|
+
$.checkArgument(!_.isUndefined(tx), 'tx cannot be undefined');
|
|
267
|
+
$.checkArgument(tx instanceof Transaction || typeof tx === 'string',
|
|
268
|
+
'Invalid tx given, tx must be a "string" or "Transaction"');
|
|
269
|
+
|
|
270
|
+
var hash = tx;
|
|
271
|
+
if(tx instanceof Transaction) {
|
|
272
|
+
// We need to reverse the id hash for the lookup
|
|
273
|
+
hash = BufferUtil.reverse(Buffer.from(tx.id, 'hex')).toString('hex');
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
var txs = [];
|
|
277
|
+
var height = this._calcTreeHeight();
|
|
278
|
+
this._traverseMerkleTree(height, 0, { txs: txs });
|
|
279
|
+
return txs.indexOf(hash) !== -1;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* @param {Buffer} - MerkleBlock data
|
|
284
|
+
* @returns {Object} - An Object representing merkleblock data
|
|
285
|
+
* @private
|
|
286
|
+
*/
|
|
287
|
+
MerkleBlock._fromBufferReader = function _fromBufferReader(br) {
|
|
288
|
+
$.checkState(!br.finished(), 'No merkleblock data received');
|
|
289
|
+
var info = {};
|
|
290
|
+
info.header = BlockHeader.fromBufferReader(br);
|
|
291
|
+
info.numTransactions = br.readUInt32LE();
|
|
292
|
+
var numHashes = br.readVarintNum();
|
|
293
|
+
info.hashes = [];
|
|
294
|
+
for (var i = 0; i < numHashes; i++) {
|
|
295
|
+
info.hashes.push(br.read(32).toString('hex'));
|
|
296
|
+
}
|
|
297
|
+
var numFlags = br.readVarintNum();
|
|
298
|
+
info.flags = [];
|
|
299
|
+
for (i = 0; i < numFlags; i++) {
|
|
300
|
+
info.flags.push(br.readUInt8());
|
|
301
|
+
}
|
|
302
|
+
return info;
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* @param {Object} - A plain JavaScript object
|
|
307
|
+
* @returns {Block} - An instance of block
|
|
308
|
+
*/
|
|
309
|
+
MerkleBlock.fromObject = function fromObject(obj) {
|
|
310
|
+
return new MerkleBlock(obj);
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
module.exports = MerkleBlock;
|
package/lib/crypto/bn.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var BN = require('bn.js');
|
|
4
|
+
var $ = require('../util/preconditions');
|
|
5
|
+
var _ = require('lodash');
|
|
6
|
+
|
|
7
|
+
var reversebuf = function(buf) {
|
|
8
|
+
var buf2 = Buffer.alloc(buf.length);
|
|
9
|
+
for (var i = 0; i < buf.length; i++) {
|
|
10
|
+
buf2[i] = buf[buf.length - 1 - i];
|
|
11
|
+
}
|
|
12
|
+
return buf2;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
BN.Zero = new BN(0);
|
|
16
|
+
BN.One = new BN(1);
|
|
17
|
+
BN.Minus1 = new BN(-1);
|
|
18
|
+
|
|
19
|
+
BN.fromNumber = function(n) {
|
|
20
|
+
if (_.isNumber(n)) {
|
|
21
|
+
if (n <= 0x1fffffffffffff) {
|
|
22
|
+
return new BN(n);
|
|
23
|
+
}
|
|
24
|
+
return new BN(n.toString(16), 16);
|
|
25
|
+
} else if (_.isString(n)) {
|
|
26
|
+
return new BN(n, 10);
|
|
27
|
+
}
|
|
28
|
+
throw new Error('Invalid input type');
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
BN.fromString = function(str, base) {
|
|
32
|
+
$.checkArgument(_.isString(str));
|
|
33
|
+
return new BN(str, base);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
BN.fromBuffer = function(buf, opts) {
|
|
37
|
+
if (typeof opts !== 'undefined' && opts.endian === 'little') {
|
|
38
|
+
buf = reversebuf(buf);
|
|
39
|
+
}
|
|
40
|
+
var hex = buf.toString('hex');
|
|
41
|
+
var bn = new BN(hex, 16);
|
|
42
|
+
return bn;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Instantiate a BigNumber from a "signed magnitude buffer"
|
|
47
|
+
* (a buffer where the most significant bit represents the sign (0 = positive, -1 = negative))
|
|
48
|
+
*/
|
|
49
|
+
BN.fromSM = function(buf, opts) {
|
|
50
|
+
var ret;
|
|
51
|
+
if (buf.length === 0) {
|
|
52
|
+
return BN.fromBuffer(Buffer.from([0]));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
var endian = 'big';
|
|
56
|
+
if (opts) {
|
|
57
|
+
endian = opts.endian;
|
|
58
|
+
}
|
|
59
|
+
if (endian === 'little') {
|
|
60
|
+
buf = reversebuf(buf);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (buf[0] & 0x80) {
|
|
64
|
+
buf[0] = buf[0] & 0x7f;
|
|
65
|
+
ret = BN.fromBuffer(buf);
|
|
66
|
+
ret.neg().copy(ret);
|
|
67
|
+
} else {
|
|
68
|
+
ret = BN.fromBuffer(buf);
|
|
69
|
+
}
|
|
70
|
+
return ret;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
BN.prototype.toNumber = function() {
|
|
75
|
+
return parseInt(this.toString(10), 10);
|
|
76
|
+
};
|
|
77
|
+
/*
|
|
78
|
+
BN.prototype.strip = function strip () {
|
|
79
|
+
this._padCount = 0;//this._padCount || 0;
|
|
80
|
+
while (this.length > 1 && this.words[this.length - 1] === 0) {
|
|
81
|
+
this.length--;
|
|
82
|
+
this._padCount++;
|
|
83
|
+
}
|
|
84
|
+
return this._normSign();
|
|
85
|
+
};
|
|
86
|
+
*/
|
|
87
|
+
BN.prototype.toBuffer = function(opts) {
|
|
88
|
+
|
|
89
|
+
var buf, hex;
|
|
90
|
+
if (opts && opts.size) {
|
|
91
|
+
|
|
92
|
+
hex = this.toString(16, 2);
|
|
93
|
+
|
|
94
|
+
var natlen = hex.length / 2;
|
|
95
|
+
buf = Buffer.from(hex, 'hex');
|
|
96
|
+
//console.log("toBuffer#######", opts, natlen, this._length)
|
|
97
|
+
|
|
98
|
+
if (natlen === opts.size) {
|
|
99
|
+
buf = buf;
|
|
100
|
+
} else if (natlen > opts.size) {
|
|
101
|
+
|
|
102
|
+
buf = BN.trim(buf, opts.size);
|
|
103
|
+
//console.log("toBuffer", opts, hex, buf)
|
|
104
|
+
} else if (natlen < opts.size) {
|
|
105
|
+
//console.log("toBuffer#######", opts, hex, buf)
|
|
106
|
+
buf = BN.pad(buf, natlen, opts.size);
|
|
107
|
+
}
|
|
108
|
+
} else {
|
|
109
|
+
hex = this.toString(16, 2);
|
|
110
|
+
buf = Buffer.from(hex, 'hex');
|
|
111
|
+
/*
|
|
112
|
+
if(this._padCount){
|
|
113
|
+
buf = BN.pad(buf, hex.length/2, hex.length/2+this._padCount);
|
|
114
|
+
}
|
|
115
|
+
*/
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (typeof opts !== 'undefined' && opts.endian === 'little') {
|
|
119
|
+
buf = reversebuf(buf);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return buf;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
BN.prototype.toSMBigEndian = function() {
|
|
126
|
+
var buf;
|
|
127
|
+
if (this.cmp(BN.Zero) === -1) {
|
|
128
|
+
buf = this.neg().toBuffer();
|
|
129
|
+
if (buf[0] & 0x80) {
|
|
130
|
+
buf = Buffer.concat([Buffer.from([0x80]), buf]);
|
|
131
|
+
} else {
|
|
132
|
+
buf[0] = buf[0] | 0x80;
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
buf = this.toBuffer();
|
|
136
|
+
if (buf[0] & 0x80) {
|
|
137
|
+
buf = Buffer.concat([Buffer.from([0x00]), buf]);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (buf.length === 1 & buf[0] === 0) {
|
|
142
|
+
buf = Buffer.from([]);
|
|
143
|
+
}
|
|
144
|
+
return buf;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
BN.prototype.toSM = function(opts) {
|
|
148
|
+
var endian = opts ? opts.endian : 'big';
|
|
149
|
+
var buf = this.toSMBigEndian();
|
|
150
|
+
|
|
151
|
+
if (endian === 'little') {
|
|
152
|
+
buf = reversebuf(buf);
|
|
153
|
+
}
|
|
154
|
+
return buf;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Create a BN from a "ScriptNum":
|
|
159
|
+
* This is analogous to the constructor for CScriptNum in bitcoind. Many ops in
|
|
160
|
+
* bitcoind's script interpreter use CScriptNum, which is not really a proper
|
|
161
|
+
* bignum. Instead, an error is thrown if trying to input a number bigger than
|
|
162
|
+
* 4 bytes. We copy that behavior here. A third argument, `size`, is provided to
|
|
163
|
+
* extend the hard limit of 4 bytes, as some usages require more than 4 bytes.
|
|
164
|
+
*/
|
|
165
|
+
BN.fromScriptNumBuffer = function(buf, fRequireMinimal, size) {
|
|
166
|
+
var nMaxNumSize = size || 4;
|
|
167
|
+
$.checkArgument(buf.length <= nMaxNumSize, new Error('script number overflow'));
|
|
168
|
+
if (fRequireMinimal && buf.length > 0) {
|
|
169
|
+
// Check that the number is encoded with the minimum possible
|
|
170
|
+
// number of bytes.
|
|
171
|
+
//
|
|
172
|
+
// If the most-significant-byte - excluding the sign bit - is zero
|
|
173
|
+
// then we're not minimal. Note how this test also rejects the
|
|
174
|
+
// negative-zero encoding, 0x80.
|
|
175
|
+
if ((buf[buf.length - 1] & 0x7f) === 0) {
|
|
176
|
+
// One exception: if there's more than one byte and the most
|
|
177
|
+
// significant bit of the second-most-significant-byte is set
|
|
178
|
+
// it would conflict with the sign bit. An example of this case
|
|
179
|
+
// is +-255, which encode to 0xff00 and 0xff80 respectively.
|
|
180
|
+
// (big-endian).
|
|
181
|
+
if (buf.length <= 1 || (buf[buf.length - 2] & 0x80) === 0) {
|
|
182
|
+
throw new Error('non-minimally encoded script number');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return BN.fromSM(buf, {
|
|
187
|
+
endian: 'little'
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The corollary to the above, with the notable exception that we do not throw
|
|
193
|
+
* an error if the output is larger than four bytes. (Which can happen if
|
|
194
|
+
* performing a numerical operation that results in an overflow to more than 4
|
|
195
|
+
* bytes).
|
|
196
|
+
*/
|
|
197
|
+
BN.prototype.toScriptNumBuffer = function() {
|
|
198
|
+
return this.toSM({
|
|
199
|
+
endian: 'little'
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
BN.trim = function(buf, natlen) {
|
|
204
|
+
return buf.slice(natlen - buf.length, buf.length);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
BN.pad = function(buf, natlen, size) {
|
|
208
|
+
var rbuf = Buffer.alloc(size);
|
|
209
|
+
for (var i = 0; i < buf.length; i++) {
|
|
210
|
+
rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i];
|
|
211
|
+
}
|
|
212
|
+
for (i = 0; i < size - natlen; i++) {
|
|
213
|
+
rbuf[i] = 0;
|
|
214
|
+
}
|
|
215
|
+
return rbuf;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
module.exports = BN;
|