@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.
Files changed (138) hide show
  1. package/.mocharc.yaml +3 -0
  2. package/index.d.ts +1541 -0
  3. package/index.js +74 -0
  4. package/lib/address.js +478 -0
  5. package/lib/block/block.js +277 -0
  6. package/lib/block/blockheader.js +295 -0
  7. package/lib/block/index.js +4 -0
  8. package/lib/block/merkleblock.js +323 -0
  9. package/lib/bn.js +3423 -0
  10. package/lib/crypto/bn.js +278 -0
  11. package/lib/crypto/ecdsa.js +339 -0
  12. package/lib/crypto/hash.browser.js +171 -0
  13. package/lib/crypto/hash.js +2 -0
  14. package/lib/crypto/hash.node.js +171 -0
  15. package/lib/crypto/point.js +221 -0
  16. package/lib/crypto/random.browser.js +28 -0
  17. package/lib/crypto/random.js +2 -0
  18. package/lib/crypto/random.node.js +11 -0
  19. package/lib/crypto/signature.js +325 -0
  20. package/lib/encoding/base58.js +111 -0
  21. package/lib/encoding/base58check.js +121 -0
  22. package/lib/encoding/bufferreader.js +212 -0
  23. package/lib/encoding/bufferwriter.js +140 -0
  24. package/lib/encoding/decode-asm.js +24 -0
  25. package/lib/encoding/decode-hex.js +32 -0
  26. package/lib/encoding/decode-script-chunks.js +43 -0
  27. package/lib/encoding/encode-hex.js +284 -0
  28. package/lib/encoding/is-hex.js +7 -0
  29. package/lib/encoding/varint.js +75 -0
  30. package/lib/errors/index.js +54 -0
  31. package/lib/errors/spec.js +314 -0
  32. package/lib/hash-cache.js +50 -0
  33. package/lib/hdprivatekey.js +678 -0
  34. package/lib/hdpublickey.js +525 -0
  35. package/lib/message/message.js +191 -0
  36. package/lib/mnemonic/mnemonic.js +303 -0
  37. package/lib/mnemonic/pbkdf2.browser.js +68 -0
  38. package/lib/mnemonic/pbkdf2.js +2 -0
  39. package/lib/mnemonic/pbkdf2.node.js +68 -0
  40. package/lib/mnemonic/words/chinese.js +2054 -0
  41. package/lib/mnemonic/words/english.js +2054 -0
  42. package/lib/mnemonic/words/french.js +2054 -0
  43. package/lib/mnemonic/words/index.js +8 -0
  44. package/lib/mnemonic/words/italian.js +2054 -0
  45. package/lib/mnemonic/words/japanese.js +2054 -0
  46. package/lib/mnemonic/words/spanish.js +2054 -0
  47. package/lib/networks.js +379 -0
  48. package/lib/opcode.js +255 -0
  49. package/lib/privatekey.js +374 -0
  50. package/lib/publickey.js +386 -0
  51. package/lib/script/index.js +5 -0
  52. package/lib/script/interpreter.js +1834 -0
  53. package/lib/script/script.js +1074 -0
  54. package/lib/script/stack.js +109 -0
  55. package/lib/script/write-i32-le.js +17 -0
  56. package/lib/script/write-push-data.js +35 -0
  57. package/lib/script/write-u16-le.js +12 -0
  58. package/lib/script/write-u32-le.js +16 -0
  59. package/lib/script/write-u64-le.js +24 -0
  60. package/lib/script/write-u8-le.js +8 -0
  61. package/lib/script/write-varint.js +46 -0
  62. package/lib/transaction/index.js +7 -0
  63. package/lib/transaction/input/index.js +5 -0
  64. package/lib/transaction/input/input.js +354 -0
  65. package/lib/transaction/input/multisig.js +242 -0
  66. package/lib/transaction/input/publickey.js +100 -0
  67. package/lib/transaction/input/publickeyhash.js +118 -0
  68. package/lib/transaction/output.js +231 -0
  69. package/lib/transaction/sighash.js +167 -0
  70. package/lib/transaction/signature.js +97 -0
  71. package/lib/transaction/transaction.js +1639 -0
  72. package/lib/transaction/unspentoutput.js +113 -0
  73. package/lib/util/_.js +47 -0
  74. package/lib/util/js.js +90 -0
  75. package/lib/util/preconditions.js +33 -0
  76. package/package.json +26 -0
  77. package/test/address.js +509 -0
  78. package/test/block/block.js +251 -0
  79. package/test/block/blockheader.js +275 -0
  80. package/test/block/merklebloack.js +211 -0
  81. package/test/crypto/bn.js +177 -0
  82. package/test/crypto/ecdsa.js +391 -0
  83. package/test/crypto/hash.browser.js +135 -0
  84. package/test/crypto/hash.js +136 -0
  85. package/test/crypto/point.js +224 -0
  86. package/test/crypto/random.js +32 -0
  87. package/test/crypto/signature.js +409 -0
  88. package/test/data/bip69.json +215 -0
  89. package/test/data/bitcoind/base58_keys_invalid.json +52 -0
  90. package/test/data/bitcoind/base58_keys_valid.json +335 -0
  91. package/test/data/bitcoind/blocks.json +22 -0
  92. package/test/data/bitcoind/script_tests.json +3822 -0
  93. package/test/data/bitcoind/sig_canonical.json +7 -0
  94. package/test/data/bitcoind/sig_noncanonical.json +36 -0
  95. package/test/data/bitcoind/tx_invalid.json +445 -0
  96. package/test/data/bitcoind/tx_valid.json +44 -0
  97. package/test/data/blk86756-testnet.dat +0 -0
  98. package/test/data/blk86756-testnet.js +14 -0
  99. package/test/data/blk86756-testnet.json +684 -0
  100. package/test/data/block.hex +1 -0
  101. package/test/data/ecdsa.json +230 -0
  102. package/test/data/merkleblocks.js +488 -0
  103. package/test/data/messages.json +22 -0
  104. package/test/data/sighash.json +12 -0
  105. package/test/data/tx_creation.json +95 -0
  106. package/test/encoding/base58.js +131 -0
  107. package/test/encoding/base58check.js +136 -0
  108. package/test/encoding/bufferreader.js +337 -0
  109. package/test/encoding/bufferwriter.js +172 -0
  110. package/test/encoding/varint.js +104 -0
  111. package/test/hashCache.js +67 -0
  112. package/test/hdkeys.js +445 -0
  113. package/test/hdprivatekey.js +332 -0
  114. package/test/hdpublickey.js +304 -0
  115. package/test/index.js +16 -0
  116. package/test/message/message.js +204 -0
  117. package/test/mnemonic/data/fixtures.json +300 -0
  118. package/test/mnemonic/mnemonic.js +259 -0
  119. package/test/mnemonic/mocha.opts +1 -0
  120. package/test/mnemonic/pbkdf2.test.js +59 -0
  121. package/test/networks.js +159 -0
  122. package/test/opcode.js +161 -0
  123. package/test/privatekey.js +439 -0
  124. package/test/publickey.js +554 -0
  125. package/test/script/interpreter.js +734 -0
  126. package/test/script/script.js +1437 -0
  127. package/test/transaction/deserialize.js +34 -0
  128. package/test/transaction/input/input.js +90 -0
  129. package/test/transaction/input/multisig.js +90 -0
  130. package/test/transaction/input/publickey.js +68 -0
  131. package/test/transaction/input/publickeyhash.js +51 -0
  132. package/test/transaction/output.js +185 -0
  133. package/test/transaction/sighash.js +65 -0
  134. package/test/transaction/signature.js +114 -0
  135. package/test/transaction/transaction.js +1109 -0
  136. package/test/transaction/unspentoutput.js +110 -0
  137. package/test/util/js.js +76 -0
  138. package/test/util/preconditions.js +79 -0
