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