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,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ScriptNum = exports.ScriptNumError = void 0;
|
|
4
|
+
const buffer_1 = require("../../util/buffer");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
class ScriptNumError extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = 'ScriptNumError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.ScriptNumError = ScriptNumError;
|
|
13
|
+
class ScriptNum {
|
|
14
|
+
static INT64_MAX = (1n << 63n) - 1n;
|
|
15
|
+
static INT64_MIN_EXCLUSIVE = -(1n << 63n) + 1n;
|
|
16
|
+
value;
|
|
17
|
+
constructor(value) {
|
|
18
|
+
this.value = value;
|
|
19
|
+
}
|
|
20
|
+
static fromBuffer(buf, requireMinimal = true, maxSize = types_1.SCRIPTNUM_MAX_ELEMENT_SIZE) {
|
|
21
|
+
if (buf.length > maxSize) {
|
|
22
|
+
throw new ScriptNumError('script number overflow');
|
|
23
|
+
}
|
|
24
|
+
if (requireMinimal && !ScriptNum.isMinimallyEncoded(buf, maxSize)) {
|
|
25
|
+
throw new ScriptNumError('non-minimally encoded script number');
|
|
26
|
+
}
|
|
27
|
+
return new ScriptNum(ScriptNum.decode(buf));
|
|
28
|
+
}
|
|
29
|
+
static isMinimallyEncoded(buf, maxSize = types_1.SCRIPTNUM_MAX_ELEMENT_SIZE) {
|
|
30
|
+
if (buf.length > maxSize) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
if (buf.length === 0) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if ((buf[buf.length - 1] & 0x7f) === 0) {
|
|
37
|
+
if (buf.length <= 1 || (buf[buf.length - 2] & 0x80) === 0) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
static minimallyEncode(data) {
|
|
44
|
+
if (data.length === 0) {
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
let last = data[data.length - 1];
|
|
48
|
+
if ((last & 0x7f) !== 0) {
|
|
49
|
+
return data;
|
|
50
|
+
}
|
|
51
|
+
let i = data.length - 1;
|
|
52
|
+
while (i > 0 && data[i] === 0) {
|
|
53
|
+
i--;
|
|
54
|
+
}
|
|
55
|
+
if (i === 0 && data[0] === 0) {
|
|
56
|
+
return buffer_1.BufferUtil.alloc(0);
|
|
57
|
+
}
|
|
58
|
+
const result = buffer_1.BufferUtil.alloc(i + 1 + ((data[i] & 0x80) !== 0 ? 1 : 0));
|
|
59
|
+
data.copy(result, 0, 0, i + 1);
|
|
60
|
+
if ((data[i] & 0x80) !== 0) {
|
|
61
|
+
result[result.length - 1] = last & 0x80;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
result[i] |= last & 0x80;
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
static decode(buf) {
|
|
69
|
+
if (buf.length === 0) {
|
|
70
|
+
return 0n;
|
|
71
|
+
}
|
|
72
|
+
let result = 0n;
|
|
73
|
+
for (let i = 0; i < buf.length; i++) {
|
|
74
|
+
result |= BigInt(buf[i]) << BigInt(8 * i);
|
|
75
|
+
}
|
|
76
|
+
if (buf[buf.length - 1] & 0x80) {
|
|
77
|
+
const mask = 0x80n << BigInt(8 * (buf.length - 1));
|
|
78
|
+
return -(result & ~mask);
|
|
79
|
+
}
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
toBuffer() {
|
|
83
|
+
return ScriptNum.serialize(this.value);
|
|
84
|
+
}
|
|
85
|
+
static serialize(value) {
|
|
86
|
+
if (value === 0n) {
|
|
87
|
+
return buffer_1.BufferUtil.alloc(0);
|
|
88
|
+
}
|
|
89
|
+
const neg = value < 0n;
|
|
90
|
+
let absvalue = neg ? -value : value;
|
|
91
|
+
const result = [];
|
|
92
|
+
while (absvalue > 0n) {
|
|
93
|
+
result.push(Number(absvalue & 0xffn));
|
|
94
|
+
absvalue >>= 8n;
|
|
95
|
+
}
|
|
96
|
+
if (result[result.length - 1] & 0x80) {
|
|
97
|
+
result.push(neg ? 0x80 : 0x00);
|
|
98
|
+
}
|
|
99
|
+
else if (neg) {
|
|
100
|
+
result[result.length - 1] |= 0x80;
|
|
101
|
+
}
|
|
102
|
+
return buffer_1.BufferUtil.from(result);
|
|
103
|
+
}
|
|
104
|
+
toNumber() {
|
|
105
|
+
const MAX_INT = 2_147_483_647;
|
|
106
|
+
const MIN_INT = -2_147_483_648;
|
|
107
|
+
if (this.value > BigInt(MAX_INT)) {
|
|
108
|
+
return MAX_INT;
|
|
109
|
+
}
|
|
110
|
+
if (this.value < BigInt(MIN_INT)) {
|
|
111
|
+
return MIN_INT;
|
|
112
|
+
}
|
|
113
|
+
return Number(this.value);
|
|
114
|
+
}
|
|
115
|
+
add(other) {
|
|
116
|
+
const result = this.value + other.value;
|
|
117
|
+
ScriptNum.checkOverflow(result);
|
|
118
|
+
return new ScriptNum(result);
|
|
119
|
+
}
|
|
120
|
+
sub(other) {
|
|
121
|
+
const result = this.value - other.value;
|
|
122
|
+
ScriptNum.checkOverflow(result);
|
|
123
|
+
return new ScriptNum(result);
|
|
124
|
+
}
|
|
125
|
+
negate() {
|
|
126
|
+
return new ScriptNum(-this.value);
|
|
127
|
+
}
|
|
128
|
+
abs() {
|
|
129
|
+
return this.value < 0n ? this.negate() : this;
|
|
130
|
+
}
|
|
131
|
+
div(other) {
|
|
132
|
+
return new ScriptNum(this.value / other.value);
|
|
133
|
+
}
|
|
134
|
+
mod(other) {
|
|
135
|
+
return new ScriptNum(this.value % other.value);
|
|
136
|
+
}
|
|
137
|
+
mulpow2(shift) {
|
|
138
|
+
if (this.value === 0n) {
|
|
139
|
+
return new ScriptNum(0n);
|
|
140
|
+
}
|
|
141
|
+
const sign = this.value > 0n ? 1n : -1n;
|
|
142
|
+
const absval = this.value > 0n ? this.value : -this.value;
|
|
143
|
+
if (shift.value > 0n) {
|
|
144
|
+
if (shift.value >= 63n) {
|
|
145
|
+
throw new ScriptNumError('script number mulpow2 non-zero shift >= 63');
|
|
146
|
+
}
|
|
147
|
+
const overflowMask = ~((1n << (63n - shift.value)) - 1n) & ((1n << 64n) - 1n);
|
|
148
|
+
if ((absval & overflowMask) !== 0n) {
|
|
149
|
+
throw new ScriptNumError('script number mulpow2 overflow');
|
|
150
|
+
}
|
|
151
|
+
return new ScriptNum(sign * (absval << shift.value));
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
const rshift = -shift.value;
|
|
155
|
+
if (rshift >= 63n) {
|
|
156
|
+
return new ScriptNum(0n);
|
|
157
|
+
}
|
|
158
|
+
return new ScriptNum(sign * (absval >> rshift));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
bitwiseAnd(mask) {
|
|
162
|
+
return new ScriptNum(this.value & mask);
|
|
163
|
+
}
|
|
164
|
+
isZero() {
|
|
165
|
+
return this.value === 0n;
|
|
166
|
+
}
|
|
167
|
+
isNegative() {
|
|
168
|
+
return this.value < 0n;
|
|
169
|
+
}
|
|
170
|
+
lt(other) {
|
|
171
|
+
const val = other instanceof ScriptNum ? other.value : other;
|
|
172
|
+
return this.value < val;
|
|
173
|
+
}
|
|
174
|
+
gt(other) {
|
|
175
|
+
const val = other instanceof ScriptNum ? other.value : other;
|
|
176
|
+
return this.value > val;
|
|
177
|
+
}
|
|
178
|
+
lte(other) {
|
|
179
|
+
const val = other instanceof ScriptNum ? other.value : other;
|
|
180
|
+
return this.value <= val;
|
|
181
|
+
}
|
|
182
|
+
gte(other) {
|
|
183
|
+
const val = other instanceof ScriptNum ? other.value : other;
|
|
184
|
+
return this.value >= val;
|
|
185
|
+
}
|
|
186
|
+
eq(other) {
|
|
187
|
+
const val = other instanceof ScriptNum ? other.value : other;
|
|
188
|
+
return this.value === val;
|
|
189
|
+
}
|
|
190
|
+
neq(other) {
|
|
191
|
+
const val = other instanceof ScriptNum ? other.value : other;
|
|
192
|
+
return this.value !== val;
|
|
193
|
+
}
|
|
194
|
+
static checkOverflow(value) {
|
|
195
|
+
if (value > ScriptNum.INT64_MAX || value < ScriptNum.INT64_MIN_EXCLUSIVE) {
|
|
196
|
+
throw new ScriptNumError('script number overflow');
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
exports.ScriptNum = ScriptNum;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NULL_SIGNATURE_CHECKER = exports.ScriptError = exports.STANDARD_SCRIPT_VERIFY_FLAGS = exports.ScriptFlags = exports.SCRIPTNUM_MAX_ELEMENT_SIZE = exports.LOCKTIME_THRESHOLD = exports.MAX_NUM2BIN_SIZE = exports.MAX_STACK_SIZE = exports.MAX_SCRIPT_SIZE = exports.MAX_PUBKEYS_PER_MULTISIG = exports.MAX_OPS_PER_SCRIPT = exports.MAX_SCRIPT_ELEMENT_SIZE = void 0;
|
|
4
|
+
exports.MAX_SCRIPT_ELEMENT_SIZE = 520;
|
|
5
|
+
exports.MAX_OPS_PER_SCRIPT = 400;
|
|
6
|
+
exports.MAX_PUBKEYS_PER_MULTISIG = 20;
|
|
7
|
+
exports.MAX_SCRIPT_SIZE = 10_000;
|
|
8
|
+
exports.MAX_STACK_SIZE = 1_000;
|
|
9
|
+
exports.MAX_NUM2BIN_SIZE = 68;
|
|
10
|
+
exports.LOCKTIME_THRESHOLD = 500_000_000;
|
|
11
|
+
exports.SCRIPTNUM_MAX_ELEMENT_SIZE = 8;
|
|
12
|
+
var ScriptFlags;
|
|
13
|
+
(function (ScriptFlags) {
|
|
14
|
+
ScriptFlags[ScriptFlags["VERIFY_NONE"] = 0] = "VERIFY_NONE";
|
|
15
|
+
ScriptFlags[ScriptFlags["TAPROOT_KEY_SPEND_PATH"] = 1] = "TAPROOT_KEY_SPEND_PATH";
|
|
16
|
+
ScriptFlags[ScriptFlags["DISABLE_TAPROOT_SIGHASH_LOTUS"] = 2] = "DISABLE_TAPROOT_SIGHASH_LOTUS";
|
|
17
|
+
ScriptFlags[ScriptFlags["VERIFY_DISCOURAGE_UPGRADABLE_NOPS"] = 128] = "VERIFY_DISCOURAGE_UPGRADABLE_NOPS";
|
|
18
|
+
ScriptFlags[ScriptFlags["VERIFY_CLEANSTACK"] = 256] = "VERIFY_CLEANSTACK";
|
|
19
|
+
ScriptFlags[ScriptFlags["VERIFY_MINIMALIF"] = 8192] = "VERIFY_MINIMALIF";
|
|
20
|
+
ScriptFlags[ScriptFlags["ENABLE_SIGHASH_FORKID"] = 65536] = "ENABLE_SIGHASH_FORKID";
|
|
21
|
+
ScriptFlags[ScriptFlags["ENABLE_REPLAY_PROTECTION"] = 131072] = "ENABLE_REPLAY_PROTECTION";
|
|
22
|
+
ScriptFlags[ScriptFlags["VERIFY_INPUT_SIGCHECKS"] = 4194304] = "VERIFY_INPUT_SIGCHECKS";
|
|
23
|
+
})(ScriptFlags || (exports.ScriptFlags = ScriptFlags = {}));
|
|
24
|
+
exports.STANDARD_SCRIPT_VERIFY_FLAGS = ScriptFlags.VERIFY_CLEANSTACK |
|
|
25
|
+
ScriptFlags.VERIFY_DISCOURAGE_UPGRADABLE_NOPS |
|
|
26
|
+
ScriptFlags.VERIFY_MINIMALIF |
|
|
27
|
+
ScriptFlags.ENABLE_SIGHASH_FORKID |
|
|
28
|
+
ScriptFlags.VERIFY_INPUT_SIGCHECKS;
|
|
29
|
+
var ScriptError;
|
|
30
|
+
(function (ScriptError) {
|
|
31
|
+
ScriptError["OK"] = "OK";
|
|
32
|
+
ScriptError["UNKNOWN"] = "UNKNOWN";
|
|
33
|
+
ScriptError["EVAL_FALSE"] = "EVAL_FALSE";
|
|
34
|
+
ScriptError["OP_RETURN"] = "OP_RETURN";
|
|
35
|
+
ScriptError["SCRIPT_SIZE"] = "SCRIPT_SIZE";
|
|
36
|
+
ScriptError["PUSH_SIZE"] = "PUSH_SIZE";
|
|
37
|
+
ScriptError["OP_COUNT"] = "OP_COUNT";
|
|
38
|
+
ScriptError["STACK_SIZE"] = "STACK_SIZE";
|
|
39
|
+
ScriptError["SIG_COUNT"] = "SIG_COUNT";
|
|
40
|
+
ScriptError["PUBKEY_COUNT"] = "PUBKEY_COUNT";
|
|
41
|
+
ScriptError["INPUT_SIGCHECKS"] = "INPUT_SIGCHECKS";
|
|
42
|
+
ScriptError["INVALID_OPERAND_SIZE"] = "INVALID_OPERAND_SIZE";
|
|
43
|
+
ScriptError["INVALID_NUMBER_RANGE"] = "INVALID_NUMBER_RANGE";
|
|
44
|
+
ScriptError["IMPOSSIBLE_ENCODING"] = "IMPOSSIBLE_ENCODING";
|
|
45
|
+
ScriptError["INVALID_SPLIT_RANGE"] = "INVALID_SPLIT_RANGE";
|
|
46
|
+
ScriptError["INVALID_BIT_COUNT"] = "INVALID_BIT_COUNT";
|
|
47
|
+
ScriptError["VERIFY"] = "VERIFY";
|
|
48
|
+
ScriptError["EQUALVERIFY"] = "EQUALVERIFY";
|
|
49
|
+
ScriptError["CHECKMULTISIGVERIFY"] = "CHECKMULTISIGVERIFY";
|
|
50
|
+
ScriptError["CHECKSIGVERIFY"] = "CHECKSIGVERIFY";
|
|
51
|
+
ScriptError["CHECKDATASIGVERIFY"] = "CHECKDATASIGVERIFY";
|
|
52
|
+
ScriptError["NUMEQUALVERIFY"] = "NUMEQUALVERIFY";
|
|
53
|
+
ScriptError["BAD_OPCODE"] = "BAD_OPCODE";
|
|
54
|
+
ScriptError["DISABLED_OPCODE"] = "DISABLED_OPCODE";
|
|
55
|
+
ScriptError["INVALID_STACK_OPERATION"] = "INVALID_STACK_OPERATION";
|
|
56
|
+
ScriptError["INVALID_ALTSTACK_OPERATION"] = "INVALID_ALTSTACK_OPERATION";
|
|
57
|
+
ScriptError["UNBALANCED_CONDITIONAL"] = "UNBALANCED_CONDITIONAL";
|
|
58
|
+
ScriptError["DIV_BY_ZERO"] = "DIV_BY_ZERO";
|
|
59
|
+
ScriptError["MOD_BY_ZERO"] = "MOD_BY_ZERO";
|
|
60
|
+
ScriptError["INVALID_BITFIELD_SIZE"] = "INVALID_BITFIELD_SIZE";
|
|
61
|
+
ScriptError["INVALID_BIT_RANGE"] = "INVALID_BIT_RANGE";
|
|
62
|
+
ScriptError["NEGATIVE_LOCKTIME"] = "NEGATIVE_LOCKTIME";
|
|
63
|
+
ScriptError["UNSATISFIED_LOCKTIME"] = "UNSATISFIED_LOCKTIME";
|
|
64
|
+
ScriptError["SIG_HASHTYPE"] = "SIG_HASHTYPE";
|
|
65
|
+
ScriptError["SIG_DER"] = "SIG_DER";
|
|
66
|
+
ScriptError["MINIMALDATA"] = "MINIMALDATA";
|
|
67
|
+
ScriptError["SIG_PUSHONLY"] = "SIG_PUSHONLY";
|
|
68
|
+
ScriptError["SIG_HIGH_S"] = "SIG_HIGH_S";
|
|
69
|
+
ScriptError["PUBKEYTYPE"] = "PUBKEYTYPE";
|
|
70
|
+
ScriptError["CLEANSTACK"] = "CLEANSTACK";
|
|
71
|
+
ScriptError["MINIMALIF"] = "MINIMALIF";
|
|
72
|
+
ScriptError["SIG_NULLFAIL"] = "SIG_NULLFAIL";
|
|
73
|
+
ScriptError["SIG_BADLENGTH"] = "SIG_BADLENGTH";
|
|
74
|
+
ScriptError["SIG_NONSCHNORR"] = "SIG_NONSCHNORR";
|
|
75
|
+
ScriptError["DISCOURAGE_UPGRADABLE_NOPS"] = "DISCOURAGE_UPGRADABLE_NOPS";
|
|
76
|
+
ScriptError["ILLEGAL_FORKID"] = "ILLEGAL_FORKID";
|
|
77
|
+
ScriptError["MUST_USE_FORKID"] = "MUST_USE_FORKID";
|
|
78
|
+
ScriptError["INVALID_NUM2BIN_SIZE"] = "INVALID_NUM2BIN_SIZE";
|
|
79
|
+
ScriptError["INVALID_OP_SCRIPTTYPE"] = "INVALID_OP_SCRIPTTYPE";
|
|
80
|
+
ScriptError["SCRIPTTYPE_INVALID_TYPE"] = "SCRIPTTYPE_INVALID_TYPE";
|
|
81
|
+
ScriptError["SCRIPTTYPE_MALFORMED_SCRIPT"] = "SCRIPTTYPE_MALFORMED_SCRIPT";
|
|
82
|
+
ScriptError["TAPROOT_KEY_SPEND_MUST_USE_LOTUS_SIGHASH"] = "TAPROOT_KEY_SPEND_MUST_USE_LOTUS_SIGHASH";
|
|
83
|
+
ScriptError["TAPROOT_KEY_SPEND_MUST_USE_SCHNORR_SIG"] = "TAPROOT_KEY_SPEND_MUST_USE_SCHNORR_SIG";
|
|
84
|
+
ScriptError["TAPROOT_VERIFY_SIGNATURE_FAILED"] = "TAPROOT_VERIFY_SIGNATURE_FAILED";
|
|
85
|
+
ScriptError["TAPROOT_ANNEX_NOT_SUPPORTED"] = "TAPROOT_ANNEX_NOT_SUPPORTED";
|
|
86
|
+
ScriptError["TAPROOT_WRONG_CONTROL_SIZE"] = "TAPROOT_WRONG_CONTROL_SIZE";
|
|
87
|
+
ScriptError["TAPROOT_VERIFY_COMMITMENT_FAILED"] = "TAPROOT_VERIFY_COMMITMENT_FAILED";
|
|
88
|
+
ScriptError["TAPROOT_LEAF_VERSION_NOT_SUPPORTED"] = "TAPROOT_LEAF_VERSION_NOT_SUPPORTED";
|
|
89
|
+
ScriptError["TAPROOT_PHASEOUT"] = "TAPROOT_PHASEOUT";
|
|
90
|
+
})(ScriptError || (exports.ScriptError = ScriptError = {}));
|
|
91
|
+
exports.NULL_SIGNATURE_CHECKER = {
|
|
92
|
+
verifySignature: () => false,
|
|
93
|
+
checkSignature: () => false,
|
|
94
|
+
checkLockTime: () => false,
|
|
95
|
+
checkSequence: () => false,
|
|
96
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TAPROOT_ANNEX_TAG = exports.TAPROOT_SIGHASH_TYPE = exports.TAPROOT_SIZE_WITH_STATE = exports.TAPROOT_SIZE_WITHOUT_STATE = exports.TAPROOT_INTRO_SIZE = exports.TAPROOT_SCRIPTTYPE = exports.TAPROOT_CONTROL_MAX_SIZE = exports.TAPROOT_CONTROL_MAX_NODE_COUNT = exports.TAPROOT_CONTROL_NODE_SIZE = exports.TAPROOT_CONTROL_BASE_SIZE = exports.TAPROOT_LEAF_TAPSCRIPT = exports.TAPROOT_LEAF_MASK = void 0;
|
|
3
|
+
exports.TAPROOT_TAG_TAPTWEAK = exports.TAPROOT_TAG_TAPBRANCH = exports.TAPROOT_TAG_TAPLEAF = exports.TAPROOT_ANNEX_TAG = exports.TAPROOT_SIGHASH_TYPE = exports.TAPROOT_SIZE_WITH_STATE = exports.TAPROOT_SIZE_WITHOUT_STATE = exports.TAPROOT_STATE_PUSH_SIZE = exports.SHA256_HASH_SIZE = exports.PUBKEY_XCOORD_SIZE = exports.PUBKEY_UNCOMPRESSED_SIZE = exports.PUBKEY_COMPRESSED_SIZE = exports.TAPROOT_INTRO_SIZE = exports.TAPROOT_SCRIPTTYPE = exports.TAPROOT_CONTROL_MAX_SIZE = exports.TAPROOT_CONTROL_MAX_NODE_COUNT = exports.TAPROOT_CONTROL_NODE_SIZE = exports.TAPROOT_CONTROL_BASE_SIZE = exports.TAPROOT_LEAF_TAPSCRIPT = exports.TAPROOT_LEAF_MASK = void 0;
|
|
4
4
|
exports.taggedHash = taggedHash;
|
|
5
5
|
exports.calculateTapTweak = calculateTapTweak;
|
|
6
6
|
exports.calculateTapLeaf = calculateTapLeaf;
|
|
@@ -11,23 +11,25 @@ exports.isTapLeafNode = isTapLeafNode;
|
|
|
11
11
|
exports.isTapBranchNode = isTapBranchNode;
|
|
12
12
|
exports.buildTapTree = buildTapTree;
|
|
13
13
|
exports.createControlBlock = createControlBlock;
|
|
14
|
+
exports.applyTweakToPublicKey = applyTweakToPublicKey;
|
|
14
15
|
exports.verifyTaprootCommitment = verifyTaprootCommitment;
|
|
15
|
-
exports.isPayToTaproot = isPayToTaproot;
|
|
16
16
|
exports.extractTaprootCommitment = extractTaprootCommitment;
|
|
17
17
|
exports.extractTaprootState = extractTaprootState;
|
|
18
18
|
exports.buildPayToTaproot = buildPayToTaproot;
|
|
19
19
|
exports.buildKeyPathTaproot = buildKeyPathTaproot;
|
|
20
20
|
exports.buildScriptPathTaproot = buildScriptPathTaproot;
|
|
21
21
|
exports.verifyTaprootScriptPath = verifyTaprootScriptPath;
|
|
22
|
+
exports.verifyTaprootScriptPathFromControlBlock = verifyTaprootScriptPathFromControlBlock;
|
|
22
23
|
exports.verifyTaprootSpend = verifyTaprootSpend;
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
const
|
|
24
|
+
const hash_1 = require("../crypto/hash");
|
|
25
|
+
const publickey_1 = require("../publickey");
|
|
26
|
+
const privatekey_1 = require("../privatekey");
|
|
27
|
+
const script_1 = require("../script");
|
|
28
|
+
const opcode_1 = require("../opcode");
|
|
29
|
+
const bn_1 = require("../crypto/bn");
|
|
30
|
+
const bufferwriter_1 = require("../encoding/bufferwriter");
|
|
31
|
+
const buffer_1 = require("../util/buffer");
|
|
32
|
+
const signature_1 = require("../crypto/signature");
|
|
31
33
|
exports.TAPROOT_LEAF_MASK = 0xfe;
|
|
32
34
|
exports.TAPROOT_LEAF_TAPSCRIPT = 0xc0;
|
|
33
35
|
exports.TAPROOT_CONTROL_BASE_SIZE = 33;
|
|
@@ -35,47 +37,53 @@ exports.TAPROOT_CONTROL_NODE_SIZE = 32;
|
|
|
35
37
|
exports.TAPROOT_CONTROL_MAX_NODE_COUNT = 128;
|
|
36
38
|
exports.TAPROOT_CONTROL_MAX_SIZE = exports.TAPROOT_CONTROL_BASE_SIZE +
|
|
37
39
|
exports.TAPROOT_CONTROL_NODE_SIZE * exports.TAPROOT_CONTROL_MAX_NODE_COUNT;
|
|
38
|
-
exports.TAPROOT_SCRIPTTYPE =
|
|
40
|
+
exports.TAPROOT_SCRIPTTYPE = opcode_1.Opcode.OP_1;
|
|
39
41
|
exports.TAPROOT_INTRO_SIZE = 3;
|
|
40
|
-
exports.
|
|
41
|
-
exports.
|
|
42
|
-
exports.
|
|
42
|
+
exports.PUBKEY_COMPRESSED_SIZE = 33;
|
|
43
|
+
exports.PUBKEY_UNCOMPRESSED_SIZE = 65;
|
|
44
|
+
exports.PUBKEY_XCOORD_SIZE = 32;
|
|
45
|
+
exports.SHA256_HASH_SIZE = 32;
|
|
46
|
+
exports.TAPROOT_STATE_PUSH_SIZE = 32;
|
|
47
|
+
exports.TAPROOT_SIZE_WITHOUT_STATE = exports.TAPROOT_INTRO_SIZE + exports.PUBKEY_COMPRESSED_SIZE;
|
|
48
|
+
exports.TAPROOT_SIZE_WITH_STATE = exports.TAPROOT_INTRO_SIZE + exports.PUBKEY_COMPRESSED_SIZE + 1 + exports.SHA256_HASH_SIZE;
|
|
49
|
+
exports.TAPROOT_SIGHASH_TYPE = signature_1.Signature.SIGHASH_ALL | signature_1.Signature.SIGHASH_LOTUS;
|
|
43
50
|
exports.TAPROOT_ANNEX_TAG = 0x50;
|
|
51
|
+
exports.TAPROOT_TAG_TAPLEAF = 'TapLeaf';
|
|
52
|
+
exports.TAPROOT_TAG_TAPBRANCH = 'TapBranch';
|
|
53
|
+
exports.TAPROOT_TAG_TAPTWEAK = 'TapTweak';
|
|
44
54
|
function taggedHash(tag, data) {
|
|
45
|
-
const tagHash =
|
|
46
|
-
const combined =
|
|
47
|
-
return
|
|
55
|
+
const tagHash = hash_1.Hash.sha256(buffer_1.BufferUtil.from(tag, 'utf8'));
|
|
56
|
+
const combined = buffer_1.BufferUtil.concat([tagHash, tagHash, data]);
|
|
57
|
+
return hash_1.Hash.sha256(combined);
|
|
48
58
|
}
|
|
49
|
-
function calculateTapTweak(internalPubKey, merkleRoot =
|
|
50
|
-
|
|
51
|
-
const data = Buffer.concat([pubKeyBytes, merkleRoot]);
|
|
52
|
-
return taggedHash('TapTweak', data);
|
|
59
|
+
function calculateTapTweak(internalPubKey, merkleRoot = buffer_1.BufferUtil.alloc(exports.SHA256_HASH_SIZE)) {
|
|
60
|
+
return taggedHash(exports.TAPROOT_TAG_TAPTWEAK, buffer_1.BufferUtil.concat([internalPubKey.toBuffer(), merkleRoot]));
|
|
53
61
|
}
|
|
54
62
|
function calculateTapLeaf(script, leafVersion = exports.TAPROOT_LEAF_TAPSCRIPT) {
|
|
55
|
-
const scriptBuf =
|
|
56
|
-
const writer = new
|
|
63
|
+
const scriptBuf = buffer_1.BufferUtil.isBuffer(script) ? script : script.toBuffer();
|
|
64
|
+
const writer = new bufferwriter_1.BufferWriter();
|
|
57
65
|
writer.writeUInt8(leafVersion);
|
|
58
66
|
writer.writeVarintNum(scriptBuf.length);
|
|
59
67
|
writer.write(scriptBuf);
|
|
60
|
-
return taggedHash(
|
|
68
|
+
return taggedHash(exports.TAPROOT_TAG_TAPLEAF, writer.toBuffer());
|
|
61
69
|
}
|
|
62
70
|
function calculateTapBranch(left, right) {
|
|
63
|
-
const ordered =
|
|
64
|
-
?
|
|
65
|
-
:
|
|
66
|
-
return taggedHash(
|
|
71
|
+
const ordered = buffer_1.BufferUtil.compare(left, right) < 0
|
|
72
|
+
? buffer_1.BufferUtil.concat([left, right])
|
|
73
|
+
: buffer_1.BufferUtil.concat([right, left]);
|
|
74
|
+
return taggedHash(exports.TAPROOT_TAG_TAPBRANCH, ordered);
|
|
67
75
|
}
|
|
68
|
-
function tweakPublicKey(internalPubKey, merkleRoot =
|
|
76
|
+
function tweakPublicKey(internalPubKey, merkleRoot = buffer_1.BufferUtil.alloc(exports.SHA256_HASH_SIZE)) {
|
|
69
77
|
const tweak = calculateTapTweak(internalPubKey, merkleRoot);
|
|
70
78
|
return internalPubKey.addScalar(tweak);
|
|
71
79
|
}
|
|
72
|
-
function tweakPrivateKey(internalPrivKey, merkleRoot =
|
|
80
|
+
function tweakPrivateKey(internalPrivKey, merkleRoot = buffer_1.BufferUtil.alloc(exports.SHA256_HASH_SIZE)) {
|
|
73
81
|
const internalPubKey = internalPrivKey.publicKey;
|
|
74
82
|
const tweak = calculateTapTweak(internalPubKey, merkleRoot);
|
|
75
|
-
const tweakBN = new
|
|
83
|
+
const tweakBN = new bn_1.BN(tweak);
|
|
76
84
|
const privKeyBN = internalPrivKey.bn;
|
|
77
|
-
const tweakedBN = privKeyBN.add(tweakBN).umod(
|
|
78
|
-
return new
|
|
85
|
+
const tweakedBN = privKeyBN.add(tweakBN).umod(publickey_1.PublicKey.getN());
|
|
86
|
+
return new privatekey_1.PrivateKey(tweakedBN);
|
|
79
87
|
}
|
|
80
88
|
function isTapLeafNode(node) {
|
|
81
89
|
return 'script' in node;
|
|
@@ -87,7 +95,7 @@ function buildTapTree(tree) {
|
|
|
87
95
|
if (isTapLeafNode(tree)) {
|
|
88
96
|
const leafNode = tree;
|
|
89
97
|
const leafVersion = leafNode.leafVersion || exports.TAPROOT_LEAF_TAPSCRIPT;
|
|
90
|
-
const scriptBuf =
|
|
98
|
+
const scriptBuf = buffer_1.BufferUtil.isBuffer(leafNode.script)
|
|
91
99
|
? leafNode.script
|
|
92
100
|
: leafNode.script.toBuffer();
|
|
93
101
|
const leafHash = calculateTapLeaf(scriptBuf, leafVersion);
|
|
@@ -95,7 +103,7 @@ function buildTapTree(tree) {
|
|
|
95
103
|
merkleRoot: leafHash,
|
|
96
104
|
leaves: [
|
|
97
105
|
{
|
|
98
|
-
script:
|
|
106
|
+
script: script_1.Script.fromBuffer(scriptBuf),
|
|
99
107
|
leafVersion,
|
|
100
108
|
leafHash,
|
|
101
109
|
merklePath: [],
|
|
@@ -126,83 +134,83 @@ function createControlBlock(internalPubKey, leafIndex, tree) {
|
|
|
126
134
|
}
|
|
127
135
|
const leaf = treeResult.leaves[leafIndex];
|
|
128
136
|
const pubKeyBytes = internalPubKey.toBuffer();
|
|
129
|
-
const parity = pubKeyBytes[0] ===
|
|
137
|
+
const parity = pubKeyBytes[0] === publickey_1.PUBKEY_PREFIX_ODD ? 1 : 0;
|
|
130
138
|
const controlByte = (leaf.leafVersion & exports.TAPROOT_LEAF_MASK) | parity;
|
|
131
|
-
const writer = new
|
|
139
|
+
const writer = new bufferwriter_1.BufferWriter();
|
|
132
140
|
writer.writeUInt8(controlByte);
|
|
133
|
-
writer.write(pubKeyBytes.slice(1,
|
|
141
|
+
writer.write(pubKeyBytes.slice(1, exports.PUBKEY_COMPRESSED_SIZE));
|
|
134
142
|
for (const node of leaf.merklePath) {
|
|
135
143
|
writer.write(node);
|
|
136
144
|
}
|
|
137
145
|
return writer.toBuffer();
|
|
138
146
|
}
|
|
139
|
-
function
|
|
140
|
-
|
|
141
|
-
return commitmentPubKey.toString() === expectedCommitment.toString();
|
|
147
|
+
function applyTweakToPublicKey(internalPubKey, tweak) {
|
|
148
|
+
return internalPubKey.addScalar(tweak);
|
|
142
149
|
}
|
|
143
|
-
function
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
150
|
+
function verifyTaprootCommitment(controlBlock, commitment, script) {
|
|
151
|
+
try {
|
|
152
|
+
if (controlBlock.length < exports.TAPROOT_CONTROL_BASE_SIZE) {
|
|
153
|
+
return { tapleafHash: buffer_1.BufferUtil.alloc(exports.SHA256_HASH_SIZE), success: false };
|
|
154
|
+
}
|
|
155
|
+
const pathLen = Math.floor((controlBlock.length - exports.TAPROOT_CONTROL_BASE_SIZE) /
|
|
156
|
+
exports.TAPROOT_CONTROL_NODE_SIZE);
|
|
157
|
+
const leafVersion = controlBlock[0] & exports.TAPROOT_LEAF_MASK;
|
|
158
|
+
let leafHash = calculateTapLeaf(script, leafVersion);
|
|
159
|
+
let merkleHash = leafHash;
|
|
160
|
+
for (let i = 0; i < pathLen; i++) {
|
|
161
|
+
const nodeOffset = exports.TAPROOT_CONTROL_BASE_SIZE + i * exports.TAPROOT_CONTROL_NODE_SIZE;
|
|
162
|
+
const node = controlBlock.slice(nodeOffset, nodeOffset + exports.TAPROOT_CONTROL_NODE_SIZE);
|
|
163
|
+
if (buffer_1.BufferUtil.compare(merkleHash, node) < 0) {
|
|
164
|
+
merkleHash = calculateTapBranch(merkleHash, node);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
merkleHash = calculateTapBranch(node, merkleHash);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const pubkeyBytes = controlBlock.slice(0, exports.TAPROOT_CONTROL_BASE_SIZE);
|
|
171
|
+
const pubkeyBuffer = buffer_1.BufferUtil.from(pubkeyBytes);
|
|
172
|
+
pubkeyBuffer[0] =
|
|
173
|
+
pubkeyBuffer[0] & 1 ? publickey_1.PUBKEY_PREFIX_ODD : publickey_1.PUBKEY_PREFIX_EVEN;
|
|
174
|
+
const internalPubKey = new publickey_1.PublicKey(pubkeyBuffer);
|
|
175
|
+
const tweakHash = calculateTapTweak(internalPubKey, merkleHash);
|
|
176
|
+
const commitmentKey = new publickey_1.PublicKey(commitment);
|
|
177
|
+
const expectedCommitment = applyTweakToPublicKey(internalPubKey, tweakHash);
|
|
178
|
+
return {
|
|
179
|
+
tapleafHash: leafHash,
|
|
180
|
+
success: commitmentKey.toString() === expectedCommitment.toString(),
|
|
181
|
+
};
|
|
153
182
|
}
|
|
154
|
-
|
|
155
|
-
return
|
|
183
|
+
catch (e) {
|
|
184
|
+
return { tapleafHash: buffer_1.BufferUtil.alloc(exports.SHA256_HASH_SIZE), success: false };
|
|
156
185
|
}
|
|
157
|
-
return (buf.length === exports.TAPROOT_SIZE_WITH_STATE &&
|
|
158
|
-
buf[exports.TAPROOT_SIZE_WITHOUT_STATE] === 32);
|
|
159
186
|
}
|
|
160
187
|
function extractTaprootCommitment(script) {
|
|
161
|
-
if (!
|
|
188
|
+
if (!script.isTaprootOut()) {
|
|
162
189
|
throw new Error('Not a valid Pay-To-Taproot script');
|
|
163
190
|
}
|
|
164
|
-
|
|
165
|
-
const commitmentBytes = buf.subarray(3, 3 + 33);
|
|
166
|
-
return publickey_js_1.PublicKey.fromBuffer(commitmentBytes);
|
|
191
|
+
return publickey_1.PublicKey.fromBuffer(script.chunks[2].buf);
|
|
167
192
|
}
|
|
168
193
|
function extractTaprootState(script) {
|
|
169
|
-
|
|
170
|
-
if (buf.length !== exports.TAPROOT_SIZE_WITH_STATE) {
|
|
194
|
+
if (!script.isTaprootOut()) {
|
|
171
195
|
return null;
|
|
172
196
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
function buildPayToTaproot(commitment, state) {
|
|
176
|
-
if (state && state.length !== 32) {
|
|
177
|
-
throw new Error('Taproot state must be exactly 32 bytes');
|
|
178
|
-
}
|
|
179
|
-
const commitmentBytes = commitment.toBuffer();
|
|
180
|
-
if (commitmentBytes.length !== 33) {
|
|
181
|
-
throw new Error('Commitment must be 33-byte compressed public key');
|
|
182
|
-
}
|
|
183
|
-
if (state) {
|
|
184
|
-
return new script_js_1.Script()
|
|
185
|
-
.add(opcode_js_1.Opcode.OP_SCRIPTTYPE)
|
|
186
|
-
.add(exports.TAPROOT_SCRIPTTYPE)
|
|
187
|
-
.add(commitmentBytes)
|
|
188
|
-
.add(state);
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
return new script_js_1.Script()
|
|
192
|
-
.add(opcode_js_1.Opcode.OP_SCRIPTTYPE)
|
|
193
|
-
.add(exports.TAPROOT_SCRIPTTYPE)
|
|
194
|
-
.add(commitmentBytes);
|
|
197
|
+
if (script.chunks.length !== 4) {
|
|
198
|
+
return null;
|
|
195
199
|
}
|
|
200
|
+
return script.chunks[3].buf;
|
|
201
|
+
}
|
|
202
|
+
function buildPayToTaproot(_commitment, _state) {
|
|
203
|
+
throw new Error('This function has been deprecated. Used Script.buildTaprootOut instead');
|
|
196
204
|
}
|
|
197
|
-
function buildKeyPathTaproot(internalPubKey
|
|
198
|
-
const merkleRoot =
|
|
205
|
+
function buildKeyPathTaproot(internalPubKey) {
|
|
206
|
+
const merkleRoot = buffer_1.BufferUtil.alloc(exports.SHA256_HASH_SIZE);
|
|
199
207
|
const commitment = tweakPublicKey(internalPubKey, merkleRoot);
|
|
200
|
-
return
|
|
208
|
+
return script_1.Script.buildTaprootOut(commitment);
|
|
201
209
|
}
|
|
202
210
|
function buildScriptPathTaproot(internalPubKey, tree, state) {
|
|
203
211
|
const treeInfo = buildTapTree(tree);
|
|
204
212
|
const commitment = tweakPublicKey(internalPubKey, treeInfo.merkleRoot);
|
|
205
|
-
const script =
|
|
213
|
+
const script = script_1.Script.buildTaprootOut(commitment, state);
|
|
206
214
|
return {
|
|
207
215
|
script,
|
|
208
216
|
commitment,
|
|
@@ -212,23 +220,23 @@ function buildScriptPathTaproot(internalPubKey, tree, state) {
|
|
|
212
220
|
}
|
|
213
221
|
function verifyTaprootScriptPath(internalPubKey, script, commitmentPubKey, leafVersion, merklePath, parity) {
|
|
214
222
|
try {
|
|
215
|
-
const pubkeyPrefix = parity === 0 ?
|
|
216
|
-
const fullPubkey =
|
|
217
|
-
|
|
223
|
+
const pubkeyPrefix = parity === 0 ? publickey_1.PUBKEY_PREFIX_EVEN : publickey_1.PUBKEY_PREFIX_ODD;
|
|
224
|
+
const fullPubkey = buffer_1.BufferUtil.concat([
|
|
225
|
+
buffer_1.BufferUtil.from([pubkeyPrefix]),
|
|
218
226
|
internalPubKey,
|
|
219
227
|
]);
|
|
220
228
|
let leafHash = calculateTapLeaf(script, leafVersion);
|
|
221
229
|
for (const pathNode of merklePath) {
|
|
222
|
-
if (
|
|
230
|
+
if (buffer_1.BufferUtil.compare(leafHash, pathNode) < 0) {
|
|
223
231
|
leafHash = calculateTapBranch(leafHash, pathNode);
|
|
224
232
|
}
|
|
225
233
|
else {
|
|
226
234
|
leafHash = calculateTapBranch(pathNode, leafHash);
|
|
227
235
|
}
|
|
228
236
|
}
|
|
229
|
-
const internalKey = new
|
|
237
|
+
const internalKey = new publickey_1.PublicKey(fullPubkey);
|
|
230
238
|
const expectedCommitment = tweakPublicKey(internalKey, leafHash);
|
|
231
|
-
const actualCommitment = new
|
|
239
|
+
const actualCommitment = new publickey_1.PublicKey(commitmentPubKey);
|
|
232
240
|
if (expectedCommitment.toString() !== actualCommitment.toString()) {
|
|
233
241
|
return false;
|
|
234
242
|
}
|
|
@@ -238,6 +246,10 @@ function verifyTaprootScriptPath(internalPubKey, script, commitmentPubKey, leafV
|
|
|
238
246
|
return false;
|
|
239
247
|
}
|
|
240
248
|
}
|
|
249
|
+
function verifyTaprootScriptPathFromControlBlock(controlBlock, commitment, script) {
|
|
250
|
+
const result = verifyTaprootCommitment(controlBlock, commitment, script);
|
|
251
|
+
return result.success;
|
|
252
|
+
}
|
|
241
253
|
function verifyTaprootSpend(scriptPubkey, stack, flags) {
|
|
242
254
|
const SCRIPT_DISABLE_TAPROOT_SIGHASH_LOTUS = 1 << 22;
|
|
243
255
|
const SCRIPT_TAPROOT_KEY_SPEND_PATH = 1 << 23;
|
|
@@ -245,7 +257,7 @@ function verifyTaprootSpend(scriptPubkey, stack, flags) {
|
|
|
245
257
|
if (flags & SCRIPT_DISABLE_TAPROOT_SIGHASH_LOTUS) {
|
|
246
258
|
return { success: false, error: 'SCRIPT_ERR_TAPROOT_PHASEOUT' };
|
|
247
259
|
}
|
|
248
|
-
if (!
|
|
260
|
+
if (!scriptPubkey.isTaprootOut()) {
|
|
249
261
|
return { success: false, error: 'SCRIPT_ERR_SCRIPTTYPE_MALFORMED_SCRIPT' };
|
|
250
262
|
}
|
|
251
263
|
const scriptBuf = scriptPubkey.toBuffer();
|
|
@@ -266,7 +278,7 @@ function verifyTaprootSpend(scriptPubkey, stack, flags) {
|
|
|
266
278
|
}
|
|
267
279
|
const controlBlock = stack[stack.length - 1];
|
|
268
280
|
const scriptBytes = stack[stack.length - 2];
|
|
269
|
-
const execScript = new
|
|
281
|
+
const execScript = new script_1.Script(scriptBytes);
|
|
270
282
|
const newStack = stack.slice(0, stack.length - 2);
|
|
271
283
|
const sizeRemainder = (controlBlock.length - exports.TAPROOT_CONTROL_BASE_SIZE) %
|
|
272
284
|
exports.TAPROOT_CONTROL_NODE_SIZE;
|