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