xpi-ts 0.2.13 → 0.2.15
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/dist/cjs/lib/bitcore/address.js +75 -74
- package/dist/cjs/lib/bitcore/block/block.js +124 -126
- package/dist/cjs/lib/bitcore/block/blockheader.js +142 -208
- package/dist/cjs/lib/bitcore/block/index.js +3 -1
- package/dist/cjs/lib/bitcore/block/merkleblock.js +301 -0
- package/dist/cjs/lib/bitcore/crypto/bn.js +18 -22
- package/dist/cjs/lib/bitcore/crypto/ecdsa.js +50 -49
- package/dist/cjs/lib/bitcore/crypto/hash.js +25 -22
- package/dist/cjs/lib/bitcore/crypto/musig2.js +86 -92
- package/dist/cjs/lib/bitcore/crypto/point.js +42 -35
- package/dist/cjs/lib/bitcore/crypto/random.js +3 -2
- package/dist/cjs/lib/bitcore/crypto/schnorr.js +46 -55
- package/dist/cjs/lib/bitcore/crypto/signature.js +67 -55
- package/dist/cjs/lib/bitcore/crypto/sigtype.js +1 -1
- package/dist/cjs/lib/bitcore/encoding/base58.js +5 -4
- package/dist/cjs/lib/bitcore/encoding/base58check.js +14 -57
- package/dist/cjs/lib/bitcore/encoding/bufferreader.js +17 -17
- package/dist/cjs/lib/bitcore/encoding/bufferwriter.js +22 -26
- package/dist/cjs/lib/bitcore/encoding/varint.js +12 -11
- package/dist/cjs/lib/bitcore/errors.js +1 -0
- package/dist/cjs/lib/bitcore/hdprivatekey.js +74 -66
- package/dist/cjs/lib/bitcore/hdpublickey.js +96 -116
- package/dist/cjs/lib/bitcore/index.js +165 -172
- package/dist/cjs/lib/bitcore/message.js +36 -35
- package/dist/cjs/lib/bitcore/mnemonic/mnemonic.js +4 -3
- package/dist/cjs/lib/bitcore/mnemonic/pbkdf2.js +6 -5
- package/dist/cjs/lib/bitcore/mnemonic/words/french.js +2053 -0
- package/dist/cjs/lib/bitcore/mnemonic/words/index.js +2 -0
- package/dist/cjs/lib/bitcore/musig2/session.js +26 -25
- package/dist/cjs/lib/bitcore/musig2/signer.js +17 -16
- package/dist/cjs/lib/bitcore/networks.js +4 -3
- package/dist/cjs/lib/bitcore/opcode.js +11 -7
- package/dist/cjs/lib/bitcore/privatekey.js +42 -42
- package/dist/cjs/lib/bitcore/publickey.js +61 -57
- package/dist/cjs/lib/bitcore/script/interpreter/condition-stack.js +41 -0
- package/dist/cjs/lib/bitcore/script/interpreter/index.js +25 -0
- package/dist/cjs/lib/bitcore/script/interpreter/interpreter.js +1362 -0
- package/dist/cjs/lib/bitcore/script/interpreter/script-num.js +200 -0
- package/dist/cjs/lib/bitcore/script/interpreter/types.js +96 -0
- package/dist/cjs/lib/bitcore/{taproot.js → script/taproot.js} +107 -95
- package/dist/cjs/lib/bitcore/script.js +163 -104
- package/dist/cjs/lib/bitcore/taproot/musig2.js +51 -42
- package/dist/cjs/lib/bitcore/transaction/index.js +21 -21
- package/dist/cjs/lib/bitcore/transaction/input/multisig.js +10 -0
- package/dist/cjs/lib/bitcore/transaction/input/multisigscripthash.js +10 -0
- package/dist/cjs/lib/bitcore/transaction/input/publickey.js +10 -0
- package/dist/cjs/lib/bitcore/transaction/input/publickeyhash.js +10 -0
- package/dist/cjs/lib/bitcore/transaction/input/taproot.js +20 -0
- package/dist/cjs/lib/bitcore/transaction/input.js +145 -166
- package/dist/cjs/lib/bitcore/transaction/outpoint.js +89 -0
- package/dist/cjs/lib/bitcore/transaction/output.js +13 -8
- package/dist/cjs/lib/bitcore/transaction/sighash.js +96 -96
- package/dist/cjs/lib/bitcore/transaction/signature.js +39 -40
- package/dist/cjs/lib/bitcore/transaction/transaction.js +130 -118
- package/dist/cjs/lib/bitcore/transaction/unspentoutput.js +27 -23
- package/dist/cjs/lib/bitcore/util/bits.js +33 -0
- package/dist/cjs/lib/bitcore/util/buffer.js +29 -18
- package/dist/cjs/lib/bitcore/util/js.js +0 -3
- package/dist/cjs/lib/bitcore/util/merkle.js +53 -0
- package/dist/cjs/lib/bitcore/util/preconditions.js +8 -7
- package/dist/cjs/lib/bitcore/xaddress.js +27 -23
- package/dist/cjs/lib/{bitcore/taproot/nft.js → nft.js} +98 -29
- package/dist/cjs/lib/rank/index.js +28 -27
- package/dist/cjs/lib/rank/script.js +2 -1
- package/dist/cjs/lib/rank/transaction.js +2 -1
- package/dist/cjs/utils/string.js +6 -5
- package/dist/esm/lib/bitcore/address.js +42 -41
- package/dist/esm/lib/bitcore/block/block.js +126 -128
- package/dist/esm/lib/bitcore/block/blockheader.js +141 -207
- package/dist/esm/lib/bitcore/block/index.js +1 -0
- package/dist/esm/lib/bitcore/block/merkleblock.js +295 -0
- package/dist/esm/lib/bitcore/crypto/bn.js +18 -22
- package/dist/esm/lib/bitcore/crypto/ecdsa.js +25 -24
- package/dist/esm/lib/bitcore/crypto/hash.js +25 -22
- package/dist/esm/lib/bitcore/crypto/musig2.js +60 -66
- package/dist/esm/lib/bitcore/crypto/point.js +22 -15
- package/dist/esm/lib/bitcore/crypto/random.js +3 -2
- package/dist/esm/lib/bitcore/crypto/schnorr.js +35 -44
- package/dist/esm/lib/bitcore/crypto/signature.js +61 -49
- package/dist/esm/lib/bitcore/crypto/sigtype.js +1 -1
- package/dist/esm/lib/bitcore/encoding/base58.js +5 -4
- package/dist/esm/lib/bitcore/encoding/base58check.js +11 -57
- package/dist/esm/lib/bitcore/encoding/bufferreader.js +7 -7
- package/dist/esm/lib/bitcore/encoding/bufferwriter.js +18 -21
- package/dist/esm/lib/bitcore/encoding/varint.js +7 -6
- package/dist/esm/lib/bitcore/errors.js +1 -0
- package/dist/esm/lib/bitcore/hdprivatekey.js +47 -39
- package/dist/esm/lib/bitcore/hdpublickey.js +77 -97
- package/dist/esm/lib/bitcore/index.js +42 -45
- package/dist/esm/lib/bitcore/message.js +16 -15
- package/dist/esm/lib/bitcore/mnemonic/mnemonic.js +4 -3
- package/dist/esm/lib/bitcore/mnemonic/pbkdf2.js +5 -4
- package/dist/esm/lib/bitcore/mnemonic/words/french.js +2050 -0
- package/dist/esm/lib/bitcore/mnemonic/words/index.js +2 -0
- package/dist/esm/lib/bitcore/musig2/session.js +17 -16
- package/dist/esm/lib/bitcore/musig2/signer.js +19 -18
- package/dist/esm/lib/bitcore/networks.js +4 -3
- package/dist/esm/lib/bitcore/opcode.js +9 -5
- package/dist/esm/lib/bitcore/privatekey.js +25 -25
- package/dist/esm/lib/bitcore/publickey.js +46 -42
- package/dist/esm/lib/bitcore/script/interpreter/condition-stack.js +37 -0
- package/dist/esm/lib/bitcore/script/interpreter/index.js +4 -0
- package/dist/esm/lib/bitcore/script/interpreter/interpreter.js +1356 -0
- package/dist/esm/lib/bitcore/script/interpreter/script-num.js +195 -0
- package/dist/esm/lib/bitcore/script/interpreter/types.js +93 -0
- package/dist/esm/lib/bitcore/{taproot.js → script/taproot.js} +92 -81
- package/dist/esm/lib/bitcore/script.js +163 -102
- package/dist/esm/lib/bitcore/taproot/musig2.js +36 -27
- package/dist/esm/lib/bitcore/transaction/index.js +6 -6
- package/dist/esm/lib/bitcore/transaction/input/multisig.js +6 -0
- package/dist/esm/lib/bitcore/transaction/input/multisigscripthash.js +6 -0
- package/dist/esm/lib/bitcore/transaction/input/publickey.js +6 -0
- package/dist/esm/lib/bitcore/transaction/input/publickeyhash.js +6 -0
- package/dist/esm/lib/bitcore/transaction/input/taproot.js +16 -0
- package/dist/esm/lib/bitcore/transaction/input.js +55 -76
- package/dist/esm/lib/bitcore/transaction/outpoint.js +85 -0
- package/dist/esm/lib/bitcore/transaction/output.js +13 -8
- package/dist/esm/lib/bitcore/transaction/sighash.js +22 -22
- package/dist/esm/lib/bitcore/transaction/signature.js +16 -17
- package/dist/esm/lib/bitcore/transaction/transaction.js +58 -46
- package/dist/esm/lib/bitcore/transaction/unspentoutput.js +15 -11
- package/dist/esm/lib/bitcore/util/bits.js +27 -0
- package/dist/esm/lib/bitcore/util/buffer.js +20 -9
- package/dist/esm/lib/bitcore/util/js.js +0 -3
- package/dist/esm/lib/bitcore/util/merkle.js +47 -0
- package/dist/esm/lib/bitcore/util/preconditions.js +3 -2
- package/dist/esm/lib/bitcore/xaddress.js +16 -12
- package/dist/esm/lib/{bitcore/taproot/nft.js → nft.js} +97 -28
- package/dist/esm/lib/rank/index.js +15 -14
- package/dist/esm/lib/rank/script.js +1 -0
- package/dist/esm/lib/rank/transaction.js +1 -0
- package/dist/esm/utils/string.js +1 -0
- package/dist/types/lib/bitcore/address.d.ts +4 -3
- package/dist/types/lib/bitcore/address.d.ts.map +1 -1
- package/dist/types/lib/bitcore/block/block.d.ts +11 -19
- package/dist/types/lib/bitcore/block/block.d.ts.map +1 -1
- package/dist/types/lib/bitcore/block/blockheader.d.ts +30 -32
- package/dist/types/lib/bitcore/block/blockheader.d.ts.map +1 -1
- package/dist/types/lib/bitcore/block/index.d.ts +2 -0
- package/dist/types/lib/bitcore/block/index.d.ts.map +1 -1
- package/dist/types/lib/bitcore/block/merkleblock.d.ts +53 -0
- package/dist/types/lib/bitcore/block/merkleblock.d.ts.map +1 -0
- package/dist/types/lib/bitcore/crypto/bn.d.ts +2 -2
- package/dist/types/lib/bitcore/crypto/bn.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/ecdsa.d.ts +5 -4
- package/dist/types/lib/bitcore/crypto/ecdsa.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/hash.d.ts +3 -1
- package/dist/types/lib/bitcore/crypto/hash.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/musig2.d.ts +22 -30
- package/dist/types/lib/bitcore/crypto/musig2.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/point.d.ts +8 -3
- package/dist/types/lib/bitcore/crypto/point.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/random.d.ts +1 -0
- package/dist/types/lib/bitcore/crypto/random.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/schnorr.d.ts +5 -4
- package/dist/types/lib/bitcore/crypto/schnorr.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/signature.d.ts +4 -1
- package/dist/types/lib/bitcore/crypto/signature.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/sigtype.d.ts +1 -0
- package/dist/types/lib/bitcore/crypto/sigtype.d.ts.map +1 -1
- package/dist/types/lib/bitcore/encoding/base58.d.ts +1 -0
- package/dist/types/lib/bitcore/encoding/base58.d.ts.map +1 -1
- package/dist/types/lib/bitcore/encoding/base58check.d.ts +1 -2
- package/dist/types/lib/bitcore/encoding/base58check.d.ts.map +1 -1
- package/dist/types/lib/bitcore/encoding/bufferreader.d.ts +2 -1
- package/dist/types/lib/bitcore/encoding/bufferreader.d.ts.map +1 -1
- package/dist/types/lib/bitcore/encoding/bufferwriter.d.ts +2 -2
- package/dist/types/lib/bitcore/encoding/bufferwriter.d.ts.map +1 -1
- package/dist/types/lib/bitcore/encoding/varint.d.ts +3 -2
- package/dist/types/lib/bitcore/encoding/varint.d.ts.map +1 -1
- package/dist/types/lib/bitcore/errors.d.ts +3 -0
- package/dist/types/lib/bitcore/errors.d.ts.map +1 -1
- package/dist/types/lib/bitcore/hdprivatekey.d.ts +5 -4
- package/dist/types/lib/bitcore/hdprivatekey.d.ts.map +1 -1
- package/dist/types/lib/bitcore/hdpublickey.d.ts +4 -3
- package/dist/types/lib/bitcore/hdpublickey.d.ts.map +1 -1
- package/dist/types/lib/bitcore/index.d.ts +55 -60
- package/dist/types/lib/bitcore/index.d.ts.map +1 -1
- package/dist/types/lib/bitcore/message.d.ts +7 -4
- package/dist/types/lib/bitcore/message.d.ts.map +1 -1
- package/dist/types/lib/bitcore/mnemonic/mnemonic.d.ts +2 -0
- package/dist/types/lib/bitcore/mnemonic/mnemonic.d.ts.map +1 -1
- package/dist/types/lib/bitcore/mnemonic/pbkdf2.d.ts +1 -0
- package/dist/types/lib/bitcore/mnemonic/pbkdf2.d.ts.map +1 -1
- package/dist/types/lib/bitcore/mnemonic/words/french.d.ts +2 -0
- package/dist/types/lib/bitcore/mnemonic/words/french.d.ts.map +1 -0
- package/dist/types/lib/bitcore/mnemonic/words/index.d.ts +1 -0
- package/dist/types/lib/bitcore/mnemonic/words/index.d.ts.map +1 -1
- package/dist/types/lib/bitcore/musig2/session.d.ts +8 -7
- package/dist/types/lib/bitcore/musig2/session.d.ts.map +1 -1
- package/dist/types/lib/bitcore/musig2/signer.d.ts +10 -9
- package/dist/types/lib/bitcore/musig2/signer.d.ts.map +1 -1
- package/dist/types/lib/bitcore/networks.d.ts +1 -0
- package/dist/types/lib/bitcore/networks.d.ts.map +1 -1
- package/dist/types/lib/bitcore/opcode.d.ts +5 -0
- package/dist/types/lib/bitcore/opcode.d.ts.map +1 -1
- package/dist/types/lib/bitcore/privatekey.d.ts +5 -4
- package/dist/types/lib/bitcore/privatekey.d.ts.map +1 -1
- package/dist/types/lib/bitcore/publickey.d.ts +13 -6
- package/dist/types/lib/bitcore/publickey.d.ts.map +1 -1
- package/dist/types/lib/bitcore/{chunk.d.ts → script/chunk.d.ts} +1 -0
- package/dist/types/lib/bitcore/script/chunk.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script/interpreter/condition-stack.d.ts +11 -0
- package/dist/types/lib/bitcore/script/interpreter/condition-stack.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script/interpreter/index.d.ts +5 -0
- package/dist/types/lib/bitcore/script/interpreter/index.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script/interpreter/interpreter.d.ts +21 -0
- package/dist/types/lib/bitcore/script/interpreter/interpreter.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script/interpreter/script-num.d.ts +35 -0
- package/dist/types/lib/bitcore/script/interpreter/script-num.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script/interpreter/types.d.ts +101 -0
- package/dist/types/lib/bitcore/script/interpreter/types.d.ts.map +1 -0
- package/dist/types/lib/bitcore/{taproot.d.ts → script/taproot.d.ts} +20 -7
- package/dist/types/lib/bitcore/script/taproot.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script.d.ts +12 -11
- package/dist/types/lib/bitcore/script.d.ts.map +1 -1
- package/dist/types/lib/bitcore/taproot/musig2.d.ts +17 -16
- package/dist/types/lib/bitcore/taproot/musig2.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/index.d.ts +11 -11
- package/dist/types/lib/bitcore/transaction/index.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/input/multisig.d.ts +5 -0
- package/dist/types/lib/bitcore/transaction/input/multisig.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/input/multisigscripthash.d.ts +5 -0
- package/dist/types/lib/bitcore/transaction/input/multisigscripthash.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/input/publickey.d.ts +5 -0
- package/dist/types/lib/bitcore/transaction/input/publickey.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/input/publickeyhash.d.ts +6 -0
- package/dist/types/lib/bitcore/transaction/input/publickeyhash.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/input/taproot.d.ts +18 -0
- package/dist/types/lib/bitcore/transaction/input/taproot.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/input.d.ts +27 -35
- package/dist/types/lib/bitcore/transaction/input.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/outpoint.d.ts +32 -0
- package/dist/types/lib/bitcore/transaction/outpoint.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/output.d.ts +3 -1
- package/dist/types/lib/bitcore/transaction/output.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/sighash.d.ts +8 -7
- package/dist/types/lib/bitcore/transaction/sighash.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/signature.d.ts +3 -2
- package/dist/types/lib/bitcore/transaction/signature.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/transaction.d.ts +18 -15
- package/dist/types/lib/bitcore/transaction/transaction.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/unspentoutput.d.ts +9 -7
- package/dist/types/lib/bitcore/transaction/unspentoutput.d.ts.map +1 -1
- package/dist/types/lib/bitcore/unit.d.ts.map +1 -1
- package/dist/types/lib/bitcore/util/base32.d.ts.map +1 -1
- package/dist/types/lib/bitcore/util/bits.d.ts +5 -0
- package/dist/types/lib/bitcore/util/bits.d.ts.map +1 -0
- package/dist/types/lib/bitcore/util/buffer.d.ts +10 -2
- package/dist/types/lib/bitcore/util/buffer.d.ts.map +1 -1
- package/dist/types/lib/bitcore/util/js.d.ts +0 -1
- package/dist/types/lib/bitcore/util/js.d.ts.map +1 -1
- package/dist/types/lib/bitcore/util/merkle.d.ts +6 -0
- package/dist/types/lib/bitcore/util/merkle.d.ts.map +1 -0
- package/dist/types/lib/bitcore/util/preconditions.d.ts.map +1 -1
- package/dist/types/lib/bitcore/xaddress.d.ts +1 -0
- package/dist/types/lib/bitcore/xaddress.d.ts.map +1 -1
- package/dist/types/lib/{bitcore/taproot/nft.d.ts → nft.d.ts} +26 -12
- package/dist/types/lib/nft.d.ts.map +1 -0
- package/dist/types/lib/rank/index.d.ts +1 -0
- package/dist/types/lib/rank/index.d.ts.map +1 -1
- package/dist/types/lib/rank/script.d.ts +1 -0
- package/dist/types/lib/rank/script.d.ts.map +1 -1
- package/dist/types/lib/rank/transaction.d.ts +1 -0
- package/dist/types/lib/rank/transaction.d.ts.map +1 -1
- package/dist/types/utils/string.d.ts +1 -0
- package/dist/types/utils/string.d.ts.map +1 -1
- package/package.json +3 -2
- package/dist/cjs/lib/bitcore/script/interpreter.js +0 -1757
- package/dist/esm/lib/bitcore/script/interpreter.js +0 -1753
- package/dist/types/lib/bitcore/chunk.d.ts.map +0 -1
- package/dist/types/lib/bitcore/script/interpreter.d.ts +0 -101
- package/dist/types/lib/bitcore/script/interpreter.d.ts.map +0 -1
- package/dist/types/lib/bitcore/taproot/nft.d.ts.map +0 -1
- package/dist/types/lib/bitcore/taproot.d.ts.map +0 -1
- /package/dist/cjs/lib/bitcore/{chunk.js → script/chunk.js} +0 -0
- /package/dist/esm/lib/bitcore/{chunk.js → script/chunk.js} +0 -0
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { Preconditions } from './util/preconditions
|
|
2
|
-
import { BitcoreError } from './errors
|
|
3
|
-
import { Base58Check } from './encoding/base58check
|
|
4
|
-
import { get as getNetwork, defaultNetwork, } from './networks
|
|
5
|
-
import { Hash } from './crypto/hash
|
|
6
|
-
import { JSUtil } from './util
|
|
7
|
-
import { PublicKey } from './publickey
|
|
8
|
-
import { XAddress } from './xaddress
|
|
9
|
-
import { Script } from './script
|
|
1
|
+
import { Preconditions as $ } from './util/preconditions';
|
|
2
|
+
import { BitcoreError } from './errors';
|
|
3
|
+
import { Base58Check } from './encoding/base58check';
|
|
4
|
+
import { get as getNetwork, defaultNetwork, } from './networks';
|
|
5
|
+
import { Hash } from './crypto/hash';
|
|
6
|
+
import { JSUtil, BufferUtil } from './util';
|
|
7
|
+
import { PublicKey } from './publickey';
|
|
8
|
+
import { XAddress } from './xaddress';
|
|
9
|
+
import { Script } from './script';
|
|
10
|
+
import { Opcode } from './opcode';
|
|
10
11
|
export class Address {
|
|
11
12
|
static PayToPublicKeyHash = 'pubkeyhash';
|
|
12
13
|
static PayToScriptHash = 'scripthash';
|
|
@@ -18,7 +19,7 @@ export class Address {
|
|
|
18
19
|
if (data instanceof Address) {
|
|
19
20
|
return data;
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
+
$.checkArgument(data !== undefined, 'data', 'First argument is required, please include address data.', 'guide/address.html');
|
|
22
23
|
if (network && !getNetwork(network)) {
|
|
23
24
|
throw new TypeError('Second argument must be "livenet", "testnet", or "regtest".');
|
|
24
25
|
}
|
|
@@ -47,18 +48,18 @@ export class Address {
|
|
|
47
48
|
}
|
|
48
49
|
network = networkObj;
|
|
49
50
|
}
|
|
50
|
-
if ((
|
|
51
|
+
if ((BufferUtil.isBuffer(data) || data instanceof Uint8Array) &&
|
|
51
52
|
data.length === 20) {
|
|
52
53
|
return Address._transformHash(data);
|
|
53
54
|
}
|
|
54
|
-
else if ((
|
|
55
|
+
else if ((BufferUtil.isBuffer(data) || data instanceof Uint8Array) &&
|
|
55
56
|
data.length === 21) {
|
|
56
57
|
return Address._transformBuffer(data, network, type);
|
|
57
58
|
}
|
|
58
|
-
else if ((
|
|
59
|
+
else if ((BufferUtil.isBuffer(data) || data instanceof Uint8Array) &&
|
|
59
60
|
data.length === 33) {
|
|
60
61
|
return {
|
|
61
|
-
hashBuffer:
|
|
62
|
+
hashBuffer: BufferUtil.from(data),
|
|
62
63
|
network: typeof network === 'string' ? getNetwork(network) : network,
|
|
63
64
|
type: type || Address.PayToTaproot,
|
|
64
65
|
};
|
|
@@ -112,7 +113,7 @@ export class Address {
|
|
|
112
113
|
throw new TypeError('Address has mismatched network type.');
|
|
113
114
|
}
|
|
114
115
|
return {
|
|
115
|
-
hashBuffer:
|
|
116
|
+
hashBuffer: BufferUtil.from(info.hashBuffer),
|
|
116
117
|
network: info.network,
|
|
117
118
|
type: info.type,
|
|
118
119
|
};
|
|
@@ -139,7 +140,7 @@ export class Address {
|
|
|
139
140
|
if (!version.network || !version.type) {
|
|
140
141
|
throw new TypeError('Address has invalid version.');
|
|
141
142
|
}
|
|
142
|
-
info.hashBuffer = decoded.
|
|
143
|
+
info.hashBuffer = decoded.slice(1);
|
|
143
144
|
info.network = version.network;
|
|
144
145
|
info.type = version.type;
|
|
145
146
|
return info;
|
|
@@ -162,10 +163,10 @@ export class Address {
|
|
|
162
163
|
const decodedType = decodedXAddress.type;
|
|
163
164
|
if (decodedType === 'taproot' || decodedType === Address.PayToTaproot) {
|
|
164
165
|
if (hashBuffer.length === 36 &&
|
|
165
|
-
hashBuffer[0] ===
|
|
166
|
-
hashBuffer[1] ===
|
|
166
|
+
hashBuffer[0] === Opcode.OP_SCRIPTTYPE &&
|
|
167
|
+
hashBuffer[1] === Opcode.OP_1 &&
|
|
167
168
|
hashBuffer[2] === 0x21) {
|
|
168
|
-
hashBuffer = hashBuffer.
|
|
169
|
+
hashBuffer = hashBuffer.slice(3, 36);
|
|
169
170
|
}
|
|
170
171
|
else if (hashBuffer.length === 33) {
|
|
171
172
|
}
|
|
@@ -179,16 +180,16 @@ export class Address {
|
|
|
179
180
|
};
|
|
180
181
|
}
|
|
181
182
|
if (hashBuffer.length === 25 &&
|
|
182
|
-
hashBuffer[0] ===
|
|
183
|
-
hashBuffer[1] ===
|
|
183
|
+
hashBuffer[0] === Opcode.OP_DUP &&
|
|
184
|
+
hashBuffer[1] === Opcode.OP_HASH160 &&
|
|
184
185
|
hashBuffer[2] === 0x14) {
|
|
185
|
-
hashBuffer = hashBuffer.
|
|
186
|
+
hashBuffer = hashBuffer.slice(3, 23);
|
|
186
187
|
}
|
|
187
188
|
else if (hashBuffer.length === 23 &&
|
|
188
|
-
hashBuffer[0] ===
|
|
189
|
+
hashBuffer[0] === Opcode.OP_HASH160 &&
|
|
189
190
|
hashBuffer[1] === 0x14 &&
|
|
190
|
-
hashBuffer[22] ===
|
|
191
|
-
hashBuffer = hashBuffer.
|
|
191
|
+
hashBuffer[22] === Opcode.OP_EQUAL) {
|
|
192
|
+
hashBuffer = hashBuffer.slice(2, 22);
|
|
192
193
|
return {
|
|
193
194
|
hashBuffer: hashBuffer,
|
|
194
195
|
network: decodedNetwork,
|
|
@@ -203,25 +204,25 @@ export class Address {
|
|
|
203
204
|
}
|
|
204
205
|
static _transformHash(hash) {
|
|
205
206
|
const info = {};
|
|
206
|
-
if (!
|
|
207
|
+
if (!BufferUtil.isBuffer(hash)) {
|
|
207
208
|
throw new TypeError('Address supplied is not a buffer.');
|
|
208
209
|
}
|
|
209
210
|
if (hash.length !== 20) {
|
|
210
211
|
throw new TypeError('Address hashbuffers must be exactly 20 bytes.');
|
|
211
212
|
}
|
|
212
|
-
info.hashBuffer =
|
|
213
|
+
info.hashBuffer = BufferUtil.from(hash);
|
|
213
214
|
return info;
|
|
214
215
|
}
|
|
215
216
|
static _transformBuffer(buffer, network, type) {
|
|
216
217
|
const info = {};
|
|
217
|
-
if (!
|
|
218
|
+
if (!BufferUtil.isBuffer(buffer)) {
|
|
218
219
|
throw new TypeError('Address supplied is not a buffer.');
|
|
219
220
|
}
|
|
220
221
|
if (buffer.length !== 21) {
|
|
221
222
|
throw new TypeError('Address buffers must be exactly 21 bytes.');
|
|
222
223
|
}
|
|
223
224
|
const networkObj = getNetwork(network);
|
|
224
|
-
const bufferVersion = Address._classifyFromVersion(
|
|
225
|
+
const bufferVersion = Address._classifyFromVersion(BufferUtil.from(buffer));
|
|
225
226
|
if (network && !networkObj) {
|
|
226
227
|
throw new TypeError('Unknown network');
|
|
227
228
|
}
|
|
@@ -232,7 +233,7 @@ export class Address {
|
|
|
232
233
|
if (!bufferVersion.type || (type && type !== bufferVersion.type)) {
|
|
233
234
|
throw new TypeError('Address has mismatched type.');
|
|
234
235
|
}
|
|
235
|
-
info.hashBuffer =
|
|
236
|
+
info.hashBuffer = BufferUtil.from(buffer).slice(1);
|
|
236
237
|
info.network = bufferVersion.network;
|
|
237
238
|
info.type = bufferVersion.type;
|
|
238
239
|
return info;
|
|
@@ -248,7 +249,7 @@ export class Address {
|
|
|
248
249
|
return info;
|
|
249
250
|
}
|
|
250
251
|
static _transformScript(script, network) {
|
|
251
|
-
|
|
252
|
+
$.checkArgument(script instanceof Script, 'script', 'script must be a Script instance');
|
|
252
253
|
const address = script.getAddressInfo();
|
|
253
254
|
if (!address) {
|
|
254
255
|
throw new BitcoreError.Script.CantDeriveAddress('Cannot derive address from script');
|
|
@@ -263,10 +264,10 @@ export class Address {
|
|
|
263
264
|
};
|
|
264
265
|
}
|
|
265
266
|
static _transformObject(data) {
|
|
266
|
-
|
|
267
|
-
|
|
267
|
+
$.checkArgument(data.hashBuffer !== undefined, 'data', 'Must provide a `hash` or `hashBuffer` property');
|
|
268
|
+
$.checkArgument(data.type !== undefined, 'data', 'Must provide a `type` property');
|
|
268
269
|
return {
|
|
269
|
-
hashBuffer: data.hashBuffer ||
|
|
270
|
+
hashBuffer: data.hashBuffer || BufferUtil.from(data.hashBuffer.toString(), 'hex'),
|
|
270
271
|
network: getNetwork(data.network) || defaultNetwork,
|
|
271
272
|
type: data.type,
|
|
272
273
|
};
|
|
@@ -305,18 +306,18 @@ export class Address {
|
|
|
305
306
|
return new Address(info.hashBuffer, info.network, info.type);
|
|
306
307
|
}
|
|
307
308
|
static fromObject(obj) {
|
|
308
|
-
|
|
309
|
-
const hashBuffer =
|
|
309
|
+
$.checkState(JSUtil.isHexa(obj.hash), 'Unexpected hash property, "' + obj.hash + '", expected to be hex.');
|
|
310
|
+
const hashBuffer = BufferUtil.from(obj.hash, 'hex');
|
|
310
311
|
return new Address(hashBuffer, obj.network, obj.type);
|
|
311
312
|
}
|
|
312
313
|
static fromScript(script, network) {
|
|
313
|
-
|
|
314
|
+
$.checkArgument(script instanceof Script, 'script', 'script must be a Script instance');
|
|
314
315
|
const info = Address._transformScript(script, network);
|
|
315
316
|
return new Address(info.hashBuffer, network, info.type);
|
|
316
317
|
}
|
|
317
318
|
static payingTo(script, network) {
|
|
318
|
-
|
|
319
|
-
|
|
319
|
+
$.checkArgument(script !== null, 'script', 'script is required');
|
|
320
|
+
$.checkArgument(script instanceof Script, 'script', 'script must be instance of Script');
|
|
320
321
|
return Address.fromScriptHash(Hash.sha256ripemd160(script.toBuffer()), network);
|
|
321
322
|
}
|
|
322
323
|
static getValidationError(data, network, type) {
|
|
@@ -344,10 +345,10 @@ export class Address {
|
|
|
344
345
|
return this.hashBuffer;
|
|
345
346
|
}
|
|
346
347
|
toFullBuffer() {
|
|
347
|
-
const version =
|
|
348
|
+
const version = BufferUtil.from([
|
|
348
349
|
this.network[this.type],
|
|
349
350
|
]);
|
|
350
|
-
const buf =
|
|
351
|
+
const buf = BufferUtil.concat([version, this.hashBuffer]);
|
|
351
352
|
return buf;
|
|
352
353
|
}
|
|
353
354
|
toCashBuffer() {
|
|
@@ -1,158 +1,76 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Buffer } from 'buffer/';
|
|
2
|
+
import { Preconditions as $ } from '../util/preconditions.js';
|
|
3
|
+
import { BufferUtil, NULL_HASH } from '../util/buffer.js';
|
|
3
4
|
import { BufferReader } from '../encoding/bufferreader.js';
|
|
4
5
|
import { BufferWriter } from '../encoding/bufferwriter.js';
|
|
5
6
|
import { Hash } from '../crypto/hash.js';
|
|
6
7
|
import { JSUtil } from '../util/js.js';
|
|
7
8
|
import { Transaction } from '../transaction/index.js';
|
|
8
9
|
import { BN } from '../crypto/bn.js';
|
|
10
|
+
import { calcTreeHeight, traverseAndBuild } from '../util/merkle.js';
|
|
9
11
|
import { BlockHeader, } from './blockheader.js';
|
|
12
|
+
import { MerkleBlock } from './merkleblock.js';
|
|
10
13
|
export class Block {
|
|
11
14
|
static MAX_BLOCK_SIZE = 32 * 1024 * 1024;
|
|
12
|
-
static
|
|
13
|
-
static NULL_HASH = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
|
|
14
|
-
static Values = {
|
|
15
|
-
START_OF_BLOCK: 0,
|
|
16
|
-
NULL_HASH: Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'),
|
|
17
|
-
};
|
|
15
|
+
static NULL_HASH = NULL_HASH;
|
|
18
16
|
header;
|
|
19
|
-
metadata;
|
|
20
17
|
transactions;
|
|
21
18
|
_id;
|
|
22
19
|
constructor(serialized) {
|
|
23
|
-
if (!(this instanceof Block)) {
|
|
24
|
-
return new Block(serialized);
|
|
25
|
-
}
|
|
26
20
|
if (serialized instanceof Block) {
|
|
27
21
|
return Block.shallowCopy(serialized);
|
|
28
22
|
}
|
|
29
23
|
else if (typeof serialized === 'string' && JSUtil.isHexa(serialized)) {
|
|
30
|
-
this.
|
|
31
|
-
}
|
|
32
|
-
else if (Buffer.isBuffer(serialized)) {
|
|
33
|
-
this.fromBuffer(serialized);
|
|
24
|
+
this._fromString(serialized);
|
|
34
25
|
}
|
|
35
|
-
else if (serialized
|
|
36
|
-
|
|
26
|
+
else if (Buffer.isBuffer(serialized) ||
|
|
27
|
+
(serialized && typeof serialized === 'object')) {
|
|
28
|
+
const info = Block._from(serialized);
|
|
29
|
+
this.header = info.header;
|
|
30
|
+
this.transactions = info.transactions;
|
|
37
31
|
}
|
|
38
32
|
else {
|
|
39
|
-
this.
|
|
33
|
+
this._initEmpty();
|
|
40
34
|
}
|
|
41
35
|
}
|
|
42
36
|
static shallowCopy(block) {
|
|
43
|
-
|
|
44
|
-
return copy;
|
|
45
|
-
}
|
|
46
|
-
static _from(arg) {
|
|
47
|
-
let info = {};
|
|
48
|
-
if (Buffer.isBuffer(arg)) {
|
|
49
|
-
info = Block._fromBufferReader(new BufferReader(arg));
|
|
50
|
-
}
|
|
51
|
-
else if (typeof arg === 'object' && arg !== null) {
|
|
52
|
-
info = Block._fromObject(arg);
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
throw new TypeError('Unrecognized argument for Block');
|
|
56
|
-
}
|
|
57
|
-
return info;
|
|
37
|
+
return new Block(block.toBuffer());
|
|
58
38
|
}
|
|
59
39
|
static fromObject(obj) {
|
|
60
40
|
const info = Block._fromObject(obj);
|
|
61
41
|
return new Block(info);
|
|
62
42
|
}
|
|
63
43
|
static fromBufferReader(br) {
|
|
64
|
-
|
|
65
|
-
const info = Block.
|
|
44
|
+
$.checkArgument(br instanceof BufferReader, 'br is required');
|
|
45
|
+
const info = Block._readFromBuffer(br);
|
|
66
46
|
return new Block(info);
|
|
67
47
|
}
|
|
68
48
|
static fromBuffer(buf) {
|
|
69
49
|
return Block.fromBufferReader(new BufferReader(buf));
|
|
70
50
|
}
|
|
71
51
|
static fromString(str) {
|
|
72
|
-
|
|
73
|
-
return Block.fromBuffer(buf);
|
|
52
|
+
return Block.fromBuffer(Buffer.from(str, 'hex'));
|
|
74
53
|
}
|
|
75
54
|
static fromRawBlock(data) {
|
|
76
55
|
if (!BufferUtil.isBuffer(data)) {
|
|
77
56
|
data = Buffer.from(data, 'binary');
|
|
78
57
|
}
|
|
79
58
|
const br = new BufferReader(data);
|
|
80
|
-
|
|
81
|
-
const info = Block._fromBufferReader(br);
|
|
59
|
+
const info = Block._readFromBuffer(br);
|
|
82
60
|
return new Block(info);
|
|
83
61
|
}
|
|
84
|
-
static _fromObject(data) {
|
|
85
|
-
const transactions = [];
|
|
86
|
-
if (data.transactions) {
|
|
87
|
-
data.transactions.forEach(tx => {
|
|
88
|
-
if (tx instanceof Transaction) {
|
|
89
|
-
transactions.push(tx);
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
transactions.push(new Transaction(tx));
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
return {
|
|
97
|
-
header: data.header
|
|
98
|
-
? data.header instanceof BlockHeader
|
|
99
|
-
? data.header
|
|
100
|
-
: new BlockHeader(data.header)
|
|
101
|
-
: new BlockHeader(),
|
|
102
|
-
metadata: data.metadata || 0x00,
|
|
103
|
-
transactions: transactions,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
static _fromBufferReader(br) {
|
|
107
|
-
Preconditions.checkState(!br.finished(), 'No block data received');
|
|
108
|
-
const header = BlockHeader.fromBufferReader(br);
|
|
109
|
-
const metadata = br.readUInt8();
|
|
110
|
-
const transactionCount = br.readVarintNum();
|
|
111
|
-
const transactions = [];
|
|
112
|
-
for (let i = 0; i < transactionCount; i++) {
|
|
113
|
-
const tx = new Transaction();
|
|
114
|
-
transactions.push(tx.fromBufferReader(br));
|
|
115
|
-
}
|
|
116
|
-
return {
|
|
117
|
-
header,
|
|
118
|
-
metadata,
|
|
119
|
-
transactions,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
_newBlock() {
|
|
123
|
-
this.header = new BlockHeader();
|
|
124
|
-
this.metadata = 0x00;
|
|
125
|
-
this.transactions = [];
|
|
126
|
-
}
|
|
127
|
-
fromBuffer(buf) {
|
|
128
|
-
const info = Block._fromBufferReader(new BufferReader(buf));
|
|
129
|
-
this.header = info.header;
|
|
130
|
-
this.metadata = info.metadata;
|
|
131
|
-
this.transactions = info.transactions;
|
|
132
|
-
return this;
|
|
133
|
-
}
|
|
134
|
-
fromString(str) {
|
|
135
|
-
const buf = Buffer.from(str, 'hex');
|
|
136
|
-
return this.fromBuffer(buf);
|
|
137
|
-
}
|
|
138
|
-
fromObject(obj) {
|
|
139
|
-
const info = Block._fromObject(obj);
|
|
140
|
-
this.header = info.header;
|
|
141
|
-
this.metadata = info.metadata;
|
|
142
|
-
this.transactions = info.transactions;
|
|
143
|
-
return this;
|
|
144
|
-
}
|
|
145
62
|
toObject() {
|
|
146
63
|
const transactions = this.transactions.map(tx => tx.toObject());
|
|
147
64
|
return {
|
|
148
65
|
id: this.id,
|
|
149
66
|
hash: this.hash,
|
|
150
67
|
header: this.header.toObject(),
|
|
151
|
-
|
|
152
|
-
transactions: transactions,
|
|
68
|
+
transactions,
|
|
153
69
|
};
|
|
154
70
|
}
|
|
155
|
-
toJSON
|
|
71
|
+
toJSON() {
|
|
72
|
+
return this.toObject();
|
|
73
|
+
}
|
|
156
74
|
toBuffer() {
|
|
157
75
|
return this.toBufferWriter().concat();
|
|
158
76
|
}
|
|
@@ -164,25 +82,21 @@ export class Block {
|
|
|
164
82
|
bw = new BufferWriter();
|
|
165
83
|
}
|
|
166
84
|
bw.write(this.header.toBuffer());
|
|
167
|
-
bw.
|
|
85
|
+
bw.writeVarintNum(0);
|
|
168
86
|
bw.writeVarintNum(this.transactions.length);
|
|
169
|
-
for (
|
|
170
|
-
|
|
87
|
+
for (const tx of this.transactions) {
|
|
88
|
+
tx.toBufferWriter(bw);
|
|
171
89
|
}
|
|
172
90
|
return bw;
|
|
173
91
|
}
|
|
174
92
|
getTransactionHashes() {
|
|
175
|
-
const hashes = [];
|
|
176
93
|
if (this.transactions.length === 0) {
|
|
177
|
-
return [Block.
|
|
94
|
+
return [Block.NULL_HASH];
|
|
178
95
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
const buf = Buffer.concat([hash, txid]);
|
|
184
|
-
const resultHash = Hash.sha256sha256(buf);
|
|
185
|
-
hashes.push(resultHash);
|
|
96
|
+
const hashes = [];
|
|
97
|
+
for (const tx of this.transactions) {
|
|
98
|
+
const combined = Buffer.concat([tx.hashBuffer, tx.txidBuffer]);
|
|
99
|
+
hashes.push(Hash.sha256sha256(combined));
|
|
186
100
|
}
|
|
187
101
|
return hashes;
|
|
188
102
|
}
|
|
@@ -191,12 +105,12 @@ export class Block {
|
|
|
191
105
|
let j = 0;
|
|
192
106
|
for (let size = tree.length; size > 1; size = Math.floor(size / 2)) {
|
|
193
107
|
if (size % 2 === 1) {
|
|
194
|
-
tree.push(Block.
|
|
108
|
+
tree.push(Block.NULL_HASH);
|
|
195
109
|
size += 1;
|
|
196
110
|
}
|
|
197
111
|
for (let i = 0; i < size; i += 2) {
|
|
198
|
-
const
|
|
199
|
-
tree.push(Hash.sha256sha256(
|
|
112
|
+
const combined = Buffer.concat([tree[j + i], tree[j + i + 1]]);
|
|
113
|
+
tree.push(Hash.sha256sha256(combined));
|
|
200
114
|
}
|
|
201
115
|
j += size;
|
|
202
116
|
}
|
|
@@ -207,16 +121,45 @@ export class Block {
|
|
|
207
121
|
return tree[tree.length - 1];
|
|
208
122
|
}
|
|
209
123
|
validMerkleRoot() {
|
|
210
|
-
const
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
return false;
|
|
214
|
-
}
|
|
215
|
-
return true;
|
|
124
|
+
const headerRoot = BN.fromBuffer(this.header.merkleRoot);
|
|
125
|
+
const computedRoot = BN.fromBuffer(this.getMerkleRoot());
|
|
126
|
+
return headerRoot.eq(computedRoot);
|
|
216
127
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
128
|
+
toMerkleBlock(txids) {
|
|
129
|
+
if (!Array.isArray(txids) || txids.length === 0) {
|
|
130
|
+
throw new TypeError('txids must be a non-empty array of transaction IDs');
|
|
131
|
+
}
|
|
132
|
+
for (const txid of txids) {
|
|
133
|
+
if (!/^[0-9a-fA-F]{64}$/.test(txid)) {
|
|
134
|
+
throw new TypeError(`Invalid transaction ID format: ${txid}`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (this.transactions.length === 0) {
|
|
138
|
+
throw new Error('Cannot create MerkleBlock from block with no transactions');
|
|
139
|
+
}
|
|
140
|
+
const vHashes = [];
|
|
141
|
+
const vMatch = [];
|
|
142
|
+
for (const tx of this.transactions) {
|
|
143
|
+
const txhash = tx.hash;
|
|
144
|
+
const txid = tx.txid;
|
|
145
|
+
const txidLower = txid.toLowerCase();
|
|
146
|
+
vMatch.push(false);
|
|
147
|
+
vHashes.push(txhash);
|
|
148
|
+
const isMatch = txids.some(id => id.toLowerCase() === txidLower);
|
|
149
|
+
vMatch.push(isMatch);
|
|
150
|
+
vHashes.push(txid);
|
|
151
|
+
}
|
|
152
|
+
const nLeaves = vHashes.length;
|
|
153
|
+
const height = calcTreeHeight(nLeaves);
|
|
154
|
+
const vBits = [];
|
|
155
|
+
const vHash = [];
|
|
156
|
+
traverseAndBuild(height, 0, nLeaves, vHashes, vMatch, vBits, vHash, Block.NULL_HASH);
|
|
157
|
+
return new MerkleBlock({
|
|
158
|
+
header: this.header,
|
|
159
|
+
numTransactions: this.transactions.length,
|
|
160
|
+
hashes: vHash,
|
|
161
|
+
flags: vBits.map(b => (b ? 1 : 0)),
|
|
162
|
+
});
|
|
220
163
|
}
|
|
221
164
|
get hash() {
|
|
222
165
|
if (!this._id) {
|
|
@@ -230,4 +173,59 @@ export class Block {
|
|
|
230
173
|
inspect() {
|
|
231
174
|
return '<Block ' + this.id + '>';
|
|
232
175
|
}
|
|
176
|
+
static _from(arg) {
|
|
177
|
+
if (Buffer.isBuffer(arg)) {
|
|
178
|
+
return Block._readFromBuffer(new BufferReader(arg));
|
|
179
|
+
}
|
|
180
|
+
else if (typeof arg === 'object' && arg !== null) {
|
|
181
|
+
return Block._fromObject(arg);
|
|
182
|
+
}
|
|
183
|
+
throw new TypeError('Unrecognized argument for Block');
|
|
184
|
+
}
|
|
185
|
+
static _fromObject(data) {
|
|
186
|
+
const transactions = [];
|
|
187
|
+
if (data.transactions) {
|
|
188
|
+
for (const tx of data.transactions) {
|
|
189
|
+
transactions.push(tx instanceof Transaction ? tx : new Transaction(tx));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
let header;
|
|
193
|
+
if (data.header instanceof BlockHeader) {
|
|
194
|
+
header = data.header;
|
|
195
|
+
}
|
|
196
|
+
else if (data.header) {
|
|
197
|
+
header = new BlockHeader(data.header);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
header = new BlockHeader();
|
|
201
|
+
}
|
|
202
|
+
return { header, transactions };
|
|
203
|
+
}
|
|
204
|
+
static _readFromBuffer(br) {
|
|
205
|
+
$.checkState(!br.finished(), 'No block data received');
|
|
206
|
+
const header = BlockHeader.fromBufferReader(br);
|
|
207
|
+
const metadataCount = br.readVarintNum();
|
|
208
|
+
for (let i = 0; i < metadataCount; i++) {
|
|
209
|
+
br.readUInt32LE();
|
|
210
|
+
const dataLength = br.readVarintNum();
|
|
211
|
+
br.read(dataLength);
|
|
212
|
+
}
|
|
213
|
+
const transactionCount = br.readVarintNum();
|
|
214
|
+
const transactions = [];
|
|
215
|
+
for (let i = 0; i < transactionCount; i++) {
|
|
216
|
+
const tx = new Transaction();
|
|
217
|
+
transactions.push(tx.fromBufferReader(br));
|
|
218
|
+
}
|
|
219
|
+
return { header, transactions };
|
|
220
|
+
}
|
|
221
|
+
_initEmpty() {
|
|
222
|
+
this.header = new BlockHeader();
|
|
223
|
+
this.transactions = [];
|
|
224
|
+
}
|
|
225
|
+
_fromString(str) {
|
|
226
|
+
const buf = Buffer.from(str, 'hex');
|
|
227
|
+
const info = Block._readFromBuffer(new BufferReader(buf));
|
|
228
|
+
this.header = info.header;
|
|
229
|
+
this.transactions = info.transactions;
|
|
230
|
+
}
|
|
233
231
|
}
|