@opcat-labs/opcat 1.0.0 → 1.0.2
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/.cjs2esm.json +18 -0
- package/.mocharc.yaml +1 -1
- package/CHANGELOG.md +13 -0
- package/README.md +6 -0
- package/{lib/address.js → cjs/address.cjs} +77 -72
- package/cjs/block/block.cjs +332 -0
- package/{lib/block/blockheader.js → cjs/block/blockheader.cjs} +8 -7
- package/cjs/block/index.cjs +2 -0
- package/{lib/block/merkleblock.js → cjs/block/merkleblock.cjs} +23 -15
- package/cjs/bn.cjs +3411 -0
- package/{lib/crypto/bn.js → cjs/crypto/bn.cjs} +3 -3
- package/{lib/crypto/ecdsa.js → cjs/crypto/ecdsa.cjs} +150 -14
- package/{lib/crypto/hash.node.js → cjs/crypto/hash.cjs} +13 -2
- package/cjs/crypto/index.cjs +16 -0
- package/{lib/crypto/point.js → cjs/crypto/point.cjs} +11 -4
- package/cjs/crypto/random.cjs +18 -0
- package/{lib/crypto/signature.js → cjs/crypto/signature.cjs} +158 -8
- package/{lib/encoding/base58.js → cjs/encoding/base58.cjs} +58 -2
- package/cjs/encoding/base58check.cjs +192 -0
- package/cjs/encoding/bufferreader.cjs +333 -0
- package/cjs/encoding/bufferwriter.cjs +244 -0
- package/{lib/encoding/decode-asm.js → cjs/encoding/decode-asm.cjs} +4 -4
- package/{lib/encoding/decode-hex.js → cjs/encoding/decode-hex.cjs} +1 -1
- package/cjs/encoding/index.cjs +14 -0
- package/cjs/encoding/varint.cjs +116 -0
- package/{lib/errors/index.js → cjs/errors/index.cjs} +9 -9
- package/{lib/errors/spec.js → cjs/errors/spec.cjs} +2 -2
- package/cjs/hash-cache.cjs +98 -0
- package/{lib/hdprivatekey.js → cjs/hdprivatekey.cjs} +232 -140
- package/{lib/hdpublickey.js → cjs/hdpublickey.cjs} +120 -93
- package/cjs/index.cjs +94 -0
- package/cjs/interpreter/index.cjs +2 -0
- package/cjs/interpreter/interpreter.cjs +1988 -0
- package/{lib/script/stack.js → cjs/interpreter/stack.cjs} +9 -2
- package/{lib/message/message.js → cjs/message/message.cjs} +62 -25
- package/cjs/mnemonic/index.cjs +3 -0
- package/{lib/mnemonic/mnemonic.js → cjs/mnemonic/mnemonic.cjs} +44 -13
- package/{lib/mnemonic/pbkdf2.node.js → cjs/mnemonic/pbkdf2.cjs} +9 -2
- package/cjs/mnemonic/words/index.cjs +66 -0
- package/cjs/network.cjs +13 -0
- package/cjs/networks.cjs +321 -0
- package/{lib/opcode.js → cjs/opcode.cjs} +69 -5
- package/cjs/privatekey.cjs +422 -0
- package/{lib/publickey.js → cjs/publickey.cjs} +14 -16
- package/cjs/script/index.cjs +2 -0
- package/{lib/script/script.js → cjs/script/script.cjs} +322 -67
- package/cjs/transaction/index.cjs +5 -0
- package/cjs/transaction/input/index.cjs +34 -0
- package/cjs/transaction/input/input.cjs +396 -0
- package/{lib/transaction/input/multisig.js → cjs/transaction/input/multisig.cjs} +112 -18
- package/{lib/transaction/input/publickey.js → cjs/transaction/input/publickey.cjs} +29 -19
- package/{lib/transaction/input/publickeyhash.js → cjs/transaction/input/publickeyhash.cjs} +25 -17
- package/{lib/transaction/output.js → cjs/transaction/output.cjs} +100 -15
- package/cjs/transaction/sighash.cjs +187 -0
- package/{lib/transaction/signature.js → cjs/transaction/signature.cjs} +30 -6
- package/cjs/transaction/transaction.cjs +2000 -0
- package/{lib/transaction/unspentoutput.js → cjs/transaction/unspentoutput.cjs} +5 -5
- package/cjs/util/derivation.cjs +53 -0
- package/cjs/util/index.cjs +11 -0
- package/cjs/util/js.cjs +95 -0
- package/{lib/util/preconditions.js → cjs/util/preconditions.cjs} +2 -2
- package/esm/address.js +483 -0
- package/{lib → esm}/block/block.js +82 -27
- package/esm/block/blockheader.js +296 -0
- package/esm/block/index.js +2 -0
- package/esm/block/merkleblock.js +331 -0
- package/esm/bn.js +3411 -0
- package/esm/crypto/bn.js +278 -0
- package/esm/crypto/ecdsa.js +475 -0
- package/{lib/crypto/hash.browser.js → esm/crypto/hash.js} +18 -7
- package/esm/crypto/index.js +16 -0
- package/esm/crypto/point.js +228 -0
- package/esm/crypto/random.js +18 -0
- package/esm/crypto/signature.js +475 -0
- package/esm/encoding/base58.js +167 -0
- package/esm/encoding/base58check.js +192 -0
- package/esm/encoding/bufferreader.js +333 -0
- package/esm/encoding/bufferwriter.js +243 -0
- package/esm/encoding/decode-asm.js +24 -0
- package/esm/encoding/decode-hex.js +32 -0
- package/esm/encoding/decode-script-chunks.js +43 -0
- package/esm/encoding/encode-hex.js +284 -0
- package/esm/encoding/index.js +14 -0
- package/esm/encoding/is-hex.js +7 -0
- package/esm/encoding/varint.js +116 -0
- package/esm/errors/index.js +54 -0
- package/esm/errors/spec.js +314 -0
- package/esm/hash-cache.js +98 -0
- package/esm/hdprivatekey.js +768 -0
- package/esm/hdpublickey.js +549 -0
- package/esm/index.js +66 -0
- package/esm/interpreter/index.js +2 -0
- package/{lib/script → esm/interpreter}/interpreter.js +219 -66
- package/esm/interpreter/stack.js +116 -0
- package/esm/message/message.js +228 -0
- package/esm/mnemonic/index.js +3 -0
- package/esm/mnemonic/mnemonic.js +332 -0
- package/{lib/mnemonic/pbkdf2.browser.js → esm/mnemonic/pbkdf2.js} +13 -6
- package/esm/mnemonic/words/chinese.js +2054 -0
- package/esm/mnemonic/words/english.js +2054 -0
- package/esm/mnemonic/words/french.js +2054 -0
- package/esm/mnemonic/words/index.js +66 -0
- package/esm/mnemonic/words/italian.js +2054 -0
- package/esm/mnemonic/words/japanese.js +2054 -0
- package/esm/mnemonic/words/spanish.js +2054 -0
- package/esm/network.js +13 -0
- package/{lib → esm}/networks.js +61 -120
- package/esm/opcode.js +319 -0
- package/{lib → esm}/privatekey.js +76 -28
- package/esm/publickey.js +384 -0
- package/esm/script/index.js +2 -0
- package/esm/script/script.js +1329 -0
- package/esm/script/write-i32-le.js +17 -0
- package/esm/script/write-push-data.js +35 -0
- package/esm/script/write-u16-le.js +12 -0
- package/esm/script/write-u32-le.js +16 -0
- package/esm/script/write-u64-le.js +24 -0
- package/esm/script/write-u8-le.js +8 -0
- package/esm/script/write-varint.js +46 -0
- package/esm/transaction/index.js +5 -0
- package/esm/transaction/input/index.js +33 -0
- package/{lib → esm}/transaction/input/input.js +132 -90
- package/esm/transaction/input/multisig.js +335 -0
- package/esm/transaction/input/publickey.js +108 -0
- package/esm/transaction/input/publickeyhash.js +124 -0
- package/esm/transaction/output.js +316 -0
- package/{lib → esm}/transaction/sighash.js +42 -22
- package/esm/transaction/signature.js +120 -0
- package/{lib → esm}/transaction/transaction.js +522 -163
- package/esm/transaction/unspentoutput.js +112 -0
- package/esm/util/_.js +47 -0
- package/esm/util/derivation.js +53 -0
- package/esm/util/index.js +12 -0
- package/esm/util/js.js +95 -0
- package/esm/util/preconditions.js +33 -0
- package/fixup.cjs +17 -0
- package/package.json +40 -26
- package/test/{address.js → address.cjs} +14 -43
- package/test/block/{block.js → block.cjs} +3 -5
- package/test/block/{blockheader.js → blockheader.cjs} +2 -2
- package/test/block/{merklebloack.js → merklebloack.cjs} +2 -2
- package/test/crypto/{ecdsa.js → ecdsa.cjs} +9 -9
- package/test/crypto/{hash.browser.js → hash.browser.cjs} +2 -1
- package/test/crypto/{signature.js → signature.cjs} +2 -2
- package/test/data/bitcoind/script_tests.json +5 -5
- package/test/{hashCache.js → hashCache.cjs} +2 -1
- package/test/{hdkeys.js → hdkeys.cjs} +4 -2
- package/test/{hdprivatekey.js → hdprivatekey.cjs} +7 -6
- package/test/{hdpublickey.js → hdpublickey.cjs} +2 -7
- package/test/mnemonic/{pbkdf2.test.js → pbkdf2.test.cjs} +2 -2
- package/test/{networks.js → networks.cjs} +12 -31
- package/test/{publickey.js → publickey.cjs} +2 -2
- package/test/script/{interpreter.js → interpreter.cjs} +5 -5
- package/test/script/{script.js → script.cjs} +8 -2
- package/test/transaction/{deserialize.js → deserialize.cjs} +2 -2
- package/test/transaction/input/{input.js → input.cjs} +1 -1
- package/test/transaction/input/{multisig.js → multisig.cjs} +2 -1
- package/test/transaction/input/{publickeyhash.js → publickeyhash.cjs} +1 -1
- package/test/transaction/{sighash.js → sighash.cjs} +1 -1
- package/test/transaction/{transaction.js → transaction.cjs} +2 -2
- package/tsconfig.json +13 -0
- package/types/address.d.cts +252 -0
- package/types/block/block.d.cts +139 -0
- package/types/block/blockheader.d.cts +125 -0
- package/types/block/index.d.cts +2 -0
- package/types/block/merkleblock.d.cts +95 -0
- package/types/bn.d.cts +202 -0
- package/types/crypto/bn.d.cts +2 -0
- package/types/crypto/ecdsa.d.cts +187 -0
- package/types/crypto/hash.d.cts +118 -0
- package/types/crypto/index.d.cts +7 -0
- package/types/crypto/point.d.cts +134 -0
- package/types/crypto/random.d.cts +13 -0
- package/types/crypto/signature.d.cts +160 -0
- package/types/encoding/base58.d.cts +106 -0
- package/types/encoding/base58check.d.cts +107 -0
- package/types/encoding/bufferreader.d.cts +164 -0
- package/types/encoding/bufferwriter.d.cts +126 -0
- package/types/encoding/decode-asm.d.cts +2 -0
- package/types/encoding/decode-hex.d.cts +2 -0
- package/types/encoding/decode-script-chunks.d.cts +14 -0
- package/types/encoding/encode-hex.d.cts +2 -0
- package/types/encoding/index.d.cts +6 -0
- package/types/encoding/is-hex.d.cts +2 -0
- package/types/encoding/varint.d.cts +66 -0
- package/types/errors/index.d.cts +4 -0
- package/types/errors/spec.d.cts +22 -0
- package/types/hash-cache.d.cts +65 -0
- package/types/hdprivatekey.d.cts +281 -0
- package/types/hdpublickey.d.cts +240 -0
- package/types/index.d.cts +26 -0
- package/types/interpreter/index.d.cts +2 -0
- package/types/interpreter/interpreter.d.cts +228 -0
- package/types/interpreter/stack.d.cts +35 -0
- package/types/message/message.d.cts +110 -0
- package/types/mnemonic/index.d.cts +2 -0
- package/types/mnemonic/mnemonic.d.cts +171 -0
- package/types/mnemonic/pbkdf2.d.cts +14 -0
- package/types/mnemonic/words/chinese.d.cts +2 -0
- package/types/mnemonic/words/english.d.cts +2 -0
- package/types/mnemonic/words/french.d.cts +2 -0
- package/types/mnemonic/words/index.d.cts +22 -0
- package/types/mnemonic/words/italian.d.cts +2 -0
- package/types/mnemonic/words/japanese.d.cts +2 -0
- package/types/mnemonic/words/spanish.d.cts +2 -0
- package/types/network.d.cts +11 -0
- package/types/networks.d.cts +76 -0
- package/types/opcode.d.cts +93 -0
- package/types/privatekey.d.cts +169 -0
- package/types/publickey.d.cts +202 -0
- package/types/script/index.d.cts +2 -0
- package/types/script/script.d.cts +449 -0
- package/types/script/write-i32-le.d.cts +2 -0
- package/types/script/write-push-data.d.cts +2 -0
- package/types/script/write-u16-le.d.cts +2 -0
- package/types/script/write-u32-le.d.cts +2 -0
- package/types/script/write-u64-le.d.cts +2 -0
- package/types/script/write-u8-le.d.cts +2 -0
- package/types/script/write-varint.d.cts +2 -0
- package/types/transaction/index.d.cts +2 -0
- package/types/transaction/input/index.d.cts +2 -0
- package/types/transaction/input/input.d.cts +178 -0
- package/types/transaction/input/multisig.d.cts +127 -0
- package/types/transaction/input/publickey.d.cts +44 -0
- package/types/transaction/input/publickeyhash.d.cts +45 -0
- package/types/transaction/output.d.cts +118 -0
- package/types/transaction/sighash.d.cts +61 -0
- package/types/transaction/signature.d.cts +43 -0
- package/types/transaction/transaction.d.cts +716 -0
- package/types/transaction/unspentoutput.d.cts +83 -0
- package/types/util/_.d.cts +26 -0
- package/types/util/derivation.d.cts +21 -0
- package/types/util/index.d.cts +5 -0
- package/types/util/js.d.cts +50 -0
- package/types/util/preconditions.d.cts +3 -0
- package/index.d.ts +0 -1541
- package/index.js +0 -74
- package/lib/block/index.js +0 -4
- package/lib/bn.js +0 -3423
- package/lib/crypto/hash.js +0 -2
- package/lib/crypto/random.browser.js +0 -28
- package/lib/crypto/random.js +0 -2
- package/lib/crypto/random.node.js +0 -11
- package/lib/encoding/base58check.js +0 -121
- package/lib/encoding/bufferreader.js +0 -212
- package/lib/encoding/bufferwriter.js +0 -140
- package/lib/encoding/varint.js +0 -75
- package/lib/hash-cache.js +0 -50
- package/lib/mnemonic/pbkdf2.js +0 -2
- package/lib/mnemonic/words/index.js +0 -8
- package/lib/script/index.js +0 -5
- package/lib/transaction/index.js +0 -7
- package/lib/transaction/input/index.js +0 -5
- package/lib/util/js.js +0 -90
- /package/{lib/encoding/decode-script-chunks.js → cjs/encoding/decode-script-chunks.cjs} +0 -0
- /package/{lib/encoding/encode-hex.js → cjs/encoding/encode-hex.cjs} +0 -0
- /package/{lib/encoding/is-hex.js → cjs/encoding/is-hex.cjs} +0 -0
- /package/{lib/mnemonic/words/chinese.js → cjs/mnemonic/words/chinese.cjs} +0 -0
- /package/{lib/mnemonic/words/english.js → cjs/mnemonic/words/english.cjs} +0 -0
- /package/{lib/mnemonic/words/french.js → cjs/mnemonic/words/french.cjs} +0 -0
- /package/{lib/mnemonic/words/italian.js → cjs/mnemonic/words/italian.cjs} +0 -0
- /package/{lib/mnemonic/words/japanese.js → cjs/mnemonic/words/japanese.cjs} +0 -0
- /package/{lib/mnemonic/words/spanish.js → cjs/mnemonic/words/spanish.cjs} +0 -0
- /package/{lib/script/write-i32-le.js → cjs/script/write-i32-le.cjs} +0 -0
- /package/{lib/script/write-push-data.js → cjs/script/write-push-data.cjs} +0 -0
- /package/{lib/script/write-u16-le.js → cjs/script/write-u16-le.cjs} +0 -0
- /package/{lib/script/write-u32-le.js → cjs/script/write-u32-le.cjs} +0 -0
- /package/{lib/script/write-u64-le.js → cjs/script/write-u64-le.cjs} +0 -0
- /package/{lib/script/write-u8-le.js → cjs/script/write-u8-le.cjs} +0 -0
- /package/{lib/script/write-varint.js → cjs/script/write-varint.cjs} +0 -0
- /package/{lib/util/_.js → cjs/util/_.cjs} +0 -0
- /package/test/crypto/{bn.js → bn.cjs} +0 -0
- /package/test/crypto/{hash.js → hash.cjs} +0 -0
- /package/test/crypto/{point.js → point.cjs} +0 -0
- /package/test/crypto/{random.js → random.cjs} +0 -0
- /package/test/data/{blk86756-testnet.js → blk86756-testnet.cjs} +0 -0
- /package/test/data/{merkleblocks.js → merkleblocks.cjs} +0 -0
- /package/test/encoding/{base58.js → base58.cjs} +0 -0
- /package/test/encoding/{base58check.js → base58check.cjs} +0 -0
- /package/test/encoding/{bufferreader.js → bufferreader.cjs} +0 -0
- /package/test/encoding/{bufferwriter.js → bufferwriter.cjs} +0 -0
- /package/test/encoding/{varint.js → varint.cjs} +0 -0
- /package/test/{index.js → index.cjs} +0 -0
- /package/test/message/{message.js → message.cjs} +0 -0
- /package/test/mnemonic/{mnemonic.js → mnemonic.cjs} +0 -0
- /package/test/{opcode.js → opcode.cjs} +0 -0
- /package/test/{privatekey.js → privatekey.cjs} +0 -0
- /package/test/transaction/input/{publickey.js → publickey.cjs} +0 -0
- /package/test/transaction/{output.js → output.cjs} +0 -0
- /package/test/transaction/{signature.js → signature.cjs} +0 -0
- /package/test/transaction/{unspentoutput.js → unspentoutput.cjs} +0 -0
- /package/test/util/{js.js → js.cjs} +0 -0
- /package/test/util/{preconditions.js → preconditions.cjs} +0 -0
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
import _ from '../util/_.js';
|
|
4
|
+
import BlockHeader from './blockheader.js';
|
|
5
|
+
import MerkleBlock from './merkleblock.js';
|
|
6
|
+
import BN from '../crypto/bn.js';
|
|
7
|
+
import BufferReader from '../encoding/bufferreader.js';
|
|
8
|
+
import BufferWriter from '../encoding/bufferwriter.js';
|
|
9
|
+
import Hash from '../crypto/hash.js';
|
|
10
|
+
import Transaction from '../transaction/index.js';
|
|
11
|
+
import $ from '../util/preconditions.js';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Instantiate a Block from a Buffer, JSON object, or Object with
|
|
@@ -25,11 +26,17 @@ function Block(arg) {
|
|
|
25
26
|
return this;
|
|
26
27
|
}
|
|
27
28
|
|
|
29
|
+
/**
|
|
30
|
+
* The maximum allowed size (in bytes) for a block.
|
|
31
|
+
* @type {number}
|
|
32
|
+
*/
|
|
28
33
|
Block.MAX_BLOCK_SIZE = 128000000;
|
|
29
34
|
|
|
35
|
+
|
|
30
36
|
/**
|
|
31
|
-
*
|
|
32
|
-
* @
|
|
37
|
+
* Creates a Block instance from the given argument.
|
|
38
|
+
* @param {*} arg - The input to convert into a Block.
|
|
39
|
+
* @returns {Block} A new Block instance.
|
|
33
40
|
* @throws {TypeError} - If the argument was not recognized
|
|
34
41
|
* @private
|
|
35
42
|
*/
|
|
@@ -45,9 +52,11 @@ Block._from = function _from(arg) {
|
|
|
45
52
|
return info;
|
|
46
53
|
};
|
|
47
54
|
|
|
55
|
+
|
|
48
56
|
/**
|
|
49
|
-
*
|
|
50
|
-
* @
|
|
57
|
+
* Creates a Block instance from a plain object.
|
|
58
|
+
* @param {Object} data - The plain object containing block data.
|
|
59
|
+
* @returns {Block} The created Block instance.
|
|
51
60
|
* @private
|
|
52
61
|
*/
|
|
53
62
|
Block._fromObject = function _fromObject(data) {
|
|
@@ -66,19 +75,23 @@ Block._fromObject = function _fromObject(data) {
|
|
|
66
75
|
return info;
|
|
67
76
|
};
|
|
68
77
|
|
|
78
|
+
|
|
69
79
|
/**
|
|
70
|
-
*
|
|
71
|
-
* @
|
|
80
|
+
* Creates a Block instance from a plain JavaScript object.
|
|
81
|
+
* @param {Object} obj - The source object to convert to a Block.
|
|
82
|
+
* @returns {Block} A new Block instance.
|
|
72
83
|
*/
|
|
73
84
|
Block.fromObject = function fromObject(obj) {
|
|
74
85
|
var info = Block._fromObject(obj);
|
|
75
86
|
return new Block(info);
|
|
76
87
|
};
|
|
77
88
|
|
|
89
|
+
|
|
78
90
|
/**
|
|
79
|
-
*
|
|
80
|
-
* @returns {Object} - An object representing the block data
|
|
91
|
+
* Creates a Block instance from a BufferReader.
|
|
81
92
|
* @private
|
|
93
|
+
* @param {BufferReader} br - The buffer reader containing block data
|
|
94
|
+
* @returns {Block} The parsed Block instance
|
|
82
95
|
*/
|
|
83
96
|
Block._fromBufferReader = function _fromBufferReader(br) {
|
|
84
97
|
var info = {};
|
|
@@ -92,9 +105,11 @@ Block._fromBufferReader = function _fromBufferReader(br) {
|
|
|
92
105
|
return info;
|
|
93
106
|
};
|
|
94
107
|
|
|
108
|
+
|
|
95
109
|
/**
|
|
96
|
-
*
|
|
97
|
-
* @
|
|
110
|
+
* Creates a Block instance from a BufferReader.
|
|
111
|
+
* @param {BufferReader} br - The buffer reader containing block data.
|
|
112
|
+
* @returns {Block} The parsed Block instance.
|
|
98
113
|
*/
|
|
99
114
|
Block.fromBufferReader = function fromBufferReader(br) {
|
|
100
115
|
$.checkArgument(br, 'br is required');
|
|
@@ -102,26 +117,32 @@ Block.fromBufferReader = function fromBufferReader(br) {
|
|
|
102
117
|
return new Block(info);
|
|
103
118
|
};
|
|
104
119
|
|
|
120
|
+
|
|
105
121
|
/**
|
|
106
|
-
*
|
|
107
|
-
* @
|
|
122
|
+
* Creates a Block instance from a buffer.
|
|
123
|
+
* @param {Buffer} buf - The input buffer to create the block from.
|
|
124
|
+
* @returns {Block} The created Block instance.
|
|
108
125
|
*/
|
|
109
126
|
Block.fromBuffer = function fromBuffer(buf) {
|
|
110
127
|
return Block.fromBufferReader(new BufferReader(buf));
|
|
111
128
|
};
|
|
112
129
|
|
|
130
|
+
|
|
113
131
|
/**
|
|
114
|
-
*
|
|
115
|
-
* @
|
|
132
|
+
* Creates a Block instance from a string representation.
|
|
133
|
+
* @param {string} str - The string to parse into a Block.
|
|
134
|
+
* @returns {Block} The parsed Block instance.
|
|
116
135
|
*/
|
|
117
136
|
Block.fromString = function fromString(str) {
|
|
118
137
|
var buf = Buffer.from(str, 'hex');
|
|
119
138
|
return Block.fromBuffer(buf);
|
|
120
139
|
};
|
|
121
140
|
|
|
141
|
+
|
|
122
142
|
/**
|
|
123
|
-
*
|
|
124
|
-
* @
|
|
143
|
+
* Creates a Block instance from raw block data.
|
|
144
|
+
* @param {Object} data - The raw block data to convert.
|
|
145
|
+
* @returns {Block} A new Block instance.
|
|
125
146
|
*/
|
|
126
147
|
Block.fromRawBlock = function fromRawBlock(data) {
|
|
127
148
|
if (!Buffer.isBuffer(data)) {
|
|
@@ -133,8 +154,10 @@ Block.fromRawBlock = function fromRawBlock(data) {
|
|
|
133
154
|
return new Block(info);
|
|
134
155
|
};
|
|
135
156
|
|
|
157
|
+
|
|
136
158
|
/**
|
|
137
|
-
*
|
|
159
|
+
* Converts the Block instance to a plain object (also aliased as toJSON).
|
|
160
|
+
* @returns {Object} The plain object representation of the Block.
|
|
138
161
|
*/
|
|
139
162
|
Block.prototype.toObject = Block.prototype.toJSON = function toObject() {
|
|
140
163
|
var transactions = [];
|
|
@@ -147,14 +170,17 @@ Block.prototype.toObject = Block.prototype.toJSON = function toObject() {
|
|
|
147
170
|
};
|
|
148
171
|
};
|
|
149
172
|
|
|
173
|
+
|
|
150
174
|
/**
|
|
151
|
-
*
|
|
175
|
+
* Converts the block to a buffer representation.
|
|
176
|
+
* @returns {Buffer} The buffer containing the block data.
|
|
152
177
|
*/
|
|
153
178
|
Block.prototype.toBuffer = function toBuffer() {
|
|
154
179
|
return this.toBufferWriter().concat();
|
|
155
180
|
};
|
|
156
181
|
|
|
157
182
|
/**
|
|
183
|
+
* Returns the string representation of the Block instance.
|
|
158
184
|
* @returns {string} - A hex encoded string of the block
|
|
159
185
|
*/
|
|
160
186
|
Block.prototype.toString = function toString() {
|
|
@@ -259,7 +285,17 @@ var idProperty = {
|
|
|
259
285
|
},
|
|
260
286
|
set: _.noop,
|
|
261
287
|
};
|
|
288
|
+
/**
|
|
289
|
+
* Defines the `id` property on the Block prototype using the provided `idProperty` descriptor.
|
|
290
|
+
* @memberof Block.prototype
|
|
291
|
+
* @name id
|
|
292
|
+
*/
|
|
262
293
|
Object.defineProperty(Block.prototype, 'id', idProperty);
|
|
294
|
+
/**
|
|
295
|
+
* Defines a property 'hash' on Block.prototype using idProperty as the descriptor.
|
|
296
|
+
* @memberof Block.prototype
|
|
297
|
+
* @name hash
|
|
298
|
+
*/
|
|
263
299
|
Object.defineProperty(Block.prototype, 'hash', idProperty);
|
|
264
300
|
|
|
265
301
|
/**
|
|
@@ -269,9 +305,28 @@ Block.prototype.inspect = function inspect() {
|
|
|
269
305
|
return '<Block ' + this.id + '>';
|
|
270
306
|
};
|
|
271
307
|
|
|
308
|
+
/**
|
|
309
|
+
* Object containing constant values used by the Block module.
|
|
310
|
+
* @namespace Block.Values
|
|
311
|
+
*/
|
|
272
312
|
Block.Values = {
|
|
273
313
|
START_OF_BLOCK: 8, // Start of block in raw block data
|
|
274
314
|
NULL_HASH: Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'),
|
|
275
315
|
};
|
|
276
316
|
|
|
277
|
-
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Assigns the BlockHeader class to the Block namespace.
|
|
320
|
+
* @memberof Block
|
|
321
|
+
* @name BlockHeader
|
|
322
|
+
*/
|
|
323
|
+
Block.BlockHeader = BlockHeader;
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Assigns the MerkleBlock class to the Block namespace.
|
|
327
|
+
* @memberof Block
|
|
328
|
+
* @name MerkleBlock
|
|
329
|
+
*/
|
|
330
|
+
Block.MerkleBlock = MerkleBlock;
|
|
331
|
+
|
|
332
|
+
export default Block;
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import _ from '../util/_.js';
|
|
4
|
+
import BN from '../crypto/bn.js';
|
|
5
|
+
import BufferReader from '../encoding/bufferreader.js';
|
|
6
|
+
import BufferWriter from '../encoding/bufferwriter.js';
|
|
7
|
+
import Hash from '../crypto/hash.js';
|
|
8
|
+
import $ from '../util/preconditions.js';
|
|
9
|
+
|
|
10
|
+
var GENESIS_BITS = 0x1d00ffff;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Instantiate a BlockHeader from a Buffer, JSON object, or Object with
|
|
14
|
+
* the properties of the BlockHeader
|
|
15
|
+
*
|
|
16
|
+
* @param {*} - A Buffer, JSON string, or Object
|
|
17
|
+
* @returns {BlockHeader} - An instance of block header
|
|
18
|
+
* @constructor
|
|
19
|
+
*/
|
|
20
|
+
function BlockHeader(arg) {
|
|
21
|
+
if (!(this instanceof BlockHeader)) {
|
|
22
|
+
return new BlockHeader(arg);
|
|
23
|
+
}
|
|
24
|
+
var info = BlockHeader._from(arg);
|
|
25
|
+
this.version = info.version;
|
|
26
|
+
this.prevHash = info.prevHash;
|
|
27
|
+
this.merkleRoot = info.merkleRoot;
|
|
28
|
+
this.time = info.time;
|
|
29
|
+
this.timestamp = info.time;
|
|
30
|
+
this.bits = info.bits;
|
|
31
|
+
this.nonce = info.nonce;
|
|
32
|
+
|
|
33
|
+
if (info.hash) {
|
|
34
|
+
$.checkState(
|
|
35
|
+
this.hash === info.hash,
|
|
36
|
+
'Argument object hash property does not match block hash.',
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param {*} - A Buffer, JSON string or Object
|
|
45
|
+
* @returns {Object} - An object representing block header data
|
|
46
|
+
* @throws {TypeError} - If the argument was not recognized
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
BlockHeader._from = function _from(arg) {
|
|
50
|
+
var info = {};
|
|
51
|
+
if (Buffer.isBuffer(arg)) {
|
|
52
|
+
info = BlockHeader._fromBufferReader(BufferReader(arg));
|
|
53
|
+
} else if (_.isObject(arg)) {
|
|
54
|
+
info = BlockHeader._fromObject(arg);
|
|
55
|
+
} else {
|
|
56
|
+
throw new TypeError('Unrecognized argument for BlockHeader');
|
|
57
|
+
}
|
|
58
|
+
return info;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @param {Object} - A JSON string
|
|
63
|
+
* @returns {Object} - An object representing block header data
|
|
64
|
+
* @private
|
|
65
|
+
*/
|
|
66
|
+
BlockHeader._fromObject = function _fromObject(data) {
|
|
67
|
+
$.checkArgument(data, 'data is required');
|
|
68
|
+
var prevHash = data.prevHash;
|
|
69
|
+
var merkleRoot = data.merkleRoot;
|
|
70
|
+
if (_.isString(data.prevHash)) {
|
|
71
|
+
prevHash = Buffer.from(data.prevHash, 'hex').reverse();
|
|
72
|
+
}
|
|
73
|
+
if (_.isString(data.merkleRoot)) {
|
|
74
|
+
merkleRoot = Buffer.from(data.merkleRoot, 'hex').reverse();
|
|
75
|
+
}
|
|
76
|
+
var info = {
|
|
77
|
+
hash: data.hash,
|
|
78
|
+
version: data.version,
|
|
79
|
+
prevHash: prevHash,
|
|
80
|
+
merkleRoot: merkleRoot,
|
|
81
|
+
time: data.time,
|
|
82
|
+
timestamp: data.time,
|
|
83
|
+
bits: data.bits,
|
|
84
|
+
nonce: data.nonce,
|
|
85
|
+
};
|
|
86
|
+
return info;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {Object} - A plain JavaScript object
|
|
91
|
+
* @returns {BlockHeader} - An instance of block header
|
|
92
|
+
*/
|
|
93
|
+
BlockHeader.fromObject = function fromObject(obj) {
|
|
94
|
+
var info = BlockHeader._fromObject(obj);
|
|
95
|
+
return new BlockHeader(info);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @param {Binary} - Raw block binary data or buffer
|
|
100
|
+
* @returns {BlockHeader} - An instance of block header
|
|
101
|
+
*/
|
|
102
|
+
BlockHeader.fromRawBlock = function fromRawBlock(data) {
|
|
103
|
+
if (!Buffer.isBuffer(data)) {
|
|
104
|
+
data = Buffer.from(data, 'binary');
|
|
105
|
+
}
|
|
106
|
+
var br = BufferReader(data);
|
|
107
|
+
br.pos = BlockHeader.Constants.START_OF_HEADER;
|
|
108
|
+
var info = BlockHeader._fromBufferReader(br);
|
|
109
|
+
return new BlockHeader(info);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @param {Buffer} - A buffer of the block header
|
|
114
|
+
* @returns {BlockHeader} - An instance of block header
|
|
115
|
+
*/
|
|
116
|
+
BlockHeader.fromBuffer = function fromBuffer(buf) {
|
|
117
|
+
var info = BlockHeader._fromBufferReader(BufferReader(buf));
|
|
118
|
+
return new BlockHeader(info);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {string} - A hex encoded buffer of the block header
|
|
123
|
+
* @returns {BlockHeader} - An instance of block header
|
|
124
|
+
*/
|
|
125
|
+
BlockHeader.fromString = function fromString(str) {
|
|
126
|
+
var buf = Buffer.from(str, 'hex');
|
|
127
|
+
return BlockHeader.fromBuffer(buf);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {BufferReader} - A BufferReader of the block header
|
|
132
|
+
* @returns {Object} - An object representing block header data
|
|
133
|
+
* @private
|
|
134
|
+
*/
|
|
135
|
+
BlockHeader._fromBufferReader = function _fromBufferReader(br) {
|
|
136
|
+
var info = {};
|
|
137
|
+
info.version = br.readInt32LE();
|
|
138
|
+
info.prevHash = br.read(32);
|
|
139
|
+
info.merkleRoot = br.read(32);
|
|
140
|
+
info.time = br.readUInt32LE();
|
|
141
|
+
info.bits = br.readUInt32LE();
|
|
142
|
+
info.nonce = br.readUInt32LE();
|
|
143
|
+
return info;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @param {BufferReader} - A BufferReader of the block header
|
|
148
|
+
* @returns {BlockHeader} - An instance of block header
|
|
149
|
+
*/
|
|
150
|
+
BlockHeader.fromBufferReader = function fromBufferReader(br) {
|
|
151
|
+
var info = BlockHeader._fromBufferReader(br);
|
|
152
|
+
return new BlockHeader(info);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @returns {Object} - A plain object of the BlockHeader
|
|
157
|
+
*/
|
|
158
|
+
BlockHeader.prototype.toObject = BlockHeader.prototype.toJSON = function toObject() {
|
|
159
|
+
return {
|
|
160
|
+
hash: this.hash,
|
|
161
|
+
version: this.version,
|
|
162
|
+
prevHash: Buffer.from(this.prevHash).reverse().toString('hex'),
|
|
163
|
+
merkleRoot: Buffer.from(this.merkleRoot).reverse().toString('hex'),
|
|
164
|
+
time: this.time,
|
|
165
|
+
bits: this.bits,
|
|
166
|
+
nonce: this.nonce,
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* @returns {Buffer} - A Buffer of the BlockHeader
|
|
172
|
+
*/
|
|
173
|
+
BlockHeader.prototype.toBuffer = function toBuffer() {
|
|
174
|
+
return this.toBufferWriter().concat();
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @returns {string} - A hex encoded string of the BlockHeader
|
|
179
|
+
*/
|
|
180
|
+
BlockHeader.prototype.toString = function toString() {
|
|
181
|
+
return this.toBuffer().toString('hex');
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @param {BufferWriter} - An existing instance BufferWriter
|
|
186
|
+
* @returns {BufferWriter} - An instance of BufferWriter representation of the BlockHeader
|
|
187
|
+
*/
|
|
188
|
+
BlockHeader.prototype.toBufferWriter = function toBufferWriter(bw) {
|
|
189
|
+
if (!bw) {
|
|
190
|
+
bw = new BufferWriter();
|
|
191
|
+
}
|
|
192
|
+
bw.writeInt32LE(this.version);
|
|
193
|
+
bw.write(this.prevHash);
|
|
194
|
+
bw.write(this.merkleRoot);
|
|
195
|
+
bw.writeUInt32LE(this.time);
|
|
196
|
+
bw.writeUInt32LE(this.bits);
|
|
197
|
+
bw.writeUInt32LE(this.nonce);
|
|
198
|
+
return bw;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Returns the target difficulty for this block
|
|
203
|
+
* @param {Number} bits
|
|
204
|
+
* @returns {BN} An instance of BN with the decoded difficulty bits
|
|
205
|
+
*/
|
|
206
|
+
BlockHeader.prototype.getTargetDifficulty = function getTargetDifficulty(bits) {
|
|
207
|
+
bits = bits || this.bits;
|
|
208
|
+
|
|
209
|
+
var target = new BN(bits & 0xffffff);
|
|
210
|
+
var mov = 8 * ((bits >>> 24) - 3);
|
|
211
|
+
while (mov-- > 0) {
|
|
212
|
+
target = target.mul(new BN(2));
|
|
213
|
+
}
|
|
214
|
+
return target;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* @link https://en.bitcoin.it/wiki/Difficulty
|
|
219
|
+
* @return {Number}
|
|
220
|
+
*/
|
|
221
|
+
BlockHeader.prototype.getDifficulty = function getDifficulty() {
|
|
222
|
+
var difficulty1TargetBN = this.getTargetDifficulty(GENESIS_BITS).mul(new BN(Math.pow(10, 8)));
|
|
223
|
+
var currentTargetBN = this.getTargetDifficulty();
|
|
224
|
+
|
|
225
|
+
var difficultyString = difficulty1TargetBN.div(currentTargetBN).toString(10);
|
|
226
|
+
var decimalPos = difficultyString.length - 8;
|
|
227
|
+
difficultyString =
|
|
228
|
+
difficultyString.slice(0, decimalPos) + '.' + difficultyString.slice(decimalPos);
|
|
229
|
+
|
|
230
|
+
return parseFloat(difficultyString);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* @returns {Buffer} - The little endian hash buffer of the header
|
|
235
|
+
* @private
|
|
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
|
+
export default BlockHeader;
|