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
|
@@ -8,13 +8,17 @@ import { Address } from './address.js';
|
|
|
8
8
|
import { BitcoreError } from './errors.js';
|
|
9
9
|
import { BufferUtil } from './util/buffer.js';
|
|
10
10
|
import { Signature } from './crypto/signature.js';
|
|
11
|
-
import { Chunk } from './chunk.js';
|
|
12
|
-
import {
|
|
11
|
+
import { Chunk } from './script/chunk.js';
|
|
12
|
+
import { PUBKEY_COMPRESSED_SIZE } from './script/taproot.js';
|
|
13
13
|
export class Script {
|
|
14
14
|
chunks;
|
|
15
15
|
_network;
|
|
16
16
|
constructor(from) {
|
|
17
|
-
if (
|
|
17
|
+
if (!from) {
|
|
18
|
+
this.chunks = [];
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (BufferUtil.isBuffer(from)) {
|
|
18
22
|
return Script.fromBuffer(from);
|
|
19
23
|
}
|
|
20
24
|
else if (from instanceof Address) {
|
|
@@ -26,14 +30,9 @@ export class Script {
|
|
|
26
30
|
else if (typeof from === 'string') {
|
|
27
31
|
return Script.fromString(from);
|
|
28
32
|
}
|
|
29
|
-
else if (typeof from === 'object' &&
|
|
30
|
-
from !== null &&
|
|
31
|
-
Array.isArray(from.chunks)) {
|
|
33
|
+
else if (typeof from === 'object' && Array.isArray(from.chunks)) {
|
|
32
34
|
this.set(from);
|
|
33
35
|
}
|
|
34
|
-
else {
|
|
35
|
-
this.chunks = [];
|
|
36
|
-
}
|
|
37
36
|
}
|
|
38
37
|
set(obj) {
|
|
39
38
|
Preconditions.checkArgument(typeof obj === 'object', 'obj', 'Must be an object');
|
|
@@ -42,18 +41,25 @@ export class Script {
|
|
|
42
41
|
this._network = obj._network;
|
|
43
42
|
return this;
|
|
44
43
|
}
|
|
44
|
+
static empty() {
|
|
45
|
+
return new Script();
|
|
46
|
+
}
|
|
45
47
|
static fromBuffer(buffer) {
|
|
46
|
-
const script =
|
|
48
|
+
const script = Script.empty();
|
|
47
49
|
script.chunks = [];
|
|
48
50
|
const br = new BufferReader(buffer);
|
|
49
51
|
while (!br.finished()) {
|
|
50
52
|
try {
|
|
51
53
|
const opcodenum = br.readUInt8();
|
|
52
54
|
let len, buf;
|
|
53
|
-
if (opcodenum
|
|
55
|
+
if (opcodenum >= 0 && opcodenum < Opcode.OP_PUSHDATA1) {
|
|
54
56
|
len = opcodenum;
|
|
57
|
+
buf = br.read(len);
|
|
58
|
+
if (buf.length !== len) {
|
|
59
|
+
throw new BitcoreError.Script.InvalidBuffer(`Push data length mismatch: expected ${len}, got ${buf.length}`);
|
|
60
|
+
}
|
|
55
61
|
script.chunks.push(new Chunk({
|
|
56
|
-
buf:
|
|
62
|
+
buf: buf,
|
|
57
63
|
len: len,
|
|
58
64
|
opcodenum: opcodenum,
|
|
59
65
|
}));
|
|
@@ -61,6 +67,9 @@ export class Script {
|
|
|
61
67
|
else if (opcodenum === Opcode.OP_PUSHDATA1) {
|
|
62
68
|
len = br.readUInt8();
|
|
63
69
|
buf = br.read(len);
|
|
70
|
+
if (buf.length !== len) {
|
|
71
|
+
throw new BitcoreError.Script.InvalidBuffer(`OP_PUSHDATA1 length mismatch: expected ${len}, got ${buf.length}`);
|
|
72
|
+
}
|
|
64
73
|
script.chunks.push(new Chunk({
|
|
65
74
|
buf: buf,
|
|
66
75
|
len: len,
|
|
@@ -70,6 +79,9 @@ export class Script {
|
|
|
70
79
|
else if (opcodenum === Opcode.OP_PUSHDATA2) {
|
|
71
80
|
len = br.readUInt16LE();
|
|
72
81
|
buf = br.read(len);
|
|
82
|
+
if (buf.length !== len) {
|
|
83
|
+
throw new BitcoreError.Script.InvalidBuffer(`OP_PUSHDATA2 length mismatch: expected ${len}, got ${buf.length}`);
|
|
84
|
+
}
|
|
73
85
|
script.chunks.push(new Chunk({
|
|
74
86
|
buf: buf,
|
|
75
87
|
len: len,
|
|
@@ -79,6 +91,9 @@ export class Script {
|
|
|
79
91
|
else if (opcodenum === Opcode.OP_PUSHDATA4) {
|
|
80
92
|
len = br.readUInt32LE();
|
|
81
93
|
buf = br.read(len);
|
|
94
|
+
if (buf.length !== len) {
|
|
95
|
+
throw new BitcoreError.Script.InvalidBuffer(`OP_PUSHDATA4 length mismatch: expected ${len}, got ${buf.length}`);
|
|
96
|
+
}
|
|
82
97
|
script.chunks.push(new Chunk({
|
|
83
98
|
buf: buf,
|
|
84
99
|
len: len,
|
|
@@ -106,7 +121,7 @@ export class Script {
|
|
|
106
121
|
if (!/^[0-9a-f]*$/.test(cleanStr)) {
|
|
107
122
|
throw new BitcoreError.Script.InvalidScriptString(str);
|
|
108
123
|
}
|
|
109
|
-
const buffer =
|
|
124
|
+
const buffer = BufferUtil.from(cleanStr, 'hex');
|
|
110
125
|
return Script.fromBuffer(buffer);
|
|
111
126
|
}
|
|
112
127
|
static fromASM(str) {
|
|
@@ -119,7 +134,7 @@ export class Script {
|
|
|
119
134
|
const opcode = new Opcode(token);
|
|
120
135
|
const opcodenum = opcode.num;
|
|
121
136
|
if (opcodenum === undefined) {
|
|
122
|
-
const buf =
|
|
137
|
+
const buf = BufferUtil.from(tokens[i], 'hex');
|
|
123
138
|
let opcodenum;
|
|
124
139
|
const len = buf.length;
|
|
125
140
|
if (len >= 0 && len < Opcode.OP_PUSHDATA1) {
|
|
@@ -154,14 +169,14 @@ export class Script {
|
|
|
154
169
|
return script;
|
|
155
170
|
}
|
|
156
171
|
static fromHex(str) {
|
|
157
|
-
return new Script(
|
|
172
|
+
return new Script(BufferUtil.from(str, 'hex'));
|
|
158
173
|
}
|
|
159
174
|
static fromAddress(address) {
|
|
160
175
|
if (typeof address === 'string') {
|
|
161
176
|
address = Address.fromString(address);
|
|
162
177
|
}
|
|
163
178
|
if (address.isPayToTaproot()) {
|
|
164
|
-
return Script.
|
|
179
|
+
return Script.buildTaprootOut(address.hashBuffer);
|
|
165
180
|
}
|
|
166
181
|
else if (address.isPayToScriptHash()) {
|
|
167
182
|
return Script.buildScriptHashOut(address);
|
|
@@ -171,6 +186,55 @@ export class Script {
|
|
|
171
186
|
}
|
|
172
187
|
throw new BitcoreError.Script.UnrecognizedAddress(address);
|
|
173
188
|
}
|
|
189
|
+
static fromPayload(scriptType, payload) {
|
|
190
|
+
const buf = typeof payload === 'string' ? BufferUtil.from(payload, 'hex') : payload;
|
|
191
|
+
switch (scriptType) {
|
|
192
|
+
case 'p2pk': {
|
|
193
|
+
if (buf.length !== 33 && buf.length !== 65) {
|
|
194
|
+
throw new Error(`Invalid p2pk payload length: expected 33 (compressed) or 65 (uncompressed), got ${buf.length}`);
|
|
195
|
+
}
|
|
196
|
+
return Script.buildPublicKeyOut(new PublicKey(buf));
|
|
197
|
+
}
|
|
198
|
+
case 'p2pkh': {
|
|
199
|
+
if (buf.length !== 20) {
|
|
200
|
+
throw new Error(`Invalid p2pkh payload length: expected 20, got ${buf.length}`);
|
|
201
|
+
}
|
|
202
|
+
const p2pkhScript = new Script();
|
|
203
|
+
p2pkhScript.chunks = [];
|
|
204
|
+
p2pkhScript
|
|
205
|
+
.add(Opcode.OP_DUP)
|
|
206
|
+
.add(Opcode.OP_HASH160)
|
|
207
|
+
.add(buf)
|
|
208
|
+
.add(Opcode.OP_EQUALVERIFY)
|
|
209
|
+
.add(Opcode.OP_CHECKSIG);
|
|
210
|
+
return p2pkhScript;
|
|
211
|
+
}
|
|
212
|
+
case 'p2sh': {
|
|
213
|
+
if (buf.length !== 20) {
|
|
214
|
+
throw new Error(`Invalid p2sh payload length: expected 20, got ${buf.length}`);
|
|
215
|
+
}
|
|
216
|
+
const p2shScript = new Script();
|
|
217
|
+
p2shScript.add(Opcode.OP_HASH160).add(buf).add(Opcode.OP_EQUAL);
|
|
218
|
+
return p2shScript;
|
|
219
|
+
}
|
|
220
|
+
case 'p2tr-commitment': {
|
|
221
|
+
if (buf.length !== 33) {
|
|
222
|
+
throw new Error(`Invalid p2tr-commitment payload length: expected 33, got ${buf.length}`);
|
|
223
|
+
}
|
|
224
|
+
return Script.buildTaprootOut(buf);
|
|
225
|
+
}
|
|
226
|
+
case 'p2tr-state': {
|
|
227
|
+
throw new Error('Cannot reconstruct full script from p2tr-state payload alone; ' +
|
|
228
|
+
'the commitment is required. Use p2tr-commitment payload instead.');
|
|
229
|
+
}
|
|
230
|
+
case 'other': {
|
|
231
|
+
return Script.fromBuffer(buf);
|
|
232
|
+
}
|
|
233
|
+
default: {
|
|
234
|
+
throw new Error(`Unknown script type: ${scriptType}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
174
238
|
static buildMultisigOut(publicKeys, threshold, opts = {}) {
|
|
175
239
|
Preconditions.checkArgument(threshold <= publicKeys.length, 'threshold', 'Number of required signatures must be less than or equal to the number of public keys');
|
|
176
240
|
const script = new Script();
|
|
@@ -313,13 +377,6 @@ export class Script {
|
|
|
313
377
|
script.add((opts.cachedMultisig || Script.buildMultisigOut(pubkeys, threshold, opts)).toBuffer());
|
|
314
378
|
return script;
|
|
315
379
|
}
|
|
316
|
-
static buildWitnessMultisigOutFromScript(script) {
|
|
317
|
-
const scriptHash = Hash.sha256(script.toBuffer());
|
|
318
|
-
const witnessScript = new Script();
|
|
319
|
-
witnessScript.add(Opcode.OP_0);
|
|
320
|
-
witnessScript.add(scriptHash);
|
|
321
|
-
return witnessScript;
|
|
322
|
-
}
|
|
323
380
|
static buildPublicKeyOut(pubkey) {
|
|
324
381
|
const script = new Script();
|
|
325
382
|
script.add(pubkey.toBuffer());
|
|
@@ -330,10 +387,10 @@ export class Script {
|
|
|
330
387
|
let buffer;
|
|
331
388
|
if (typeof data === 'string') {
|
|
332
389
|
if (encoding === 'hex') {
|
|
333
|
-
buffer =
|
|
390
|
+
buffer = BufferUtil.from(data, 'hex');
|
|
334
391
|
}
|
|
335
392
|
else {
|
|
336
|
-
buffer =
|
|
393
|
+
buffer = BufferUtil.from(data, 'utf8');
|
|
337
394
|
}
|
|
338
395
|
}
|
|
339
396
|
else {
|
|
@@ -389,10 +446,10 @@ export class Script {
|
|
|
389
446
|
script.add(publicKey.toBuffer());
|
|
390
447
|
return script;
|
|
391
448
|
}
|
|
392
|
-
static
|
|
449
|
+
static buildTaprootOut(commitment, state) {
|
|
393
450
|
Preconditions.checkArgument(commitment !== undefined, 'commitment', 'Must be defined');
|
|
394
451
|
const commitmentBuf = commitment instanceof PublicKey ? commitment.toBuffer() : commitment;
|
|
395
|
-
if (commitmentBuf.length !==
|
|
452
|
+
if (commitmentBuf.length !== PUBKEY_COMPRESSED_SIZE) {
|
|
396
453
|
throw new Error('Taproot commitment must be 33-byte compressed public key');
|
|
397
454
|
}
|
|
398
455
|
if (state && state.length !== 32) {
|
|
@@ -413,7 +470,7 @@ export class Script {
|
|
|
413
470
|
opcodenum: chunk.num,
|
|
414
471
|
}));
|
|
415
472
|
}
|
|
416
|
-
else if (
|
|
473
|
+
else if (BufferUtil.isBuffer(chunk)) {
|
|
417
474
|
const chunkObj = {
|
|
418
475
|
buf: chunk,
|
|
419
476
|
len: chunk.length,
|
|
@@ -443,6 +500,17 @@ export class Script {
|
|
|
443
500
|
}
|
|
444
501
|
return this;
|
|
445
502
|
}
|
|
503
|
+
addTaprootState(state) {
|
|
504
|
+
if (state.length !== 32) {
|
|
505
|
+
throw new Error('Taproot state must be exactly 32 bytes');
|
|
506
|
+
}
|
|
507
|
+
if (this.chunks[3]) {
|
|
508
|
+
this.chunks[3].buf = state;
|
|
509
|
+
return this;
|
|
510
|
+
}
|
|
511
|
+
this.add(state);
|
|
512
|
+
return this;
|
|
513
|
+
}
|
|
446
514
|
toBuffer() {
|
|
447
515
|
const bw = new BufferWriter();
|
|
448
516
|
for (const chunk of this.chunks) {
|
|
@@ -465,18 +533,6 @@ export class Script {
|
|
|
465
533
|
toString() {
|
|
466
534
|
return this.toBuffer().toString('hex');
|
|
467
535
|
}
|
|
468
|
-
toP2PKH() {
|
|
469
|
-
if (!this.isPayToPublicKeyHash()) {
|
|
470
|
-
throw new Error('Script is not a P2PKH address');
|
|
471
|
-
}
|
|
472
|
-
return this.chunks[2].buf.toString('hex');
|
|
473
|
-
}
|
|
474
|
-
toP2SH() {
|
|
475
|
-
if (!this.isPayToScriptHash()) {
|
|
476
|
-
throw new Error('Script is not a P2SH address');
|
|
477
|
-
}
|
|
478
|
-
return this.chunks[1].buf.toString('hex');
|
|
479
|
-
}
|
|
480
536
|
toASM() {
|
|
481
537
|
let str = '';
|
|
482
538
|
for (let i = 0; i < this.chunks.length; i++) {
|
|
@@ -555,53 +611,37 @@ export class Script {
|
|
|
555
611
|
}
|
|
556
612
|
return str;
|
|
557
613
|
}
|
|
558
|
-
|
|
614
|
+
isPublicKeyHashOut() {
|
|
559
615
|
return (this.chunks.length === 5 &&
|
|
560
616
|
this.chunks[0].opcodenum === Opcode.OP_DUP &&
|
|
561
617
|
this.chunks[1].opcodenum === Opcode.OP_HASH160 &&
|
|
562
618
|
this.chunks[2].opcodenum === 20 &&
|
|
563
|
-
this.chunks[2].buf
|
|
564
|
-
this.chunks[3].opcodenum === Opcode.OP_EQUALVERIFY &&
|
|
565
|
-
this.chunks[4].opcodenum === Opcode.OP_CHECKSIG);
|
|
566
|
-
}
|
|
567
|
-
isPublicKeyHashOut() {
|
|
568
|
-
return !!(this.chunks.length === 5 &&
|
|
569
|
-
this.chunks[0].opcodenum === Opcode.OP_DUP &&
|
|
570
|
-
this.chunks[1].opcodenum === Opcode.OP_HASH160 &&
|
|
571
|
-
this.chunks[2].buf &&
|
|
572
|
-
this.chunks[2].buf.length === 20 &&
|
|
619
|
+
this.chunks[2].buf?.length === 20 &&
|
|
573
620
|
this.chunks[3].opcodenum === Opcode.OP_EQUALVERIFY &&
|
|
574
621
|
this.chunks[4].opcodenum === Opcode.OP_CHECKSIG);
|
|
575
622
|
}
|
|
576
|
-
|
|
623
|
+
isScriptHashOut() {
|
|
577
624
|
return (this.chunks.length === 3 &&
|
|
578
625
|
this.chunks[0].opcodenum === Opcode.OP_HASH160 &&
|
|
579
626
|
this.chunks[1].opcodenum === 20 &&
|
|
580
|
-
this.chunks[1].buf
|
|
627
|
+
this.chunks[1].buf?.length === 20 &&
|
|
581
628
|
this.chunks[2].opcodenum === Opcode.OP_EQUAL);
|
|
582
629
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
buf[0] === Opcode.OP_HASH160 &&
|
|
587
|
-
buf[1] === 0x14 &&
|
|
588
|
-
buf[buf.length - 1] === Opcode.OP_EQUAL);
|
|
589
|
-
}
|
|
590
|
-
isPayToTaproot() {
|
|
591
|
-
const buf = this.toBuffer();
|
|
592
|
-
if (buf.length < TAPROOT_SIZE_WITHOUT_STATE ||
|
|
593
|
-
buf[0] !== Opcode.OP_SCRIPTTYPE ||
|
|
594
|
-
buf[1] !== Opcode.OP_1 ||
|
|
595
|
-
buf[2] !== 33) {
|
|
630
|
+
isTaprootOut() {
|
|
631
|
+
if (this.chunks[0].opcodenum !== Opcode.OP_SCRIPTTYPE ||
|
|
632
|
+
this.chunks[1].opcodenum !== Opcode.OP_1) {
|
|
596
633
|
return false;
|
|
597
634
|
}
|
|
598
|
-
if (
|
|
599
|
-
return
|
|
635
|
+
if (this.chunks.length === 3) {
|
|
636
|
+
return (this.chunks[2].opcodenum === 33 && this.chunks[2].buf?.length === 33);
|
|
600
637
|
}
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
638
|
+
if (this.chunks.length === 4) {
|
|
639
|
+
return (this.chunks[2].opcodenum === 33 &&
|
|
640
|
+
this.chunks[2].buf?.length === 33 &&
|
|
641
|
+
this.chunks[3].opcodenum === 32 &&
|
|
642
|
+
this.chunks[3].buf?.length === 32);
|
|
643
|
+
}
|
|
644
|
+
return false;
|
|
605
645
|
}
|
|
606
646
|
isTaprootIn() {
|
|
607
647
|
if (this.chunks.length === 0) {
|
|
@@ -632,20 +672,45 @@ export class Script {
|
|
|
632
672
|
getData() {
|
|
633
673
|
if (this.isScriptHashOut()) {
|
|
634
674
|
if (this.chunks[1] === undefined) {
|
|
635
|
-
return
|
|
675
|
+
return BufferUtil.alloc(0);
|
|
636
676
|
}
|
|
637
677
|
else {
|
|
638
|
-
return
|
|
678
|
+
return BufferUtil.from(this.chunks[1].buf);
|
|
639
679
|
}
|
|
640
680
|
}
|
|
641
681
|
if (this.isPublicKeyHashOut()) {
|
|
642
|
-
return
|
|
682
|
+
return BufferUtil.from(this.chunks[2].buf);
|
|
643
683
|
}
|
|
644
|
-
if (this.
|
|
645
|
-
return
|
|
684
|
+
if (this.isTaprootOut()) {
|
|
685
|
+
return BufferUtil.from(this.chunks[2].buf);
|
|
646
686
|
}
|
|
647
687
|
throw new Error('Unrecognized script type to get data from');
|
|
648
688
|
}
|
|
689
|
+
getPayload() {
|
|
690
|
+
let type;
|
|
691
|
+
let payload;
|
|
692
|
+
if (this.isPublicKeyOut()) {
|
|
693
|
+
type = 'p2pk';
|
|
694
|
+
payload = BufferUtil.from(this.chunks[0].buf);
|
|
695
|
+
}
|
|
696
|
+
else if (this.isPublicKeyHashOut()) {
|
|
697
|
+
type = 'p2pkh';
|
|
698
|
+
payload = BufferUtil.from(this.chunks[2].buf);
|
|
699
|
+
}
|
|
700
|
+
else if (this.isScriptHashOut()) {
|
|
701
|
+
type = 'p2sh';
|
|
702
|
+
payload = BufferUtil.from(this.chunks[1].buf);
|
|
703
|
+
}
|
|
704
|
+
else if (this.isTaprootOut()) {
|
|
705
|
+
type = this.chunks.length === 4 ? 'p2tr-state' : 'p2tr-commitment';
|
|
706
|
+
payload = BufferUtil.from(this.chunks[2].buf);
|
|
707
|
+
}
|
|
708
|
+
else {
|
|
709
|
+
type = 'other';
|
|
710
|
+
payload = this.toBuffer();
|
|
711
|
+
}
|
|
712
|
+
return { type, payload, payloadHex: payload.toString('hex') };
|
|
713
|
+
}
|
|
649
714
|
getAddressInfo() {
|
|
650
715
|
if (this._isInput) {
|
|
651
716
|
return this._getInputAddressInfo();
|
|
@@ -663,7 +728,7 @@ export class Script {
|
|
|
663
728
|
}
|
|
664
729
|
_getOutputAddressInfo() {
|
|
665
730
|
const info = {};
|
|
666
|
-
if (this.
|
|
731
|
+
if (this.isTaprootOut()) {
|
|
667
732
|
const buf = this.toBuffer();
|
|
668
733
|
info.hashBuffer = buf.slice(3, 36);
|
|
669
734
|
info.type = Address.PayToTaproot;
|
|
@@ -703,7 +768,7 @@ export class Script {
|
|
|
703
768
|
}
|
|
704
769
|
if (info instanceof Address) {
|
|
705
770
|
if (network) {
|
|
706
|
-
if (this.
|
|
771
|
+
if (this.isTaprootOut()) {
|
|
707
772
|
const buf = this.toBuffer();
|
|
708
773
|
const commitment = buf.slice(3, 36);
|
|
709
774
|
return Address.fromTaprootCommitment(commitment, network);
|
|
@@ -758,7 +823,16 @@ export class Script {
|
|
|
758
823
|
}
|
|
759
824
|
isValid() {
|
|
760
825
|
try {
|
|
761
|
-
|
|
826
|
+
for (const chunk of this.chunks) {
|
|
827
|
+
if (chunk.opcodenum === undefined) {
|
|
828
|
+
return false;
|
|
829
|
+
}
|
|
830
|
+
if (chunk.buf &&
|
|
831
|
+
(chunk.len === undefined || chunk.len !== chunk.buf.length)) {
|
|
832
|
+
return false;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
return true;
|
|
762
836
|
}
|
|
763
837
|
catch (e) {
|
|
764
838
|
return false;
|
|
@@ -768,7 +842,7 @@ export class Script {
|
|
|
768
842
|
const cloned = new Script();
|
|
769
843
|
cloned.chunks = this.chunks.map(chunk => new Chunk({
|
|
770
844
|
opcodenum: chunk.opcodenum,
|
|
771
|
-
buf: chunk.buf ?
|
|
845
|
+
buf: chunk.buf ? BufferUtil.from(chunk.buf) : undefined,
|
|
772
846
|
len: chunk.len,
|
|
773
847
|
}));
|
|
774
848
|
return cloned;
|
|
@@ -859,7 +933,7 @@ export class Script {
|
|
|
859
933
|
return (this.chunks.length > 3 &&
|
|
860
934
|
this._isSmallIntOp(this.chunks[0].opcodenum) &&
|
|
861
935
|
this.chunks.slice(1, this.chunks.length - 2).every(function (obj) {
|
|
862
|
-
return obj.buf &&
|
|
936
|
+
return obj.buf && BufferUtil.isBuffer(obj.buf);
|
|
863
937
|
}) &&
|
|
864
938
|
this._isSmallIntOp(this.chunks[this.chunks.length - 2].opcodenum) &&
|
|
865
939
|
this.chunks[this.chunks.length - 1].opcodenum === Opcode.OP_CHECKMULTISIG);
|
|
@@ -871,7 +945,7 @@ export class Script {
|
|
|
871
945
|
return (this.chunks.length >= 2 &&
|
|
872
946
|
this.chunks[0].opcodenum === 0 &&
|
|
873
947
|
this.chunks.slice(1, this.chunks.length).every(function (obj) {
|
|
874
|
-
return obj.buf &&
|
|
948
|
+
return (obj.buf && BufferUtil.isBuffer(obj.buf) && Signature.isTxDER(obj.buf));
|
|
875
949
|
}));
|
|
876
950
|
}
|
|
877
951
|
isDataOut() {
|
|
@@ -941,11 +1015,8 @@ export class Script {
|
|
|
941
1015
|
return this.classify() !== ScriptTypes.UNKNOWN;
|
|
942
1016
|
}
|
|
943
1017
|
getType() {
|
|
944
|
-
if (this.
|
|
945
|
-
|
|
946
|
-
return buf.length === TAPROOT_SIZE_WITH_STATE
|
|
947
|
-
? 'p2tr-state'
|
|
948
|
-
: 'p2tr-commitment';
|
|
1018
|
+
if (this.isTaprootOut()) {
|
|
1019
|
+
return this.chunks.length === 4 ? 'p2tr-state' : 'p2tr-commitment';
|
|
949
1020
|
}
|
|
950
1021
|
else if (this.isPublicKeyOut()) {
|
|
951
1022
|
return 'p2pk';
|
|
@@ -968,11 +1039,11 @@ export class Script {
|
|
|
968
1039
|
return false;
|
|
969
1040
|
}
|
|
970
1041
|
for (let i = 0; i < this.chunks.length; i++) {
|
|
971
|
-
if (
|
|
972
|
-
!
|
|
1042
|
+
if (BufferUtil.isBuffer(this.chunks[i].buf) &&
|
|
1043
|
+
!BufferUtil.isBuffer(script.chunks[i].buf)) {
|
|
973
1044
|
return false;
|
|
974
1045
|
}
|
|
975
|
-
if (
|
|
1046
|
+
if (BufferUtil.isBuffer(this.chunks[i].buf) &&
|
|
976
1047
|
!BufferUtil.equals(this.chunks[i].buf, script.chunks[i].buf)) {
|
|
977
1048
|
return false;
|
|
978
1049
|
}
|
|
@@ -992,7 +1063,7 @@ export class Script {
|
|
|
992
1063
|
else if (obj instanceof Opcode) {
|
|
993
1064
|
this._addOpcode(obj, prepend);
|
|
994
1065
|
}
|
|
995
|
-
else if (
|
|
1066
|
+
else if (BufferUtil.isBuffer(obj)) {
|
|
996
1067
|
this._addBuffer(obj, prepend);
|
|
997
1068
|
}
|
|
998
1069
|
else if (obj instanceof Script) {
|
|
@@ -1133,13 +1204,3 @@ export const ScriptTypes = {
|
|
|
1133
1204
|
MULTISIG_IN: 'Spend from multisig',
|
|
1134
1205
|
DATA_OUT: 'Data push',
|
|
1135
1206
|
};
|
|
1136
|
-
export function toAddress(script, network) {
|
|
1137
|
-
const addr = script.toAddress(network);
|
|
1138
|
-
if (!addr || typeof addr === 'boolean') {
|
|
1139
|
-
throw new Error('Cannot convert script to address');
|
|
1140
|
-
}
|
|
1141
|
-
return addr;
|
|
1142
|
-
}
|
|
1143
|
-
export function empty() {
|
|
1144
|
-
return new Script();
|
|
1145
|
-
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { Address } from '../address
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { Address } from '../address';
|
|
2
|
+
import { BufferUtil } from '../util/buffer';
|
|
3
|
+
import { BN, Point, Hash } from '../crypto/index';
|
|
4
|
+
import { MUSIG2_TAG_NONCE_COEFF, muSig2KeyAgg, muSig2PartialSign, muSig2PartialSigVerify, muSig2TaggedHash, } from '../crypto/musig2';
|
|
5
|
+
import { buildKeyPathTaproot, buildScriptPathTaproot, calculateTapTweak, tweakPublicKey, } from '../script/taproot';
|
|
6
|
+
export function buildMuSig2TaprootKey(signerPubKeys) {
|
|
7
|
+
const keyAggContext = muSig2KeyAgg(signerPubKeys);
|
|
7
8
|
const aggregatedPubKey = keyAggContext.aggregatedPubKey;
|
|
8
|
-
const merkleRoot =
|
|
9
|
+
const merkleRoot = BufferUtil.alloc(32);
|
|
9
10
|
const tweak = calculateTapTweak(aggregatedPubKey, merkleRoot);
|
|
10
11
|
const commitment = tweakPublicKey(aggregatedPubKey, merkleRoot);
|
|
11
|
-
const script = buildKeyPathTaproot(aggregatedPubKey
|
|
12
|
+
const script = buildKeyPathTaproot(aggregatedPubKey);
|
|
12
13
|
return {
|
|
13
14
|
aggregatedPubKey,
|
|
14
15
|
commitment,
|
|
@@ -18,8 +19,8 @@ export function buildMuSigTaprootKey(signerPubKeys, state) {
|
|
|
18
19
|
tweak,
|
|
19
20
|
};
|
|
20
21
|
}
|
|
21
|
-
export function
|
|
22
|
-
const keyAggContext =
|
|
22
|
+
export function buildMuSig2TaprootKeyWithScripts(signerPubKeys, scriptTree, state) {
|
|
23
|
+
const keyAggContext = muSig2KeyAgg(signerPubKeys);
|
|
23
24
|
const aggregatedPubKey = keyAggContext.aggregatedPubKey;
|
|
24
25
|
const { script, commitment, merkleRoot, leaves } = buildScriptPathTaproot(aggregatedPubKey, scriptTree, state);
|
|
25
26
|
const tweak = calculateTapTweak(aggregatedPubKey, merkleRoot);
|
|
@@ -40,22 +41,26 @@ export function signTaprootKeyPathWithMuSig2(secretNonce, privateKey, keyAggCont
|
|
|
40
41
|
...keyAggContext,
|
|
41
42
|
aggregatedPubKey: commitment,
|
|
42
43
|
};
|
|
43
|
-
const partialSig =
|
|
44
|
+
const partialSig = muSig2PartialSign(secretNonce, privateKey, modifiedKeyAggContext, signerIndex, aggregatedNonce, message);
|
|
44
45
|
if (signerIndex === 0) {
|
|
45
46
|
const { R1, R2 } = aggregatedNonce;
|
|
46
|
-
const nonceCoefData =
|
|
47
|
+
const nonceCoefData = BufferUtil.concat([
|
|
47
48
|
commitment.toBuffer(),
|
|
48
49
|
Point.pointToCompressed(R1),
|
|
49
50
|
Point.pointToCompressed(R2),
|
|
50
51
|
message,
|
|
51
52
|
]);
|
|
52
|
-
const b =
|
|
53
|
+
const b = BN.fromBuffer(muSig2TaggedHash(MUSIG2_TAG_NONCE_COEFF, nonceCoefData));
|
|
53
54
|
const R = R1.add(R2.mul(b));
|
|
54
|
-
const R_x = R.
|
|
55
|
+
const R_x = R.x.toBuffer({ size: 32 });
|
|
55
56
|
const commitment_compressed = Point.pointToCompressed(commitment.point);
|
|
56
|
-
const challengeData =
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
const challengeData = BufferUtil.concat([
|
|
58
|
+
R_x,
|
|
59
|
+
commitment_compressed,
|
|
60
|
+
message,
|
|
61
|
+
]);
|
|
62
|
+
const e = BN.fromBuffer(Hash.sha256(challengeData)).umod(n);
|
|
63
|
+
const tweakBN = BN.fromBuffer(tweak).umod(n);
|
|
59
64
|
const tweakTerm = e.mul(tweakBN).umod(n);
|
|
60
65
|
return partialSig.add(tweakTerm).umod(n);
|
|
61
66
|
}
|
|
@@ -71,28 +76,32 @@ export function verifyTaprootKeyPathMuSigPartial(partialSig, publicNonce, public
|
|
|
71
76
|
let adjustedPartialSig = partialSig;
|
|
72
77
|
if (signerIndex === 0) {
|
|
73
78
|
const { R1, R2 } = aggregatedNonce;
|
|
74
|
-
const nonceCoefData =
|
|
79
|
+
const nonceCoefData = BufferUtil.concat([
|
|
75
80
|
commitment.toBuffer(),
|
|
76
81
|
Point.pointToCompressed(R1),
|
|
77
82
|
Point.pointToCompressed(R2),
|
|
78
83
|
message,
|
|
79
84
|
]);
|
|
80
|
-
const b =
|
|
85
|
+
const b = BN.fromBuffer(muSig2TaggedHash(MUSIG2_TAG_NONCE_COEFF, nonceCoefData));
|
|
81
86
|
const R = R1.add(R2.mul(b));
|
|
82
|
-
const R_x = R.
|
|
87
|
+
const R_x = R.x.toBuffer({ size: 32 });
|
|
83
88
|
const commitment_compressed = Point.pointToCompressed(commitment.point);
|
|
84
|
-
const challengeData =
|
|
85
|
-
|
|
86
|
-
|
|
89
|
+
const challengeData = BufferUtil.concat([
|
|
90
|
+
R_x,
|
|
91
|
+
commitment_compressed,
|
|
92
|
+
message,
|
|
93
|
+
]);
|
|
94
|
+
const e = BN.fromBuffer(Hash.sha256(challengeData)).umod(n);
|
|
95
|
+
const tweakBN = BN.fromBuffer(tweak).umod(n);
|
|
87
96
|
const tweakTerm = e.mul(tweakBN).umod(n);
|
|
88
97
|
adjustedPartialSig = partialSig.sub(tweakTerm).umod(n);
|
|
89
98
|
}
|
|
90
|
-
return
|
|
99
|
+
return muSig2PartialSigVerify(adjustedPartialSig, publicNonce, publicKey, modifiedKeyAggContext, signerIndex, aggregatedNonce, message);
|
|
91
100
|
}
|
|
92
101
|
export function isMuSigTaprootOutput(script) {
|
|
93
|
-
return script.
|
|
102
|
+
return script.isTaprootOut();
|
|
94
103
|
}
|
|
95
|
-
export function createMuSigTaprootAddress(signerPubKeys, network
|
|
104
|
+
export function createMuSigTaprootAddress(signerPubKeys, network) {
|
|
96
105
|
if (signerPubKeys.length === 0) {
|
|
97
106
|
throw new Error('At least one signer public key is required');
|
|
98
107
|
}
|
|
@@ -101,7 +110,7 @@ export function createMuSigTaprootAddress(signerPubKeys, network, state) {
|
|
|
101
110
|
if (mismatchedKeyIndex !== -1) {
|
|
102
111
|
throw new Error(`Public key network mismatch at index ${mismatchedKeyIndex}: expected '${inferredNetwork}', got '${signerPubKeys[mismatchedKeyIndex].network.name}'`);
|
|
103
112
|
}
|
|
104
|
-
const result =
|
|
113
|
+
const result = buildMuSig2TaprootKey(signerPubKeys);
|
|
105
114
|
const address = Address.fromTaprootCommitment(result.commitment, inferredNetwork);
|
|
106
115
|
return {
|
|
107
116
|
address,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { Input, MultisigInput, MultisigScriptHashInput, PublicKeyInput, PublicKeyHashInput, TaprootInput,
|
|
2
|
-
export { Output } from './output
|
|
3
|
-
export { UnspentOutput } from './unspentoutput
|
|
4
|
-
export { sighash, sign, verify } from './sighash
|
|
5
|
-
export { TransactionSignature } from './signature
|
|
6
|
-
export { Transaction } from './transaction
|
|
1
|
+
export { Input, MultisigInput, MultisigScriptHashInput, PublicKeyInput, PublicKeyHashInput, TaprootInput, MuSig2TaprootInput, } from './input';
|
|
2
|
+
export { Output } from './output';
|
|
3
|
+
export { UnspentOutput } from './unspentoutput';
|
|
4
|
+
export { sighash, sign, verify } from './sighash';
|
|
5
|
+
export { TransactionSignature } from './signature';
|
|
6
|
+
export { Transaction } from './transaction';
|