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
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { Buffer } from 'buffer/';
|
|
2
|
+
import { BlockHeader, } from './blockheader.js';
|
|
3
|
+
import { BufferReader } from '../encoding/bufferreader.js';
|
|
4
|
+
import { BufferWriter } from '../encoding/bufferwriter.js';
|
|
5
|
+
import { Hash } from '../crypto/hash.js';
|
|
6
|
+
import { BufferUtil, NULL_HASH } from '../util/buffer.js';
|
|
7
|
+
import { Transaction } from '../transaction/index.js';
|
|
8
|
+
import { BitcoreError } from '../errors.js';
|
|
9
|
+
import { Preconditions as $ } from '../util/preconditions.js';
|
|
10
|
+
import { bitsToBytes, bytesToBits, extractBit } from '../util/bits.js';
|
|
11
|
+
import { calcTreeWidth } from '../util/merkle.js';
|
|
12
|
+
export class InvalidMerkleBlockError extends Error {
|
|
13
|
+
code;
|
|
14
|
+
context;
|
|
15
|
+
constructor(message, context = {}) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = 'InvalidMerkleBlockError';
|
|
18
|
+
this.code = 'INVALID_MERKLE_BLOCK';
|
|
19
|
+
this.context = context;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export class MerkleBlockValidationError extends Error {
|
|
23
|
+
code;
|
|
24
|
+
context;
|
|
25
|
+
constructor(message, context = {}) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.name = 'MerkleBlockValidationError';
|
|
28
|
+
this.code = 'MERKLE_BLOCK_VALIDATION_ERROR';
|
|
29
|
+
this.context = context;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export class MerkleBlock {
|
|
33
|
+
static NULL_HASH = NULL_HASH;
|
|
34
|
+
header;
|
|
35
|
+
numTransactions;
|
|
36
|
+
hashes;
|
|
37
|
+
flags;
|
|
38
|
+
constructor(arg) {
|
|
39
|
+
if (arg instanceof MerkleBlock) {
|
|
40
|
+
return arg;
|
|
41
|
+
}
|
|
42
|
+
if (!arg) {
|
|
43
|
+
throw new InvalidMerkleBlockError('Argument is required for MerkleBlock', {
|
|
44
|
+
received: arg,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const info = MerkleBlock._from(arg);
|
|
48
|
+
if (info.header instanceof BlockHeader) {
|
|
49
|
+
this.header = info.header;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
this.header = new BlockHeader(info.header);
|
|
53
|
+
}
|
|
54
|
+
this.numTransactions = info.numTransactions;
|
|
55
|
+
this.hashes = info.hashes;
|
|
56
|
+
this.flags = info.flags;
|
|
57
|
+
}
|
|
58
|
+
static fromObject(obj) {
|
|
59
|
+
if (!obj.header) {
|
|
60
|
+
throw new MerkleBlockValidationError('Block header is required', {
|
|
61
|
+
header: obj.header,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return new MerkleBlock({
|
|
65
|
+
header: BlockHeader.fromObject(obj.header),
|
|
66
|
+
numTransactions: obj.numTransactions,
|
|
67
|
+
hashes: obj.hashes,
|
|
68
|
+
flags: obj.flags,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
static fromBuffer(buf) {
|
|
72
|
+
if (!BufferUtil.isBuffer(buf) || buf.length === 0) {
|
|
73
|
+
throw new InvalidMerkleBlockError('Invalid buffer provided', {
|
|
74
|
+
isBuffer: BufferUtil.isBuffer(buf),
|
|
75
|
+
length: buf?.length,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return MerkleBlock.fromBufferReader(new BufferReader(buf));
|
|
79
|
+
}
|
|
80
|
+
static fromBufferReader(br) {
|
|
81
|
+
const info = MerkleBlock._readFromBuffer(br);
|
|
82
|
+
return new MerkleBlock(info);
|
|
83
|
+
}
|
|
84
|
+
static fromString(str) {
|
|
85
|
+
$.checkArgument(typeof str === 'string' && str.length > 0, 'str', 'Invalid hex string provided');
|
|
86
|
+
return MerkleBlock.fromBuffer(Buffer.from(str, 'hex'));
|
|
87
|
+
}
|
|
88
|
+
toBuffer() {
|
|
89
|
+
return this.toBufferWriter().concat();
|
|
90
|
+
}
|
|
91
|
+
toBufferWriter(bw) {
|
|
92
|
+
if (!bw) {
|
|
93
|
+
bw = new BufferWriter();
|
|
94
|
+
}
|
|
95
|
+
bw.write(this.header.toBuffer());
|
|
96
|
+
bw.writeUInt32LE(this.numTransactions);
|
|
97
|
+
bw.writeVarintNum(this.hashes.length);
|
|
98
|
+
for (const hash of this.hashes) {
|
|
99
|
+
bw.write(Buffer.from(hash, 'hex'));
|
|
100
|
+
}
|
|
101
|
+
const flagBytes = bitsToBytes(this.flags.map(f => f !== 0));
|
|
102
|
+
bw.writeVarintNum(flagBytes.length);
|
|
103
|
+
for (const byte of flagBytes) {
|
|
104
|
+
bw.writeUInt8(byte);
|
|
105
|
+
}
|
|
106
|
+
return bw;
|
|
107
|
+
}
|
|
108
|
+
toObject() {
|
|
109
|
+
return {
|
|
110
|
+
header: this.header.toObject(),
|
|
111
|
+
numTransactions: this.numTransactions,
|
|
112
|
+
hashes: this.hashes,
|
|
113
|
+
flags: this.flags,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
toJSON() {
|
|
117
|
+
return this.toObject();
|
|
118
|
+
}
|
|
119
|
+
validMerkleTree() {
|
|
120
|
+
if (!Array.isArray(this.flags) || !Array.isArray(this.hashes)) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
if (this.hashes.length > this.numTransactions) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
if (this.flags.length * 8 < this.hashes.length) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
const height = this._calcTreeHeight();
|
|
130
|
+
const state = {
|
|
131
|
+
hashesUsed: 0,
|
|
132
|
+
flagBitsUsed: 0,
|
|
133
|
+
txs: [],
|
|
134
|
+
};
|
|
135
|
+
const root = this._traverseAndExtract(height, 0, state);
|
|
136
|
+
if (state.hashesUsed !== this.hashes.length) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
return BufferUtil.equals(root, this.header.merkleRoot);
|
|
140
|
+
}
|
|
141
|
+
filteredTxsHash() {
|
|
142
|
+
if (!Array.isArray(this.flags) || !Array.isArray(this.hashes)) {
|
|
143
|
+
throw new BitcoreError.MerkleBlock.InvalidMerkleTree();
|
|
144
|
+
}
|
|
145
|
+
if (this.hashes.length > this.numTransactions) {
|
|
146
|
+
throw new BitcoreError.MerkleBlock.InvalidMerkleTree();
|
|
147
|
+
}
|
|
148
|
+
if (this.flags.length * 8 < this.hashes.length) {
|
|
149
|
+
throw new BitcoreError.MerkleBlock.InvalidMerkleTree();
|
|
150
|
+
}
|
|
151
|
+
if (this.hashes.length === 1) {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
const height = this._calcTreeHeight();
|
|
155
|
+
const state = {
|
|
156
|
+
hashesUsed: 0,
|
|
157
|
+
flagBitsUsed: 0,
|
|
158
|
+
txs: [],
|
|
159
|
+
};
|
|
160
|
+
this._traverseAndExtract(height, 0, state);
|
|
161
|
+
if (state.hashesUsed !== this.hashes.length) {
|
|
162
|
+
throw new BitcoreError.MerkleBlock.InvalidMerkleTree();
|
|
163
|
+
}
|
|
164
|
+
return state.txs;
|
|
165
|
+
}
|
|
166
|
+
hasTransaction(tx) {
|
|
167
|
+
if (!tx) {
|
|
168
|
+
throw new BitcoreError('Transaction cannot be undefined');
|
|
169
|
+
}
|
|
170
|
+
if (!(tx instanceof Transaction) && typeof tx !== 'string') {
|
|
171
|
+
throw new BitcoreError('Invalid transaction: must be string or Transaction');
|
|
172
|
+
}
|
|
173
|
+
if (typeof tx === 'string' && !/^[0-9a-fA-F]{64}$/.test(tx)) {
|
|
174
|
+
throw new BitcoreError('Invalid transaction ID format: must be 64-character hex string');
|
|
175
|
+
}
|
|
176
|
+
let searchTxid;
|
|
177
|
+
let searchHash;
|
|
178
|
+
if (tx instanceof Transaction) {
|
|
179
|
+
searchTxid = BufferUtil.reverse(Buffer.from(tx.txid, 'hex')).toString('hex');
|
|
180
|
+
searchHash = BufferUtil.reverse(Buffer.from(tx.hash, 'hex')).toString('hex');
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
searchTxid = BufferUtil.reverse(Buffer.from(tx, 'hex')).toString('hex');
|
|
184
|
+
searchHash = searchTxid;
|
|
185
|
+
}
|
|
186
|
+
const state = {
|
|
187
|
+
hashesUsed: 0,
|
|
188
|
+
flagBitsUsed: 0,
|
|
189
|
+
txs: [],
|
|
190
|
+
};
|
|
191
|
+
const height = this._calcTreeHeight();
|
|
192
|
+
this._traverseAndExtract(height, 0, state);
|
|
193
|
+
return state.txs.includes(searchTxid) || state.txs.includes(searchHash);
|
|
194
|
+
}
|
|
195
|
+
static _from(arg) {
|
|
196
|
+
if (BufferUtil.isBuffer(arg)) {
|
|
197
|
+
return MerkleBlock._readFromBuffer(new BufferReader(arg));
|
|
198
|
+
}
|
|
199
|
+
else if (typeof arg === 'object' && arg !== null) {
|
|
200
|
+
return MerkleBlock._validateData(arg);
|
|
201
|
+
}
|
|
202
|
+
throw new InvalidMerkleBlockError('Unrecognized argument for MerkleBlock', {
|
|
203
|
+
type: typeof arg,
|
|
204
|
+
arg,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
static _validateData(data) {
|
|
208
|
+
if (!data.header) {
|
|
209
|
+
throw new MerkleBlockValidationError('Block header is required', {
|
|
210
|
+
header: data.header,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
if (typeof data.numTransactions !== 'number' || data.numTransactions < 0) {
|
|
214
|
+
throw new MerkleBlockValidationError('Number of transactions must be a non-negative number', {
|
|
215
|
+
numTransactions: data.numTransactions,
|
|
216
|
+
type: typeof data.numTransactions,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
if (!Array.isArray(data.hashes)) {
|
|
220
|
+
throw new MerkleBlockValidationError('Hashes must be an array', {
|
|
221
|
+
hashes: data.hashes,
|
|
222
|
+
type: typeof data.hashes,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (!Array.isArray(data.flags)) {
|
|
226
|
+
throw new MerkleBlockValidationError('Flags must be an array', {
|
|
227
|
+
flags: data.flags,
|
|
228
|
+
type: typeof data.flags,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
const header = data.header instanceof BlockHeader
|
|
232
|
+
? data.header
|
|
233
|
+
: new BlockHeader(data.header);
|
|
234
|
+
return {
|
|
235
|
+
header,
|
|
236
|
+
numTransactions: data.numTransactions,
|
|
237
|
+
hashes: data.hashes,
|
|
238
|
+
flags: data.flags,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
static _readFromBuffer(br) {
|
|
242
|
+
if (br.finished()) {
|
|
243
|
+
throw new InvalidMerkleBlockError('No merkleblock data received');
|
|
244
|
+
}
|
|
245
|
+
const header = BlockHeader.fromBufferReader(br);
|
|
246
|
+
const numTransactions = br.readUInt32LE();
|
|
247
|
+
const numHashes = br.readVarintNum();
|
|
248
|
+
const hashes = [];
|
|
249
|
+
for (let i = 0; i < numHashes; i++) {
|
|
250
|
+
hashes.push(br.read(32).toString('hex'));
|
|
251
|
+
}
|
|
252
|
+
const numFlags = br.readVarintNum();
|
|
253
|
+
const flagBytes = [];
|
|
254
|
+
for (let i = 0; i < numFlags; i++) {
|
|
255
|
+
flagBytes.push(br.readUInt8());
|
|
256
|
+
}
|
|
257
|
+
const flags = bytesToBits(flagBytes).map(b => (b ? 1 : 0));
|
|
258
|
+
return { header, numTransactions, hashes, flags };
|
|
259
|
+
}
|
|
260
|
+
_calcTreeWidth(height) {
|
|
261
|
+
return calcTreeWidth(this.numTransactions * 2, height);
|
|
262
|
+
}
|
|
263
|
+
_calcTreeHeight() {
|
|
264
|
+
let height = 0;
|
|
265
|
+
while (this._calcTreeWidth(height) > 1) {
|
|
266
|
+
height++;
|
|
267
|
+
}
|
|
268
|
+
return height;
|
|
269
|
+
}
|
|
270
|
+
_traverseAndExtract(depth, pos, state) {
|
|
271
|
+
if (state.flagBitsUsed > this.flags.length * 8) {
|
|
272
|
+
return MerkleBlock.NULL_HASH;
|
|
273
|
+
}
|
|
274
|
+
const isParentOfMatch = extractBit(this.flags, state.flagBitsUsed++);
|
|
275
|
+
if (depth === 0 || !isParentOfMatch) {
|
|
276
|
+
if (state.hashesUsed >= this.hashes.length) {
|
|
277
|
+
return MerkleBlock.NULL_HASH;
|
|
278
|
+
}
|
|
279
|
+
const hash = this.hashes[state.hashesUsed++];
|
|
280
|
+
if (depth === 0 && isParentOfMatch) {
|
|
281
|
+
state.txs.push(hash);
|
|
282
|
+
}
|
|
283
|
+
return Buffer.from(hash, 'hex');
|
|
284
|
+
}
|
|
285
|
+
const left = this._traverseAndExtract(depth - 1, pos * 2, state);
|
|
286
|
+
let right;
|
|
287
|
+
if (pos * 2 + 1 < this._calcTreeWidth(depth - 1)) {
|
|
288
|
+
right = this._traverseAndExtract(depth - 1, pos * 2 + 1, state);
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
right = MerkleBlock.NULL_HASH;
|
|
292
|
+
}
|
|
293
|
+
return Hash.sha256sha256(Buffer.concat([left, right]));
|
|
294
|
+
}
|
|
295
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import BN from 'bn.js';
|
|
2
|
+
import { BufferUtil } from '../util';
|
|
2
3
|
class BNWrapper {
|
|
3
4
|
_bn;
|
|
4
5
|
static Zero = new BNWrapper(0);
|
|
@@ -8,7 +9,7 @@ class BNWrapper {
|
|
|
8
9
|
if (n instanceof BN) {
|
|
9
10
|
this._bn = n.clone();
|
|
10
11
|
}
|
|
11
|
-
else if (
|
|
12
|
+
else if (BufferUtil.isBuffer(n)) {
|
|
12
13
|
if (base === 'le' || base === 'be') {
|
|
13
14
|
this._bn = new BN(n, undefined, base);
|
|
14
15
|
}
|
|
@@ -27,7 +28,7 @@ class BNWrapper {
|
|
|
27
28
|
static fromBuffer(buf, opts) {
|
|
28
29
|
let buffer = buf;
|
|
29
30
|
if (opts?.size) {
|
|
30
|
-
buffer =
|
|
31
|
+
buffer = BufferUtil.alloc(opts.size);
|
|
31
32
|
buf.copy(buffer, opts.size - buf.length);
|
|
32
33
|
}
|
|
33
34
|
if (opts?.endian === 'little') {
|
|
@@ -37,7 +38,7 @@ class BNWrapper {
|
|
|
37
38
|
}
|
|
38
39
|
static fromSM(buf, opts) {
|
|
39
40
|
if (buf.length === 0) {
|
|
40
|
-
return BNWrapper.fromBuffer(
|
|
41
|
+
return BNWrapper.fromBuffer(BufferUtil.from([0]));
|
|
41
42
|
}
|
|
42
43
|
let endian = 'big';
|
|
43
44
|
if (opts) {
|
|
@@ -73,17 +74,17 @@ class BNWrapper {
|
|
|
73
74
|
return BNWrapper.fromSM(buf, { endian: 'little' });
|
|
74
75
|
}
|
|
75
76
|
static reversebuf(buf) {
|
|
76
|
-
const buf2 =
|
|
77
|
+
const buf2 = BufferUtil.alloc(buf.length);
|
|
77
78
|
for (let i = 0; i < buf.length; i++) {
|
|
78
79
|
buf2[i] = buf[buf.length - 1 - i];
|
|
79
80
|
}
|
|
80
81
|
return buf2;
|
|
81
82
|
}
|
|
82
83
|
static trim(buf, natlen) {
|
|
83
|
-
return buf.
|
|
84
|
+
return buf.slice(natlen - buf.length, buf.length);
|
|
84
85
|
}
|
|
85
86
|
static pad(buf, natlen, size) {
|
|
86
|
-
const rbuf =
|
|
87
|
+
const rbuf = BufferUtil.alloc(size);
|
|
87
88
|
for (let i = 0; i < buf.length; i++) {
|
|
88
89
|
rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i];
|
|
89
90
|
}
|
|
@@ -114,28 +115,23 @@ class BNWrapper {
|
|
|
114
115
|
return this._bn.toString(base);
|
|
115
116
|
}
|
|
116
117
|
toBuffer(opts) {
|
|
117
|
-
let
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (buf.length
|
|
121
|
-
|
|
122
|
-
else if (buf.length > opts.size) {
|
|
123
|
-
buf = buf.subarray(buf.length - opts.size);
|
|
118
|
+
let hex = this._bn.toString('hex', 2);
|
|
119
|
+
let buf = BufferUtil.from(hex, 'hex');
|
|
120
|
+
if (opts?.size) {
|
|
121
|
+
if (buf.length > opts.size) {
|
|
122
|
+
buf = buf.slice(buf.length - opts.size);
|
|
124
123
|
}
|
|
125
124
|
else if (buf.length < opts.size) {
|
|
126
|
-
const padded =
|
|
125
|
+
const padded = BufferUtil.alloc(opts.size);
|
|
127
126
|
buf.copy(padded, opts.size - buf.length);
|
|
128
127
|
buf = padded;
|
|
129
128
|
}
|
|
130
129
|
}
|
|
131
|
-
|
|
132
|
-
buf =
|
|
130
|
+
if (opts?.endian === 'little') {
|
|
131
|
+
buf = BufferUtil.from(buf).reverse();
|
|
133
132
|
}
|
|
134
133
|
return buf;
|
|
135
134
|
}
|
|
136
|
-
toArrayLike(ArrayType, endian, length) {
|
|
137
|
-
return this._bn.toArrayLike(ArrayType, endian || 'be', length);
|
|
138
|
-
}
|
|
139
135
|
mul(r) {
|
|
140
136
|
return new BNWrapper(this._bn.mul(r._bn));
|
|
141
137
|
}
|
|
@@ -196,7 +192,7 @@ class BNWrapper {
|
|
|
196
192
|
if (this.cmp(BNWrapper.Zero) === -1) {
|
|
197
193
|
buf = this.neg().toBuffer();
|
|
198
194
|
if (buf[0] & 0x80) {
|
|
199
|
-
buf =
|
|
195
|
+
buf = BufferUtil.concat([BufferUtil.from([0x80]), buf]);
|
|
200
196
|
}
|
|
201
197
|
else {
|
|
202
198
|
buf[0] = buf[0] | 0x80;
|
|
@@ -205,11 +201,11 @@ class BNWrapper {
|
|
|
205
201
|
else {
|
|
206
202
|
buf = this.toBuffer();
|
|
207
203
|
if (buf[0] & 0x80) {
|
|
208
|
-
buf =
|
|
204
|
+
buf = BufferUtil.concat([BufferUtil.from([0x00]), buf]);
|
|
209
205
|
}
|
|
210
206
|
}
|
|
211
207
|
if (buf.length === 1 && buf[0] === 0) {
|
|
212
|
-
buf =
|
|
208
|
+
buf = BufferUtil.from([]);
|
|
213
209
|
}
|
|
214
210
|
return buf;
|
|
215
211
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { BN } from './bn
|
|
2
|
-
import { Point } from './point
|
|
3
|
-
import { Signature } from './signature
|
|
4
|
-
import { Hash } from './hash
|
|
5
|
-
import { Random } from './random
|
|
6
|
-
import { PublicKey } from '../publickey
|
|
1
|
+
import { BN } from './bn';
|
|
2
|
+
import { Point } from './point';
|
|
3
|
+
import { Signature } from './signature';
|
|
4
|
+
import { Hash } from './hash';
|
|
5
|
+
import { Random } from './random';
|
|
6
|
+
import { PublicKey } from '../publickey';
|
|
7
|
+
import { BufferUtil } from '../util';
|
|
7
8
|
export class ECDSA {
|
|
8
9
|
hashbuf;
|
|
9
10
|
endian;
|
|
@@ -62,29 +63,29 @@ export class ECDSA {
|
|
|
62
63
|
const N = Point.getN();
|
|
63
64
|
let k;
|
|
64
65
|
do {
|
|
65
|
-
k =
|
|
66
|
+
k = BN.fromBuffer(Random.getRandomBuffer(32));
|
|
66
67
|
} while (!(k.lt(N) && k.gt(new BN(0))));
|
|
67
68
|
this.k = k;
|
|
68
69
|
return this;
|
|
69
70
|
}
|
|
70
71
|
deterministicK(badrs = 0) {
|
|
71
|
-
let v =
|
|
72
|
+
let v = BufferUtil.alloc(32);
|
|
72
73
|
v.fill(0x01);
|
|
73
|
-
let k =
|
|
74
|
+
let k = BufferUtil.alloc(32);
|
|
74
75
|
k.fill(0x00);
|
|
75
76
|
const x = this.privkey.toBuffer();
|
|
76
77
|
const hashbuf = this.endian === 'little'
|
|
77
78
|
? this.reverseBuffer(this.hashbuf)
|
|
78
79
|
: this.hashbuf;
|
|
79
|
-
k = Hash.sha256hmac(
|
|
80
|
+
k = Hash.sha256hmac(BufferUtil.concat([v, BufferUtil.from([0x00]), x, hashbuf]), k);
|
|
80
81
|
v = Hash.sha256hmac(v, k);
|
|
81
|
-
k = Hash.sha256hmac(
|
|
82
|
+
k = Hash.sha256hmac(BufferUtil.concat([v, BufferUtil.from([0x01]), x, hashbuf]), k);
|
|
82
83
|
v = Hash.sha256hmac(v, k);
|
|
83
84
|
v = Hash.sha256hmac(v, k);
|
|
84
85
|
let T = new BN(v, 'be');
|
|
85
86
|
const N = Point.getN();
|
|
86
87
|
for (let i = 0; i < badrs || !(T.lt(N) && T.gt(new BN(0))); i++) {
|
|
87
|
-
k = Hash.sha256hmac(
|
|
88
|
+
k = Hash.sha256hmac(BufferUtil.concat([v, BufferUtil.from([0x00])]), k);
|
|
88
89
|
v = Hash.sha256hmac(v, k);
|
|
89
90
|
v = Hash.sha256hmac(v, k);
|
|
90
91
|
T = new BN(v, 'be');
|
|
@@ -97,7 +98,7 @@ export class ECDSA {
|
|
|
97
98
|
if (!(i === 0 || i === 1 || i === 2 || i === 3)) {
|
|
98
99
|
throw new Error('i must be equal to 0, 1, 2, or 3');
|
|
99
100
|
}
|
|
100
|
-
const e =
|
|
101
|
+
const e = BN.fromBuffer(this.hashbuf, { endian: 'big' });
|
|
101
102
|
const r = this.sig.r;
|
|
102
103
|
const s = this.sig.s;
|
|
103
104
|
const isYOdd = (i & 1) !== 0;
|
|
@@ -110,14 +111,14 @@ export class ECDSA {
|
|
|
110
111
|
if (!nR.isInfinity()) {
|
|
111
112
|
throw new Error('nR is not a valid curve point');
|
|
112
113
|
}
|
|
113
|
-
const eNeg = e.neg().
|
|
114
|
+
const eNeg = e.neg().umod(n);
|
|
114
115
|
const rInv = r.invm(n);
|
|
115
116
|
const Q = R.mul(s).add(G.mul(eNeg)).mul(rInv);
|
|
116
117
|
const pubkey = PublicKey.fromPoint(Q, this.sig.compressed);
|
|
117
118
|
return pubkey;
|
|
118
119
|
}
|
|
119
120
|
sigError() {
|
|
120
|
-
if (!
|
|
121
|
+
if (!BufferUtil.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) {
|
|
121
122
|
return 'hashbuf must be a 32 byte buffer';
|
|
122
123
|
}
|
|
123
124
|
const r = this.sig.r;
|
|
@@ -126,16 +127,16 @@ export class ECDSA {
|
|
|
126
127
|
if (!(r.gt(new BN(0)) && r.lt(N)) || !(s.gt(new BN(0)) && s.lt(N))) {
|
|
127
128
|
return 'r and s not in range';
|
|
128
129
|
}
|
|
129
|
-
const e =
|
|
130
|
+
const e = BN.fromBuffer(this.hashbuf, { endian: this.endian || 'big' });
|
|
130
131
|
const n = Point.getN();
|
|
131
132
|
const sinv = s.invm(n);
|
|
132
|
-
const u1 = sinv.mul(e).
|
|
133
|
-
const u2 = sinv.mul(r).
|
|
133
|
+
const u1 = sinv.mul(e).umod(n);
|
|
134
|
+
const u2 = sinv.mul(r).umod(n);
|
|
134
135
|
const p = Point.getG().mulAdd(u1, this.pubkey.point, u2);
|
|
135
136
|
if (p.isInfinity()) {
|
|
136
137
|
return 'p is infinity';
|
|
137
138
|
}
|
|
138
|
-
if (p.
|
|
139
|
+
if (p.x.umod(n).cmp(r) !== 0) {
|
|
139
140
|
return 'Invalid signature';
|
|
140
141
|
}
|
|
141
142
|
else {
|
|
@@ -161,11 +162,11 @@ export class ECDSA {
|
|
|
161
162
|
badrs++;
|
|
162
163
|
k = this.k;
|
|
163
164
|
Q = G.mul(k);
|
|
164
|
-
r = Q.
|
|
165
|
+
r = Q.x.umod(N);
|
|
165
166
|
s = k
|
|
166
167
|
.invm(N)
|
|
167
168
|
.mul(e.add(d.mul(r)))
|
|
168
|
-
.
|
|
169
|
+
.umod(N);
|
|
169
170
|
} while (r.cmp(new BN(0)) <= 0 || s.cmp(new BN(0)) <= 0);
|
|
170
171
|
s = ECDSA.toLowS(s);
|
|
171
172
|
return { s, r, compressed: this.pubkey.compressed };
|
|
@@ -177,10 +178,10 @@ export class ECDSA {
|
|
|
177
178
|
if (!hashbuf || !privkey || !d) {
|
|
178
179
|
throw new Error('invalid parameters');
|
|
179
180
|
}
|
|
180
|
-
if (!
|
|
181
|
+
if (!BufferUtil.isBuffer(hashbuf) || hashbuf.length !== 32) {
|
|
181
182
|
throw new Error('hashbuf must be a 32 byte buffer');
|
|
182
183
|
}
|
|
183
|
-
const e =
|
|
184
|
+
const e = BN.fromBuffer(hashbuf, { endian: this.endian || 'big' });
|
|
184
185
|
const obj = this._findSignature(d, e);
|
|
185
186
|
obj.compressed = this.pubkey.compressed;
|
|
186
187
|
this.sig = new Signature(obj);
|
|
@@ -238,7 +239,7 @@ export class ECDSA {
|
|
|
238
239
|
.verify().verified;
|
|
239
240
|
}
|
|
240
241
|
reverseBuffer(buf) {
|
|
241
|
-
const buf2 =
|
|
242
|
+
const buf2 = BufferUtil.alloc(buf.length);
|
|
242
243
|
for (let i = 0; i < buf.length; i++) {
|
|
243
244
|
buf2[i] = buf[buf.length - 1 - i];
|
|
244
245
|
}
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import { sha1 } from '@noble/hashes/
|
|
2
|
-
import { sha256 } from '@noble/hashes/
|
|
3
|
-
import {
|
|
4
|
-
import { ripemd160 } from '@noble/hashes/ripemd160';
|
|
1
|
+
import { ripemd160, sha1 } from '@noble/hashes/legacy';
|
|
2
|
+
import { sha256, sha512 } from '@noble/hashes/sha2';
|
|
3
|
+
import { BufferUtil } from '../util';
|
|
5
4
|
const sha1Func = (buf) => {
|
|
6
|
-
if (!
|
|
5
|
+
if (!BufferUtil.isBuffer(buf)) {
|
|
7
6
|
throw new Error('Argument must be a Buffer');
|
|
8
7
|
}
|
|
9
|
-
return
|
|
8
|
+
return BufferUtil.from(sha1(buf));
|
|
10
9
|
};
|
|
11
10
|
sha1Func.blocksize = 512;
|
|
12
11
|
const sha256Func = (buf) => {
|
|
13
|
-
if (!
|
|
12
|
+
if (!BufferUtil.isBuffer(buf)) {
|
|
13
|
+
console.log('sha256Func: buf is not a Buffer', buf, typeof buf);
|
|
14
14
|
throw new Error('Argument must be a Buffer');
|
|
15
15
|
}
|
|
16
|
-
return
|
|
16
|
+
return BufferUtil.from(sha256(buf));
|
|
17
17
|
};
|
|
18
18
|
sha256Func.blocksize = 512;
|
|
19
19
|
const sha512Func = (buf) => {
|
|
20
|
-
if (!
|
|
20
|
+
if (!BufferUtil.isBuffer(buf)) {
|
|
21
21
|
throw new Error('Argument must be a Buffer');
|
|
22
22
|
}
|
|
23
|
-
return
|
|
23
|
+
return BufferUtil.from(sha512(buf));
|
|
24
24
|
};
|
|
25
25
|
sha512Func.blocksize = 1024;
|
|
26
26
|
export class Hash {
|
|
@@ -28,28 +28,28 @@ export class Hash {
|
|
|
28
28
|
static sha256 = sha256Func;
|
|
29
29
|
static sha512 = sha512Func;
|
|
30
30
|
static sha256sha256 = function (buf) {
|
|
31
|
-
if (!
|
|
31
|
+
if (!BufferUtil.isBuffer(buf)) {
|
|
32
32
|
throw new Error('Argument must be a Buffer');
|
|
33
33
|
}
|
|
34
34
|
return Hash.sha256(Hash.sha256(buf));
|
|
35
35
|
};
|
|
36
36
|
static ripemd160 = function (buf) {
|
|
37
|
-
if (!
|
|
37
|
+
if (!BufferUtil.isBuffer(buf)) {
|
|
38
38
|
throw new Error('Argument must be a Buffer');
|
|
39
39
|
}
|
|
40
|
-
return
|
|
40
|
+
return BufferUtil.from(ripemd160(buf));
|
|
41
41
|
};
|
|
42
42
|
static sha256ripemd160 = function (buf) {
|
|
43
|
-
if (!
|
|
43
|
+
if (!BufferUtil.isBuffer(buf)) {
|
|
44
44
|
throw new Error('Argument must be a Buffer');
|
|
45
45
|
}
|
|
46
46
|
return Hash.ripemd160(Hash.sha256(buf));
|
|
47
47
|
};
|
|
48
48
|
static hmac(hashf, data, key) {
|
|
49
|
-
if (!
|
|
49
|
+
if (!BufferUtil.isBuffer(data)) {
|
|
50
50
|
throw new Error('Data must be a Buffer');
|
|
51
51
|
}
|
|
52
|
-
if (!
|
|
52
|
+
if (!BufferUtil.isBuffer(key)) {
|
|
53
53
|
throw new Error('Key must be a Buffer');
|
|
54
54
|
}
|
|
55
55
|
if (!hashf.blocksize) {
|
|
@@ -61,22 +61,25 @@ export class Hash {
|
|
|
61
61
|
processedKey = hashf(key);
|
|
62
62
|
}
|
|
63
63
|
else if (key.length < blocksize) {
|
|
64
|
-
const fill =
|
|
64
|
+
const fill = BufferUtil.alloc(blocksize);
|
|
65
65
|
fill.fill(0);
|
|
66
66
|
key.copy(fill);
|
|
67
67
|
processedKey = fill;
|
|
68
68
|
}
|
|
69
|
-
const o_key =
|
|
69
|
+
const o_key = BufferUtil.alloc(blocksize);
|
|
70
70
|
o_key.fill(0x5c);
|
|
71
|
-
const i_key =
|
|
71
|
+
const i_key = BufferUtil.alloc(blocksize);
|
|
72
72
|
i_key.fill(0x36);
|
|
73
|
-
const o_key_pad =
|
|
74
|
-
const i_key_pad =
|
|
73
|
+
const o_key_pad = BufferUtil.alloc(blocksize);
|
|
74
|
+
const i_key_pad = BufferUtil.alloc(blocksize);
|
|
75
75
|
for (let i = 0; i < blocksize; i++) {
|
|
76
76
|
o_key_pad[i] = o_key[i] ^ processedKey[i];
|
|
77
77
|
i_key_pad[i] = i_key[i] ^ processedKey[i];
|
|
78
78
|
}
|
|
79
|
-
return hashf(
|
|
79
|
+
return hashf(BufferUtil.concat([
|
|
80
|
+
o_key_pad,
|
|
81
|
+
hashf(BufferUtil.concat([i_key_pad, data])),
|
|
82
|
+
]));
|
|
80
83
|
}
|
|
81
84
|
static sha256hmac(data, key) {
|
|
82
85
|
return Hash.hmac(Hash.sha256, data, key);
|