@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.
- package/.mocharc.yaml +3 -0
- package/index.d.ts +1541 -0
- package/index.js +74 -0
- package/lib/address.js +478 -0
- package/lib/block/block.js +277 -0
- package/lib/block/blockheader.js +295 -0
- package/lib/block/index.js +4 -0
- package/lib/block/merkleblock.js +323 -0
- package/lib/bn.js +3423 -0
- package/lib/crypto/bn.js +278 -0
- package/lib/crypto/ecdsa.js +339 -0
- package/lib/crypto/hash.browser.js +171 -0
- package/lib/crypto/hash.js +2 -0
- package/lib/crypto/hash.node.js +171 -0
- package/lib/crypto/point.js +221 -0
- package/lib/crypto/random.browser.js +28 -0
- package/lib/crypto/random.js +2 -0
- package/lib/crypto/random.node.js +11 -0
- package/lib/crypto/signature.js +325 -0
- package/lib/encoding/base58.js +111 -0
- package/lib/encoding/base58check.js +121 -0
- package/lib/encoding/bufferreader.js +212 -0
- package/lib/encoding/bufferwriter.js +140 -0
- package/lib/encoding/decode-asm.js +24 -0
- package/lib/encoding/decode-hex.js +32 -0
- package/lib/encoding/decode-script-chunks.js +43 -0
- package/lib/encoding/encode-hex.js +284 -0
- package/lib/encoding/is-hex.js +7 -0
- package/lib/encoding/varint.js +75 -0
- package/lib/errors/index.js +54 -0
- package/lib/errors/spec.js +314 -0
- package/lib/hash-cache.js +50 -0
- package/lib/hdprivatekey.js +678 -0
- package/lib/hdpublickey.js +525 -0
- package/lib/message/message.js +191 -0
- package/lib/mnemonic/mnemonic.js +303 -0
- package/lib/mnemonic/pbkdf2.browser.js +68 -0
- package/lib/mnemonic/pbkdf2.js +2 -0
- package/lib/mnemonic/pbkdf2.node.js +68 -0
- package/lib/mnemonic/words/chinese.js +2054 -0
- package/lib/mnemonic/words/english.js +2054 -0
- package/lib/mnemonic/words/french.js +2054 -0
- package/lib/mnemonic/words/index.js +8 -0
- package/lib/mnemonic/words/italian.js +2054 -0
- package/lib/mnemonic/words/japanese.js +2054 -0
- package/lib/mnemonic/words/spanish.js +2054 -0
- package/lib/networks.js +379 -0
- package/lib/opcode.js +255 -0
- package/lib/privatekey.js +374 -0
- package/lib/publickey.js +386 -0
- package/lib/script/index.js +5 -0
- package/lib/script/interpreter.js +1834 -0
- package/lib/script/script.js +1074 -0
- package/lib/script/stack.js +109 -0
- package/lib/script/write-i32-le.js +17 -0
- package/lib/script/write-push-data.js +35 -0
- package/lib/script/write-u16-le.js +12 -0
- package/lib/script/write-u32-le.js +16 -0
- package/lib/script/write-u64-le.js +24 -0
- package/lib/script/write-u8-le.js +8 -0
- package/lib/script/write-varint.js +46 -0
- package/lib/transaction/index.js +7 -0
- package/lib/transaction/input/index.js +5 -0
- package/lib/transaction/input/input.js +354 -0
- package/lib/transaction/input/multisig.js +242 -0
- package/lib/transaction/input/publickey.js +100 -0
- package/lib/transaction/input/publickeyhash.js +118 -0
- package/lib/transaction/output.js +231 -0
- package/lib/transaction/sighash.js +167 -0
- package/lib/transaction/signature.js +97 -0
- package/lib/transaction/transaction.js +1639 -0
- package/lib/transaction/unspentoutput.js +113 -0
- package/lib/util/_.js +47 -0
- package/lib/util/js.js +90 -0
- package/lib/util/preconditions.js +33 -0
- package/package.json +26 -0
- package/test/address.js +509 -0
- package/test/block/block.js +251 -0
- package/test/block/blockheader.js +275 -0
- package/test/block/merklebloack.js +211 -0
- package/test/crypto/bn.js +177 -0
- package/test/crypto/ecdsa.js +391 -0
- package/test/crypto/hash.browser.js +135 -0
- package/test/crypto/hash.js +136 -0
- package/test/crypto/point.js +224 -0
- package/test/crypto/random.js +32 -0
- package/test/crypto/signature.js +409 -0
- package/test/data/bip69.json +215 -0
- package/test/data/bitcoind/base58_keys_invalid.json +52 -0
- package/test/data/bitcoind/base58_keys_valid.json +335 -0
- package/test/data/bitcoind/blocks.json +22 -0
- package/test/data/bitcoind/script_tests.json +3822 -0
- package/test/data/bitcoind/sig_canonical.json +7 -0
- package/test/data/bitcoind/sig_noncanonical.json +36 -0
- package/test/data/bitcoind/tx_invalid.json +445 -0
- package/test/data/bitcoind/tx_valid.json +44 -0
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +14 -0
- package/test/data/blk86756-testnet.json +684 -0
- package/test/data/block.hex +1 -0
- package/test/data/ecdsa.json +230 -0
- package/test/data/merkleblocks.js +488 -0
- package/test/data/messages.json +22 -0
- package/test/data/sighash.json +12 -0
- package/test/data/tx_creation.json +95 -0
- package/test/encoding/base58.js +131 -0
- package/test/encoding/base58check.js +136 -0
- package/test/encoding/bufferreader.js +337 -0
- package/test/encoding/bufferwriter.js +172 -0
- package/test/encoding/varint.js +104 -0
- package/test/hashCache.js +67 -0
- package/test/hdkeys.js +445 -0
- package/test/hdprivatekey.js +332 -0
- package/test/hdpublickey.js +304 -0
- package/test/index.js +16 -0
- package/test/message/message.js +204 -0
- package/test/mnemonic/data/fixtures.json +300 -0
- package/test/mnemonic/mnemonic.js +259 -0
- package/test/mnemonic/mocha.opts +1 -0
- package/test/mnemonic/pbkdf2.test.js +59 -0
- package/test/networks.js +159 -0
- package/test/opcode.js +161 -0
- package/test/privatekey.js +439 -0
- package/test/publickey.js +554 -0
- package/test/script/interpreter.js +734 -0
- package/test/script/script.js +1437 -0
- package/test/transaction/deserialize.js +34 -0
- package/test/transaction/input/input.js +90 -0
- package/test/transaction/input/multisig.js +90 -0
- package/test/transaction/input/publickey.js +68 -0
- package/test/transaction/input/publickeyhash.js +51 -0
- package/test/transaction/output.js +185 -0
- package/test/transaction/sighash.js +65 -0
- package/test/transaction/signature.js +114 -0
- package/test/transaction/transaction.js +1109 -0
- package/test/transaction/unspentoutput.js +110 -0
- package/test/util/js.js +76 -0
- package/test/util/preconditions.js +79 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var should = require('chai').should();
|
|
4
|
+
|
|
5
|
+
var opcat = require('../../index.js');
|
|
6
|
+
var MerkleBlock = opcat.MerkleBlock;
|
|
7
|
+
var BufferReader = opcat.encoding.BufferReader;
|
|
8
|
+
var BufferWriter = opcat.encoding.BufferWriter;
|
|
9
|
+
var Transaction = opcat.Transaction;
|
|
10
|
+
var data = require('../data/merkleblocks.js');
|
|
11
|
+
var transactionVector = require('../data/tx_creation');
|
|
12
|
+
|
|
13
|
+
describe('MerkleBlock', function () {
|
|
14
|
+
var blockhex = data.HEX[0];
|
|
15
|
+
var blockbuf = Buffer.from(blockhex, 'hex');
|
|
16
|
+
var blockJSON = JSON.stringify(data.JSON[0]);
|
|
17
|
+
var blockObject = JSON.parse(JSON.stringify(data.JSON[0]));
|
|
18
|
+
|
|
19
|
+
describe('#constructor', function () {
|
|
20
|
+
it('should make a new merkleblock from buffer', function () {
|
|
21
|
+
var b = MerkleBlock(blockbuf);
|
|
22
|
+
b.toBuffer().toString('hex').should.equal(blockhex);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should make a new merkleblock from object', function () {
|
|
26
|
+
var b = MerkleBlock(blockObject);
|
|
27
|
+
b.toObject().should.deep.equal(blockObject);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should make a new merkleblock from JSON', function () {
|
|
31
|
+
var b = MerkleBlock(JSON.parse(blockJSON));
|
|
32
|
+
JSON.stringify(b).should.equal(blockJSON);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should not make an empty block', function () {
|
|
36
|
+
(function () {
|
|
37
|
+
return new MerkleBlock();
|
|
38
|
+
}).should.throw('Unrecognized argument for MerkleBlock');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('#fromObject', function () {
|
|
43
|
+
it('should set these known values', function () {
|
|
44
|
+
var block = MerkleBlock.fromObject(JSON.parse(blockJSON));
|
|
45
|
+
should.exist(block.header);
|
|
46
|
+
should.exist(block.numTransactions);
|
|
47
|
+
should.exist(block.hashes);
|
|
48
|
+
should.exist(block.flags);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should set these known values', function () {
|
|
52
|
+
var block = MerkleBlock(JSON.parse(blockJSON));
|
|
53
|
+
should.exist(block.header);
|
|
54
|
+
should.exist(block.numTransactions);
|
|
55
|
+
should.exist(block.hashes);
|
|
56
|
+
should.exist(block.flags);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('accepts an object as argument', function () {
|
|
60
|
+
var block = MerkleBlock(blockbuf);
|
|
61
|
+
should.exist(MerkleBlock.fromObject(block.toObject()));
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe('#toJSON', function () {
|
|
66
|
+
it('should recover these known values', function () {
|
|
67
|
+
var block = new MerkleBlock(JSON.parse(blockJSON));
|
|
68
|
+
var b = JSON.parse(JSON.stringify(block));
|
|
69
|
+
should.exist(block.header);
|
|
70
|
+
should.exist(block.numTransactions);
|
|
71
|
+
should.exist(block.hashes);
|
|
72
|
+
should.exist(block.flags);
|
|
73
|
+
should.exist(b.header);
|
|
74
|
+
should.exist(b.numTransactions);
|
|
75
|
+
should.exist(b.hashes);
|
|
76
|
+
should.exist(b.flags);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('#fromBuffer', function () {
|
|
81
|
+
it('should make a block from this known buffer', function () {
|
|
82
|
+
var block = MerkleBlock.fromBuffer(blockbuf);
|
|
83
|
+
block.toBuffer().toString('hex').should.equal(blockhex);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('#fromBufferReader', function () {
|
|
88
|
+
it('should make a block from this known buffer', function () {
|
|
89
|
+
var block = MerkleBlock.fromBufferReader(BufferReader(blockbuf));
|
|
90
|
+
block.toBuffer().toString('hex').should.equal(blockhex);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('#toBuffer', function () {
|
|
95
|
+
it('should recover a block from this known buffer', function () {
|
|
96
|
+
var block = MerkleBlock.fromBuffer(blockbuf);
|
|
97
|
+
block.toBuffer().toString('hex').should.equal(blockhex);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('#toBufferWriter', function () {
|
|
102
|
+
it('should recover a block from this known buffer', function () {
|
|
103
|
+
var block = MerkleBlock.fromBuffer(blockbuf);
|
|
104
|
+
block.toBufferWriter().concat().toString('hex').should.equal(blockhex);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("doesn't create a bufferWriter if one provided", function () {
|
|
108
|
+
var writer = new BufferWriter();
|
|
109
|
+
var block = MerkleBlock.fromBuffer(blockbuf);
|
|
110
|
+
block.toBufferWriter(writer).should.equal(writer);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('#validMerkleTree', function () {
|
|
115
|
+
it('should validate good merkleblocks', function () {
|
|
116
|
+
data.JSON.forEach(function (data) {
|
|
117
|
+
var b = MerkleBlock(data);
|
|
118
|
+
b.validMerkleTree().should.equal(true);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should not validate merkleblocks with too many hashes', function () {
|
|
123
|
+
var b = MerkleBlock(data.JSON[0]);
|
|
124
|
+
// Add too many hashes
|
|
125
|
+
var i = 0;
|
|
126
|
+
while (i <= b.numTransactions) {
|
|
127
|
+
b.hashes.push('bad' + i++);
|
|
128
|
+
}
|
|
129
|
+
b.validMerkleTree().should.equal(false);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('should not validate merkleblocks with too few bit flags', function () {
|
|
133
|
+
var b = MerkleBlock(JSON.parse(blockJSON));
|
|
134
|
+
b.flags.pop();
|
|
135
|
+
b.validMerkleTree().should.equal(false);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('#filteredTxsHash', function () {
|
|
140
|
+
it('should validate good merkleblocks', function () {
|
|
141
|
+
var hashOfFilteredTx = '6f64fd5aa9dd01f74c03656d376625cf80328d83d9afebe60cc68b8f0e245bd9';
|
|
142
|
+
var b = MerkleBlock(data.JSON[3]);
|
|
143
|
+
b.filteredTxsHash()[0].should.equal(hashOfFilteredTx);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should fail with merkleblocks with too many hashes', function () {
|
|
147
|
+
var b = MerkleBlock(data.JSON[0]);
|
|
148
|
+
// Add too many hashes
|
|
149
|
+
var i = 0;
|
|
150
|
+
while (i <= b.numTransactions) {
|
|
151
|
+
b.hashes.push('bad' + i++);
|
|
152
|
+
}
|
|
153
|
+
(function () {
|
|
154
|
+
b.filteredTxsHash();
|
|
155
|
+
}).should.throw('This MerkleBlock contain an invalid Merkle Tree');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should fail with merkleblocks with too few bit flags', function () {
|
|
159
|
+
var b = MerkleBlock(JSON.parse(blockJSON));
|
|
160
|
+
b.flags.pop();
|
|
161
|
+
(function () {
|
|
162
|
+
b.filteredTxsHash();
|
|
163
|
+
}).should.throw('This MerkleBlock contain an invalid Merkle Tree');
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe('#hasTransaction', function () {
|
|
168
|
+
it('should find transactions via hash string', function () {
|
|
169
|
+
var jsonData = data.JSON[0];
|
|
170
|
+
var txId = Buffer.from(jsonData.hashes[1], 'hex').toString('hex');
|
|
171
|
+
var b = MerkleBlock(jsonData);
|
|
172
|
+
b.hasTransaction(txId + 'abcd').should.equal(false);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('should find transactions via Transaction object', function () {
|
|
176
|
+
// TODO: update data.TXHEX[0][0]
|
|
177
|
+
// var jsonData = data.JSON[0];
|
|
178
|
+
// var txBuf = Buffer.from(data.TXHEX[0][0], 'hex');
|
|
179
|
+
// var tx = new Transaction().fromBuffer(txBuf);
|
|
180
|
+
// var b = MerkleBlock(jsonData);
|
|
181
|
+
// b.hasTransaction(tx).should.equal(true);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should not find non-existant Transaction object', function () {
|
|
185
|
+
// TODO: update data.TXHEX[0][0]
|
|
186
|
+
// Reuse another transaction already in data/ dir
|
|
187
|
+
// var serialized = transactionVector[0][7];
|
|
188
|
+
// var tx = new Transaction().fromBuffer(Buffer.from(serialized, 'hex'));
|
|
189
|
+
// var b = MerkleBlock(data.JSON[0]);
|
|
190
|
+
// b.validMerkleTree().should.equal(true);
|
|
191
|
+
// b.hasTransaction(tx).should.equal(false);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('should not match with merkle nodes', function () {
|
|
195
|
+
var b = MerkleBlock(data.JSON[0]);
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
var hashData = [
|
|
200
|
+
['3612262624047ee87660be1a707519a443b1c1ce3d248cbfc6c15870f6c5daa2', false],
|
|
201
|
+
['019f5b01d4195ecbc9398fbf3c3b1fa9bb3183301d7a1fb3bd174fcfa40a2b65', true],
|
|
202
|
+
['41ed70551dd7e841883ab8f0b16bf04176b7d1480e4f0af9f3d4c3595768d068', false],
|
|
203
|
+
['20d2a7bc994987302e5b1ac80fc425fe25f8b63169ea78e68fbaaefa59379bbf', false],
|
|
204
|
+
];
|
|
205
|
+
|
|
206
|
+
hashData.forEach(function check(d) {
|
|
207
|
+
b.hasTransaction(d[0]).should.equal(d[1]);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var should = require('chai').should();
|
|
4
|
+
var opcat = require('../..');
|
|
5
|
+
var BN = opcat.crypto.BN;
|
|
6
|
+
|
|
7
|
+
describe('BN', function () {
|
|
8
|
+
it('should create a bn', function () {
|
|
9
|
+
var bn = new BN(50);
|
|
10
|
+
should.exist(bn);
|
|
11
|
+
bn.toString().should.equal('50');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should parse this number', function () {
|
|
15
|
+
var bn = new BN(999970000);
|
|
16
|
+
bn.toString().should.equal('999970000');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should parse numbers below and at bn.js internal word size', function () {
|
|
20
|
+
var bn = new BN(Math.pow(2, 26) - 1);
|
|
21
|
+
bn.toString().should.equal((Math.pow(2, 26) - 1).toString());
|
|
22
|
+
bn = new BN(Math.pow(2, 26));
|
|
23
|
+
bn.toString().should.equal(Math.pow(2, 26).toString());
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('#add', function () {
|
|
27
|
+
it('should add two small numbers together', function () {
|
|
28
|
+
var bn1 = new BN(50);
|
|
29
|
+
var bn2 = new BN(75);
|
|
30
|
+
var bn3 = bn1.add(bn2);
|
|
31
|
+
bn3.toString().should.equal('125');
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('#sub', function () {
|
|
36
|
+
it('should subtract a small number', function () {
|
|
37
|
+
var bn1 = new BN(50);
|
|
38
|
+
var bn2 = new BN(25);
|
|
39
|
+
var bn3 = bn1.sub(bn2);
|
|
40
|
+
bn3.toString().should.equal('25');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('#gt', function () {
|
|
45
|
+
it('should say 1 is greater than 0', function () {
|
|
46
|
+
var bn1 = new BN(1);
|
|
47
|
+
var bn0 = new BN(0);
|
|
48
|
+
bn1.gt(bn0).should.equal(true);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should say a big number is greater than a small big number', function () {
|
|
52
|
+
var bn1 = new BN('24023452345398529485723980457');
|
|
53
|
+
var bn0 = new BN('34098234283412341234049357');
|
|
54
|
+
bn1.gt(bn0).should.equal(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should say a big number is great than a standard number', function () {
|
|
58
|
+
var bn1 = new BN('24023452345398529485723980457');
|
|
59
|
+
var bn0 = new BN(5);
|
|
60
|
+
bn1.gt(bn0).should.equal(true);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('to/from ScriptNumBuffer', function () {
|
|
65
|
+
[0, 1, 10, 256, 1000, 65536, 65537, -1, -1000, -65536, -65537].forEach(function (n) {
|
|
66
|
+
it('rountrips correctly for ' + n, function () {
|
|
67
|
+
BN.fromScriptNumBuffer(new BN(n).toScriptNumBuffer()).toNumber().should.equal(n);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('#fromString', function () {
|
|
73
|
+
it('should make BN from a string', function () {
|
|
74
|
+
BN.fromString('5').toString().should.equal('5');
|
|
75
|
+
});
|
|
76
|
+
it('should work with hex string', function () {
|
|
77
|
+
BN.fromString('7fffff0000000000000000000000000000000000000000000000000000000000', 16)
|
|
78
|
+
.toString(16)
|
|
79
|
+
.should.equal('7fffff0000000000000000000000000000000000000000000000000000000000');
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('#toString', function () {
|
|
84
|
+
it('should make a string', function () {
|
|
85
|
+
new BN(5).toString().should.equal('5');
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('@fromBuffer', function () {
|
|
90
|
+
it('should work with big endian', function () {
|
|
91
|
+
var bn = BN.fromBuffer(Buffer.from('0001', 'hex'), {
|
|
92
|
+
endian: 'big',
|
|
93
|
+
});
|
|
94
|
+
bn.toString().should.equal('1');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should work with big endian 256', function () {
|
|
98
|
+
var bn = BN.fromBuffer(Buffer.from('0100', 'hex'), {
|
|
99
|
+
endian: 'big',
|
|
100
|
+
});
|
|
101
|
+
bn.toString().should.equal('256');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should work with little endian if we specify the size', function () {
|
|
105
|
+
var bn = BN.fromBuffer(Buffer.from('0100', 'hex'), {
|
|
106
|
+
size: 2,
|
|
107
|
+
endian: 'little',
|
|
108
|
+
});
|
|
109
|
+
bn.toString().should.equal('1');
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('#toBuffer', function () {
|
|
114
|
+
it('should create a 4 byte buffer', function () {
|
|
115
|
+
var bn = new BN(1);
|
|
116
|
+
bn.toBuffer({
|
|
117
|
+
size: 4,
|
|
118
|
+
})
|
|
119
|
+
.toString('hex')
|
|
120
|
+
.should.equal('00000001');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('should create a 4 byte buffer in little endian', function () {
|
|
124
|
+
var bn = new BN(1);
|
|
125
|
+
bn.toBuffer({
|
|
126
|
+
size: 4,
|
|
127
|
+
endian: 'little',
|
|
128
|
+
})
|
|
129
|
+
.toString('hex')
|
|
130
|
+
.should.equal('01000000');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should create a 2 byte buffer even if you ask for a 1 byte', function () {
|
|
134
|
+
var bn = new BN('ff00', 16);
|
|
135
|
+
bn.toBuffer({
|
|
136
|
+
size: 1,
|
|
137
|
+
})
|
|
138
|
+
.toString('hex')
|
|
139
|
+
.should.equal('ff00');
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should create a 4 byte buffer even if you ask for a 1 byte', function () {
|
|
143
|
+
var bn = new BN('ffffff00', 16);
|
|
144
|
+
bn.toBuffer({
|
|
145
|
+
size: 4,
|
|
146
|
+
})
|
|
147
|
+
.toString('hex')
|
|
148
|
+
.should.equal('ffffff00');
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe('@fromHex', function () {
|
|
153
|
+
it('should work with big endian', function () {
|
|
154
|
+
var bn = BN.fromHex('0001', {
|
|
155
|
+
endian: 'big',
|
|
156
|
+
});
|
|
157
|
+
bn.toString().should.equal('1');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should work with little endian if we specify the size', function () {
|
|
161
|
+
var bn = BN.fromHex('0100', {
|
|
162
|
+
size: 2,
|
|
163
|
+
endian: 'little',
|
|
164
|
+
});
|
|
165
|
+
bn.toString().should.equal('1');
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe('#toHex', function () {
|
|
170
|
+
it('should create a 4 byte he string', function () {
|
|
171
|
+
var bn = new BN(1);
|
|
172
|
+
bn.toHex({
|
|
173
|
+
size: 4,
|
|
174
|
+
}).should.equal('00000001');
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
});
|