@opcat-labs/opcat 1.0.1 → 1.0.2
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 +6 -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,31 +1,35 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
var Input = require('./input');
|
|
3
|
+
import _ from '../util/_.js';
|
|
4
|
+
import $ from '../util/preconditions.js';
|
|
5
|
+
import errors from '../errors/index.js';
|
|
6
|
+
import JSUtil from '../util/js.js';
|
|
7
|
+
import BufferReader from '../encoding/bufferreader.js';
|
|
8
|
+
import BufferWriter from '../encoding/bufferwriter.js';
|
|
9
|
+
import Varint from '../encoding/varint.js';
|
|
10
|
+
import Hash from '../crypto/hash.js';
|
|
11
|
+
import Signature from '../crypto/signature.js';
|
|
12
|
+
import TransactionSignature from './signature.js';
|
|
13
|
+
import Sighash from './sighash.js';
|
|
14
|
+
import Address from '../address.js';
|
|
15
|
+
import UnspentOutput from './unspentoutput.js';
|
|
16
|
+
import Input from './input/index.js';
|
|
18
17
|
var PublicKeyHashInput = Input.PublicKeyHash;
|
|
19
18
|
var PublicKeyInput = Input.PublicKey;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
import Output from './output.js';
|
|
20
|
+
import Script from '../script/index.js';
|
|
21
|
+
import PrivateKey from '../privatekey.js';
|
|
22
|
+
import BN from '../crypto/bn.js';
|
|
23
|
+
|
|
24
24
|
/**
|
|
25
25
|
* Represents a transaction, a set of inputs and outputs to change ownership of tokens
|
|
26
|
-
*
|
|
27
|
-
* @param {*} serialized
|
|
28
26
|
* @constructor
|
|
27
|
+
* @param {string|Buffer|Object|Transaction} [serialized] - Optional serialized data to initialize the transaction.
|
|
28
|
+
* Can be a hex string, Buffer, plain object, or another Transaction instance.
|
|
29
|
+
* @throws {errors.InvalidArgument} If invalid serialization format is provided.
|
|
30
|
+
* @property {Array} inputs - Transaction input objects.
|
|
31
|
+
* @property {Array} outputs - Transaction output objects.
|
|
32
|
+
* @property {boolean} sealed - Indicates if transaction is finalized.
|
|
29
33
|
*/
|
|
30
34
|
function Transaction(serialized) {
|
|
31
35
|
if (!(this instanceof Transaction)) {
|
|
@@ -62,52 +66,96 @@ function Transaction(serialized) {
|
|
|
62
66
|
var CURRENT_VERSION = 1;
|
|
63
67
|
var DEFAULT_NLOCKTIME = 0;
|
|
64
68
|
|
|
65
|
-
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The minimum amount (in satoshis) considered non-dust in a transaction.
|
|
72
|
+
* @type {number}
|
|
73
|
+
* @default 1
|
|
74
|
+
*/
|
|
66
75
|
Transaction.DUST_AMOUNT = 1;
|
|
67
76
|
|
|
68
|
-
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Margin of error to allow fees in the vecinity of the expected value but doesn't allow a big difference
|
|
80
|
+
* @type {number}
|
|
81
|
+
* @default 150
|
|
82
|
+
*/
|
|
69
83
|
Transaction.FEE_SECURITY_MARGIN = 150;
|
|
70
84
|
|
|
71
85
|
// max amount of satoshis in circulation
|
|
86
|
+
/**
|
|
87
|
+
* The maximum amount of money (in satoshis) that can be transacted in a single transaction.
|
|
88
|
+
* This follows Bitcoin's 21 million coin limit (21000000 * 1e8 satoshis).
|
|
89
|
+
* @type {number}
|
|
90
|
+
* @default 2100000000000000
|
|
91
|
+
*/
|
|
72
92
|
Transaction.MAX_MONEY = 21000000 * 1e8;
|
|
73
93
|
|
|
74
|
-
|
|
94
|
+
/**
|
|
95
|
+
* The maximum block height value allowed for nLockTime in a transaction.
|
|
96
|
+
* Transactions with nLockTime above this value are interpreted as timestamp-based.
|
|
97
|
+
* @type {number}
|
|
98
|
+
* @default 500000000
|
|
99
|
+
*/
|
|
75
100
|
Transaction.NLOCKTIME_BLOCKHEIGHT_LIMIT = 5e8;
|
|
76
101
|
|
|
77
|
-
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The maximum value allowed for the nLockTime field in a transaction (2^32 - 1).
|
|
105
|
+
* @type {number}
|
|
106
|
+
* @default 4294967295
|
|
107
|
+
*/
|
|
78
108
|
Transaction.NLOCKTIME_MAX_VALUE = 4294967295;
|
|
79
109
|
|
|
80
|
-
|
|
81
|
-
|
|
110
|
+
/**
|
|
111
|
+
* The fee per kilobyte (KB) for transactions, in satoshis.
|
|
112
|
+
* @type {number}
|
|
113
|
+
* @default 1
|
|
114
|
+
*/
|
|
115
|
+
Transaction.FEE_PER_KB = 1;
|
|
116
|
+
|
|
82
117
|
|
|
83
|
-
// Safe upper bound for change address script size in bytes
|
|
84
|
-
Transaction.CHANGE_OUTPUT_MAX_SIZE = 20 + 4 + 34 + 4;
|
|
85
118
|
|
|
86
119
|
/**
|
|
87
120
|
* a dummy privatekey
|
|
121
|
+
* @type {PrivateKey}
|
|
88
122
|
*/
|
|
89
123
|
Transaction.DUMMY_PRIVATEKEY = PrivateKey.fromWIF(
|
|
90
124
|
'cQ3nCBQB9RsFSyjNQM15NQLVpXXMtWh9PUyeFz5KxLJCHsuRH2Su',
|
|
91
125
|
);
|
|
92
126
|
|
|
93
|
-
/* Constructors and Serialization */
|
|
94
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Creates a Transaction instance from a raw hexadecimal string.
|
|
130
|
+
* @param {string} rawHex - The hexadecimal string representation of the transaction.
|
|
131
|
+
* @returns {Transaction} A new Transaction instance populated from the input string.
|
|
132
|
+
*/
|
|
95
133
|
Transaction.fromString = function (rawHex) {
|
|
96
134
|
return new Transaction().fromString(rawHex)
|
|
97
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Creates a Transaction instance from a buffer.
|
|
138
|
+
* @param {Buffer} buffer - The input buffer containing transaction data.
|
|
139
|
+
* @returns {Transaction} A new Transaction instance populated from the buffer.
|
|
140
|
+
*/
|
|
98
141
|
Transaction.fromBuffer = function (buffer) {
|
|
99
142
|
return new Transaction().fromBuffer(buffer)
|
|
100
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Creates a Transaction instance from a plain object.
|
|
146
|
+
* @param {Object} obj - The plain object to convert to a Transaction.
|
|
147
|
+
* @returns {Transaction} A new Transaction instance populated from the object.
|
|
148
|
+
*/
|
|
101
149
|
Transaction.fromObject = function (obj) {
|
|
102
150
|
return new Transaction().fromObject(obj)
|
|
103
151
|
}
|
|
104
152
|
|
|
153
|
+
|
|
105
154
|
/**
|
|
106
155
|
* Create a 'shallow' copy of the transaction, by serializing and deserializing
|
|
107
156
|
* it dropping any additional information that inputs and outputs may have hold
|
|
108
|
-
*
|
|
109
|
-
* @
|
|
110
|
-
* @return {Transaction}
|
|
157
|
+
* @param {Transaction} transaction - The transaction to copy.
|
|
158
|
+
* @returns {Transaction} A new Transaction instance with the same data.
|
|
111
159
|
*/
|
|
112
160
|
Transaction.shallowCopy = function (transaction) {
|
|
113
161
|
var copy = new Transaction(transaction.toBuffer());
|
|
@@ -122,25 +170,55 @@ var hashProperty = {
|
|
|
122
170
|
return this._hash;
|
|
123
171
|
},
|
|
124
172
|
};
|
|
173
|
+
/**
|
|
174
|
+
* The unique identifier hash of the transaction.
|
|
175
|
+
* @name Transaction.prototype.hash
|
|
176
|
+
* @memberof Transaction
|
|
177
|
+
* @instance
|
|
178
|
+
* @type {string}
|
|
179
|
+
*/
|
|
125
180
|
Object.defineProperty(Transaction.prototype, 'hash', hashProperty);
|
|
181
|
+
/**
|
|
182
|
+
* The unique identifier hash of the transaction.
|
|
183
|
+
* @name Transaction.prototype.id
|
|
184
|
+
* @memberof Transaction
|
|
185
|
+
* @instance
|
|
186
|
+
* @type {string}
|
|
187
|
+
*/
|
|
126
188
|
Object.defineProperty(Transaction.prototype, 'id', hashProperty);
|
|
127
189
|
|
|
128
|
-
|
|
190
|
+
/**
|
|
191
|
+
* Gets the inputs amount for the transaction.
|
|
192
|
+
* @name Transaction.prototype.inputAmount
|
|
193
|
+
* @memberof Transaction
|
|
194
|
+
* @type {number}
|
|
195
|
+
*/
|
|
196
|
+
Object.defineProperty(Transaction.prototype, 'inputAmount', {
|
|
129
197
|
configurable: false,
|
|
130
198
|
enumerable: true,
|
|
131
199
|
get: function () {
|
|
132
200
|
return this._getInputAmount();
|
|
133
201
|
},
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Gets the outputs amount for the transaction.
|
|
206
|
+
* @name Transaction.prototype.outputAmount
|
|
207
|
+
* @memberof Transaction
|
|
208
|
+
* @type {number}
|
|
209
|
+
*/
|
|
210
|
+
Object.defineProperty(Transaction.prototype, 'outputAmount', {
|
|
211
|
+
configurable: false,
|
|
212
|
+
enumerable: true,
|
|
213
|
+
get: function () {
|
|
137
214
|
return this._getOutputAmount();
|
|
138
|
-
}
|
|
139
|
-
|
|
215
|
+
},
|
|
216
|
+
});
|
|
140
217
|
|
|
141
218
|
/**
|
|
142
219
|
* Retrieve the little endian hash of the transaction (used for serialization)
|
|
143
220
|
* @return {Buffer}
|
|
221
|
+
* @private
|
|
144
222
|
*/
|
|
145
223
|
Transaction.prototype._getHash = function () {
|
|
146
224
|
return Hash.sha256sha256(this.toBufferWriter(true).toBuffer());
|
|
@@ -167,6 +245,10 @@ Transaction.prototype.serialize = function (unsafe) {
|
|
|
167
245
|
}
|
|
168
246
|
};
|
|
169
247
|
|
|
248
|
+
/**
|
|
249
|
+
* Creates a deep clone of the Transaction instance.
|
|
250
|
+
* @returns {Transaction} A new Transaction instance with cloned inputs.
|
|
251
|
+
*/
|
|
170
252
|
Transaction.prototype.clone = function () {
|
|
171
253
|
const tx = Transaction.fromString(this.uncheckedSerialize());
|
|
172
254
|
this.inputs.forEach((input, index) => {
|
|
@@ -177,6 +259,11 @@ Transaction.prototype.clone = function () {
|
|
|
177
259
|
return tx;
|
|
178
260
|
};
|
|
179
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Serializes the transaction to a hexadecimal string.
|
|
264
|
+
* This method is aliased as `toString()` and `toHex()` for convenience.
|
|
265
|
+
* @returns {string} Hexadecimal representation of the transaction.
|
|
266
|
+
*/
|
|
180
267
|
Transaction.prototype.uncheckedSerialize = Transaction.prototype.toString = Transaction.prototype.toHex = function () {
|
|
181
268
|
return this.toBuffer().toString('hex');
|
|
182
269
|
};
|
|
@@ -198,6 +285,10 @@ Transaction.prototype.checkedSerialize = function (opts) {
|
|
|
198
285
|
return this.uncheckedSerialize();
|
|
199
286
|
};
|
|
200
287
|
|
|
288
|
+
/**
|
|
289
|
+
* Checks if any output in the transaction has invalid satoshis.
|
|
290
|
+
* @returns {boolean} True if at least one output has invalid satoshis, false otherwise.
|
|
291
|
+
*/
|
|
201
292
|
Transaction.prototype.invalidSatoshis = function () {
|
|
202
293
|
var invalid = false;
|
|
203
294
|
for (var i = 0; i < this.outputs.length; i++) {
|
|
@@ -235,6 +326,19 @@ Transaction.prototype.getSerializationError = function (opts) {
|
|
|
235
326
|
return unspentError || this._hasDustOutputs(opts) || this._isMissingSignatures(opts);
|
|
236
327
|
};
|
|
237
328
|
|
|
329
|
+
/**
|
|
330
|
+
* Checks for fee-related errors in a transaction.
|
|
331
|
+
*
|
|
332
|
+
* @param {Object} opts - Transaction options
|
|
333
|
+
* @param {number} unspent - The unspent amount to be used as fee
|
|
334
|
+
* @returns {errors.Transaction.FeeError.Different|errors.Transaction.ChangeAddressMissing|errors.Transaction.FeeError.TooLarge|undefined}
|
|
335
|
+
* Returns a fee error if:
|
|
336
|
+
* - Specified fee doesn't match unspent amount
|
|
337
|
+
* - Fee is too large and no change address is provided (when large fees are enabled)
|
|
338
|
+
* - Fee exceeds security margin (when large fees are enabled)
|
|
339
|
+
* Otherwise returns undefined if no errors are found.
|
|
340
|
+
* @private
|
|
341
|
+
*/
|
|
238
342
|
Transaction.prototype._hasFeeError = function (opts, unspent) {
|
|
239
343
|
if (!_.isUndefined(this._fee) && this._fee !== unspent) {
|
|
240
344
|
return new errors.Transaction.FeeError.Different(
|
|
@@ -257,10 +361,22 @@ Transaction.prototype._hasFeeError = function (opts, unspent) {
|
|
|
257
361
|
}
|
|
258
362
|
};
|
|
259
363
|
|
|
364
|
+
/**
|
|
365
|
+
* Checks if the transaction is missing a change script.
|
|
366
|
+
* @returns {boolean} True if change script is missing, false otherwise.
|
|
367
|
+
* @private
|
|
368
|
+
*/
|
|
260
369
|
Transaction.prototype._missingChange = function () {
|
|
261
370
|
return !this._changeScript;
|
|
262
371
|
};
|
|
263
372
|
|
|
373
|
+
/**
|
|
374
|
+
* Checks if the transaction contains any dust outputs (outputs below the dust limit).
|
|
375
|
+
* @param {Object} opts - Options object.
|
|
376
|
+
* @param {boolean} [opts.disableDustOutputs] - If true, skips dust output checking.
|
|
377
|
+
* @returns {errors.Transaction.DustOutputs|undefined} Returns DustOutputs error if dust outputs are found, otherwise undefined.
|
|
378
|
+
* @private
|
|
379
|
+
*/
|
|
264
380
|
Transaction.prototype._hasDustOutputs = function (opts) {
|
|
265
381
|
if (opts.disableDustOutputs) {
|
|
266
382
|
return;
|
|
@@ -278,6 +394,12 @@ Transaction.prototype._hasDustOutputs = function (opts) {
|
|
|
278
394
|
}
|
|
279
395
|
};
|
|
280
396
|
|
|
397
|
+
/**
|
|
398
|
+
* Checks if the transaction is missing signatures.
|
|
399
|
+
* @param {Object} opts - Options object.
|
|
400
|
+
* @param {boolean} [opts.disableIsFullySigned] - If true, skips the check.
|
|
401
|
+
* @returns {errors.Transaction.MissingSignatures|undefined} Returns MissingSignatures error if not fully signed, otherwise undefined.
|
|
402
|
+
*/
|
|
281
403
|
Transaction.prototype._isMissingSignatures = function (opts) {
|
|
282
404
|
if (opts.disableIsFullySigned) {
|
|
283
405
|
return;
|
|
@@ -287,27 +409,55 @@ Transaction.prototype._isMissingSignatures = function (opts) {
|
|
|
287
409
|
}
|
|
288
410
|
};
|
|
289
411
|
|
|
412
|
+
/**
|
|
413
|
+
* Returns a string representation of the Transaction object for debugging/inspection.
|
|
414
|
+
* The format is: '<Transaction: [serializedData]>' where serializedData comes from uncheckedSerialize().
|
|
415
|
+
* @returns {string} Formatted transaction inspection string.
|
|
416
|
+
*/
|
|
290
417
|
Transaction.prototype.inspect = function () {
|
|
291
418
|
return '<Transaction: ' + this.uncheckedSerialize() + '>';
|
|
292
419
|
};
|
|
293
420
|
|
|
421
|
+
/**
|
|
422
|
+
* Converts the transaction to a Buffer.
|
|
423
|
+
* @returns {Buffer} The serialized transaction as a Buffer.
|
|
424
|
+
*/
|
|
294
425
|
Transaction.prototype.toBuffer = function () {
|
|
295
426
|
var writer = new BufferWriter();
|
|
296
427
|
return this.toBufferWriter(false, writer).toBuffer();
|
|
297
428
|
};
|
|
298
429
|
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Calculates the double SHA-256 hash of the transaction preimage for signature verification.
|
|
433
|
+
* The resulting hash is returned in reverse byte order (little-endian).
|
|
434
|
+
*
|
|
435
|
+
* @param {number} inputIndex - Index of the input being signed
|
|
436
|
+
* @param {number} hashType - SIGHASH type flag
|
|
437
|
+
* @returns {Buffer} The hash result in little-endian format
|
|
438
|
+
*/
|
|
299
439
|
Transaction.prototype.hashForSignature = function (inputIndex, hashType) {
|
|
300
440
|
var preimage = this.getPreimage(inputIndex, hashType);
|
|
301
441
|
var ret = Hash.sha256sha256(preimage)
|
|
302
442
|
return new BufferReader(ret).readReverse()
|
|
303
443
|
}
|
|
304
444
|
|
|
445
|
+
/**
|
|
446
|
+
* Converts the transaction to a hash preimage by serializing it into a buffer.
|
|
447
|
+
* @returns {Buffer} The serialized transaction data as a buffer.
|
|
448
|
+
*/
|
|
305
449
|
Transaction.prototype.toTxHashPreimage = function () {
|
|
306
450
|
var writer = new BufferWriter();
|
|
307
451
|
return this.toBufferWriter(true, writer).toBuffer();
|
|
308
452
|
}
|
|
309
453
|
|
|
310
454
|
|
|
455
|
+
/**
|
|
456
|
+
* Serializes the transaction to a BufferWriter.
|
|
457
|
+
* @param {boolean} forTxHash - Whether to serialize for transaction hash calculation (excludes some fields)
|
|
458
|
+
* @param {BufferWriter} [writer] - Optional BufferWriter instance to write to
|
|
459
|
+
* @returns {BufferWriter} The BufferWriter containing serialized transaction data
|
|
460
|
+
*/
|
|
311
461
|
Transaction.prototype.toBufferWriter = function (forTxHash, writer) {
|
|
312
462
|
$.checkArgument(typeof forTxHash === 'boolean', 'forTxHash parameter must be a boolean');
|
|
313
463
|
writer = writer || new BufferWriter();
|
|
@@ -324,11 +474,22 @@ Transaction.prototype.toBufferWriter = function (forTxHash, writer) {
|
|
|
324
474
|
return writer;
|
|
325
475
|
};
|
|
326
476
|
|
|
477
|
+
/**
|
|
478
|
+
* Initializes the transaction from a buffer.
|
|
479
|
+
* @param {Buffer} buffer - The buffer containing transaction data.
|
|
480
|
+
* @returns {Transaction} The transaction instance.
|
|
481
|
+
*/
|
|
327
482
|
Transaction.prototype.fromBuffer = function (buffer) {
|
|
328
483
|
var reader = new BufferReader(buffer);
|
|
329
484
|
return this.fromBufferReader(reader);
|
|
330
485
|
};
|
|
331
486
|
|
|
487
|
+
/**
|
|
488
|
+
* Reads transaction data from a buffer reader and populates the transaction instance.
|
|
489
|
+
* @param {BufferReader} reader - The buffer reader containing transaction data.
|
|
490
|
+
* @returns {Transaction} The transaction instance with populated data.
|
|
491
|
+
* @throws {Error} If no transaction data is received (reader is finished).
|
|
492
|
+
*/
|
|
332
493
|
Transaction.prototype.fromBufferReader = function (reader) {
|
|
333
494
|
$.checkArgument(!reader.finished(), 'No transaction data received');
|
|
334
495
|
var i, sizeTxIns, sizeTxOuts;
|
|
@@ -347,6 +508,12 @@ Transaction.prototype.fromBufferReader = function (reader) {
|
|
|
347
508
|
return this;
|
|
348
509
|
};
|
|
349
510
|
|
|
511
|
+
/**
|
|
512
|
+
* Converts the Transaction object to a plain JavaScript object (POJO) for serialization.
|
|
513
|
+
* Includes transaction details like hash, version, inputs, outputs, and lock time.
|
|
514
|
+
* Optionally includes change script, change address, change index, and fee if they are defined.
|
|
515
|
+
* @returns {Object} A plain object representation of the transaction.
|
|
516
|
+
*/
|
|
350
517
|
Transaction.prototype.toObject = Transaction.prototype.toJSON = function toObject() {
|
|
351
518
|
var inputs = [];
|
|
352
519
|
this.inputs.forEach(function (input) {
|
|
@@ -380,6 +547,14 @@ Transaction.prototype.toObject = Transaction.prototype.toJSON = function toObjec
|
|
|
380
547
|
return obj;
|
|
381
548
|
};
|
|
382
549
|
|
|
550
|
+
/**
|
|
551
|
+
* Creates a Transaction instance from a plain object or another Transaction.
|
|
552
|
+
* Handles conversion of inputs/outputs and other transaction properties.
|
|
553
|
+
*
|
|
554
|
+
* @param {Object|Transaction} arg - Either a transaction object or Transaction instance
|
|
555
|
+
* @returns {Transaction} The populated Transaction instance
|
|
556
|
+
* @throws {Error} If argument is not an object or Transaction instance
|
|
557
|
+
*/
|
|
383
558
|
Transaction.prototype.fromObject = function fromObject(arg) {
|
|
384
559
|
$.checkArgument(_.isObject(arg) || arg instanceof Transaction);
|
|
385
560
|
var self = this;
|
|
@@ -429,6 +604,14 @@ Transaction.prototype.fromObject = function fromObject(arg) {
|
|
|
429
604
|
return this;
|
|
430
605
|
};
|
|
431
606
|
|
|
607
|
+
/**
|
|
608
|
+
* Checks transaction consistency by validating:
|
|
609
|
+
* - Change output script and address (if change index is set)
|
|
610
|
+
* - Transaction hash (if provided in arg)
|
|
611
|
+
* @param {Object} [arg] - Optional argument containing expected hash
|
|
612
|
+
* @throws {Error} If any consistency check fails
|
|
613
|
+
* @private
|
|
614
|
+
*/
|
|
432
615
|
Transaction.prototype._checkConsistency = function (arg) {
|
|
433
616
|
if (!_.isUndefined(this._changeIndex)) {
|
|
434
617
|
$.checkState(this._changeScript, 'Change script is expected.');
|
|
@@ -444,12 +627,13 @@ Transaction.prototype._checkConsistency = function (arg) {
|
|
|
444
627
|
}
|
|
445
628
|
};
|
|
446
629
|
|
|
630
|
+
|
|
447
631
|
/**
|
|
448
632
|
* Sets nLockTime so that transaction is not valid until the desired date(a
|
|
449
633
|
* timestamp in seconds since UNIX epoch is also accepted)
|
|
450
|
-
*
|
|
451
|
-
* @
|
|
452
|
-
* @
|
|
634
|
+
* @param {number|Date} time - The lock time as a timestamp (number) or Date object.
|
|
635
|
+
* @throws {Transaction.LockTimeTooEarly} If the time is a number below NLOCKTIME_BLOCKHEIGHT_LIMIT.
|
|
636
|
+
* @returns {Transaction} The transaction instance for chaining.
|
|
453
637
|
*/
|
|
454
638
|
Transaction.prototype.lockUntilDate = function (time) {
|
|
455
639
|
$.checkArgument(time);
|
|
@@ -470,12 +654,15 @@ Transaction.prototype.lockUntilDate = function (time) {
|
|
|
470
654
|
return this;
|
|
471
655
|
};
|
|
472
656
|
|
|
657
|
+
|
|
473
658
|
/**
|
|
474
|
-
* Sets
|
|
475
|
-
* height.
|
|
476
|
-
*
|
|
477
|
-
* @param {
|
|
478
|
-
* @
|
|
659
|
+
* Sets the transaction's lock time to a specific block height.
|
|
660
|
+
* Validates the height is within allowed bounds (0 <= height < NLOCKTIME_BLOCKHEIGHT_LIMIT).
|
|
661
|
+
* Updates sequence numbers of inputs to enable lock time if using default sequence.
|
|
662
|
+
* @param {number} height - The block height to lock until (must be non-negative and below limit)
|
|
663
|
+
* @returns {Transaction} Returns the transaction instance for chaining
|
|
664
|
+
* @throws {Transaction.BlockHeightTooHigh} If height exceeds block height limit
|
|
665
|
+
* @throws {Transaction.NLockTimeOutOfRange} If height is negative
|
|
479
666
|
*/
|
|
480
667
|
Transaction.prototype.lockUntilBlockHeight = function (height) {
|
|
481
668
|
$.checkArgument(_.isNumber(height));
|
|
@@ -513,10 +700,20 @@ Transaction.prototype.getLockTime = function () {
|
|
|
513
700
|
return new Date(1000 * this.nLockTime);
|
|
514
701
|
};
|
|
515
702
|
|
|
703
|
+
/**
|
|
704
|
+
* Converts a hex string into a transaction buffer and initializes the transaction.
|
|
705
|
+
* @param {string} string - Hex string representation of the transaction data.
|
|
706
|
+
* @returns {Transaction} The transaction instance initialized from the hex string.
|
|
707
|
+
*/
|
|
516
708
|
Transaction.prototype.fromString = function (string) {
|
|
517
709
|
return this.fromBuffer(Buffer.from(string, 'hex'));
|
|
518
710
|
};
|
|
519
711
|
|
|
712
|
+
/**
|
|
713
|
+
* Initializes a new transaction with default values.
|
|
714
|
+
* Sets the version to CURRENT_VERSION and nLockTime to DEFAULT_NLOCKTIME.
|
|
715
|
+
* @private
|
|
716
|
+
*/
|
|
520
717
|
Transaction.prototype._newTransaction = function () {
|
|
521
718
|
this.version = CURRENT_VERSION;
|
|
522
719
|
this.nLockTime = DEFAULT_NLOCKTIME;
|
|
@@ -569,6 +766,7 @@ Transaction.prototype._newTransaction = function () {
|
|
|
569
766
|
* @param {(Array.<Transaction~fromObject>|Transaction~fromObject)} utxo
|
|
570
767
|
* @param {Array=} pubkeys
|
|
571
768
|
* @param {number=} threshold
|
|
769
|
+
* @returns {Transaction} The transaction instance for chaining.
|
|
572
770
|
*/
|
|
573
771
|
Transaction.prototype.from = function (utxo) {
|
|
574
772
|
if (_.isArray(utxo)) {
|
|
@@ -589,6 +787,18 @@ Transaction.prototype.from = function (utxo) {
|
|
|
589
787
|
return this;
|
|
590
788
|
};
|
|
591
789
|
|
|
790
|
+
/**
|
|
791
|
+
* Adds an input to the transaction from a UTXO (Unspent Transaction Output).
|
|
792
|
+
* Determines the appropriate input type (PublicKeyHashInput, PublicKeyInput, or generic Input)
|
|
793
|
+
* based on the UTXO's script type.
|
|
794
|
+
* @param {Object} utxo - The UTXO to create input from
|
|
795
|
+
* @param {Script} utxo.script - The output script
|
|
796
|
+
* @param {number} utxo.satoshis - The output amount in satoshis
|
|
797
|
+
* @param {Buffer} [utxo.data] - Optional output data
|
|
798
|
+
* @param {string} utxo.txId - The previous transaction ID
|
|
799
|
+
* @param {number} utxo.outputIndex - The output index in previous transaction
|
|
800
|
+
* @private
|
|
801
|
+
*/
|
|
592
802
|
Transaction.prototype._fromUTXO = function (utxo) {
|
|
593
803
|
var Clazz;
|
|
594
804
|
utxo = new UnspentOutput(utxo);
|
|
@@ -613,15 +823,17 @@ Transaction.prototype._fromUTXO = function (utxo) {
|
|
|
613
823
|
);
|
|
614
824
|
};
|
|
615
825
|
|
|
826
|
+
|
|
616
827
|
/**
|
|
617
828
|
* Add an input to this transaction. The input must be an instance of the `Input` class.
|
|
618
829
|
* It should have information about the Output that it's spending, but if it's not already
|
|
619
|
-
* set,
|
|
620
|
-
*
|
|
621
|
-
* @param {
|
|
622
|
-
* @param {
|
|
623
|
-
* @param {
|
|
624
|
-
* @
|
|
830
|
+
* set, three additional parameters, `outputScript`, `satoshis` and `data` can be provided.
|
|
831
|
+
* @param {Input} input - The input to add
|
|
832
|
+
* @param {Script|string} [outputScript] - The output script (required if input doesn't have output)
|
|
833
|
+
* @param {number} [satoshis] - The satoshis amount (required if input doesn't have output)
|
|
834
|
+
* @param {Buffer|string} [data] - Additional data for the output
|
|
835
|
+
* @returns {Transaction} The transaction instance for chaining
|
|
836
|
+
* @throws {errors.Transaction.NeedMoreInfo} If input has no output and missing required params
|
|
625
837
|
*/
|
|
626
838
|
Transaction.prototype.addInput = function (input, outputScript, satoshis, data) {
|
|
627
839
|
$.checkArgumentType(input, Input, 'input');
|
|
@@ -724,8 +936,10 @@ Transaction.prototype.change = function (address, data) {
|
|
|
724
936
|
return this;
|
|
725
937
|
};
|
|
726
938
|
|
|
939
|
+
|
|
727
940
|
/**
|
|
728
|
-
*
|
|
941
|
+
* Gets the change output from the transaction outputs.
|
|
942
|
+
* @returns {Output|null} The change output object if defined, otherwise null.
|
|
729
943
|
*/
|
|
730
944
|
Transaction.prototype.getChangeOutput = function () {
|
|
731
945
|
if (!_.isUndefined(this._changeIndex)) {
|
|
@@ -734,18 +948,16 @@ Transaction.prototype.getChangeOutput = function () {
|
|
|
734
948
|
return null;
|
|
735
949
|
};
|
|
736
950
|
|
|
951
|
+
|
|
737
952
|
/**
|
|
738
|
-
*
|
|
953
|
+
* Gets the change address for this transaction.
|
|
954
|
+
* @returns {Address|null} The change address if set, otherwise null.
|
|
739
955
|
*/
|
|
740
956
|
Transaction.prototype.getChangeAddress = function () {
|
|
741
957
|
return this._changeAddress ? this._changeAddress : null;
|
|
742
958
|
};
|
|
743
959
|
|
|
744
|
-
|
|
745
|
-
* @typedef {Object} Transaction~toObject
|
|
746
|
-
* @property {(string|Address)} address
|
|
747
|
-
* @property {number} satoshis
|
|
748
|
-
*/
|
|
960
|
+
|
|
749
961
|
|
|
750
962
|
/**
|
|
751
963
|
* Add an output to the transaction.
|
|
@@ -753,7 +965,7 @@ Transaction.prototype.getChangeAddress = function () {
|
|
|
753
965
|
* Beware that this resets all the signatures for inputs (in further versions,
|
|
754
966
|
* SIGHASH_SINGLE or SIGHASH_NONE signatures will not be reset).
|
|
755
967
|
*
|
|
756
|
-
* @param {(string|Address|Array
|
|
968
|
+
* @param {(string|Address|Array<Address>)} address
|
|
757
969
|
* @param {number} amount in satoshis
|
|
758
970
|
* @return {Transaction} this, for chaining
|
|
759
971
|
*/
|
|
@@ -843,6 +1055,11 @@ Transaction.prototype.clearOutputs = function () {
|
|
|
843
1055
|
return this;
|
|
844
1056
|
};
|
|
845
1057
|
|
|
1058
|
+
/**
|
|
1059
|
+
* Adds an output to the transaction.
|
|
1060
|
+
* @param {Output} output - The output to add to the transaction.
|
|
1061
|
+
* @private
|
|
1062
|
+
*/
|
|
846
1063
|
Transaction.prototype._addOutput = function (output) {
|
|
847
1064
|
this.outputs.push(output);
|
|
848
1065
|
this._outputAmount = undefined;
|
|
@@ -852,6 +1069,7 @@ Transaction.prototype._addOutput = function (output) {
|
|
|
852
1069
|
* Calculates or gets the total output amount in satoshis
|
|
853
1070
|
*
|
|
854
1071
|
* @return {Number} the transaction total output amount
|
|
1072
|
+
* @private
|
|
855
1073
|
*/
|
|
856
1074
|
Transaction.prototype._getOutputAmount = function () {
|
|
857
1075
|
if (_.isUndefined(this._outputAmount)) {
|
|
@@ -868,6 +1086,7 @@ Transaction.prototype._getOutputAmount = function () {
|
|
|
868
1086
|
* Calculates or gets the total input amount in satoshis
|
|
869
1087
|
*
|
|
870
1088
|
* @return {Number} the transaction total input amount
|
|
1089
|
+
* @private
|
|
871
1090
|
*/
|
|
872
1091
|
Transaction.prototype._getInputAmount = function () {
|
|
873
1092
|
if (_.isUndefined(this._inputAmount)) {
|
|
@@ -883,6 +1102,15 @@ Transaction.prototype._getInputAmount = function () {
|
|
|
883
1102
|
return this._inputAmount;
|
|
884
1103
|
};
|
|
885
1104
|
|
|
1105
|
+
/**
|
|
1106
|
+
* Updates the change output of the transaction.
|
|
1107
|
+
* - Removes existing change output if present.
|
|
1108
|
+
* - Calculates available change amount after fee.
|
|
1109
|
+
* - Adds new change output if amount is above dust threshold.
|
|
1110
|
+
* - Clears any existing signatures.
|
|
1111
|
+
* @throws {TransactionAlreadySealed} If transaction is already sealed.
|
|
1112
|
+
* @private
|
|
1113
|
+
*/
|
|
886
1114
|
Transaction.prototype._updateChangeOutput = function () {
|
|
887
1115
|
if (this.sealed) {
|
|
888
1116
|
throw new errors.Transaction.TransactionAlreadySealed();
|
|
@@ -953,16 +1181,25 @@ Transaction.prototype.getFee = function () {
|
|
|
953
1181
|
|
|
954
1182
|
/**
|
|
955
1183
|
* Estimates fee from serialized transaction size in bytes.
|
|
1184
|
+
* @private
|
|
956
1185
|
*/
|
|
957
1186
|
Transaction.prototype._estimateFee = function () {
|
|
958
1187
|
var estimatedSize = this._estimateSize();
|
|
959
1188
|
return Math.ceil((estimatedSize / 1000) * (this._feePerKb || Transaction.FEE_PER_KB));
|
|
960
1189
|
};
|
|
961
1190
|
|
|
1191
|
+
/**
|
|
1192
|
+
* Calculates the unspent value (difference between input and output amounts) of the transaction.
|
|
1193
|
+
* @returns {number} The unspent value (input amount minus output amount).
|
|
1194
|
+
*/
|
|
962
1195
|
Transaction.prototype.getUnspentValue = function () {
|
|
963
1196
|
return this._getInputAmount() - this._getOutputAmount();
|
|
964
1197
|
};
|
|
965
1198
|
|
|
1199
|
+
/**
|
|
1200
|
+
* Clears all signatures from the transaction inputs.
|
|
1201
|
+
* @private
|
|
1202
|
+
*/
|
|
966
1203
|
Transaction.prototype._clearSignatures = function () {
|
|
967
1204
|
_.each(this.inputs, function (input) {
|
|
968
1205
|
input.clearSignatures();
|
|
@@ -980,10 +1217,24 @@ Transaction.prototype._clearSignatures = function () {
|
|
|
980
1217
|
// ??? script
|
|
981
1218
|
//
|
|
982
1219
|
// 4 locktime
|
|
1220
|
+
/**
|
|
1221
|
+
* Gets the estimated size of the transaction.
|
|
1222
|
+
* @returns {number} The estimated size in bytes.
|
|
1223
|
+
*/
|
|
983
1224
|
Transaction.prototype.getEstimateSize = function () {
|
|
984
1225
|
return this._estimateSize();
|
|
985
1226
|
};
|
|
986
1227
|
|
|
1228
|
+
/**
|
|
1229
|
+
* Estimates the total size of the transaction in bytes.
|
|
1230
|
+
* Calculates the sum of:
|
|
1231
|
+
* - Fixed header size (version + locktime)
|
|
1232
|
+
* - Varint-encoded input/output counts
|
|
1233
|
+
* - Each input's estimated size
|
|
1234
|
+
* - Each output's fixed size
|
|
1235
|
+
* @returns {number} The estimated transaction size in bytes
|
|
1236
|
+
* @private
|
|
1237
|
+
*/
|
|
987
1238
|
Transaction.prototype._estimateSize = function () {
|
|
988
1239
|
var result = 4 + 4; // size of version + size of locktime
|
|
989
1240
|
result += Varint(this.inputs.length).toBuffer().length;
|
|
@@ -997,12 +1248,21 @@ Transaction.prototype._estimateSize = function () {
|
|
|
997
1248
|
return result;
|
|
998
1249
|
};
|
|
999
1250
|
|
|
1251
|
+
/**
|
|
1252
|
+
* Removes an output from the transaction by index.
|
|
1253
|
+
* @param {number} index - The index of the output to remove.
|
|
1254
|
+
* @private
|
|
1255
|
+
*/
|
|
1000
1256
|
Transaction.prototype._removeOutput = function (index) {
|
|
1001
1257
|
var output = this.outputs[index];
|
|
1002
1258
|
this.outputs = _.without(this.outputs, output);
|
|
1003
1259
|
this._outputAmount = undefined;
|
|
1004
1260
|
};
|
|
1005
1261
|
|
|
1262
|
+
/**
|
|
1263
|
+
* Removes an output from the transaction at the specified index and updates the change output.
|
|
1264
|
+
* @param {number} index - The index of the output to remove.
|
|
1265
|
+
*/
|
|
1006
1266
|
Transaction.prototype.removeOutput = function (index) {
|
|
1007
1267
|
this._removeOutput(index);
|
|
1008
1268
|
this._updateChangeOutput();
|
|
@@ -1073,6 +1333,16 @@ Transaction.prototype.sortInputs = function (sortingFunction) {
|
|
|
1073
1333
|
return this;
|
|
1074
1334
|
};
|
|
1075
1335
|
|
|
1336
|
+
/**
|
|
1337
|
+
* Updates the order of transaction outputs while maintaining consistency.
|
|
1338
|
+
* Validates that new outputs match original outputs before applying the change.
|
|
1339
|
+
* Adjusts the change output index if it exists.
|
|
1340
|
+
*
|
|
1341
|
+
* @param {Array} newOutputs - The new ordered outputs array
|
|
1342
|
+
* @returns {Transaction} Returns the transaction instance for chaining
|
|
1343
|
+
* @throws {Transaction.InvalidSorting} If output arrays don't match
|
|
1344
|
+
* @private
|
|
1345
|
+
*/
|
|
1076
1346
|
Transaction.prototype._newOutputOrder = function (newOutputs) {
|
|
1077
1347
|
var isInvalidSorting =
|
|
1078
1348
|
this.outputs.length !== newOutputs.length ||
|
|
@@ -1090,6 +1360,12 @@ Transaction.prototype._newOutputOrder = function (newOutputs) {
|
|
|
1090
1360
|
return this;
|
|
1091
1361
|
};
|
|
1092
1362
|
|
|
1363
|
+
/**
|
|
1364
|
+
* Removes an input from the transaction by either its index or txId/outputIndex pair.
|
|
1365
|
+
* @param {string|number} txId - Transaction ID (as hex string) or input index if outputIndex is omitted.
|
|
1366
|
+
* @param {number} [outputIndex] - Output index of the input to remove (required if txId is string).
|
|
1367
|
+
* @throws {Transaction.InvalidIndex} If input index is out of bounds.
|
|
1368
|
+
*/
|
|
1093
1369
|
Transaction.prototype.removeInput = function (txId, outputIndex) {
|
|
1094
1370
|
var index;
|
|
1095
1371
|
if (!outputIndex && _.isNumber(txId)) {
|
|
@@ -1108,17 +1384,16 @@ Transaction.prototype.removeInput = function (txId, outputIndex) {
|
|
|
1108
1384
|
this._updateChangeOutput();
|
|
1109
1385
|
};
|
|
1110
1386
|
|
|
1111
|
-
/* Signature handling */
|
|
1112
1387
|
|
|
1113
1388
|
/**
|
|
1114
1389
|
* Sign the transaction using one or more private keys.
|
|
1115
1390
|
*
|
|
1116
1391
|
* It tries to sign each input, verifying that the signature will be valid
|
|
1117
1392
|
* (matches a public key).
|
|
1118
|
-
*
|
|
1119
|
-
* @param {
|
|
1120
|
-
* @
|
|
1121
|
-
* @
|
|
1393
|
+
* @param {Buffer|Array<Buffer>} privateKey - Private key(s) to sign the transaction with.
|
|
1394
|
+
* @param {number} [sigtype] - Optional signature type.
|
|
1395
|
+
* @returns {Transaction} Returns the transaction instance for chaining.
|
|
1396
|
+
* @throws {Error} Throws if not all UTXO information is available.
|
|
1122
1397
|
*/
|
|
1123
1398
|
Transaction.prototype.sign = function (privateKey, sigtype) {
|
|
1124
1399
|
$.checkState(
|
|
@@ -1141,6 +1416,12 @@ Transaction.prototype.sign = function (privateKey, sigtype) {
|
|
|
1141
1416
|
return this;
|
|
1142
1417
|
};
|
|
1143
1418
|
|
|
1419
|
+
/**
|
|
1420
|
+
* Generates signatures for all inputs in the transaction using the provided private key.
|
|
1421
|
+
* @param {string|PrivateKey} privKey - The private key to sign with (can be string or PrivateKey instance).
|
|
1422
|
+
* @param {number} [sigtype=Signature.SIGHASH_ALL] - The signature hash type (defaults to SIGHASH_ALL).
|
|
1423
|
+
* @returns {Array} Array of generated signatures for the transaction inputs.
|
|
1424
|
+
*/
|
|
1144
1425
|
Transaction.prototype.getSignatures = function (privKey, sigtype) {
|
|
1145
1426
|
privKey = new PrivateKey(privKey);
|
|
1146
1427
|
// By default, signs using SIGHASH_ALL
|
|
@@ -1174,6 +1455,12 @@ Transaction.prototype.applySignature = function (signature) {
|
|
|
1174
1455
|
return this;
|
|
1175
1456
|
};
|
|
1176
1457
|
|
|
1458
|
+
/**
|
|
1459
|
+
* Checks if all inputs in the transaction are fully signed.
|
|
1460
|
+
* @returns {boolean} True if all inputs have valid signatures, false otherwise.
|
|
1461
|
+
* @throws {errors.Transaction.UnableToVerifySignature} If any input has an unrecognized script kind
|
|
1462
|
+
* or insufficient information to verify signatures (common when deserializing transactions).
|
|
1463
|
+
*/
|
|
1177
1464
|
Transaction.prototype.isFullySigned = function () {
|
|
1178
1465
|
_.each(this.inputs, function (input) {
|
|
1179
1466
|
if (input.isFullySigned === Input.prototype.isFullySigned) {
|
|
@@ -1190,6 +1477,12 @@ Transaction.prototype.isFullySigned = function () {
|
|
|
1190
1477
|
);
|
|
1191
1478
|
};
|
|
1192
1479
|
|
|
1480
|
+
/**
|
|
1481
|
+
* Validates a signature for a transaction input.
|
|
1482
|
+
* @param {Object} signature - The signature object to validate.
|
|
1483
|
+
* @throws {errors.Transaction.UnableToVerifySignature} If the input script is unrecognized or lacks execution info.
|
|
1484
|
+
* @returns {boolean} True if the signature is valid for the specified input.
|
|
1485
|
+
*/
|
|
1193
1486
|
Transaction.prototype.isValidSignature = function (signature) {
|
|
1194
1487
|
var self = this;
|
|
1195
1488
|
if (this.inputs[signature.inputIndex].isValidSignature === Input.prototype.isValidSignature) {
|
|
@@ -1201,20 +1494,36 @@ Transaction.prototype.isValidSignature = function (signature) {
|
|
|
1201
1494
|
return this.inputs[signature.inputIndex].isValidSignature(self, signature);
|
|
1202
1495
|
};
|
|
1203
1496
|
|
|
1497
|
+
|
|
1204
1498
|
/**
|
|
1205
|
-
*
|
|
1499
|
+
* Verifies a signature for this transaction.
|
|
1500
|
+
* @param {Buffer} sig - The signature to verify.
|
|
1501
|
+
* @param {Buffer} pubkey - The public key corresponding to the signature.
|
|
1502
|
+
* @param {number} nin - The input index being signed.
|
|
1503
|
+
* @returns {boolean} True if the signature is valid, false otherwise.
|
|
1206
1504
|
*/
|
|
1207
1505
|
Transaction.prototype.verifySignature = function (sig, pubkey, nin) {
|
|
1208
1506
|
return Sighash.verify(this, sig, pubkey, nin);
|
|
1209
1507
|
};
|
|
1210
1508
|
|
|
1509
|
+
|
|
1211
1510
|
/**
|
|
1212
1511
|
* Check that a transaction passes basic sanity tests. If not, return a string
|
|
1213
1512
|
* describing the error. This function contains the same logic as
|
|
1214
1513
|
* CheckTransaction in bitcoin core.
|
|
1514
|
+
*
|
|
1515
|
+
* Checks include:
|
|
1516
|
+
* - Non-empty inputs and outputs
|
|
1517
|
+
* - Valid output satoshis (non-negative, not exceeding MAX_MONEY)
|
|
1518
|
+
* - No duplicate inputs
|
|
1519
|
+
* - Coinbase script size validation (if coinbase)
|
|
1520
|
+
* - Input null checks and verification (if not coinbase and notVerifyInput is false)
|
|
1521
|
+
*
|
|
1522
|
+
* @param {boolean} [notVerifyInput=false] - Whether to skip input verification
|
|
1523
|
+
* @returns {true|string} Returns true if valid, or an error message string if invalid
|
|
1215
1524
|
*/
|
|
1216
1525
|
Transaction.prototype.verify = function (notVerifyInput) {
|
|
1217
|
-
|
|
1526
|
+
// Basic checks that don't depend on any context
|
|
1218
1527
|
if (this.inputs.length === 0) {
|
|
1219
1528
|
return 'transaction txins empty';
|
|
1220
1529
|
}
|
|
@@ -1275,18 +1584,26 @@ Transaction.prototype.verify = function (notVerifyInput) {
|
|
|
1275
1584
|
return true;
|
|
1276
1585
|
};
|
|
1277
1586
|
|
|
1587
|
+
|
|
1278
1588
|
/**
|
|
1279
|
-
*
|
|
1589
|
+
* Checks if the transaction is a coinbase transaction.
|
|
1590
|
+
* A coinbase transaction has exactly one input and that input is null.
|
|
1591
|
+
* @returns {boolean} True if the transaction is a coinbase, false otherwise.
|
|
1280
1592
|
*/
|
|
1281
1593
|
Transaction.prototype.isCoinbase = function () {
|
|
1282
1594
|
return this.inputs.length === 1 && this.inputs[0].isNull();
|
|
1283
1595
|
};
|
|
1284
1596
|
|
|
1597
|
+
|
|
1285
1598
|
/**
|
|
1286
|
-
*
|
|
1287
|
-
* @param {number
|
|
1288
|
-
* @param {
|
|
1289
|
-
* @
|
|
1599
|
+
* Sets the input script for a transaction input.
|
|
1600
|
+
* @param {number|Object} options - Either an input index number or an options object
|
|
1601
|
+
* @param {number} [options.inputIndex=0] - Input index if options is an object
|
|
1602
|
+
* @param {string} [options.privateKey] - Private key for signing
|
|
1603
|
+
* @param {number} [options.sigtype=Signature.SIGHASH_ALL] - Signature hash type
|
|
1604
|
+
* @param {boolean} [options.isLowS=false] - Whether to use low-S signatures
|
|
1605
|
+
* @param {Function|Script} unlockScriptOrCallback - Either a script or callback function that returns a script
|
|
1606
|
+
* @returns {Transaction} Returns the transaction instance for chaining
|
|
1290
1607
|
*/
|
|
1291
1608
|
Transaction.prototype.setInputScript = function (options, unlockScriptOrCallback) {
|
|
1292
1609
|
var inputIndex = 0;
|
|
@@ -1320,51 +1637,26 @@ Transaction.prototype.setInputScript = function (options, unlockScriptOrCallback
|
|
|
1320
1637
|
return this;
|
|
1321
1638
|
};
|
|
1322
1639
|
|
|
1640
|
+
|
|
1323
1641
|
/**
|
|
1324
|
-
*
|
|
1325
|
-
* @param {number
|
|
1326
|
-
* @param {
|
|
1327
|
-
* @returns
|
|
1642
|
+
* Sets the sequence number for a specific transaction input.
|
|
1643
|
+
* @param {number} inputIndex - The index of the input to update.
|
|
1644
|
+
* @param {number} sequence - The sequence number to set.
|
|
1645
|
+
* @returns {Transaction} Returns the transaction instance for chaining.
|
|
1328
1646
|
*/
|
|
1329
|
-
Transaction.prototype.setInputScriptAsync = async function (options, unlockScriptOrCallback) {
|
|
1330
|
-
var inputIndex = 0;
|
|
1331
|
-
var sigtype;
|
|
1332
|
-
var isLowS = false;
|
|
1333
|
-
if (typeof options === 'number') {
|
|
1334
|
-
inputIndex = options;
|
|
1335
|
-
sigtype = Signature.SIGHASH_ALL;
|
|
1336
|
-
} else {
|
|
1337
|
-
inputIndex = options.inputIndex || 0;
|
|
1338
|
-
sigtype = options.sigtype || Signature.SIGHASH_ALL;
|
|
1339
|
-
isLowS = options.isLowS || false;
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
if (unlockScriptOrCallback instanceof Function) {
|
|
1343
|
-
var outputInPrevTx = this.inputs[inputIndex].output;
|
|
1344
|
-
this._inputsMap.set(inputIndex, {
|
|
1345
|
-
sigtype,
|
|
1346
|
-
isLowS,
|
|
1347
|
-
callback: unlockScriptOrCallback,
|
|
1348
|
-
});
|
|
1349
|
-
var unlockScript = await unlockScriptOrCallback(this, outputInPrevTx);
|
|
1350
|
-
this.inputs[inputIndex].setScript(unlockScript);
|
|
1351
|
-
} else {
|
|
1352
|
-
throw new errors.InvalidArgument('Must provide a callback returns a unlocking script');
|
|
1353
|
-
}
|
|
1354
|
-
|
|
1355
|
-
return this;
|
|
1356
|
-
};
|
|
1357
|
-
|
|
1358
1647
|
Transaction.prototype.setInputSequence = function (inputIndex, sequence) {
|
|
1359
1648
|
this.inputs[inputIndex].sequenceNumber = sequence;
|
|
1360
1649
|
return this;
|
|
1361
1650
|
};
|
|
1362
1651
|
|
|
1652
|
+
|
|
1363
1653
|
/**
|
|
1364
|
-
*
|
|
1365
|
-
*
|
|
1366
|
-
*
|
|
1367
|
-
* @
|
|
1654
|
+
* Sets an output at the specified index, either directly or via a callback function.
|
|
1655
|
+
* If a callback is provided, it will be invoked with the transaction instance to generate the output value.
|
|
1656
|
+
* Automatically updates the change output after setting.
|
|
1657
|
+
* @param {number} outputIndex - The index of the output to set
|
|
1658
|
+
* @param {any|Function} outputOrcb - The output value or a callback function that returns the output value
|
|
1659
|
+
* @returns {Transaction} Returns the transaction instance for chaining
|
|
1368
1660
|
*/
|
|
1369
1661
|
Transaction.prototype.setOutput = function (outputIndex, outputOrcb) {
|
|
1370
1662
|
if (outputOrcb instanceof Function) {
|
|
@@ -1378,9 +1670,15 @@ Transaction.prototype.setOutput = function (outputIndex, outputOrcb) {
|
|
|
1378
1670
|
return this;
|
|
1379
1671
|
};
|
|
1380
1672
|
|
|
1673
|
+
|
|
1381
1674
|
/**
|
|
1382
|
-
*
|
|
1383
|
-
*
|
|
1675
|
+
* Seals the transaction by processing all outputs and inputs.
|
|
1676
|
+
* - For each output, executes the registered callback to generate the final output.
|
|
1677
|
+
* - Updates the change output if applicable.
|
|
1678
|
+
* - For each input, generates and sets the unlock script using the registered callback.
|
|
1679
|
+
* - If a private key is provided, signs the transaction.
|
|
1680
|
+
* - Marks the transaction as sealed and returns the instance.
|
|
1681
|
+
* @returns {Transaction} The sealed transaction instance.
|
|
1384
1682
|
*/
|
|
1385
1683
|
Transaction.prototype.seal = function () {
|
|
1386
1684
|
var self = this;
|
|
@@ -1406,54 +1704,21 @@ Transaction.prototype.seal = function () {
|
|
|
1406
1704
|
return this;
|
|
1407
1705
|
};
|
|
1408
1706
|
|
|
1707
|
+
|
|
1409
1708
|
/**
|
|
1410
|
-
*
|
|
1411
|
-
*
|
|
1709
|
+
* Sets the lock time for the transaction.
|
|
1710
|
+
* @param {number} nLockTime - The lock time to set.
|
|
1711
|
+
* @returns {Transaction} Returns the transaction instance for chaining.
|
|
1412
1712
|
*/
|
|
1413
|
-
Transaction.prototype.sealAsync = async function () {
|
|
1414
|
-
var self = this;
|
|
1415
|
-
|
|
1416
|
-
this._outputsMap.forEach(function (callback, key) {
|
|
1417
|
-
self.outputs[key] = callback(self);
|
|
1418
|
-
});
|
|
1419
|
-
|
|
1420
|
-
this._updateChangeOutput();
|
|
1421
|
-
|
|
1422
|
-
var promises = [];
|
|
1423
|
-
|
|
1424
|
-
this._inputsMap.forEach(function (options, key) {
|
|
1425
|
-
var outputInPrevTx = self.inputs[key].output;
|
|
1426
|
-
|
|
1427
|
-
promises.push(
|
|
1428
|
-
Promise.resolve(options.callback(self, outputInPrevTx)).then((unlockScript) => {
|
|
1429
|
-
return { key, unlockScript };
|
|
1430
|
-
}),
|
|
1431
|
-
);
|
|
1432
|
-
});
|
|
1433
|
-
|
|
1434
|
-
await Promise.all(promises).then((items) => {
|
|
1435
|
-
items.forEach(({ key, unlockScript }) => {
|
|
1436
|
-
self.inputs[key].setScript(unlockScript);
|
|
1437
|
-
});
|
|
1438
|
-
});
|
|
1439
|
-
|
|
1440
|
-
if (this._privateKey) {
|
|
1441
|
-
this.sign(this._privateKey, this._sigType);
|
|
1442
|
-
}
|
|
1443
|
-
|
|
1444
|
-
this.sealed = true;
|
|
1445
|
-
|
|
1446
|
-
return this;
|
|
1447
|
-
};
|
|
1448
|
-
|
|
1449
1713
|
Transaction.prototype.setLockTime = function (nLockTime) {
|
|
1450
1714
|
this.nLockTime = nLockTime;
|
|
1451
1715
|
return this;
|
|
1452
1716
|
};
|
|
1453
1717
|
|
|
1718
|
+
|
|
1454
1719
|
/**
|
|
1455
|
-
*
|
|
1456
|
-
* @returns satoshis
|
|
1720
|
+
* Gets the amount of change (in satoshis) for this transaction.
|
|
1721
|
+
* @returns {number} The change amount in satoshis, or 0 if no change exists.
|
|
1457
1722
|
*/
|
|
1458
1723
|
Transaction.prototype.getChangeAmount = function () {
|
|
1459
1724
|
if (_.isUndefined(this._changeIndex)) {
|
|
@@ -1463,18 +1728,20 @@ Transaction.prototype.getChangeAmount = function () {
|
|
|
1463
1728
|
return this.outputs[this._changeIndex].satoshis;
|
|
1464
1729
|
};
|
|
1465
1730
|
|
|
1731
|
+
|
|
1466
1732
|
/**
|
|
1467
|
-
*
|
|
1468
|
-
* @returns
|
|
1733
|
+
* Gets the estimated fee for the transaction.
|
|
1734
|
+
* @returns {number} The estimated fee value.
|
|
1469
1735
|
*/
|
|
1470
1736
|
Transaction.prototype.getEstimateFee = function () {
|
|
1471
1737
|
return this._estimateFee();
|
|
1472
1738
|
};
|
|
1473
1739
|
|
|
1740
|
+
|
|
1474
1741
|
/**
|
|
1475
|
-
*
|
|
1476
|
-
* @param {number} feePerKb
|
|
1477
|
-
* @returns
|
|
1742
|
+
* Checks if the transaction's fee rate meets or exceeds the expected rate.
|
|
1743
|
+
* @param {number} [feePerKb] - Optional fee per KB (in satoshis). Falls back to instance or default fee.
|
|
1744
|
+
* @returns {boolean} True if actual fee rate (fee/size) >= expected rate.
|
|
1478
1745
|
*/
|
|
1479
1746
|
Transaction.prototype.checkFeeRate = function (feePerKb) {
|
|
1480
1747
|
var fee = this.getUnspentValue();
|
|
@@ -1485,9 +1752,10 @@ Transaction.prototype.checkFeeRate = function (feePerKb) {
|
|
|
1485
1752
|
return realFeeRate >= expectedRate;
|
|
1486
1753
|
};
|
|
1487
1754
|
|
|
1755
|
+
|
|
1488
1756
|
/**
|
|
1489
|
-
*
|
|
1490
|
-
* @returns
|
|
1757
|
+
* Serializes the transaction's inputs (prevTxId and outputIndex) into a hex string.
|
|
1758
|
+
* @returns {string} Hex-encoded serialized input data.
|
|
1491
1759
|
*/
|
|
1492
1760
|
Transaction.prototype.prevouts = function () {
|
|
1493
1761
|
var writer = new BufferWriter();
|
|
@@ -1501,14 +1769,22 @@ Transaction.prototype.prevouts = function () {
|
|
|
1501
1769
|
return buf.toString('hex');
|
|
1502
1770
|
};
|
|
1503
1771
|
|
|
1772
|
+
|
|
1504
1773
|
/**
|
|
1505
|
-
*
|
|
1506
|
-
* @returns if the transaction is sealed
|
|
1774
|
+
* Checks if the transaction is sealed.
|
|
1775
|
+
* @returns {boolean} True if the transaction is sealed, false otherwise.
|
|
1507
1776
|
*/
|
|
1508
1777
|
Transaction.prototype.isSealed = function () {
|
|
1509
1778
|
return this.sealed;
|
|
1510
1779
|
};
|
|
1511
1780
|
|
|
1781
|
+
/**
|
|
1782
|
+
* Gets the preimage for a transaction input.
|
|
1783
|
+
* @param {number} inputIndex - The index of the input to get the preimage for.
|
|
1784
|
+
* @param {number} [sigtype=Signature.SIGHASH_ALL] - The signature hash type.
|
|
1785
|
+
* @param {boolean} [isLowS=false] - Whether to use low-S signatures.
|
|
1786
|
+
* @returns {*} The preimage for the specified input.
|
|
1787
|
+
*/
|
|
1512
1788
|
Transaction.prototype.getPreimage = function (inputIndex, sigtype, isLowS) {
|
|
1513
1789
|
$.checkArgumentType(inputIndex, 'number', 'inputIndex');
|
|
1514
1790
|
sigtype = sigtype || Signature.SIGHASH_ALL;
|
|
@@ -1517,6 +1793,13 @@ Transaction.prototype.getPreimage = function (inputIndex, sigtype, isLowS) {
|
|
|
1517
1793
|
return this.inputs[inputIndex].getPreimage(this, inputIndex, sigtype, isLowS);
|
|
1518
1794
|
};
|
|
1519
1795
|
|
|
1796
|
+
/**
|
|
1797
|
+
* Gets the signature(s) for a transaction input.
|
|
1798
|
+
* @param {number} inputIndex - Index of the input to sign.
|
|
1799
|
+
* @param {Array|Buffer|string} [privateKeys] - Private key(s) to sign with. Defaults to input's privateKey or transaction's _privateKey.
|
|
1800
|
+
* @param {number} [sigtypes] - Signature hash type. Defaults to SIGHASH_ALL.
|
|
1801
|
+
* @returns {string|Array} - Single signature hex string or array of signatures. Returns empty array if no privateKeys provided.
|
|
1802
|
+
*/
|
|
1520
1803
|
Transaction.prototype.getSignature = function (inputIndex, privateKeys, sigtypes) {
|
|
1521
1804
|
$.checkArgumentType(inputIndex, 'number', 'inputIndex');
|
|
1522
1805
|
var results = [];
|
|
@@ -1542,6 +1825,13 @@ Transaction.prototype.getSignature = function (inputIndex, privateKeys, sigtypes
|
|
|
1542
1825
|
return [];
|
|
1543
1826
|
};
|
|
1544
1827
|
|
|
1828
|
+
/**
|
|
1829
|
+
* Adds an input to the transaction from a previous transaction's output.
|
|
1830
|
+
* @param {Transaction} prevTx - The previous transaction containing the output to spend.
|
|
1831
|
+
* @param {number} [outputIndex=0] - The index of the output in the previous transaction.
|
|
1832
|
+
* @returns {Transaction} The transaction instance for chaining.
|
|
1833
|
+
* @throws {Error} If prevTx is not a valid Transaction.
|
|
1834
|
+
*/
|
|
1545
1835
|
Transaction.prototype.addInputFromPrevTx = function (prevTx, outputIndex) {
|
|
1546
1836
|
$.checkArgumentType(prevTx, Transaction, 'prevTx');
|
|
1547
1837
|
|
|
@@ -1570,6 +1860,14 @@ Transaction.prototype.addInputFromPrevTx = function (prevTx, outputIndex) {
|
|
|
1570
1860
|
}
|
|
1571
1861
|
};
|
|
1572
1862
|
|
|
1863
|
+
/**
|
|
1864
|
+
* Adds a dummy input to the transaction with the specified script and satoshis.
|
|
1865
|
+
* The dummy input uses a placeholder script and a fixed previous transaction ID.
|
|
1866
|
+
*
|
|
1867
|
+
* @param {Script} script - The script to use for the output of the dummy input.
|
|
1868
|
+
* @param {number} satoshis - The amount in satoshis for the output of the dummy input.
|
|
1869
|
+
* @returns {Transaction} The transaction instance for chaining.
|
|
1870
|
+
*/
|
|
1573
1871
|
Transaction.prototype.addDummyInput = function (script, satoshis) {
|
|
1574
1872
|
$.checkArgumentType(script, Script, 'script');
|
|
1575
1873
|
$.checkArgumentType(satoshis, 'number', 'satoshis');
|
|
@@ -1599,6 +1897,12 @@ Transaction.prototype.dummyChange = function () {
|
|
|
1599
1897
|
return this.change(Transaction.DUMMY_PRIVATEKEY.toAddress());
|
|
1600
1898
|
};
|
|
1601
1899
|
|
|
1900
|
+
/**
|
|
1901
|
+
* Verifies the script for a specific transaction input.
|
|
1902
|
+
* @param {number} inputIndex - Index of the input to verify.
|
|
1903
|
+
* @throws {errors.Transaction.Input.MissingInput} If input at given index doesn't exist.
|
|
1904
|
+
* @returns {boolean} True if the script verification passes.
|
|
1905
|
+
*/
|
|
1602
1906
|
Transaction.prototype.verifyScript = function (inputIndex) {
|
|
1603
1907
|
$.checkArgumentType(inputIndex, 'number', 'inputIndex');
|
|
1604
1908
|
|
|
@@ -1609,13 +1913,22 @@ Transaction.prototype.verifyScript = function (inputIndex) {
|
|
|
1609
1913
|
return this.inputs[inputIndex].verify(this, inputIndex);
|
|
1610
1914
|
};
|
|
1611
1915
|
|
|
1916
|
+
|
|
1612
1917
|
/**
|
|
1613
|
-
*
|
|
1918
|
+
* Verifies the input script for a specific input index in the transaction.
|
|
1919
|
+
* @param {number} inputIndex - The index of the input to verify.
|
|
1920
|
+
* @returns {boolean} True if the input script is valid, false otherwise.
|
|
1614
1921
|
*/
|
|
1615
1922
|
Transaction.prototype.verifyInputScript = function (inputIndex) {
|
|
1616
1923
|
return this.verifyScript(inputIndex);
|
|
1617
1924
|
};
|
|
1618
1925
|
|
|
1926
|
+
/**
|
|
1927
|
+
* Gets the amount of satoshis for a specific transaction input.
|
|
1928
|
+
* @param {number} inputIndex - The index of the input to query.
|
|
1929
|
+
* @returns {number} The satoshis amount of the specified input.
|
|
1930
|
+
* @throws {errors.Transaction.Input.MissingInput} If the input at the specified index doesn't exist.
|
|
1931
|
+
*/
|
|
1619
1932
|
Transaction.prototype.getInputAmount = function (inputIndex) {
|
|
1620
1933
|
$.checkArgumentType(inputIndex, 'number', 'inputIndex');
|
|
1621
1934
|
|
|
@@ -1626,6 +1939,12 @@ Transaction.prototype.getInputAmount = function (inputIndex) {
|
|
|
1626
1939
|
return this.inputs[inputIndex].output.satoshis;
|
|
1627
1940
|
};
|
|
1628
1941
|
|
|
1942
|
+
/**
|
|
1943
|
+
* Gets the output amount in satoshis for the specified output index.
|
|
1944
|
+
* @param {number} outputIndex - The index of the output to retrieve.
|
|
1945
|
+
* @returns {number} The output amount in satoshis.
|
|
1946
|
+
* @throws {errors.Transaction.MissingOutput} If the output index is invalid.
|
|
1947
|
+
*/
|
|
1629
1948
|
Transaction.prototype.getOutputAmount = function (outputIndex) {
|
|
1630
1949
|
$.checkArgumentType(outputIndex, 'number', 'outputIndex');
|
|
1631
1950
|
|
|
@@ -1636,4 +1955,44 @@ Transaction.prototype.getOutputAmount = function (outputIndex) {
|
|
|
1636
1955
|
return this.outputs[outputIndex].satoshis;
|
|
1637
1956
|
};
|
|
1638
1957
|
|
|
1639
|
-
|
|
1958
|
+
|
|
1959
|
+
/**
|
|
1960
|
+
* Assigns the Input class to the Transaction namespace.
|
|
1961
|
+
* @memberof Transaction
|
|
1962
|
+
* @name Input
|
|
1963
|
+
* @alias Input
|
|
1964
|
+
*/
|
|
1965
|
+
Transaction.Input = Input;
|
|
1966
|
+
/**
|
|
1967
|
+
* Assigns the Output class to Transaction.Output property.
|
|
1968
|
+
* @memberof Transaction
|
|
1969
|
+
* @name Output
|
|
1970
|
+
* @alias Output
|
|
1971
|
+
*/
|
|
1972
|
+
Transaction.Output = Output;
|
|
1973
|
+
|
|
1974
|
+
/**
|
|
1975
|
+
* Attaches the Sighash module to the Transaction class.
|
|
1976
|
+
* @memberof Transaction
|
|
1977
|
+
* @name Sighash
|
|
1978
|
+
* @alias Sighash
|
|
1979
|
+
*/
|
|
1980
|
+
Transaction.Sighash = Sighash;
|
|
1981
|
+
/**
|
|
1982
|
+
* Attaches the UnspentOutput class to the Transaction namespace.
|
|
1983
|
+
* @memberof Transaction
|
|
1984
|
+
* @name UnspentOutput
|
|
1985
|
+
* @alias UnspentOutput
|
|
1986
|
+
*/
|
|
1987
|
+
Transaction.UnspentOutput = UnspentOutput;
|
|
1988
|
+
|
|
1989
|
+
/**
|
|
1990
|
+
* Attaches the TransactionSignature class to the Transaction namespace.
|
|
1991
|
+
* @memberof Transaction
|
|
1992
|
+
* @name Signature
|
|
1993
|
+
* @alias TransactionSignature
|
|
1994
|
+
*/
|
|
1995
|
+
Transaction.Signature = TransactionSignature;
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
export default Transaction;
|