@opcat-labs/opcat 1.0.1 → 1.0.3
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/.cjs2esm.json +18 -0
- package/.mocharc.yaml +1 -1
- package/CHANGELOG.md +12 -0
- package/README.md +6 -0
- package/{lib/address.js → cjs/address.cjs} +77 -72
- package/cjs/block/block.cjs +332 -0
- package/{lib/block/blockheader.js → cjs/block/blockheader.cjs} +8 -7
- package/cjs/block/index.cjs +2 -0
- package/{lib/block/merkleblock.js → cjs/block/merkleblock.cjs} +23 -15
- package/cjs/bn.cjs +3411 -0
- package/{lib/crypto/bn.js → cjs/crypto/bn.cjs} +3 -3
- package/{lib/crypto/ecdsa.js → cjs/crypto/ecdsa.cjs} +150 -14
- package/{lib/crypto/hash.node.js → cjs/crypto/hash.cjs} +13 -2
- package/cjs/crypto/index.cjs +16 -0
- package/{lib/crypto/point.js → cjs/crypto/point.cjs} +11 -4
- package/cjs/crypto/random.cjs +18 -0
- package/{lib/crypto/signature.js → cjs/crypto/signature.cjs} +158 -8
- package/{lib/encoding/base58.js → cjs/encoding/base58.cjs} +58 -2
- package/cjs/encoding/base58check.cjs +192 -0
- package/cjs/encoding/bufferreader.cjs +333 -0
- package/cjs/encoding/bufferwriter.cjs +244 -0
- package/{lib/encoding/decode-asm.js → cjs/encoding/decode-asm.cjs} +4 -4
- package/{lib/encoding/decode-hex.js → cjs/encoding/decode-hex.cjs} +1 -1
- package/cjs/encoding/index.cjs +14 -0
- package/cjs/encoding/varint.cjs +116 -0
- package/{lib/errors/index.js → cjs/errors/index.cjs} +9 -9
- package/{lib/errors/spec.js → cjs/errors/spec.cjs} +2 -2
- package/cjs/hash-cache.cjs +98 -0
- package/{lib/hdprivatekey.js → cjs/hdprivatekey.cjs} +232 -140
- package/{lib/hdpublickey.js → cjs/hdpublickey.cjs} +120 -93
- package/cjs/index.cjs +94 -0
- package/cjs/interpreter/index.cjs +2 -0
- package/cjs/interpreter/interpreter.cjs +1988 -0
- package/{lib/script/stack.js → cjs/interpreter/stack.cjs} +9 -2
- package/{lib/message/message.js → cjs/message/message.cjs} +62 -25
- package/cjs/mnemonic/index.cjs +3 -0
- package/{lib/mnemonic/mnemonic.js → cjs/mnemonic/mnemonic.cjs} +44 -13
- package/{lib/mnemonic/pbkdf2.node.js → cjs/mnemonic/pbkdf2.cjs} +9 -2
- package/cjs/mnemonic/words/index.cjs +66 -0
- package/cjs/network.cjs +13 -0
- package/cjs/networks.cjs +321 -0
- package/{lib/opcode.js → cjs/opcode.cjs} +69 -5
- package/cjs/privatekey.cjs +422 -0
- package/{lib/publickey.js → cjs/publickey.cjs} +14 -16
- package/cjs/script/index.cjs +2 -0
- package/{lib/script/script.js → cjs/script/script.cjs} +322 -67
- package/cjs/transaction/index.cjs +5 -0
- package/cjs/transaction/input/index.cjs +34 -0
- package/cjs/transaction/input/input.cjs +396 -0
- package/{lib/transaction/input/multisig.js → cjs/transaction/input/multisig.cjs} +112 -18
- package/{lib/transaction/input/publickey.js → cjs/transaction/input/publickey.cjs} +29 -19
- package/{lib/transaction/input/publickeyhash.js → cjs/transaction/input/publickeyhash.cjs} +25 -17
- package/{lib/transaction/output.js → cjs/transaction/output.cjs} +100 -15
- package/cjs/transaction/sighash.cjs +187 -0
- package/{lib/transaction/signature.js → cjs/transaction/signature.cjs} +30 -6
- package/cjs/transaction/transaction.cjs +2000 -0
- package/{lib/transaction/unspentoutput.js → cjs/transaction/unspentoutput.cjs} +5 -5
- package/cjs/util/derivation.cjs +53 -0
- package/cjs/util/index.cjs +11 -0
- package/cjs/util/js.cjs +95 -0
- package/{lib/util/preconditions.js → cjs/util/preconditions.cjs} +2 -2
- package/esm/address.js +483 -0
- package/{lib → esm}/block/block.js +82 -27
- package/esm/block/blockheader.js +296 -0
- package/esm/block/index.js +2 -0
- package/esm/block/merkleblock.js +331 -0
- package/esm/bn.js +3411 -0
- package/esm/crypto/bn.js +278 -0
- package/esm/crypto/ecdsa.js +475 -0
- package/{lib/crypto/hash.browser.js → esm/crypto/hash.js} +18 -7
- package/esm/crypto/index.js +16 -0
- package/esm/crypto/point.js +228 -0
- package/esm/crypto/random.js +18 -0
- package/esm/crypto/signature.js +475 -0
- package/esm/encoding/base58.js +167 -0
- package/esm/encoding/base58check.js +192 -0
- package/esm/encoding/bufferreader.js +333 -0
- package/esm/encoding/bufferwriter.js +243 -0
- package/esm/encoding/decode-asm.js +24 -0
- package/esm/encoding/decode-hex.js +32 -0
- package/esm/encoding/decode-script-chunks.js +43 -0
- package/esm/encoding/encode-hex.js +284 -0
- package/esm/encoding/index.js +14 -0
- package/esm/encoding/is-hex.js +7 -0
- package/esm/encoding/varint.js +116 -0
- package/esm/errors/index.js +54 -0
- package/esm/errors/spec.js +314 -0
- package/esm/hash-cache.js +98 -0
- package/esm/hdprivatekey.js +768 -0
- package/esm/hdpublickey.js +549 -0
- package/esm/index.js +66 -0
- package/esm/interpreter/index.js +2 -0
- package/{lib/script → esm/interpreter}/interpreter.js +219 -66
- package/esm/interpreter/stack.js +116 -0
- package/esm/message/message.js +228 -0
- package/esm/mnemonic/index.js +3 -0
- package/esm/mnemonic/mnemonic.js +332 -0
- package/{lib/mnemonic/pbkdf2.browser.js → esm/mnemonic/pbkdf2.js} +13 -6
- package/esm/mnemonic/words/chinese.js +2054 -0
- package/esm/mnemonic/words/english.js +2054 -0
- package/esm/mnemonic/words/french.js +2054 -0
- package/esm/mnemonic/words/index.js +66 -0
- package/esm/mnemonic/words/italian.js +2054 -0
- package/esm/mnemonic/words/japanese.js +2054 -0
- package/esm/mnemonic/words/spanish.js +2054 -0
- package/esm/network.js +13 -0
- package/{lib → esm}/networks.js +61 -120
- package/esm/opcode.js +319 -0
- package/{lib → esm}/privatekey.js +76 -28
- package/esm/publickey.js +384 -0
- package/esm/script/index.js +2 -0
- package/esm/script/script.js +1329 -0
- package/esm/script/write-i32-le.js +17 -0
- package/esm/script/write-push-data.js +35 -0
- package/esm/script/write-u16-le.js +12 -0
- package/esm/script/write-u32-le.js +16 -0
- package/esm/script/write-u64-le.js +24 -0
- package/esm/script/write-u8-le.js +8 -0
- package/esm/script/write-varint.js +46 -0
- package/esm/transaction/index.js +5 -0
- package/esm/transaction/input/index.js +33 -0
- package/{lib → esm}/transaction/input/input.js +132 -90
- package/esm/transaction/input/multisig.js +335 -0
- package/esm/transaction/input/publickey.js +108 -0
- package/esm/transaction/input/publickeyhash.js +124 -0
- package/esm/transaction/output.js +316 -0
- package/{lib → esm}/transaction/sighash.js +42 -22
- package/esm/transaction/signature.js +120 -0
- package/{lib → esm}/transaction/transaction.js +522 -163
- package/esm/transaction/unspentoutput.js +112 -0
- package/esm/util/_.js +47 -0
- package/esm/util/derivation.js +53 -0
- package/esm/util/index.js +12 -0
- package/esm/util/js.js +95 -0
- package/esm/util/preconditions.js +33 -0
- package/fixup.cjs +17 -0
- package/package.json +18 -4
- package/test/{address.js → address.cjs} +14 -43
- package/test/block/{block.js → block.cjs} +3 -5
- package/test/block/{blockheader.js → blockheader.cjs} +2 -2
- package/test/block/{merklebloack.js → merklebloack.cjs} +2 -2
- package/test/crypto/{ecdsa.js → ecdsa.cjs} +9 -9
- package/test/crypto/{hash.browser.js → hash.browser.cjs} +2 -1
- package/test/crypto/{signature.js → signature.cjs} +2 -2
- package/test/data/bitcoind/script_tests.json +5 -5
- package/test/{hashCache.js → hashCache.cjs} +2 -1
- package/test/{hdkeys.js → hdkeys.cjs} +4 -2
- package/test/{hdprivatekey.js → hdprivatekey.cjs} +7 -6
- package/test/{hdpublickey.js → hdpublickey.cjs} +2 -7
- package/test/mnemonic/{pbkdf2.test.js → pbkdf2.test.cjs} +2 -2
- package/test/{networks.js → networks.cjs} +12 -31
- package/test/{publickey.js → publickey.cjs} +2 -2
- package/test/script/{interpreter.js → interpreter.cjs} +5 -5
- package/test/script/{script.js → script.cjs} +8 -2
- package/test/transaction/{deserialize.js → deserialize.cjs} +2 -2
- package/test/transaction/input/{input.js → input.cjs} +1 -1
- package/test/transaction/input/{multisig.js → multisig.cjs} +2 -1
- package/test/transaction/input/{publickeyhash.js → publickeyhash.cjs} +1 -1
- package/test/transaction/{sighash.js → sighash.cjs} +1 -1
- package/test/transaction/{transaction.js → transaction.cjs} +2 -2
- package/tsconfig.json +13 -0
- package/types/address.d.cts +252 -0
- package/types/block/block.d.cts +139 -0
- package/types/block/blockheader.d.cts +125 -0
- package/types/block/index.d.cts +2 -0
- package/types/block/merkleblock.d.cts +95 -0
- package/types/bn.d.cts +202 -0
- package/types/crypto/bn.d.cts +2 -0
- package/types/crypto/ecdsa.d.cts +187 -0
- package/types/crypto/hash.d.cts +118 -0
- package/types/crypto/index.d.cts +7 -0
- package/types/crypto/point.d.cts +134 -0
- package/types/crypto/random.d.cts +13 -0
- package/types/crypto/signature.d.cts +160 -0
- package/types/encoding/base58.d.cts +106 -0
- package/types/encoding/base58check.d.cts +107 -0
- package/types/encoding/bufferreader.d.cts +164 -0
- package/types/encoding/bufferwriter.d.cts +126 -0
- package/types/encoding/decode-asm.d.cts +2 -0
- package/types/encoding/decode-hex.d.cts +2 -0
- package/types/encoding/decode-script-chunks.d.cts +14 -0
- package/types/encoding/encode-hex.d.cts +2 -0
- package/types/encoding/index.d.cts +6 -0
- package/types/encoding/is-hex.d.cts +2 -0
- package/types/encoding/varint.d.cts +66 -0
- package/types/errors/index.d.cts +4 -0
- package/types/errors/spec.d.cts +22 -0
- package/types/hash-cache.d.cts +65 -0
- package/types/hdprivatekey.d.cts +281 -0
- package/types/hdpublickey.d.cts +240 -0
- package/types/index.d.cts +26 -0
- package/types/interpreter/index.d.cts +2 -0
- package/types/interpreter/interpreter.d.cts +228 -0
- package/types/interpreter/stack.d.cts +35 -0
- package/types/message/message.d.cts +110 -0
- package/types/mnemonic/index.d.cts +2 -0
- package/types/mnemonic/mnemonic.d.cts +171 -0
- package/types/mnemonic/pbkdf2.d.cts +14 -0
- package/types/mnemonic/words/chinese.d.cts +2 -0
- package/types/mnemonic/words/english.d.cts +2 -0
- package/types/mnemonic/words/french.d.cts +2 -0
- package/types/mnemonic/words/index.d.cts +22 -0
- package/types/mnemonic/words/italian.d.cts +2 -0
- package/types/mnemonic/words/japanese.d.cts +2 -0
- package/types/mnemonic/words/spanish.d.cts +2 -0
- package/types/network.d.cts +11 -0
- package/types/networks.d.cts +76 -0
- package/types/opcode.d.cts +93 -0
- package/types/privatekey.d.cts +169 -0
- package/types/publickey.d.cts +202 -0
- package/types/script/index.d.cts +2 -0
- package/types/script/script.d.cts +449 -0
- package/types/script/write-i32-le.d.cts +2 -0
- package/types/script/write-push-data.d.cts +2 -0
- package/types/script/write-u16-le.d.cts +2 -0
- package/types/script/write-u32-le.d.cts +2 -0
- package/types/script/write-u64-le.d.cts +2 -0
- package/types/script/write-u8-le.d.cts +2 -0
- package/types/script/write-varint.d.cts +2 -0
- package/types/transaction/index.d.cts +2 -0
- package/types/transaction/input/index.d.cts +2 -0
- package/types/transaction/input/input.d.cts +178 -0
- package/types/transaction/input/multisig.d.cts +127 -0
- package/types/transaction/input/publickey.d.cts +44 -0
- package/types/transaction/input/publickeyhash.d.cts +45 -0
- package/types/transaction/output.d.cts +118 -0
- package/types/transaction/sighash.d.cts +61 -0
- package/types/transaction/signature.d.cts +43 -0
- package/types/transaction/transaction.d.cts +716 -0
- package/types/transaction/unspentoutput.d.cts +83 -0
- package/types/util/_.d.cts +26 -0
- package/types/util/derivation.d.cts +21 -0
- package/types/util/index.d.cts +5 -0
- package/types/util/js.d.cts +50 -0
- package/types/util/preconditions.d.cts +3 -0
- package/index.d.ts +0 -1541
- package/index.js +0 -74
- package/lib/block/index.js +0 -4
- package/lib/bn.js +0 -3423
- package/lib/crypto/hash.js +0 -2
- package/lib/crypto/random.browser.js +0 -28
- package/lib/crypto/random.js +0 -2
- package/lib/crypto/random.node.js +0 -11
- package/lib/encoding/base58check.js +0 -121
- package/lib/encoding/bufferreader.js +0 -212
- package/lib/encoding/bufferwriter.js +0 -140
- package/lib/encoding/varint.js +0 -75
- package/lib/hash-cache.js +0 -50
- package/lib/mnemonic/pbkdf2.js +0 -2
- package/lib/mnemonic/words/index.js +0 -8
- package/lib/script/index.js +0 -5
- package/lib/transaction/index.js +0 -7
- package/lib/transaction/input/index.js +0 -5
- package/lib/util/js.js +0 -90
- /package/{lib/encoding/decode-script-chunks.js → cjs/encoding/decode-script-chunks.cjs} +0 -0
- /package/{lib/encoding/encode-hex.js → cjs/encoding/encode-hex.cjs} +0 -0
- /package/{lib/encoding/is-hex.js → cjs/encoding/is-hex.cjs} +0 -0
- /package/{lib/mnemonic/words/chinese.js → cjs/mnemonic/words/chinese.cjs} +0 -0
- /package/{lib/mnemonic/words/english.js → cjs/mnemonic/words/english.cjs} +0 -0
- /package/{lib/mnemonic/words/french.js → cjs/mnemonic/words/french.cjs} +0 -0
- /package/{lib/mnemonic/words/italian.js → cjs/mnemonic/words/italian.cjs} +0 -0
- /package/{lib/mnemonic/words/japanese.js → cjs/mnemonic/words/japanese.cjs} +0 -0
- /package/{lib/mnemonic/words/spanish.js → cjs/mnemonic/words/spanish.cjs} +0 -0
- /package/{lib/script/write-i32-le.js → cjs/script/write-i32-le.cjs} +0 -0
- /package/{lib/script/write-push-data.js → cjs/script/write-push-data.cjs} +0 -0
- /package/{lib/script/write-u16-le.js → cjs/script/write-u16-le.cjs} +0 -0
- /package/{lib/script/write-u32-le.js → cjs/script/write-u32-le.cjs} +0 -0
- /package/{lib/script/write-u64-le.js → cjs/script/write-u64-le.cjs} +0 -0
- /package/{lib/script/write-u8-le.js → cjs/script/write-u8-le.cjs} +0 -0
- /package/{lib/script/write-varint.js → cjs/script/write-varint.cjs} +0 -0
- /package/{lib/util/_.js → cjs/util/_.cjs} +0 -0
- /package/test/crypto/{bn.js → bn.cjs} +0 -0
- /package/test/crypto/{hash.js → hash.cjs} +0 -0
- /package/test/crypto/{point.js → point.cjs} +0 -0
- /package/test/crypto/{random.js → random.cjs} +0 -0
- /package/test/data/{blk86756-testnet.js → blk86756-testnet.cjs} +0 -0
- /package/test/data/{merkleblocks.js → merkleblocks.cjs} +0 -0
- /package/test/encoding/{base58.js → base58.cjs} +0 -0
- /package/test/encoding/{base58check.js → base58check.cjs} +0 -0
- /package/test/encoding/{bufferreader.js → bufferreader.cjs} +0 -0
- /package/test/encoding/{bufferwriter.js → bufferwriter.cjs} +0 -0
- /package/test/encoding/{varint.js → varint.cjs} +0 -0
- /package/test/{index.js → index.cjs} +0 -0
- /package/test/message/{message.js → message.cjs} +0 -0
- /package/test/mnemonic/{mnemonic.js → mnemonic.cjs} +0 -0
- /package/test/{opcode.js → opcode.cjs} +0 -0
- /package/test/{privatekey.js → privatekey.cjs} +0 -0
- /package/test/transaction/input/{publickey.js → publickey.cjs} +0 -0
- /package/test/transaction/{output.js → output.cjs} +0 -0
- /package/test/transaction/{signature.js → signature.cjs} +0 -0
- /package/test/transaction/{unspentoutput.js → unspentoutput.cjs} +0 -0
- /package/test/util/{js.js → js.cjs} +0 -0
- /package/test/util/{preconditions.js → preconditions.cjs} +0 -0
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var Address = require('../address');
|
|
4
|
-
var BufferWriter = require('../encoding/bufferwriter');
|
|
5
|
-
var Hash = require('../crypto/hash');
|
|
6
|
-
var Opcode = require('../opcode');
|
|
7
|
-
var PublicKey = require('../publickey');
|
|
8
|
-
var Signature = require('../crypto/signature');
|
|
9
|
-
var Networks = require('../networks');
|
|
10
|
-
var $ = require('../util/preconditions');
|
|
11
|
-
var _ = require('../util/_');
|
|
12
|
-
var errors = require('../errors');
|
|
13
|
-
var JSUtil = require('../util/js');
|
|
14
|
-
const decodeScriptChunks = require('../encoding/decode-script-chunks');
|
|
15
|
-
const decodeASM = require('../encoding/decode-asm');
|
|
16
|
-
const encodeHex = require('../encoding/encode-hex');
|
|
3
|
+
var Address = require('../address.cjs');
|
|
4
|
+
var BufferWriter = require('../encoding/bufferwriter.cjs');
|
|
5
|
+
var Hash = require('../crypto/hash.cjs');
|
|
6
|
+
var Opcode = require('../opcode.cjs');
|
|
7
|
+
var PublicKey = require('../publickey.cjs');
|
|
8
|
+
var Signature = require('../crypto/signature.cjs');
|
|
9
|
+
var Networks = require('../networks.cjs');
|
|
10
|
+
var $ = require('../util/preconditions.cjs');
|
|
11
|
+
var _ = require('../util/_.cjs');
|
|
12
|
+
var errors = require('../errors/index.cjs');
|
|
13
|
+
var JSUtil = require('../util/js.cjs');
|
|
14
|
+
const decodeScriptChunks = require('../encoding/decode-script-chunks.cjs');
|
|
15
|
+
const decodeASM = require('../encoding/decode-asm.cjs');
|
|
16
|
+
const encodeHex = require('../encoding/encode-hex.cjs');
|
|
17
17
|
|
|
18
18
|
// These WeakMap caches allow the objects themselves to maintain their immutability
|
|
19
19
|
const SCRIPT_TO_CHUNKS_CACHE = new WeakMap();
|
|
@@ -25,9 +25,9 @@ const SCRIPT_TO_CHUNKS_CACHE = new WeakMap();
|
|
|
25
25
|
* See https://en.bitcoin.it/wiki/Script
|
|
26
26
|
*
|
|
27
27
|
* @constructor
|
|
28
|
-
* @param {Object|string|Buffer
|
|
28
|
+
* @param {Object|string|Buffer} [from] optional data to populate script
|
|
29
29
|
*/
|
|
30
|
-
|
|
30
|
+
function Script(from) {
|
|
31
31
|
if (!(this instanceof Script)) {
|
|
32
32
|
return new Script(from);
|
|
33
33
|
}
|
|
@@ -48,6 +48,14 @@ var Script = function Script(from) {
|
|
|
48
48
|
}
|
|
49
49
|
};
|
|
50
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Sets the script content from an object.
|
|
53
|
+
* @param {Object} obj - The source object containing either chunks array or buffer.
|
|
54
|
+
* @param {Array} [obj.chunks] - Optional array of chunks to create script from.
|
|
55
|
+
* @param {Buffer} [obj.buffer] - Optional buffer containing script data.
|
|
56
|
+
* @returns {Script} Returns the script instance for chaining.
|
|
57
|
+
* @throws Will throw if argument is invalid (not object or missing required buffer).
|
|
58
|
+
*/
|
|
51
59
|
Script.prototype.set = function (obj) {
|
|
52
60
|
$.checkArgument(_.isObject(obj));
|
|
53
61
|
if (obj.chunks && _.isArray(obj.chunks)) {
|
|
@@ -61,6 +69,12 @@ Script.prototype.set = function (obj) {
|
|
|
61
69
|
return this;
|
|
62
70
|
};
|
|
63
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Creates a Script instance from a Buffer.
|
|
74
|
+
* @param {Buffer} buffer - The buffer containing the script data.
|
|
75
|
+
* @returns {Script} A new Script instance with the provided buffer.
|
|
76
|
+
* @throws {Error} Throws if the input is not a Buffer.
|
|
77
|
+
*/
|
|
64
78
|
Script.fromBuffer = function (buffer) {
|
|
65
79
|
$.checkArgument(Buffer.isBuffer(buffer));
|
|
66
80
|
var script = new Script();
|
|
@@ -68,6 +82,13 @@ Script.fromBuffer = function (buffer) {
|
|
|
68
82
|
return script;
|
|
69
83
|
};
|
|
70
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Creates a Script instance from an array of opcode chunks.
|
|
87
|
+
* Handles different pushdata opcodes (OP_PUSHDATA1, OP_PUSHDATA2, OP_PUSHDATA4)
|
|
88
|
+
* by writing appropriate length prefixes before the buffer data.
|
|
89
|
+
* @param {Array} chunks - Array of opcode chunks containing opcodenum and optional buf/len
|
|
90
|
+
* @returns {Script} A new Script instance with compiled buffer
|
|
91
|
+
*/
|
|
71
92
|
Script.fromChunks = function (chunks) {
|
|
72
93
|
var script = new Script();
|
|
73
94
|
|
|
@@ -96,18 +117,41 @@ Script.fromChunks = function (chunks) {
|
|
|
96
117
|
return script;
|
|
97
118
|
};
|
|
98
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Returns the underlying buffer of the script.
|
|
122
|
+
* @returns {Buffer} The script's buffer data.
|
|
123
|
+
*/
|
|
99
124
|
Script.prototype.toBuffer = function () {
|
|
100
125
|
return this.buffer;
|
|
101
126
|
};
|
|
102
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Creates a Script instance from ASM (Assembly) formatted string.
|
|
130
|
+
* @param {string} str - ASM formatted string to decode
|
|
131
|
+
* @returns {Script} Script instance created from decoded ASM
|
|
132
|
+
*/
|
|
103
133
|
Script.fromASM = function (str) {
|
|
104
134
|
return Script.fromBuffer(decodeASM(str));
|
|
105
135
|
};
|
|
106
136
|
|
|
137
|
+
/**
|
|
138
|
+
* Creates a Script instance from a hex string.
|
|
139
|
+
* @param {string} str - Hex string to convert to Script.
|
|
140
|
+
* @returns {Script} New Script instance created from the hex string.
|
|
141
|
+
*/
|
|
107
142
|
Script.fromHex = function (str) {
|
|
108
143
|
return new Script(Buffer.from(str, 'hex'));
|
|
109
144
|
};
|
|
110
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Converts a string representation of a script into a Script object.
|
|
148
|
+
* Handles hex strings, empty strings, and space-separated opcode tokens.
|
|
149
|
+
* For pushdata operations (OP_PUSHDATA1/2/4), validates format and length.
|
|
150
|
+
* Throws errors for invalid script formats or data lengths.
|
|
151
|
+
* @param {string} str - The script string to parse (hex or opcode tokens)
|
|
152
|
+
* @returns {Script} The constructed Script object
|
|
153
|
+
* @throws {Error} When script format is invalid or data lengths don't match
|
|
154
|
+
*/
|
|
111
155
|
Script.fromString = function (str) {
|
|
112
156
|
if (JSUtil.isHexa(str) || str.length === 0) {
|
|
113
157
|
return new Script(Buffer.from(str, 'hex'));
|
|
@@ -162,10 +206,22 @@ Script.fromString = function (str) {
|
|
|
162
206
|
return Script.fromChunks(chunks);
|
|
163
207
|
};
|
|
164
208
|
|
|
209
|
+
/**
|
|
210
|
+
* Gets a portion of the script's buffer as a new buffer.
|
|
211
|
+
* @param {number} [start] - The beginning index of the specified portion of the buffer.
|
|
212
|
+
* @param {number} [end] - The end index of the specified portion of the buffer.
|
|
213
|
+
* @returns {Buffer} A new Buffer that contains the specified portion of the original buffer.
|
|
214
|
+
*/
|
|
165
215
|
Script.prototype.slice = function (start, end) {
|
|
166
216
|
return this.buffer.slice(start, end);
|
|
167
217
|
};
|
|
168
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Gets the chunks associated with the Script instance.
|
|
221
|
+
* @memberof Script.prototype
|
|
222
|
+
* @name chunks
|
|
223
|
+
* @type {Array}
|
|
224
|
+
*/
|
|
169
225
|
Object.defineProperty(Script.prototype, 'chunks', {
|
|
170
226
|
get() {
|
|
171
227
|
if (SCRIPT_TO_CHUNKS_CACHE.has(this)) return SCRIPT_TO_CHUNKS_CACHE.get(this);
|
|
@@ -175,12 +231,27 @@ Object.defineProperty(Script.prototype, 'chunks', {
|
|
|
175
231
|
},
|
|
176
232
|
});
|
|
177
233
|
|
|
234
|
+
/**
|
|
235
|
+
* Gets the length of the script in bytes.
|
|
236
|
+
* @memberof Script.prototype
|
|
237
|
+
* @name length
|
|
238
|
+
* @type {number}
|
|
239
|
+
*/
|
|
178
240
|
Object.defineProperty(Script.prototype, 'length', {
|
|
179
241
|
get() {
|
|
180
242
|
return this.buffer.length;
|
|
181
243
|
},
|
|
182
244
|
});
|
|
183
245
|
|
|
246
|
+
/**
|
|
247
|
+
* Converts a script chunk to a string representation based on the given type.
|
|
248
|
+
* Handles both data chunks and opcode chunks, with special formatting for ASM output.
|
|
249
|
+
*
|
|
250
|
+
* @param {Object} chunk - The script chunk to convert, containing opcodenum and optional buf/len
|
|
251
|
+
* @param {string} type - The output type ('asm' or other)
|
|
252
|
+
* @returns {string} The formatted string representation of the chunk
|
|
253
|
+
* @private
|
|
254
|
+
*/
|
|
184
255
|
Script.prototype._chunkToString = function (chunk, type) {
|
|
185
256
|
var opcodenum = chunk.opcodenum;
|
|
186
257
|
var asm = type === 'asm';
|
|
@@ -235,6 +306,11 @@ Script.prototype._chunkToString = function (chunk, type) {
|
|
|
235
306
|
return str;
|
|
236
307
|
};
|
|
237
308
|
|
|
309
|
+
/**
|
|
310
|
+
* Converts the script chunks to ASM (Assembly) format string representation.
|
|
311
|
+
* Iterates through each chunk and appends its ASM string representation.
|
|
312
|
+
* @returns {string} The ASM formatted string (excluding the first character).
|
|
313
|
+
*/
|
|
238
314
|
Script.prototype.toASM = function () {
|
|
239
315
|
var str = '';
|
|
240
316
|
var chunks = this.chunks;
|
|
@@ -246,6 +322,12 @@ Script.prototype.toASM = function () {
|
|
|
246
322
|
return str.substr(1);
|
|
247
323
|
};
|
|
248
324
|
|
|
325
|
+
/**
|
|
326
|
+
* Converts the script's chunks to a string representation.
|
|
327
|
+
* Iterates through each chunk and appends its string representation,
|
|
328
|
+
* then removes the leading character from the result.
|
|
329
|
+
* @returns {string} The concatenated string of all chunks.
|
|
330
|
+
*/
|
|
249
331
|
Script.prototype.toString = function () {
|
|
250
332
|
var str = '';
|
|
251
333
|
for (var i = 0; i < this.chunks.length; i++) {
|
|
@@ -256,18 +338,33 @@ Script.prototype.toString = function () {
|
|
|
256
338
|
return str.substr(1);
|
|
257
339
|
};
|
|
258
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Converts the script's buffer to a hexadecimal string.
|
|
343
|
+
* @returns {string} Hex-encoded representation of the script buffer.
|
|
344
|
+
*/
|
|
259
345
|
Script.prototype.toHex = function () {
|
|
260
346
|
return encodeHex(this.buffer);
|
|
261
347
|
};
|
|
262
348
|
|
|
349
|
+
/**
|
|
350
|
+
* Custom inspect method for Script instances.
|
|
351
|
+
* @returns {string} String representation of the Script object in format '<Script: [content]>'.
|
|
352
|
+
*/
|
|
263
353
|
Script.prototype.inspect = function () {
|
|
264
354
|
return '<Script: ' + this.toString() + '>';
|
|
265
355
|
};
|
|
266
356
|
|
|
267
357
|
// script classification methods
|
|
268
358
|
|
|
359
|
+
|
|
269
360
|
/**
|
|
270
|
-
*
|
|
361
|
+
* Checks if the script is a standard public key hash output script (P2PKH).
|
|
362
|
+
* @returns {boolean} True if the script matches the P2PKH pattern:
|
|
363
|
+
* - OP_DUP
|
|
364
|
+
* - OP_HASH160
|
|
365
|
+
* - 20-byte hash
|
|
366
|
+
* - OP_EQUALVERIFY
|
|
367
|
+
* - OP_CHECKSIG
|
|
271
368
|
*/
|
|
272
369
|
Script.prototype.isPublicKeyHashOut = function () {
|
|
273
370
|
return !!(
|
|
@@ -281,8 +378,12 @@ Script.prototype.isPublicKeyHashOut = function () {
|
|
|
281
378
|
);
|
|
282
379
|
};
|
|
283
380
|
|
|
381
|
+
|
|
284
382
|
/**
|
|
285
|
-
*
|
|
383
|
+
* Checks if the script contains a valid public key hash.
|
|
384
|
+
* @returns {boolean} True if the script has exactly 2 chunks (signature and public key),
|
|
385
|
+
* the signature starts with 0x30, and the public key has a valid version
|
|
386
|
+
* and length (65 bytes for versions 0x04/0x06/0x07, 33 bytes for 0x02/0x03).
|
|
286
387
|
*/
|
|
287
388
|
Script.prototype.isPublicKeyHashIn = function () {
|
|
288
389
|
if (this.chunks.length === 2) {
|
|
@@ -306,18 +407,33 @@ Script.prototype.isPublicKeyHashIn = function () {
|
|
|
306
407
|
return false;
|
|
307
408
|
};
|
|
308
409
|
|
|
410
|
+
/**
|
|
411
|
+
* Gets the public key from a script output.
|
|
412
|
+
* @returns {Buffer} The public key buffer.
|
|
413
|
+
* @throws {Error} If the script is not a public key output.
|
|
414
|
+
*/
|
|
309
415
|
Script.prototype.getPublicKey = function () {
|
|
310
416
|
$.checkState(this.isPublicKeyOut(), "Can't retrieve PublicKey from a non-PK output");
|
|
311
417
|
return this.chunks[0].buf;
|
|
312
418
|
};
|
|
313
419
|
|
|
420
|
+
/**
|
|
421
|
+
* Retrieves the PublicKeyHash from a script output.
|
|
422
|
+
* @returns {Buffer} The PublicKeyHash buffer.
|
|
423
|
+
* @throws {Error} If the script output is not a PublicKeyHash output.
|
|
424
|
+
*/
|
|
314
425
|
Script.prototype.getPublicKeyHash = function () {
|
|
315
426
|
$.checkState(this.isPublicKeyHashOut(), "Can't retrieve PublicKeyHash from a non-PKH output");
|
|
316
427
|
return this.chunks[2].buf;
|
|
317
428
|
};
|
|
318
429
|
|
|
430
|
+
|
|
319
431
|
/**
|
|
320
|
-
*
|
|
432
|
+
* Checks if the script is a standard public key output script.
|
|
433
|
+
* @returns {boolean} True if the script matches the standard public key output format:
|
|
434
|
+
* - Contains exactly 2 chunks
|
|
435
|
+
* - First chunk is a valid public key buffer (65 bytes for uncompressed, 33 bytes for compressed)
|
|
436
|
+
* - Second chunk is OP_CHECKSIG opcode
|
|
321
437
|
*/
|
|
322
438
|
Script.prototype.isPublicKeyOut = function () {
|
|
323
439
|
if (
|
|
@@ -341,8 +457,10 @@ Script.prototype.isPublicKeyOut = function () {
|
|
|
341
457
|
return false;
|
|
342
458
|
};
|
|
343
459
|
|
|
460
|
+
|
|
344
461
|
/**
|
|
345
|
-
*
|
|
462
|
+
* Checks if the script contains a valid public key signature.
|
|
463
|
+
* @returns {boolean} True if the script has exactly one chunk that starts with 0x30 (DER signature marker), false otherwise.
|
|
346
464
|
*/
|
|
347
465
|
Script.prototype.isPublicKeyIn = function () {
|
|
348
466
|
if (this.chunks.length === 1) {
|
|
@@ -354,8 +472,15 @@ Script.prototype.isPublicKeyIn = function () {
|
|
|
354
472
|
return false;
|
|
355
473
|
};
|
|
356
474
|
|
|
475
|
+
|
|
357
476
|
/**
|
|
358
|
-
*
|
|
477
|
+
* Checks if the script is a multisig output script.
|
|
478
|
+
* @returns {boolean} True if the script matches the multisig output pattern:
|
|
479
|
+
* - Has more than 3 chunks
|
|
480
|
+
* - First chunk is a small integer opcode
|
|
481
|
+
* - Middle chunks are all buffers
|
|
482
|
+
* - Second-to-last chunk is a small integer opcode
|
|
483
|
+
* - Last chunk is OP_CHECKMULTISIG
|
|
359
484
|
*/
|
|
360
485
|
Script.prototype.isMultisigOut = function () {
|
|
361
486
|
return (
|
|
@@ -391,8 +516,10 @@ Script.prototype.decodeMultisigOut = function () {
|
|
|
391
516
|
};
|
|
392
517
|
};
|
|
393
518
|
|
|
519
|
+
|
|
394
520
|
/**
|
|
395
|
-
*
|
|
521
|
+
* Checks if the script is a multisig input script.
|
|
522
|
+
* @returns {boolean} True if the script is a valid multisig input (starts with OP_0 and has valid DER signatures).
|
|
396
523
|
*/
|
|
397
524
|
Script.prototype.isMultisigIn = function () {
|
|
398
525
|
return (
|
|
@@ -404,8 +531,10 @@ Script.prototype.isMultisigIn = function () {
|
|
|
404
531
|
);
|
|
405
532
|
};
|
|
406
533
|
|
|
534
|
+
|
|
407
535
|
/**
|
|
408
|
-
*
|
|
536
|
+
* Checks if the script is a data-only output script (OP_RETURN followed by push-only data).
|
|
537
|
+
* @returns {boolean} True if the script is a valid data-only output, false otherwise.
|
|
409
538
|
*/
|
|
410
539
|
Script.prototype.isDataOut = function () {
|
|
411
540
|
var step1 = this.buffer.length >= 1 && this.buffer[0] === Opcode.OP_RETURN;
|
|
@@ -415,6 +544,11 @@ Script.prototype.isDataOut = function () {
|
|
|
415
544
|
return script2.isPushOnly();
|
|
416
545
|
};
|
|
417
546
|
|
|
547
|
+
/**
|
|
548
|
+
* Checks if the script is a safe data output script.
|
|
549
|
+
* A safe data output script must start with OP_FALSE followed by a valid data output script.
|
|
550
|
+
* @returns {boolean} True if the script is a safe data output, false otherwise.
|
|
551
|
+
*/
|
|
418
552
|
Script.prototype.isSafeDataOut = function () {
|
|
419
553
|
if (this.buffer.length < 2) {
|
|
420
554
|
return false;
|
|
@@ -453,9 +587,10 @@ Script.prototype.getData = function () {
|
|
|
453
587
|
throw new Error('Unrecognized script type to get data from');
|
|
454
588
|
};
|
|
455
589
|
|
|
590
|
+
|
|
456
591
|
/**
|
|
457
|
-
*
|
|
458
|
-
*
|
|
592
|
+
* Checks if the script consists only of push operations (OP_0 to OP_16) or data push operations (OP_PUSHDATA1/2/4).
|
|
593
|
+
* @returns {boolean} True if all chunks are push operations, false otherwise.
|
|
459
594
|
*/
|
|
460
595
|
Script.prototype.isPushOnly = function () {
|
|
461
596
|
return _.every(this.chunks, function (chunk) {
|
|
@@ -468,20 +603,66 @@ Script.prototype.isPushOnly = function () {
|
|
|
468
603
|
});
|
|
469
604
|
};
|
|
470
605
|
|
|
606
|
+
/**
|
|
607
|
+
* Registry for script types.
|
|
608
|
+
* @namespace
|
|
609
|
+
*/
|
|
471
610
|
Script.types = {};
|
|
611
|
+
/**
|
|
612
|
+
* Represents the 'Unknown' type constant for script types.
|
|
613
|
+
* @type {string}
|
|
614
|
+
*/
|
|
472
615
|
Script.types.UNKNOWN = 'Unknown';
|
|
616
|
+
/**
|
|
617
|
+
* Defines the display text for the public key output script type.
|
|
618
|
+
* @type {string}
|
|
619
|
+
*/
|
|
473
620
|
Script.types.PUBKEY_OUT = 'Pay to public key';
|
|
621
|
+
/**
|
|
622
|
+
* Script type constant for spending from a public key input.
|
|
623
|
+
* @type {string}
|
|
624
|
+
*/
|
|
474
625
|
Script.types.PUBKEY_IN = 'Spend from public key';
|
|
626
|
+
/**
|
|
627
|
+
* Script type constant for Pay to Public Key Hash (P2PKH) output.
|
|
628
|
+
* @type {string}
|
|
629
|
+
*/
|
|
475
630
|
Script.types.PUBKEYHASH_OUT = 'Pay to public key hash';
|
|
631
|
+
/**
|
|
632
|
+
* Constant defining the display string for public key hash input script type.
|
|
633
|
+
* @type {string}
|
|
634
|
+
*/
|
|
476
635
|
Script.types.PUBKEYHASH_IN = 'Spend from public key hash';
|
|
636
|
+
/**
|
|
637
|
+
* Script type constant for Pay to Script Hash (P2SH) output.
|
|
638
|
+
* @type {string}
|
|
639
|
+
*/
|
|
477
640
|
Script.types.SCRIPTHASH_OUT = 'Pay to script hash';
|
|
641
|
+
/**
|
|
642
|
+
* Constant defining the string representation for spending from a script hash input type.
|
|
643
|
+
* @type {string}
|
|
644
|
+
*/
|
|
478
645
|
Script.types.SCRIPTHASH_IN = 'Spend from script hash';
|
|
646
|
+
/**
|
|
647
|
+
* Multisig output script type identifier.
|
|
648
|
+
* @type {string}
|
|
649
|
+
*/
|
|
479
650
|
Script.types.MULTISIG_OUT = 'Pay to multisig';
|
|
651
|
+
/**
|
|
652
|
+
* Multisig input script type - represents spending from a multisig address.
|
|
653
|
+
*/
|
|
480
654
|
Script.types.MULTISIG_IN = 'Spend from multisig';
|
|
655
|
+
/**
|
|
656
|
+
* Constant defining the type string for data output operations.
|
|
657
|
+
* @type {string}
|
|
658
|
+
*/
|
|
481
659
|
Script.types.DATA_OUT = 'Data push';
|
|
660
|
+
/**
|
|
661
|
+
* Constant defining the type string for safe data output operations.
|
|
662
|
+
* @type {string}
|
|
663
|
+
*/
|
|
482
664
|
Script.types.SAFE_DATA_OUT = 'Safe data push';
|
|
483
665
|
|
|
484
|
-
Script.OP_RETURN_STANDARD_SIZE = 220;
|
|
485
666
|
|
|
486
667
|
/**
|
|
487
668
|
* @returns {object} The Script type if it is a known form,
|
|
@@ -556,8 +737,12 @@ Script.prototype.prepend = function (obj) {
|
|
|
556
737
|
return this;
|
|
557
738
|
};
|
|
558
739
|
|
|
740
|
+
|
|
559
741
|
/**
|
|
560
|
-
* Compares
|
|
742
|
+
* Compares this script with another script for equality.
|
|
743
|
+
* @param {Script} script - The script to compare with.
|
|
744
|
+
* @returns {boolean} True if the scripts have identical buffer contents, false otherwise.
|
|
745
|
+
* @throws {Error} If the provided argument is not a Script instance.
|
|
561
746
|
*/
|
|
562
747
|
Script.prototype.equals = function (script) {
|
|
563
748
|
$.checkState(script instanceof Script, 'Must provide another script');
|
|
@@ -573,18 +758,25 @@ Script.prototype.equals = function (script) {
|
|
|
573
758
|
return true;
|
|
574
759
|
};
|
|
575
760
|
|
|
761
|
+
|
|
576
762
|
/**
|
|
577
763
|
* Adds a script element to the end of the script.
|
|
578
|
-
*
|
|
579
|
-
* @
|
|
580
|
-
* @returns {Script} this script instance
|
|
581
|
-
*
|
|
764
|
+
* @param {Object} obj - The object to add.
|
|
765
|
+
* @returns {Script} Returns the script instance for chaining.
|
|
582
766
|
*/
|
|
583
767
|
Script.prototype.add = function (obj) {
|
|
584
768
|
this._addByType(obj, false);
|
|
585
769
|
return this;
|
|
586
770
|
};
|
|
587
771
|
|
|
772
|
+
/**
|
|
773
|
+
* Adds a script element to the script by type.
|
|
774
|
+
* Handles strings, numbers, Opcode instances, Buffers, Script instances, or objects.
|
|
775
|
+
* @param {string|number|Opcode|Buffer|Script|Object} obj - The element to add
|
|
776
|
+
* @param {boolean} [prepend=false] - Whether to prepend (true) or append (false)
|
|
777
|
+
* @throws {Error} If the input is an invalid script chunk
|
|
778
|
+
* @private
|
|
779
|
+
*/
|
|
588
780
|
Script.prototype._addByType = function (obj, prepend) {
|
|
589
781
|
if (typeof obj === 'string') {
|
|
590
782
|
this._addOpcode(obj, prepend);
|
|
@@ -604,6 +796,12 @@ Script.prototype._addByType = function (obj, prepend) {
|
|
|
604
796
|
}
|
|
605
797
|
};
|
|
606
798
|
|
|
799
|
+
/**
|
|
800
|
+
* Inserts a buffer at the specified position in the script's buffer.
|
|
801
|
+
* @param {Buffer} buf - The buffer to insert.
|
|
802
|
+
* @param {boolean} prepend - If true, inserts before the existing buffer; otherwise appends after.
|
|
803
|
+
* @private
|
|
804
|
+
*/
|
|
607
805
|
Script.prototype._insertAtPosition = function (buf, prepend) {
|
|
608
806
|
var bw = new BufferWriter();
|
|
609
807
|
|
|
@@ -617,6 +815,13 @@ Script.prototype._insertAtPosition = function (buf, prepend) {
|
|
|
617
815
|
this.buffer = bw.toBuffer();
|
|
618
816
|
};
|
|
619
817
|
|
|
818
|
+
/**
|
|
819
|
+
* Adds an opcode to the script.
|
|
820
|
+
* @param {number|Opcode|string} opcode - The opcode to add (can be a number, Opcode instance, or string).
|
|
821
|
+
* @param {boolean} [prepend=false] - Whether to prepend the opcode (true) or append it (false).
|
|
822
|
+
* @returns {Script} Returns the script instance for chaining.
|
|
823
|
+
* @throws {errors.Script.InvalidOpcode} Throws if the opcode value exceeds 255.
|
|
824
|
+
*/
|
|
620
825
|
Script.prototype._addOpcode = function (opcode, prepend) {
|
|
621
826
|
var op;
|
|
622
827
|
if (typeof opcode === 'number') {
|
|
@@ -642,6 +847,14 @@ Script.prototype._addOpcode = function (opcode, prepend) {
|
|
|
642
847
|
return this;
|
|
643
848
|
};
|
|
644
849
|
|
|
850
|
+
/**
|
|
851
|
+
* Adds a buffer to the script with appropriate opcode based on buffer length.
|
|
852
|
+
* Handles different buffer sizes by using corresponding pushdata opcodes.
|
|
853
|
+
* @param {Buffer} buf - The buffer to add to the script
|
|
854
|
+
* @param {boolean} [prepend] - Whether to prepend the buffer (default: append)
|
|
855
|
+
* @returns {Script} Returns the script instance for chaining
|
|
856
|
+
* @throws {Error} If buffer length exceeds maximum allowed size (2^32)
|
|
857
|
+
*/
|
|
645
858
|
Script.prototype._addBuffer = function (buf, prepend) {
|
|
646
859
|
var bw = new BufferWriter();
|
|
647
860
|
var opcodenum;
|
|
@@ -676,10 +889,19 @@ Script.prototype._addBuffer = function (buf, prepend) {
|
|
|
676
889
|
return this;
|
|
677
890
|
};
|
|
678
891
|
|
|
892
|
+
/**
|
|
893
|
+
* Creates a shallow copy of the Script instance.
|
|
894
|
+
* @returns {Script} A new Script instance with the same buffer content.
|
|
895
|
+
*/
|
|
679
896
|
Script.prototype.clone = function () {
|
|
680
897
|
return Script.fromBuffer(this.buffer.slice());
|
|
681
898
|
};
|
|
682
899
|
|
|
900
|
+
/**
|
|
901
|
+
* Removes all OP_CODESEPARATOR opcodes from the script chunks.
|
|
902
|
+
* Updates the script buffer with the filtered chunks and clears the cache.
|
|
903
|
+
* @returns {Script} The modified script instance for chaining.
|
|
904
|
+
*/
|
|
683
905
|
Script.prototype.removeCodeseparators = function () {
|
|
684
906
|
var chunks = [];
|
|
685
907
|
for (var i = 0; i < this.chunks.length; i++) {
|
|
@@ -716,16 +938,14 @@ Script.prototype.subScript = function (n) {
|
|
|
716
938
|
return this;
|
|
717
939
|
};
|
|
718
940
|
|
|
719
|
-
|
|
720
|
-
|
|
941
|
+
|
|
721
942
|
/**
|
|
722
|
-
*
|
|
723
|
-
*
|
|
724
|
-
* @param {
|
|
725
|
-
* @param {
|
|
726
|
-
* @param {
|
|
727
|
-
*
|
|
728
|
-
* public keys before creating the script
|
|
943
|
+
* Builds a multisig output script from given public keys and threshold.
|
|
944
|
+
* @param {Array} publicKeys - Array of public keys to include in the multisig
|
|
945
|
+
* @param {number} threshold - Minimum number of signatures required
|
|
946
|
+
* @param {Object} [opts] - Optional parameters
|
|
947
|
+
* @param {boolean} [opts.noSorting] - If true, skips sorting of public keys
|
|
948
|
+
* @returns {Script} The constructed multisig script
|
|
729
949
|
*/
|
|
730
950
|
Script.buildMultisigOut = function (publicKeys, threshold, opts) {
|
|
731
951
|
$.checkArgument(
|
|
@@ -779,10 +999,12 @@ Script.buildMultisigIn = function (pubkeys, threshold, signatures, opts) {
|
|
|
779
999
|
return s;
|
|
780
1000
|
};
|
|
781
1001
|
|
|
1002
|
+
|
|
782
1003
|
/**
|
|
783
|
-
*
|
|
784
|
-
* address or
|
|
785
|
-
* @
|
|
1004
|
+
* Builds a standard P2PKH (Pay-to-Public-Key-Hash) script for a given recipient.
|
|
1005
|
+
* @param {PublicKey|Address|string} to - Recipient's public key, address, or address string
|
|
1006
|
+
* @returns {Script} A P2PKH script with the format: `OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG`
|
|
1007
|
+
* @throws {Error} If 'to' argument is undefined or invalid type
|
|
786
1008
|
*/
|
|
787
1009
|
Script.buildPublicKeyHashOut = function (to) {
|
|
788
1010
|
$.checkArgument(!_.isUndefined(to));
|
|
@@ -802,9 +1024,11 @@ Script.buildPublicKeyHashOut = function (to) {
|
|
|
802
1024
|
return s;
|
|
803
1025
|
};
|
|
804
1026
|
|
|
1027
|
+
|
|
805
1028
|
/**
|
|
806
|
-
*
|
|
807
|
-
*
|
|
1029
|
+
* Builds a standard P2PK (Pay-to-Public-Key) script output.
|
|
1030
|
+
* @param {PublicKey} pubkey - The public key to create the script for
|
|
1031
|
+
* @returns {Script} A new script containing the public key and OP_CHECKSIG opcode
|
|
808
1032
|
*/
|
|
809
1033
|
Script.buildPublicKeyOut = function (pubkey) {
|
|
810
1034
|
$.checkArgument(pubkey instanceof PublicKey);
|
|
@@ -815,8 +1039,8 @@ Script.buildPublicKeyOut = function (pubkey) {
|
|
|
815
1039
|
|
|
816
1040
|
/**
|
|
817
1041
|
* @returns {Script} a new OP_RETURN script with data
|
|
818
|
-
* @param {
|
|
819
|
-
* @param {
|
|
1042
|
+
* @param {string|Buffer|Array} data - the data to embed in the output - it is a string, buffer, or array of strings or buffers
|
|
1043
|
+
* @param {string} encoding - the type of encoding of the string(s)
|
|
820
1044
|
*/
|
|
821
1045
|
Script.buildDataOut = function (data, encoding) {
|
|
822
1046
|
$.checkArgument(
|
|
@@ -842,8 +1066,8 @@ Script.buildDataOut = function (data, encoding) {
|
|
|
842
1066
|
|
|
843
1067
|
/**
|
|
844
1068
|
* @returns {Script} a new OP_RETURN script with data
|
|
845
|
-
* @param {
|
|
846
|
-
* @param {
|
|
1069
|
+
* @param {string|Buffer|Array} data - the data to embed in the output - it is a string, buffer, or array of strings or buffers
|
|
1070
|
+
* @param {string} encoding - the type of encoding of the string(s)
|
|
847
1071
|
*/
|
|
848
1072
|
Script.buildSafeDataOut = function (data, encoding) {
|
|
849
1073
|
var s2 = Script.buildDataOut(data, encoding);
|
|
@@ -858,7 +1082,7 @@ Script.buildSafeDataOut = function (data, encoding) {
|
|
|
858
1082
|
* Builds a scriptSig (a script for an input) that signs a public key output script.
|
|
859
1083
|
*
|
|
860
1084
|
* @param {Signature|Buffer} signature - a Signature object, or the signature in DER canonical encoding
|
|
861
|
-
* @param {number
|
|
1085
|
+
* @param {number} [sigtype] - the type of the signature (defaults to SIGHASH_ALL)
|
|
862
1086
|
*/
|
|
863
1087
|
Script.buildPublicKeyIn = function (signature, sigtype) {
|
|
864
1088
|
$.checkArgument(signature instanceof Signature || Buffer.isBuffer(signature));
|
|
@@ -877,7 +1101,7 @@ Script.buildPublicKeyIn = function (signature, sigtype) {
|
|
|
877
1101
|
*
|
|
878
1102
|
* @param {Buffer|string|PublicKey} publicKey
|
|
879
1103
|
* @param {Signature|Buffer} signature - a Signature object, or the signature in DER canonical encoding
|
|
880
|
-
* @param {number
|
|
1104
|
+
* @param {number} [sigtype] - the type of the signature (defaults to SIGHASH_ALL)
|
|
881
1105
|
*/
|
|
882
1106
|
Script.buildPublicKeyHashIn = function (publicKey, signature, sigtype) {
|
|
883
1107
|
$.checkArgument(signature instanceof Signature || Buffer.isBuffer(signature));
|
|
@@ -891,15 +1115,21 @@ Script.buildPublicKeyHashIn = function (publicKey, signature, sigtype) {
|
|
|
891
1115
|
return script;
|
|
892
1116
|
};
|
|
893
1117
|
|
|
1118
|
+
|
|
894
1119
|
/**
|
|
895
|
-
*
|
|
1120
|
+
* Creates and returns an empty Script instance.
|
|
1121
|
+
* @returns {Script} A new empty Script object.
|
|
896
1122
|
*/
|
|
897
1123
|
Script.empty = function () {
|
|
898
1124
|
return new Script();
|
|
899
1125
|
};
|
|
900
1126
|
|
|
1127
|
+
|
|
901
1128
|
/**
|
|
902
|
-
*
|
|
1129
|
+
* Creates a Script from an address.
|
|
1130
|
+
* @param {Address|string} address - The address to convert to a script.
|
|
1131
|
+
* @returns {Script} A Pay-to-PublicKeyHash (P2PKH) script for the given address.
|
|
1132
|
+
* @throws {errors.Script.UnrecognizedAddress} If the address type is not supported.
|
|
903
1133
|
*/
|
|
904
1134
|
Script.fromAddress = function (address) {
|
|
905
1135
|
address = Address(address);
|
|
@@ -909,11 +1139,15 @@ Script.fromAddress = function (address) {
|
|
|
909
1139
|
throw new errors.Script.UnrecognizedAddress(address);
|
|
910
1140
|
};
|
|
911
1141
|
|
|
1142
|
+
|
|
912
1143
|
/**
|
|
913
|
-
*
|
|
914
|
-
*
|
|
1144
|
+
* Gets address information for the script.
|
|
1145
|
+
* For input scripts, returns input address info.
|
|
1146
|
+
* For output scripts, returns output address info.
|
|
1147
|
+
* For general scripts, tries output address info first, falls back to input if not available.
|
|
1148
|
+
* @returns {Object} Address information object
|
|
915
1149
|
*/
|
|
916
|
-
Script.prototype.getAddressInfo = function (
|
|
1150
|
+
Script.prototype.getAddressInfo = function () {
|
|
917
1151
|
if (this._isInput) {
|
|
918
1152
|
return this._getInputAddressInfo();
|
|
919
1153
|
} else if (this._isOutput) {
|
|
@@ -927,10 +1161,12 @@ Script.prototype.getAddressInfo = function (opts) {
|
|
|
927
1161
|
}
|
|
928
1162
|
};
|
|
929
1163
|
|
|
1164
|
+
|
|
930
1165
|
/**
|
|
931
|
-
*
|
|
932
|
-
* @
|
|
933
|
-
* @
|
|
1166
|
+
* Gets the output address information from the script.
|
|
1167
|
+
* @returns {Object|boolean} An object containing the hash buffer and address type if the script is a public key hash output, otherwise false.
|
|
1168
|
+
* @property {Buffer} info.hashBuffer - The hash buffer of the address.
|
|
1169
|
+
* @property {number} info.type - The type of the address (Address.PayToPublicKeyHash).
|
|
934
1170
|
*/
|
|
935
1171
|
Script.prototype._getOutputAddressInfo = function () {
|
|
936
1172
|
var info = {};
|
|
@@ -960,26 +1196,32 @@ Script.prototype._getInputAddressInfo = function () {
|
|
|
960
1196
|
return info;
|
|
961
1197
|
};
|
|
962
1198
|
|
|
1199
|
+
|
|
963
1200
|
/**
|
|
964
|
-
*
|
|
965
|
-
* @
|
|
1201
|
+
* Converts the script to an Address object for the specified network.
|
|
1202
|
+
* @param {string|Network} [network] - optianal, the network name or identifier.
|
|
1203
|
+
* @returns {Address} The derived Address object.
|
|
1204
|
+
* @throws {errors.Script.CantDeriveAddress} If address information cannot be derived from the script.
|
|
966
1205
|
*/
|
|
967
1206
|
Script.prototype.toAddress = function (network) {
|
|
968
1207
|
var info = this.getAddressInfo();
|
|
969
1208
|
if (!info) {
|
|
970
|
-
|
|
1209
|
+
throw new errors.Script.CantDeriveAddress(this);
|
|
971
1210
|
}
|
|
972
1211
|
info.network = Networks.get(network) || this._network || Networks.defaultNetwork;
|
|
973
1212
|
return new Address(info);
|
|
974
1213
|
};
|
|
975
1214
|
|
|
976
1215
|
/**
|
|
1216
|
+
* Finds and deletes a matching script chunk from the current script.
|
|
977
1217
|
* Analogous to bitcoind's FindAndDelete. Find and delete equivalent chunks,
|
|
978
1218
|
* typically used with push data chunks. Note that this will find and delete
|
|
979
1219
|
* not just the same data, but the same data with the same push data op as
|
|
980
1220
|
* produced by default. i.e., if a pushdata in a tx does not use the minimal
|
|
981
1221
|
* pushdata op, then when you try to remove the data it is pushing, it will not
|
|
982
1222
|
* be removed, because they do not use the same pushdata op.
|
|
1223
|
+
* @param {Script} script - The script chunk to find and delete.
|
|
1224
|
+
* @returns {Script} The modified script instance after deletion.
|
|
983
1225
|
*/
|
|
984
1226
|
Script.prototype.findAndDelete = function (script) {
|
|
985
1227
|
var buf = script.toBuffer();
|
|
@@ -997,9 +1239,20 @@ Script.prototype.findAndDelete = function (script) {
|
|
|
997
1239
|
return this;
|
|
998
1240
|
};
|
|
999
1241
|
|
|
1242
|
+
|
|
1000
1243
|
/**
|
|
1001
|
-
*
|
|
1002
|
-
*
|
|
1244
|
+
* Checks if a script chunk uses the minimal push operation possible.
|
|
1245
|
+
*
|
|
1246
|
+
* @param {number} i - Index of the chunk to check
|
|
1247
|
+
* @returns {boolean} True if the chunk uses minimal push operation, false otherwise
|
|
1248
|
+
*
|
|
1249
|
+
* The function verifies if the chunk could have been represented with:
|
|
1250
|
+
* - OP_0 for empty buffer
|
|
1251
|
+
* - OP_1 to OP_16 for single-byte values 1-16
|
|
1252
|
+
* - OP_1NEGATE for 0x81
|
|
1253
|
+
* - Direct push for buffers ≤75 bytes
|
|
1254
|
+
* - OP_PUSHDATA1 for buffers ≤255 bytes
|
|
1255
|
+
* - OP_PUSHDATA2 for buffers ≤65535 bytes
|
|
1003
1256
|
*/
|
|
1004
1257
|
Script.prototype.checkMinimalPush = function (i) {
|
|
1005
1258
|
var chunk = this.chunks[i];
|
|
@@ -1034,6 +1287,7 @@ Script.prototype.checkMinimalPush = function (i) {
|
|
|
1034
1287
|
* Comes from bitcoind's script DecodeOP_N function
|
|
1035
1288
|
* @param {number} opcode
|
|
1036
1289
|
* @returns {number} numeric value in range of 0 to 16
|
|
1290
|
+
* @private
|
|
1037
1291
|
*/
|
|
1038
1292
|
Script.prototype._decodeOP_N = function (opcode) {
|
|
1039
1293
|
if (opcode === Opcode.OP_0) {
|
|
@@ -1045,10 +1299,11 @@ Script.prototype._decodeOP_N = function (opcode) {
|
|
|
1045
1299
|
}
|
|
1046
1300
|
};
|
|
1047
1301
|
|
|
1302
|
+
|
|
1048
1303
|
/**
|
|
1049
|
-
*
|
|
1050
|
-
* @param {boolean}
|
|
1051
|
-
* @returns {number}
|
|
1304
|
+
* Counts the number of signature operations in the script.
|
|
1305
|
+
* @param {boolean} [accurate=true] - Whether to count accurately for OP_CHECKMULTISIG(VERIFY).
|
|
1306
|
+
* @returns {number} The total count of signature operations.
|
|
1052
1307
|
*/
|
|
1053
1308
|
Script.prototype.getSignatureOperationsCount = function (accurate) {
|
|
1054
1309
|
accurate = _.isUndefined(accurate) ? true : accurate;
|