@@ -0,0 +1,251 @@
1
+ 'use strict';
2
+
3
+ var opcat = require('../..');
4
+ var BN = require('../../lib/crypto/bn');
5
+ var BufferReader = opcat.encoding.BufferReader;
6
+ var BufferWriter = opcat.encoding.BufferWriter;
7
+ var BlockHeader = opcat.BlockHeader;
8
+ var Block = opcat.Block;
9
+ var chai = require('chai');
10
+ var fs = require('fs');
11
+ var should = chai.should();
12
+ var Transaction = opcat.Transaction;
13
+
14
+ // https://test-insight.bitpay.com/block/000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11
15
+ var dataRawBlockBuffer = fs.readFileSync('test/data/blk86756-testnet.dat');
16
+ var dataRawBlockBinary = fs.readFileSync('test/data/blk86756-testnet.dat', 'binary');
17
+ var dataJson = fs.readFileSync('test/data/blk86756-testnet.json').toString();
18
+ var data = require('../data/blk86756-testnet');
19
+ var dataBlocks = require('../data/bitcoind/blocks');
20
+ var bockHex = fs.readFileSync('test/data/block.hex').toString();
21
+
22
+ describe('Block', function () {
23
+ var blockhex = data.blockhex;
24
+ var blockbuf = Buffer.from(blockhex, 'hex');
25
+ var bh = BlockHeader.fromBuffer(Buffer.from(data.blockheaderhex, 'hex'));
26
+ var txs = [];
27
+ JSON.parse(dataJson).transactions.forEach(function (tx) {
28
+ txs.push(new Transaction().fromObject(tx));
29
+ });
30
+ var json = dataJson;
31
+
32
+ var genesishex =
33
+ '010000000000000000000000000000000000000000000000000000000000000000000000f0a5e03e6de20c2c4af63d0e25f7815c4559ea6c0c02dcfc5cd72a58a9533700dae5494dffff7f20020000000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac0000000000';
34
+ var genesisbuf = Buffer.from(genesishex, 'hex');
35
+ var genesisidhex = '207a246c25611aaf0f2758cad7f342ded534c47b1b16abfd2beee94305e5f726';
36
+ var blockOneHex =
37
+ '0000002026f7e50543e9ee2bfdab161b7bc434d5de42f3d7ca58270faf1a61256c247a2079b3053ae552b1f4e6767371fba2e17e5d0809b0e0d89593615165f684b56c9f71d03f68ffff7f20010000000102000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0c510101082f454233322e302fffffffff0100f2052a010000001976a914fff49bb07d131b98555c1162332307f1aff41fc488ac0000000000';
38
+ var blockOneBuf = Buffer.from(blockOneHex, 'hex');
39
+ var blockOneId = '7dcc05c4a8145156780a8ffb6494aada528a7d6d66e52ff29ac312f4190c1358';
40
+
41
+ it('should make a new block', function () {
42
+ var b = Block(blockbuf);
43
+ b.toBuffer().toString('hex').should.equal(blockhex);
44
+ });
45
+
46
+ it('should not make an empty block', function () {
47
+ (function () {
48
+ return new Block();
49
+ }).should.throw('Unrecognized argument for Block');
50
+ });
51
+
52
+ describe('#constructor', function () {
53
+ it('should set these known values', function () {
54
+ var b = new Block({
55
+ header: bh,
56
+ transactions: txs,
57
+ });
58
+ should.exist(b.header);
59
+ should.exist(b.transactions);
60
+ });
61
+
62
+ it('should properly deserialize blocks', function () {
63
+ dataBlocks.forEach(function (block) {
64
+ var b = Block.fromBuffer(Buffer.from(block.data, 'hex'));
65
+ b.transactions.length.should.equal(block.transactions);
66
+ });
67
+ });
68
+ });
69
+
70
+ describe('#fromRawBlock', function () {
71
+ it('should instantiate from a raw block binary', function () {
72
+ // TODO:
73
+ // var x = Block.fromRawBlock(dataRawBlockBinary);
74
+ // x.header.version.should.equal(2);
75
+ // new BN(x.header.bits).toString('hex').should.equal('1c3fffc0');
76
+ });
77
+
78
+ it('should instantiate from raw block buffer', function () {
79
+ // TODO:
80
+ // var x = Block.fromRawBlock(dataRawBlockBuffer);
81
+ // x.header.version.should.equal(2);
82
+ // new BN(x.header.bits).toString('hex').should.equal('1c3fffc0');
83
+ });
84
+ });
85
+
86
+ describe('#fromJSON', function () {
87
+ it('should set these known values', function () {
88
+ var block = Block.fromObject(JSON.parse(json));
89
+ should.exist(block.header);
90
+ should.exist(block.transactions);
91
+ });
92
+
93
+ it('should set these known values', function () {
94
+ var block = new Block(JSON.parse(json));
95
+ should.exist(block.header);
96
+ should.exist(block.transactions);
97
+ });
98
+ });
99
+
100
+ describe('#toJSON', function () {
101
+ it('should recover these known values', function () {
102
+ var block = Block.fromObject(JSON.parse(json));
103
+ var b = block.toJSON();
104
+ should.exist(b.header);
105
+ should.exist(b.transactions);
106
+ });
107
+ });
108
+
109
+ describe('#fromString/#toString', function () {
110
+ it('should output/input a block hex string', function () {
111
+ var b = Block.fromString(blockhex);
112
+ b.toString().should.equal(blockhex);
113
+ });
114
+ });
115
+
116
+ describe('#fromBuffer', function () {
117
+ it('should make a block from this known buffer', function () {
118
+ var block = Block.fromBuffer(blockbuf);
119
+ block.toBuffer().toString('hex').should.equal(blockhex);
120
+ });
121
+
122
+ it('should instantiate from block buffer from the network', function () {
123
+ var networkBlock = bockHex;
124
+ var x = Block.fromBuffer(networkBlock);
125
+ x.toBuffer().toString('hex').should.equal(networkBlock);
126
+ });
127
+ });
128
+
129
+ describe('#fromBufferReader', function () {
130
+ it('should make a block from this known buffer', function () {
131
+ var block = Block.fromBufferReader(BufferReader(blockbuf));
132
+ block.toBuffer().toString('hex').should.equal(blockhex);
133
+ });
134
+ });
135
+
136
+ describe('#toBuffer', function () {
137
+ it('should recover a block from this known buffer', function () {
138
+ var block = Block.fromBuffer(blockbuf);
139
+ block.toBuffer().toString('hex').should.equal(blockhex);
140
+ });
141
+ });
142
+
143
+ describe('#toBufferWriter', function () {
144
+ it('should recover a block from this known buffer', function () {
145
+ var block = Block.fromBuffer(blockbuf);
146
+ block.toBufferWriter().concat().toString('hex').should.equal(blockhex);
147
+ });
148
+
149
+ it("doesn't create a bufferWriter if one provided", function () {
150
+ var writer = new BufferWriter();
151
+ var block = Block.fromBuffer(blockbuf);
152
+ block.toBufferWriter(writer).should.equal(writer);
153
+ });
154
+ });
155
+
156
+ describe('#toObject', function () {
157
+ it('should recover a block from genesis block buffer', function () {
158
+ var block = Block.fromBuffer(blockOneBuf);
159
+ block.id.should.equal(blockOneId);
160
+ block.toObject().should.deep.equal({
161
+ header: {
162
+ hash: '7dcc05c4a8145156780a8ffb6494aada528a7d6d66e52ff29ac312f4190c1358',
163
+ version: 536870912,
164
+ prevHash: '207a246c25611aaf0f2758cad7f342ded534c47b1b16abfd2beee94305e5f726',
165
+ merkleRoot: '9f6cb584f66551619395d8e0b009085d7ee1a2fb717376e6f4b152e53a05b379',
166
+ time: 1749012593,
167
+ bits: 545259519,
168
+ nonce: 1,
169
+ },
170
+ transactions: [
171
+ {
172
+ hash: '9f6cb584f66551619395d8e0b009085d7ee1a2fb717376e6f4b152e53a05b379',
173
+ version: 2,
174
+ inputs: [
175
+ {
176
+ prevTxId: '0000000000000000000000000000000000000000000000000000000000000000',
177
+ outputIndex: 4294967295,
178
+ sequenceNumber: 4294967295,
179
+ script: '510101082f454233322e302f',
180
+ },
181
+ ],
182
+ outputs: [
183
+ {
184
+ data: '',
185
+ satoshis: 5000000000,
186
+ script:
187
+ '76a914fff49bb07d131b98555c1162332307f1aff41fc488ac',
188
+ },
189
+ ],
190
+ nLockTime: 0,
191
+ },
192
+ ],
193
+ });
194
+ });
195
+
196
+ it('roundtrips correctly', function () {
197
+ var block = Block.fromBuffer(blockOneBuf);
198
+ var obj = block.toObject();
199
+ var block2 = Block.fromObject(obj);
200
+ block2.toObject().should.deep.equal(block.toObject());
201
+ });
202
+ });
203
+
204
+ describe('#_getHash', function () {
205
+ it('should return the correct hash of the genesis block', function () {
206
+ var block = Block.fromBuffer(genesisbuf);
207
+ var blockhash = Buffer.from(Array.apply([], Buffer.from(genesisidhex, 'hex')).reverse());
208
+ block._getHash().toString('hex').should.equal(blockhash.toString('hex'));
209
+ });
210
+ });
211
+
212
+ describe('#id', function () {
213
+ it('should return the correct id of the genesis block', function () {
214
+ var block = Block.fromBuffer(genesisbuf);
215
+ block.id.should.equal(genesisidhex);
216
+ });
217
+ it('"hash" should be the same as "id"', function () {
218
+ var block = Block.fromBuffer(genesisbuf);
219
+ block.id.should.equal(block.hash);
220
+ });
221
+ });
222
+
223
+ describe('#inspect', function () {
224
+ it('should return the correct inspect of the genesis block', function () {
225
+ var block = Block.fromBuffer(genesisbuf);
226
+ block.inspect().should.equal('<Block ' + genesisidhex + '>');
227
+ });
228
+ });
229
+
230
+ describe('#merkleRoot', function () {
231
+ it('should describe as valid merkle root', function () {
232
+ // var x = Block.fromRawBlock(dataRawBlockBinary);
233
+ // var valid = x.validMerkleRoot();
234
+ // valid.should.equal(true);
235
+ });
236
+
237
+ it('should describe as invalid merkle root', function () {
238
+ // var x = Block.fromRawBlock(dataRawBlockBinary);
239
+ // x.transactions.push(new Transaction());
240
+ // var valid = x.validMerkleRoot();
241
+ // valid.should.equal(false);
242
+ });
243
+
244
+ it('should get a null hash merkle root', function () {
245
+ // var x = Block.fromRawBlock(dataRawBlockBinary);
246
+ // x.transactions = []; // empty the txs
247
+ // var mr = x.getMerkleRoot();
248
+ // mr.should.deep.equal(Block.Values.NULL_HASH);
249
+ });
250
+ });
251
+ });
@@ -0,0 +1,275 @@
1
+ 'use strict';
2
+
3
+ var opcat = require('../..');
4
+ var BN = require('../../lib/crypto/bn');
5
+ var BufferReader = opcat.encoding.BufferReader;
6
+ var BufferWriter = opcat.encoding.BufferWriter;
7
+
8
+ var BlockHeader = opcat.BlockHeader;
9
+ var fs = require('fs');
10
+ var should = require('chai').should();
11
+
12
+ // https://test-insight.bitpay.com/block/000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11
13
+ var dataRawBlockBuffer = fs.readFileSync('test/data/blk86756-testnet.dat');
14
+ var dataRawBlockBinary = fs.readFileSync('test/data/blk86756-testnet.dat', 'binary');
15
+ var dataRawId = '000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11';
16
+ var data = require('../data/blk86756-testnet');
17
+
18
+ describe('BlockHeader', function () {
19
+ var version = data.version;
20
+ var prevblockidbuf = Buffer.from(data.prevblockidhex, 'hex');
21
+ var merklerootbuf = Buffer.from(data.merkleroothex, 'hex');
22
+ var time = data.time;
23
+ var bits = data.bits;
24
+ var nonce = data.nonce;
25
+ var bh = new BlockHeader({
26
+ version: version,
27
+ prevHash: prevblockidbuf,
28
+ merkleRoot: merklerootbuf,
29
+ time: time,
30
+ bits: bits,
31
+ nonce: nonce,
32
+ });
33
+ var bhhex = data.blockheaderhex;
34
+ var bhbuf = Buffer.from(bhhex, 'hex');
35
+
36
+ it('should make a new blockheader', function () {
37
+ BlockHeader(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
38
+ });
39
+
40
+ it('should not make an empty block', function () {
41
+ (function () {
42
+ BlockHeader();
43
+ }).should.throw('Unrecognized argument for BlockHeader');
44
+ });
45
+
46
+ describe('#constructor', function () {
47
+ it('should set all the variables', function () {
48
+ var bh = new BlockHeader({
49
+ version: version,
50
+ prevHash: prevblockidbuf,
51
+ merkleRoot: merklerootbuf,
52
+ time: time,
53
+ bits: bits,
54
+ nonce: nonce,
55
+ });
56
+ should.exist(bh.version);
57
+ should.exist(bh.prevHash);
58
+ should.exist(bh.merkleRoot);
59
+ should.exist(bh.time);
60
+ should.exist(bh.bits);
61
+ should.exist(bh.nonce);
62
+ });
63
+
64
+ it("will throw an error if the argument object hash property doesn't match", function () {
65
+ (function () {
66
+ new BlockHeader({
67
+ hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
68
+ version: version,
69
+ prevHash: prevblockidbuf,
70
+ merkleRoot: merklerootbuf,
71
+ time: time,
72
+ bits: bits,
73
+ nonce: nonce,
74
+ });
75
+ }).should.throw('Argument object hash property does not match block hash.');
76
+ });
77
+ });
78
+
79
+ describe('version', function () {
80
+ it('is interpreted as an int32le', function () {
81
+ var hex =
82
+ 'ffffffff00000000000000000000000000000000000000000000000000000000000000004141414141414141414141414141414141414141414141414141414141414141010000000200000003000000';
83
+ var header = BlockHeader.fromBuffer(Buffer.from(hex, 'hex'));
84
+ header.version.should.equal(-1);
85
+ header.timestamp.should.equal(1);
86
+ });
87
+ });
88
+
89
+ describe('#fromObject', function () {
90
+ it('should set all the variables', function () {
91
+ var bh = BlockHeader.fromObject({
92
+ version: version,
93
+ prevHash: prevblockidbuf.toString('hex'),
94
+ merkleRoot: merklerootbuf.toString('hex'),
95
+ time: time,
96
+ bits: bits,
97
+ nonce: nonce,
98
+ });
99
+ should.exist(bh.version);
100
+ should.exist(bh.prevHash);
101
+ should.exist(bh.merkleRoot);
102
+ should.exist(bh.time);
103
+ should.exist(bh.bits);
104
+ should.exist(bh.nonce);
105
+ });
106
+ });
107
+
108
+ describe('#toJSON', function () {
109
+ it('should set all the variables', function () {
110
+ var json = bh.toJSON();
111
+ should.exist(json.version);
112
+ should.exist(json.prevHash);
113
+ should.exist(json.merkleRoot);
114
+ should.exist(json.time);
115
+ should.exist(json.bits);
116
+ should.exist(json.nonce);
117
+ });
118
+ });
119
+
120
+ describe('#fromJSON', function () {
121
+ it('should parse this known json string', function () {
122
+ var jsonString = JSON.stringify({
123
+ version: version,
124
+ prevHash: prevblockidbuf,
125
+ merkleRoot: merklerootbuf,
126
+ time: time,
127
+ bits: bits,
128
+ nonce: nonce,
129
+ });
130
+
131
+ var json = new BlockHeader(JSON.parse(jsonString));
132
+ should.exist(json.version);
133
+ should.exist(json.prevHash);
134
+ should.exist(json.merkleRoot);
135
+ should.exist(json.time);
136
+ should.exist(json.bits);
137
+ should.exist(json.nonce);
138
+ });
139
+ });
140
+
141
+ describe('#fromString/#toString', function () {
142
+ it('should output/input a block hex string', function () {
143
+ var b = BlockHeader.fromString(bhhex);
144
+ b.toString().should.equal(bhhex);
145
+ });
146
+ });
147
+
148
+ describe('#fromBuffer', function () {
149
+ it('should parse this known buffer', function () {
150
+ BlockHeader.fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
151
+ });
152
+ });
153
+
154
+ describe('#fromBufferReader', function () {
155
+ it('should parse this known buffer', function () {
156
+ BlockHeader.fromBufferReader(BufferReader(bhbuf))
157
+ .toBuffer()
158
+ .toString('hex')
159
+ .should.equal(bhhex);
160
+ });
161
+ });
162
+
163
+ describe('#toBuffer', function () {
164
+ it('should output this known buffer', function () {
165
+ BlockHeader.fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
166
+ });
167
+ });
168
+
169
+ describe('#toBufferWriter', function () {
170
+ it('should output this known buffer', function () {
171
+ BlockHeader.fromBuffer(bhbuf).toBufferWriter().concat().toString('hex').should.equal(bhhex);
172
+ });
173
+
174
+ it("doesn't create a bufferWriter if one provided", function () {
175
+ var writer = new BufferWriter();
176
+ var blockHeader = BlockHeader.fromBuffer(bhbuf);
177
+ blockHeader.toBufferWriter(writer).should.equal(writer);
178
+ });
179
+ });
180
+
181
+ describe('#inspect', function () {
182
+ it('should return the correct inspect of the genesis block', function () {
183
+ var block = BlockHeader.fromRawBlock(dataRawBlockBinary);
184
+ block.inspect().should.equal('<BlockHeader ' + dataRawId + '>');
185
+ });
186
+ });
187
+
188
+ describe('#fromRawBlock', function () {
189
+ it('should instantiate from a raw block binary', function () {
190
+ var x = BlockHeader.fromRawBlock(dataRawBlockBinary);
191
+ x.version.should.equal(2);
192
+ new BN(x.bits).toString('hex').should.equal('1c3fffc0');
193
+ });
194
+
195
+ it('should instantiate from raw block buffer', function () {
196
+ var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
197
+ x.version.should.equal(2);
198
+ new BN(x.bits).toString('hex').should.equal('1c3fffc0');
199
+ });
200
+ });
201
+
202
+ describe('#validTimestamp', function () {
203
+ var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
204
+
205
+ it('should validate timpstamp as true', function () {
206
+ var valid = x.validTimestamp(x);
207
+ valid.should.equal(true);
208
+ });
209
+
210
+ it('should validate timestamp as false', function () {
211
+ x.time =
212
+ Math.round(new Date().getTime() / 1000) + BlockHeader.Constants.MAX_TIME_OFFSET + 100;
213
+ var valid = x.validTimestamp(x);
214
+ valid.should.equal(false);
215
+ });
216
+ });
217
+
218
+ describe('#validProofOfWork', function () {
219
+ it('should validate proof-of-work as true', function () {
220
+ var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
221
+ var valid = x.validProofOfWork(x);
222
+ valid.should.equal(true);
223
+ });
224
+
225
+ it('should validate proof of work as false because incorrect proof of work', function () {
226
+ var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
227
+ var nonce = x.nonce;
228
+ x.nonce = 0;
229
+ var valid = x.validProofOfWork(x);
230
+ valid.should.equal(false);
231
+ x.nonce = nonce;
232
+ });
233
+ });
234
+
235
+ describe('#getDifficulty', function () {
236
+ it('should get the correct difficulty for block 86756', function () {
237
+ var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
238
+ x.bits.should.equal(0x1c3fffc0);
239
+ x.getDifficulty().should.equal(4);
240
+ });
241
+
242
+ it('should get the correct difficulty for testnet block 552065', function () {
243
+ var x = new BlockHeader({
244
+ bits: 0x1b00c2a8,
245
+ });
246
+ x.getDifficulty().should.equal(86187.62562209);
247
+ });
248
+
249
+ it('should get the correct difficulty for livenet block 373043', function () {
250
+ var x = new BlockHeader({
251
+ bits: 0x18134dc1,
252
+ });
253
+ x.getDifficulty().should.equal(56957648455.01001);
254
+ });
255
+
256
+ it('should get the correct difficulty for livenet block 340000', function () {
257
+ var x = new BlockHeader({
258
+ bits: 0x1819012f,
259
+ });
260
+ x.getDifficulty().should.equal(43971662056.08958);
261
+ });
262
+
263
+ it('should use exponent notation if difficulty is larger than Javascript number', function () {
264
+ var x = new BlockHeader({
265
+ bits: 0x0900c2a8,
266
+ });
267
+ x.getDifficulty().should.equal(1.9220482782645836 * 1e48);
268
+ });
269
+ });
270
+
271
+ it('coverage: caches the "_id" property', function () {
272
+ var blockHeader = BlockHeader.fromRawBlock(dataRawBlockBuffer);
273
+ blockHeader.id.should.equal(blockHeader.id);
274
+ });
275
+ });