@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
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import _ from '../util/_.js';
|
|
4
|
+
import BN from '../crypto/bn.js';
|
|
5
|
+
import JSUtil from '../util/js.js';
|
|
6
|
+
import BufferWriter from '../encoding/bufferwriter.js';
|
|
7
|
+
import Varint from '../encoding/varint.js';
|
|
8
|
+
import Script from '../script/index.js';
|
|
9
|
+
import $ from '../util/preconditions.js';
|
|
10
|
+
import errors from '../errors/index.js';
|
|
11
|
+
import Hash from '../crypto/hash.js';
|
|
12
|
+
|
|
13
|
+
var MAX_SAFE_INTEGER = 0x1fffffffffffff;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Represents a transaction output in the Bitcoin protocol.
|
|
17
|
+
* @constructor
|
|
18
|
+
* @param {Object} args - The arguments to create an Output.
|
|
19
|
+
* @param {number} args.satoshis - The amount in satoshis.
|
|
20
|
+
* @param {Buffer|string|Script} args.script - The output script (either as Buffer or hex string).
|
|
21
|
+
* @param {Buffer|string} [args.data] - Additional data associated with the output.
|
|
22
|
+
* @throws {TypeError} If arguments are invalid or unrecognized.
|
|
23
|
+
*/
|
|
24
|
+
function Output(args) {
|
|
25
|
+
if (!(this instanceof Output)) {
|
|
26
|
+
return new Output(args);
|
|
27
|
+
}
|
|
28
|
+
if (_.isObject(args)) {
|
|
29
|
+
this.satoshis = args.satoshis;
|
|
30
|
+
if (Buffer.isBuffer(args.script)) {
|
|
31
|
+
this.setScriptFromBuffer(args.script);
|
|
32
|
+
} else {
|
|
33
|
+
var script;
|
|
34
|
+
if (_.isString(args.script) && JSUtil.isHexa(args.script)) {
|
|
35
|
+
script = Buffer.from(args.script, 'hex');
|
|
36
|
+
} else {
|
|
37
|
+
script = args.script;
|
|
38
|
+
}
|
|
39
|
+
this.setScript(script);
|
|
40
|
+
}
|
|
41
|
+
this.setData(args.data)
|
|
42
|
+
} else {
|
|
43
|
+
throw new TypeError('Unrecognized argument for Output');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Gets or sets the script associated with this Output instance.
|
|
48
|
+
* @memberof Output.prototype
|
|
49
|
+
* @name script
|
|
50
|
+
*/
|
|
51
|
+
Object.defineProperty(Output.prototype, 'script', {
|
|
52
|
+
configurable: false,
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () {
|
|
55
|
+
return this._script;
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Gets or sets the transaction output data.
|
|
61
|
+
* @memberof Output.prototype
|
|
62
|
+
* @name data
|
|
63
|
+
* @type {Buffer|string}
|
|
64
|
+
*/
|
|
65
|
+
Object.defineProperty(Output.prototype, 'data', {
|
|
66
|
+
configurable: false,
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function () {
|
|
69
|
+
return this._data;
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
/**
|
|
73
|
+
* Gets the satoshis value of the Output instance.
|
|
74
|
+
* @memberof Output.prototype
|
|
75
|
+
* @name satoshis
|
|
76
|
+
* @type {BN|string|number}
|
|
77
|
+
*/
|
|
78
|
+
Object.defineProperty(Output.prototype, 'satoshis', {
|
|
79
|
+
configurable: false,
|
|
80
|
+
enumerable: true,
|
|
81
|
+
get: function () {
|
|
82
|
+
return this._satoshis;
|
|
83
|
+
},
|
|
84
|
+
set: function (num) {
|
|
85
|
+
if (num instanceof BN) {
|
|
86
|
+
this._satoshisBN = num;
|
|
87
|
+
this._satoshis = num.toNumber();
|
|
88
|
+
} else if (_.isString(num)) {
|
|
89
|
+
this._satoshis = parseInt(num);
|
|
90
|
+
this._satoshisBN = BN.fromNumber(this._satoshis);
|
|
91
|
+
} else {
|
|
92
|
+
$.checkArgument(JSUtil.isNaturalNumber(num), 'Output satoshis is not a natural number');
|
|
93
|
+
this._satoshisBN = BN.fromNumber(num);
|
|
94
|
+
this._satoshis = num;
|
|
95
|
+
}
|
|
96
|
+
$.checkState(JSUtil.isNaturalNumber(this._satoshis), 'Output satoshis is not a natural number');
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Checks if the satoshis value in this output is invalid.
|
|
102
|
+
* @returns {string|boolean} Returns an error message string if invalid (satoshis exceed max safe integer,
|
|
103
|
+
* corrupted value, or negative), otherwise returns false.
|
|
104
|
+
*/
|
|
105
|
+
Output.prototype.invalidSatoshis = function () {
|
|
106
|
+
if (this._satoshis > MAX_SAFE_INTEGER) {
|
|
107
|
+
return 'transaction txout satoshis greater than max safe integer';
|
|
108
|
+
}
|
|
109
|
+
if (this._satoshis !== this._satoshisBN.toNumber()) {
|
|
110
|
+
return 'transaction txout satoshis has corrupted value';
|
|
111
|
+
}
|
|
112
|
+
if (this._satoshis < 0) {
|
|
113
|
+
return 'transaction txout negative';
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Gets the satoshis value as a BN (BigNumber) instance.
|
|
120
|
+
* @memberof Output.prototype
|
|
121
|
+
* @name satoshisBN
|
|
122
|
+
* @type {BN}
|
|
123
|
+
*/
|
|
124
|
+
Object.defineProperty(Output.prototype, 'satoshisBN', {
|
|
125
|
+
configurable: false,
|
|
126
|
+
enumerable: true,
|
|
127
|
+
get: function () {
|
|
128
|
+
return this._satoshisBN;
|
|
129
|
+
},
|
|
130
|
+
set: function (num) {
|
|
131
|
+
this._satoshisBN = num;
|
|
132
|
+
this._satoshis = num.toNumber();
|
|
133
|
+
$.checkState(JSUtil.isNaturalNumber(this._satoshis), 'Output satoshis is not a natural number');
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Converts the Output instance to a plain object representation.
|
|
139
|
+
* The resulting object includes satoshis, script (as hex string), and data (as hex string).
|
|
140
|
+
* @returns {Object} - An object with satoshis, script, and data properties.
|
|
141
|
+
*/
|
|
142
|
+
Output.prototype.toObject = Output.prototype.toJSON = function toObject() {
|
|
143
|
+
var obj = {
|
|
144
|
+
satoshis: this.satoshis,
|
|
145
|
+
};
|
|
146
|
+
obj.script = this._script.toBuffer().toString('hex');
|
|
147
|
+
obj.data = this._data.toString('hex')
|
|
148
|
+
return obj;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Sets the output data.
|
|
153
|
+
* @param {Buffer|string} data - The data to set. Can be a Buffer or hex string.
|
|
154
|
+
* @throws {TypeError} If data is not a Buffer or valid hex string.
|
|
155
|
+
*/
|
|
156
|
+
Output.prototype.setData = function (data) {
|
|
157
|
+
if (!data) {
|
|
158
|
+
this._data = Buffer.from([])
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (Buffer.isBuffer(data)) {
|
|
162
|
+
this._data = data
|
|
163
|
+
} else if (_.isString(data) && JSUtil.isHexa(data)) {
|
|
164
|
+
this._data = Buffer.from(data, 'hex')
|
|
165
|
+
} else {
|
|
166
|
+
throw new TypeError('Invalid argument type: data for output.setData')
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Creates an Output instance from a plain JavaScript object.
|
|
172
|
+
* @param {Object} data - The input object to convert to an Output
|
|
173
|
+
* @returns {Output} A new Output instance
|
|
174
|
+
* @static
|
|
175
|
+
*/
|
|
176
|
+
Output.fromObject = function (data) {
|
|
177
|
+
return new Output(data);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Sets the script for this output from a buffer.
|
|
182
|
+
* @param {Buffer} buffer - The buffer containing the script data.
|
|
183
|
+
* @throws {errors.Script.InvalidBuffer} If the buffer is invalid.
|
|
184
|
+
*/
|
|
185
|
+
Output.prototype.setScriptFromBuffer = function (buffer) {
|
|
186
|
+
try {
|
|
187
|
+
this._script = Script.fromBuffer(buffer);
|
|
188
|
+
this._script._isOutput = true;
|
|
189
|
+
} catch (e) {
|
|
190
|
+
if (e instanceof errors.Script.InvalidBuffer) {
|
|
191
|
+
this._script = null;
|
|
192
|
+
} else {
|
|
193
|
+
throw e;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Sets the script for this output.
|
|
200
|
+
* @param {Script|string|Buffer} script - The script to set, which can be a Script instance, hex string, or Buffer.
|
|
201
|
+
* @returns {Output} Returns the output instance for chaining.
|
|
202
|
+
* @throws {TypeError} Throws if the script type is invalid.
|
|
203
|
+
*/
|
|
204
|
+
Output.prototype.setScript = function (script) {
|
|
205
|
+
if (script instanceof Script) {
|
|
206
|
+
this._script = script;
|
|
207
|
+
this._script._isOutput = true;
|
|
208
|
+
} else if (_.isString(script)) {
|
|
209
|
+
this._script = Script.fromString(script);
|
|
210
|
+
this._script._isOutput = true;
|
|
211
|
+
} else if (Buffer.isBuffer(script)) {
|
|
212
|
+
this.setScriptFromBuffer(script);
|
|
213
|
+
} else {
|
|
214
|
+
throw new TypeError('Invalid argument type: script');
|
|
215
|
+
}
|
|
216
|
+
return this;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Returns a human-readable string representation of the Output object.
|
|
221
|
+
* Format: '<Output (satoshis sats) scriptString>'
|
|
222
|
+
* @returns {string} Formatted string showing satoshis and script inspection result
|
|
223
|
+
*/
|
|
224
|
+
Output.prototype.inspect = function () {
|
|
225
|
+
var scriptStr;
|
|
226
|
+
if (this.script) {
|
|
227
|
+
scriptStr = this.script.inspect();
|
|
228
|
+
}
|
|
229
|
+
return '<Output (' + this.satoshis + ' sats) ' + scriptStr + '>';
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Creates an Output instance from a BufferReader.
|
|
234
|
+
* @param {BufferReader} br - The buffer reader containing output data
|
|
235
|
+
* @returns {Output} A new Output instance
|
|
236
|
+
* @throws {TypeError} If the buffer contains unrecognized output format
|
|
237
|
+
* @static
|
|
238
|
+
*/
|
|
239
|
+
Output.fromBufferReader = function (br) {
|
|
240
|
+
var obj = {}
|
|
241
|
+
obj.satoshis = br.readUInt64LEBN()
|
|
242
|
+
var scriptSize = br.readVarintNum()
|
|
243
|
+
if (scriptSize !== 0) {
|
|
244
|
+
if (br.remaining() < scriptSize) {
|
|
245
|
+
throw new TypeError('Unrecognized Output')
|
|
246
|
+
}
|
|
247
|
+
obj.script = br.read(scriptSize)
|
|
248
|
+
} else {
|
|
249
|
+
obj.script = Buffer.from([])
|
|
250
|
+
}
|
|
251
|
+
var dataSize = br.readVarintNum()
|
|
252
|
+
if (dataSize !== 0) {
|
|
253
|
+
if (br.remaining() < dataSize) {
|
|
254
|
+
throw new TypeError('Unrecognized Output')
|
|
255
|
+
}
|
|
256
|
+
obj.data = br.read(dataSize)
|
|
257
|
+
} else {
|
|
258
|
+
obj.data = Buffer.from([])
|
|
259
|
+
}
|
|
260
|
+
return new Output(obj)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Converts the Output instance to a buffer writer format.
|
|
265
|
+
* @param {boolean} hashScriptPubkey - If true, hashes script and data with SHA256; otherwise writes them directly.
|
|
266
|
+
* @param {BufferWriter} [writer] - Optional BufferWriter instance. If not provided, a new one is created.
|
|
267
|
+
* @returns {BufferWriter} The buffer writer containing the serialized output data.
|
|
268
|
+
*/
|
|
269
|
+
Output.prototype.toBufferWriter = function (hashScriptPubkey, writer) {
|
|
270
|
+
$.checkArgument(typeof hashScriptPubkey === 'boolean', 'hashScriptSig should be boolean')
|
|
271
|
+
if (!writer) {
|
|
272
|
+
writer = new BufferWriter();
|
|
273
|
+
}
|
|
274
|
+
writer.writeUInt64LEBN(this._satoshisBN);
|
|
275
|
+
var script = this._script.toBuffer();
|
|
276
|
+
var data = this._data
|
|
277
|
+
if(hashScriptPubkey) {
|
|
278
|
+
writer.write(Hash.sha256(script))
|
|
279
|
+
writer.write(Hash.sha256(data))
|
|
280
|
+
} else {
|
|
281
|
+
writer.writeVarintNum(script.length);
|
|
282
|
+
writer.write(script);
|
|
283
|
+
writer.writeVarintNum(data.length)
|
|
284
|
+
writer.write(data)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
return writer
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Calculates the total size of the output in bytes.
|
|
293
|
+
* Includes the script size, data size, and their respective varint sizes,
|
|
294
|
+
* plus a fixed 8-byte overhead.
|
|
295
|
+
* 8 value
|
|
296
|
+
* ??? script+data size (VARINT)
|
|
297
|
+
* script size
|
|
298
|
+
* data size
|
|
299
|
+
* @returns {number} The total output size in bytes.
|
|
300
|
+
*/
|
|
301
|
+
Output.prototype.getSize = function () {
|
|
302
|
+
var scriptSize = this.script.toBuffer().length;
|
|
303
|
+
var dataSize = this.data.length
|
|
304
|
+
var varintSize = Varint(scriptSize).toBuffer().length + Varint(dataSize).toBuffer().length
|
|
305
|
+
return 8 + varintSize + scriptSize + dataSize
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Creates a shallow clone of the Output instance.
|
|
310
|
+
* @returns {Output} A new Output instance with the same properties as the original.
|
|
311
|
+
*/
|
|
312
|
+
Output.prototype.clone = function () {
|
|
313
|
+
return Output.fromObject(this.toObject());
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export default Output;
|
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
'use strict'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
import Signature from '../crypto/signature.js';
|
|
3
|
+
import Script from '../script/index.js';
|
|
4
|
+
import Output from './output.js';
|
|
5
|
+
import BufferReader from '../encoding/bufferreader.js';
|
|
6
|
+
import BufferWriter from '../encoding/bufferwriter.js';
|
|
7
|
+
import Hash from '../crypto/hash.js';
|
|
8
|
+
import ECDSA from '../crypto/ecdsa.js';
|
|
9
|
+
import $ from '../util/preconditions.js';
|
|
10
|
+
import _ from '../util/_.js';
|
|
11
11
|
|
|
12
12
|
var SIGHASH_SINGLE_BUG = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Represents a Sighash utility for cryptographic signature operations.
|
|
16
|
+
* @constructor
|
|
17
|
+
*/
|
|
18
|
+
function Sighash() {
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
13
22
|
/**
|
|
14
23
|
* Returns a buffer with the which is hashed with sighash that needs to be signed
|
|
15
24
|
* for OP_CHECKSIG.
|
|
@@ -22,7 +31,7 @@ var SIGHASH_SINGLE_BUG = Buffer.from('000000000000000000000000000000000000000000
|
|
|
22
31
|
* @param {satoshisBN} input's amount (for ForkId signatures)
|
|
23
32
|
*
|
|
24
33
|
*/
|
|
25
|
-
|
|
34
|
+
Sighash.sighashPreimage = function (transaction, sighashType, inputNumber) {
|
|
26
35
|
// Check that all inputs have an output, prevent shallow transaction
|
|
27
36
|
_.each(transaction.inputs, function (input) {
|
|
28
37
|
$.checkState(input.output instanceof Output, 'input.output must be an instance of Output')
|
|
@@ -100,6 +109,22 @@ var sighashPreimage = function sighashPreimage (transaction, sighashType, inputN
|
|
|
100
109
|
return bw.toBuffer()
|
|
101
110
|
}
|
|
102
111
|
|
|
112
|
+
Sighash.getLowSSighashPreimage = function(tx, sigtype, inputIndex) {
|
|
113
|
+
var i = 0;
|
|
114
|
+
do {
|
|
115
|
+
var preimage = Sighash.sighashPreimage(tx, sigtype, inputIndex);
|
|
116
|
+
|
|
117
|
+
var sighash = Hash.sha256sha256(preimage);
|
|
118
|
+
|
|
119
|
+
if (_.isPositiveNumber(sighash.readUInt8()) && _.isPositiveNumber(sighash.readUInt8(31))) {
|
|
120
|
+
return preimage;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
tx.nLockTime++;
|
|
124
|
+
} while (i < Number.MAX_SAFE_INTEGER);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
103
128
|
/**
|
|
104
129
|
* Returns a buffer of length 32 bytes with the hash that needs to be signed
|
|
105
130
|
* for OP_CHECKSIG.
|
|
@@ -110,8 +135,8 @@ var sighashPreimage = function sighashPreimage (transaction, sighashType, inputN
|
|
|
110
135
|
* @param {number} inputNumber the input index for the signature
|
|
111
136
|
*
|
|
112
137
|
*/
|
|
113
|
-
|
|
114
|
-
var preimage = sighashPreimage(transaction, sighashType, inputNumber)
|
|
138
|
+
Sighash.sighash = function (transaction, sighashType, inputNumber) {
|
|
139
|
+
var preimage = Sighash.sighashPreimage(transaction, sighashType, inputNumber)
|
|
115
140
|
if (preimage.compare(SIGHASH_SINGLE_BUG) === 0) return preimage
|
|
116
141
|
var ret = Hash.sha256sha256(preimage)
|
|
117
142
|
ret = new BufferReader(ret).readReverse()
|
|
@@ -127,8 +152,8 @@ var sighash = function sighash (transaction, sighashType, inputNumber) {
|
|
|
127
152
|
* @param {number} inputIndex
|
|
128
153
|
* @return {Signature}
|
|
129
154
|
*/
|
|
130
|
-
function
|
|
131
|
-
var hashbuf = sighash(transaction, sighashType, inputIndex)
|
|
155
|
+
Sighash.sign = function (transaction, privateKey, sighashType, inputIndex) {
|
|
156
|
+
var hashbuf = Sighash.sighash(transaction, sighashType, inputIndex)
|
|
132
157
|
|
|
133
158
|
var sig = ECDSA.sign(hashbuf, privateKey, 'little').set({
|
|
134
159
|
nhashtype: sighashType
|
|
@@ -149,19 +174,14 @@ function sign (transaction, privateKey, sighashType, inputIndex) {
|
|
|
149
174
|
* @param {flags} verification flags
|
|
150
175
|
* @return {boolean}
|
|
151
176
|
*/
|
|
152
|
-
function
|
|
177
|
+
Sighash.verify = function (transaction, signature, publicKey, inputIndex) {
|
|
153
178
|
$.checkArgument(!_.isUndefined(transaction))
|
|
154
179
|
$.checkArgument(!_.isUndefined(signature) && !_.isUndefined(signature.nhashtype))
|
|
155
|
-
var hashbuf = sighash(transaction, signature.nhashtype, inputIndex)
|
|
180
|
+
var hashbuf = Sighash.sighash(transaction, signature.nhashtype, inputIndex)
|
|
156
181
|
return ECDSA.verify(hashbuf, signature, publicKey, 'little')
|
|
157
182
|
}
|
|
158
183
|
|
|
159
184
|
/**
|
|
160
185
|
* @namespace Signing
|
|
161
186
|
*/
|
|
162
|
-
|
|
163
|
-
sighashPreimage: sighashPreimage,
|
|
164
|
-
sighash: sighash,
|
|
165
|
-
sign: sign,
|
|
166
|
-
verify: verify
|
|
167
|
-
}
|
|
187
|
+
export default Sighash;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import _ from '../util/_.js';
|
|
4
|
+
import $ from '../util/preconditions.js';
|
|
5
|
+
import inherits from 'inherits';
|
|
6
|
+
import JSUtil from '../util/js.js';
|
|
7
|
+
import PublicKey from '../publickey.js';
|
|
8
|
+
import errors from '../errors/index.js';
|
|
9
|
+
import Signature from '../crypto/signature.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @desc
|
|
13
|
+
* Wrapper around Signature with fields related to signing a transaction specifically
|
|
14
|
+
*
|
|
15
|
+
* @param {Object|string|TransactionSignature} arg
|
|
16
|
+
* @constructor
|
|
17
|
+
*/
|
|
18
|
+
function TransactionSignature(arg) {
|
|
19
|
+
if (!(this instanceof TransactionSignature)) {
|
|
20
|
+
return new TransactionSignature(arg);
|
|
21
|
+
}
|
|
22
|
+
if (arg instanceof TransactionSignature) {
|
|
23
|
+
return arg;
|
|
24
|
+
}
|
|
25
|
+
if (_.isObject(arg)) {
|
|
26
|
+
return this._fromObject(arg);
|
|
27
|
+
}
|
|
28
|
+
throw new errors.InvalidArgument('TransactionSignatures must be instantiated from an object');
|
|
29
|
+
}
|
|
30
|
+
inherits(TransactionSignature, Signature);
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Initializes the TransactionSignature instance from a plain object.
|
|
34
|
+
* Validates and converts object properties into appropriate types.
|
|
35
|
+
* @param {Object} arg - The source object containing signature data
|
|
36
|
+
* @param {Buffer|string} arg.prevTxId - Previous transaction ID (buffer or hex string)
|
|
37
|
+
* @param {number} arg.outputIndex - Output index
|
|
38
|
+
* @param {number} arg.inputIndex - Input index
|
|
39
|
+
* @param {Signature|Buffer|string} arg.signature - Signature in various formats
|
|
40
|
+
* @param {number} arg.sigtype - Signature type
|
|
41
|
+
* @returns {TransactionSignature} Returns the instance for chaining
|
|
42
|
+
* @private
|
|
43
|
+
*/
|
|
44
|
+
TransactionSignature.prototype._fromObject = function (arg) {
|
|
45
|
+
this._checkObjectArgs(arg);
|
|
46
|
+
this.publicKey = new PublicKey(arg.publicKey);
|
|
47
|
+
this.prevTxId = Buffer.isBuffer(arg.prevTxId) ? arg.prevTxId : Buffer.from(arg.prevTxId, 'hex');
|
|
48
|
+
this.outputIndex = arg.outputIndex;
|
|
49
|
+
this.inputIndex = arg.inputIndex;
|
|
50
|
+
this.signature =
|
|
51
|
+
arg.signature instanceof Signature
|
|
52
|
+
? arg.signature
|
|
53
|
+
: Buffer.isBuffer(arg.signature)
|
|
54
|
+
? Signature.fromBuffer(arg.signature)
|
|
55
|
+
: Signature.fromString(arg.signature);
|
|
56
|
+
this.sigtype = arg.sigtype;
|
|
57
|
+
return this;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Validates the arguments for a transaction signature object.
|
|
62
|
+
* @private
|
|
63
|
+
* @param {Object} arg - The signature object to validate
|
|
64
|
+
* @param {PublicKey} arg.publicKey - The public key
|
|
65
|
+
* @param {number} arg.inputIndex - The input index (must be a number)
|
|
66
|
+
* @param {number} arg.outputIndex - The output index (must be a number)
|
|
67
|
+
* @param {Buffer|string|Signature} arg.signature - The signature (must be Buffer, hex string, or Signature)
|
|
68
|
+
* @param {Buffer|string} arg.prevTxId - Previous transaction ID (must be Buffer or hex string)
|
|
69
|
+
* @param {number} arg.sigtype - Signature type (must be a number)
|
|
70
|
+
* @throws {Error} If any argument fails validation
|
|
71
|
+
*/
|
|
72
|
+
TransactionSignature.prototype._checkObjectArgs = function (arg) {
|
|
73
|
+
$.checkArgument(PublicKey(arg.publicKey), 'publicKey');
|
|
74
|
+
$.checkArgument(!_.isUndefined(arg.inputIndex), 'inputIndex');
|
|
75
|
+
$.checkArgument(!_.isUndefined(arg.outputIndex), 'outputIndex');
|
|
76
|
+
$.checkState(_.isNumber(arg.inputIndex), 'inputIndex must be a number');
|
|
77
|
+
$.checkState(_.isNumber(arg.outputIndex), 'outputIndex must be a number');
|
|
78
|
+
$.checkArgument(arg.signature, 'signature');
|
|
79
|
+
$.checkArgument(arg.prevTxId, 'prevTxId');
|
|
80
|
+
$.checkState(
|
|
81
|
+
arg.signature instanceof Signature ||
|
|
82
|
+
Buffer.isBuffer(arg.signature) ||
|
|
83
|
+
JSUtil.isHexa(arg.signature),
|
|
84
|
+
'signature must be a buffer or hexa value',
|
|
85
|
+
);
|
|
86
|
+
$.checkState(
|
|
87
|
+
Buffer.isBuffer(arg.prevTxId) || JSUtil.isHexa(arg.prevTxId),
|
|
88
|
+
'prevTxId must be a buffer or hexa value',
|
|
89
|
+
);
|
|
90
|
+
$.checkArgument(arg.sigtype, 'sigtype');
|
|
91
|
+
$.checkState(_.isNumber(arg.sigtype), 'sigtype must be a number');
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Serializes a transaction to a plain JS object
|
|
96
|
+
* @return {Object}
|
|
97
|
+
*/
|
|
98
|
+
TransactionSignature.prototype.toObject = TransactionSignature.prototype.toJSON =
|
|
99
|
+
function toObject() {
|
|
100
|
+
return {
|
|
101
|
+
publicKey: this.publicKey.toString(),
|
|
102
|
+
prevTxId: this.prevTxId.toString('hex'),
|
|
103
|
+
outputIndex: this.outputIndex,
|
|
104
|
+
inputIndex: this.inputIndex,
|
|
105
|
+
signature: this.signature.toString(),
|
|
106
|
+
sigtype: this.sigtype,
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Builds a TransactionSignature from an object
|
|
112
|
+
* @param {Object} object
|
|
113
|
+
* @return {TransactionSignature}
|
|
114
|
+
*/
|
|
115
|
+
TransactionSignature.fromObject = function (object) {
|
|
116
|
+
$.checkArgument(object);
|
|
117
|
+
return new TransactionSignature(object);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export default TransactionSignature;
|