@btc-vision/bitcoin 6.3.0
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/LICENSE +21 -0
- package/README.md +201 -0
- package/package.json +95 -0
- package/src/address.d.ts +42 -0
- package/src/address.js +191 -0
- package/src/bip66.d.ts +7 -0
- package/src/bip66.js +103 -0
- package/src/block.d.ts +30 -0
- package/src/block.js +224 -0
- package/src/bufferutils.d.ts +54 -0
- package/src/bufferutils.js +182 -0
- package/src/crypto.d.ts +18 -0
- package/src/crypto.js +128 -0
- package/src/ecc_lib.d.ts +17 -0
- package/src/ecc_lib.js +122 -0
- package/src/hooks/AdvancedSignatureManager.d.ts +44 -0
- package/src/hooks/AdvancedSignatureManager.js +88 -0
- package/src/hooks/HookedSigner.d.ts +4 -0
- package/src/hooks/HookedSigner.js +90 -0
- package/src/hooks/SignatureManager.d.ts +35 -0
- package/src/hooks/SignatureManager.js +72 -0
- package/src/index.d.ts +42 -0
- package/src/index.js +87 -0
- package/src/merkle.d.ts +10 -0
- package/src/merkle.js +30 -0
- package/src/networks.d.ts +29 -0
- package/src/networks.js +71 -0
- package/src/ops.d.ts +126 -0
- package/src/ops.js +131 -0
- package/src/payments/bip341.d.ts +49 -0
- package/src/payments/bip341.js +124 -0
- package/src/payments/embed.d.ts +9 -0
- package/src/payments/embed.js +54 -0
- package/src/payments/index.d.ts +48 -0
- package/src/payments/index.js +69 -0
- package/src/payments/lazy.d.ts +2 -0
- package/src/payments/lazy.js +32 -0
- package/src/payments/p2ms.d.ts +9 -0
- package/src/payments/p2ms.js +158 -0
- package/src/payments/p2pk.d.ts +10 -0
- package/src/payments/p2pk.js +82 -0
- package/src/payments/p2pkh.d.ts +10 -0
- package/src/payments/p2pkh.js +143 -0
- package/src/payments/p2sh.d.ts +10 -0
- package/src/payments/p2sh.js +204 -0
- package/src/payments/p2tr.d.ts +10 -0
- package/src/payments/p2tr.js +315 -0
- package/src/payments/p2wpkh.d.ts +10 -0
- package/src/payments/p2wpkh.js +146 -0
- package/src/payments/p2wsh.d.ts +10 -0
- package/src/payments/p2wsh.js +226 -0
- package/src/psbt/bip371.d.ts +42 -0
- package/src/psbt/bip371.js +424 -0
- package/src/psbt/psbtutils.d.ts +64 -0
- package/src/psbt/psbtutils.js +191 -0
- package/src/psbt.d.ts +235 -0
- package/src/psbt.js +1825 -0
- package/src/push_data.d.ts +29 -0
- package/src/push_data.js +83 -0
- package/src/script.d.ts +42 -0
- package/src/script.js +231 -0
- package/src/script_number.d.ts +19 -0
- package/src/script_number.js +78 -0
- package/src/script_signature.d.ts +21 -0
- package/src/script_signature.js +79 -0
- package/src/transaction.d.ts +60 -0
- package/src/transaction.js +571 -0
- package/src/types.d.ts +54 -0
- package/src/types.js +106 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
('get' in desc
|
|
11
|
+
? !m.__esModule
|
|
12
|
+
: desc.writable || desc.configurable)
|
|
13
|
+
) {
|
|
14
|
+
desc = {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () {
|
|
17
|
+
return m[k];
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
Object.defineProperty(o, k2, desc);
|
|
22
|
+
}
|
|
23
|
+
: function (o, m, k, k2) {
|
|
24
|
+
if (k2 === undefined) k2 = k;
|
|
25
|
+
o[k2] = m[k];
|
|
26
|
+
});
|
|
27
|
+
var __exportStar =
|
|
28
|
+
(this && this.__exportStar) ||
|
|
29
|
+
function (m, exports) {
|
|
30
|
+
for (var p in m)
|
|
31
|
+
if (
|
|
32
|
+
p !== 'default' &&
|
|
33
|
+
!Object.prototype.hasOwnProperty.call(exports, p)
|
|
34
|
+
)
|
|
35
|
+
__createBinding(exports, m, p);
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
38
|
+
exports.initEccLib =
|
|
39
|
+
exports.Transaction =
|
|
40
|
+
exports.opcodes =
|
|
41
|
+
exports.Block =
|
|
42
|
+
exports.script =
|
|
43
|
+
exports.payments =
|
|
44
|
+
exports.networks =
|
|
45
|
+
exports.crypto =
|
|
46
|
+
exports.address =
|
|
47
|
+
void 0;
|
|
48
|
+
const address = require('./address');
|
|
49
|
+
exports.address = address;
|
|
50
|
+
const crypto = require('./crypto');
|
|
51
|
+
exports.crypto = crypto;
|
|
52
|
+
const networks = require('./networks');
|
|
53
|
+
exports.networks = networks;
|
|
54
|
+
const payments = require('./payments');
|
|
55
|
+
exports.payments = payments;
|
|
56
|
+
const script = require('./script');
|
|
57
|
+
exports.script = script;
|
|
58
|
+
var block_1 = require('./block');
|
|
59
|
+
Object.defineProperty(exports, 'Block', {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
get: function () {
|
|
62
|
+
return block_1.Block;
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
__exportStar(require('./psbt'), exports);
|
|
66
|
+
/** @hidden */
|
|
67
|
+
var ops_1 = require('./ops');
|
|
68
|
+
Object.defineProperty(exports, 'opcodes', {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
get: function () {
|
|
71
|
+
return ops_1.OPS;
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
var transaction_1 = require('./transaction');
|
|
75
|
+
Object.defineProperty(exports, 'Transaction', {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
get: function () {
|
|
78
|
+
return transaction_1.Transaction;
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
var ecc_lib_1 = require('./ecc_lib');
|
|
82
|
+
Object.defineProperty(exports, 'initEccLib', {
|
|
83
|
+
enumerable: true,
|
|
84
|
+
get: function () {
|
|
85
|
+
return ecc_lib_1.initEccLib;
|
|
86
|
+
},
|
|
87
|
+
});
|
package/src/merkle.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the Merkle root of an array of buffers using a specified digest function.
|
|
4
|
+
*
|
|
5
|
+
* @param values - The array of buffers.
|
|
6
|
+
* @param digestFn - The digest function used to calculate the hash of the concatenated buffers.
|
|
7
|
+
* @returns The Merkle root as a buffer.
|
|
8
|
+
* @throws {TypeError} If the values parameter is not an array or the digestFn parameter is not a function.
|
|
9
|
+
*/
|
|
10
|
+
export declare function fastMerkleRoot(values: Buffer[], digestFn: (b: Buffer) => Buffer): Buffer;
|
package/src/merkle.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.fastMerkleRoot = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Calculates the Merkle root of an array of buffers using a specified digest function.
|
|
6
|
+
*
|
|
7
|
+
* @param values - The array of buffers.
|
|
8
|
+
* @param digestFn - The digest function used to calculate the hash of the concatenated buffers.
|
|
9
|
+
* @returns The Merkle root as a buffer.
|
|
10
|
+
* @throws {TypeError} If the values parameter is not an array or the digestFn parameter is not a function.
|
|
11
|
+
*/
|
|
12
|
+
function fastMerkleRoot(values, digestFn) {
|
|
13
|
+
if (!Array.isArray(values)) throw TypeError('Expected values Array');
|
|
14
|
+
if (typeof digestFn !== 'function')
|
|
15
|
+
throw TypeError('Expected digest Function');
|
|
16
|
+
let length = values.length;
|
|
17
|
+
const results = values.concat();
|
|
18
|
+
while (length > 1) {
|
|
19
|
+
let j = 0;
|
|
20
|
+
for (let i = 0; i < length; i += 2, ++j) {
|
|
21
|
+
const left = results[i];
|
|
22
|
+
const right = i + 1 === length ? left : results[i + 1];
|
|
23
|
+
const data = Buffer.concat([left, right]);
|
|
24
|
+
results[j] = digestFn(data);
|
|
25
|
+
}
|
|
26
|
+
length = j;
|
|
27
|
+
}
|
|
28
|
+
return results[0];
|
|
29
|
+
}
|
|
30
|
+
exports.fastMerkleRoot = fastMerkleRoot;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a Bitcoin network configuration,including messagePrefix, bech32, bip32, pubKeyHash, scriptHash, wif.
|
|
3
|
+
* Support bitcoin、bitcoin testnet and bitcoin regtest.
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
export interface Bip32 {
|
|
7
|
+
public: number;
|
|
8
|
+
private: number;
|
|
9
|
+
}
|
|
10
|
+
export interface Network {
|
|
11
|
+
wif: number;
|
|
12
|
+
bip32: Bip32;
|
|
13
|
+
messagePrefix: string;
|
|
14
|
+
bech32: string;
|
|
15
|
+
pubKeyHash: number;
|
|
16
|
+
scriptHash: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Represents the Bitcoin network configuration.
|
|
20
|
+
*/
|
|
21
|
+
export declare const bitcoin: Network;
|
|
22
|
+
/**
|
|
23
|
+
* Represents the regtest network configuration.
|
|
24
|
+
*/
|
|
25
|
+
export declare const regtest: Network;
|
|
26
|
+
/**
|
|
27
|
+
* Represents the testnet network configuration.
|
|
28
|
+
*/
|
|
29
|
+
export declare const testnet: Network;
|
package/src/networks.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// https://en.bitcoin.it/wiki/List_of_address_prefixes
|
|
3
|
+
// Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
exports.testnet = exports.regtest = exports.bitcoin = void 0;
|
|
6
|
+
/**
|
|
7
|
+
* Represents the Bitcoin network configuration.
|
|
8
|
+
*/
|
|
9
|
+
exports.bitcoin = {
|
|
10
|
+
/**
|
|
11
|
+
* The message prefix used for signing Bitcoin messages.
|
|
12
|
+
*/
|
|
13
|
+
messagePrefix: '\x18Bitcoin Signed Message:\n',
|
|
14
|
+
/**
|
|
15
|
+
* The Bech32 prefix used for Bitcoin addresses.
|
|
16
|
+
*/
|
|
17
|
+
bech32: 'bc',
|
|
18
|
+
/**
|
|
19
|
+
* The BIP32 key prefixes for Bitcoin.
|
|
20
|
+
*/
|
|
21
|
+
bip32: {
|
|
22
|
+
/**
|
|
23
|
+
* The public key prefix for BIP32 extended public keys.
|
|
24
|
+
*/
|
|
25
|
+
public: 0x0488b21e,
|
|
26
|
+
/**
|
|
27
|
+
* The private key prefix for BIP32 extended private keys.
|
|
28
|
+
*/
|
|
29
|
+
private: 0x0488ade4,
|
|
30
|
+
},
|
|
31
|
+
/**
|
|
32
|
+
* The prefix for Bitcoin public key hashes.
|
|
33
|
+
*/
|
|
34
|
+
pubKeyHash: 0x00,
|
|
35
|
+
/**
|
|
36
|
+
* The prefix for Bitcoin script hashes.
|
|
37
|
+
*/
|
|
38
|
+
scriptHash: 0x05,
|
|
39
|
+
/**
|
|
40
|
+
* The prefix for Bitcoin Wallet Import Format (WIF) private keys.
|
|
41
|
+
*/
|
|
42
|
+
wif: 0x80,
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Represents the regtest network configuration.
|
|
46
|
+
*/
|
|
47
|
+
exports.regtest = {
|
|
48
|
+
messagePrefix: '\x18Bitcoin Signed Message:\n',
|
|
49
|
+
bech32: 'bcrt',
|
|
50
|
+
bip32: {
|
|
51
|
+
public: 0x043587cf,
|
|
52
|
+
private: 0x04358394,
|
|
53
|
+
},
|
|
54
|
+
pubKeyHash: 0x6f,
|
|
55
|
+
scriptHash: 0xc4,
|
|
56
|
+
wif: 0xef,
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Represents the testnet network configuration.
|
|
60
|
+
*/
|
|
61
|
+
exports.testnet = {
|
|
62
|
+
messagePrefix: '\x18Bitcoin Signed Message:\n',
|
|
63
|
+
bech32: 'tb',
|
|
64
|
+
bip32: {
|
|
65
|
+
public: 0x043587cf,
|
|
66
|
+
private: 0x04358394,
|
|
67
|
+
},
|
|
68
|
+
pubKeyHash: 0x6f,
|
|
69
|
+
scriptHash: 0xc4,
|
|
70
|
+
wif: 0xef,
|
|
71
|
+
};
|
package/src/ops.d.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
export interface Opcodes {
|
|
2
|
+
OP_FALSE: number;
|
|
3
|
+
OP_0: number;
|
|
4
|
+
OP_PUSHDATA1: number;
|
|
5
|
+
OP_PUSHDATA2: number;
|
|
6
|
+
OP_PUSHDATA4: number;
|
|
7
|
+
OP_1NEGATE: number;
|
|
8
|
+
OP_RESERVED: number;
|
|
9
|
+
OP_TRUE: number;
|
|
10
|
+
OP_1: number;
|
|
11
|
+
OP_2: number;
|
|
12
|
+
OP_3: number;
|
|
13
|
+
OP_4: number;
|
|
14
|
+
OP_5: number;
|
|
15
|
+
OP_6: number;
|
|
16
|
+
OP_7: number;
|
|
17
|
+
OP_8: number;
|
|
18
|
+
OP_9: number;
|
|
19
|
+
OP_10: number;
|
|
20
|
+
OP_11: number;
|
|
21
|
+
OP_12: number;
|
|
22
|
+
OP_13: number;
|
|
23
|
+
OP_14: number;
|
|
24
|
+
OP_15: number;
|
|
25
|
+
OP_16: number;
|
|
26
|
+
OP_NOP: number;
|
|
27
|
+
OP_VER: number;
|
|
28
|
+
OP_IF: number;
|
|
29
|
+
OP_NOTIF: number;
|
|
30
|
+
OP_VERIF: number;
|
|
31
|
+
OP_VERNOTIF: number;
|
|
32
|
+
OP_ELSE: number;
|
|
33
|
+
OP_ENDIF: number;
|
|
34
|
+
OP_VERIFY: number;
|
|
35
|
+
OP_RETURN: number;
|
|
36
|
+
OP_TOALTSTACK: number;
|
|
37
|
+
OP_FROMALTSTACK: number;
|
|
38
|
+
OP_2DROP: number;
|
|
39
|
+
OP_2DUP: number;
|
|
40
|
+
OP_3DUP: number;
|
|
41
|
+
OP_2OVER: number;
|
|
42
|
+
OP_2ROT: number;
|
|
43
|
+
OP_2SWAP: number;
|
|
44
|
+
OP_IFDUP: number;
|
|
45
|
+
OP_DEPTH: number;
|
|
46
|
+
OP_DROP: number;
|
|
47
|
+
OP_DUP: number;
|
|
48
|
+
OP_NIP: number;
|
|
49
|
+
OP_OVER: number;
|
|
50
|
+
OP_PICK: number;
|
|
51
|
+
OP_ROLL: number;
|
|
52
|
+
OP_ROT: number;
|
|
53
|
+
OP_SWAP: number;
|
|
54
|
+
OP_TUCK: number;
|
|
55
|
+
OP_CAT: number;
|
|
56
|
+
OP_SUBSTR: number;
|
|
57
|
+
OP_LEFT: number;
|
|
58
|
+
OP_RIGHT: number;
|
|
59
|
+
OP_SIZE: number;
|
|
60
|
+
OP_INVERT: number;
|
|
61
|
+
OP_AND: number;
|
|
62
|
+
OP_OR: number;
|
|
63
|
+
OP_XOR: number;
|
|
64
|
+
OP_EQUAL: number;
|
|
65
|
+
OP_EQUALVERIFY: number;
|
|
66
|
+
OP_RESERVED1: number;
|
|
67
|
+
OP_RESERVED2: number;
|
|
68
|
+
OP_1ADD: number;
|
|
69
|
+
OP_1SUB: number;
|
|
70
|
+
OP_2MUL: number;
|
|
71
|
+
OP_2DIV: number;
|
|
72
|
+
OP_NEGATE: number;
|
|
73
|
+
OP_ABS: number;
|
|
74
|
+
OP_NOT: number;
|
|
75
|
+
OP_0NOTEQUAL: number;
|
|
76
|
+
OP_ADD: number;
|
|
77
|
+
OP_SUB: number;
|
|
78
|
+
OP_MUL: number;
|
|
79
|
+
OP_DIV: number;
|
|
80
|
+
OP_MOD: number;
|
|
81
|
+
OP_LSHIFT: number;
|
|
82
|
+
OP_RSHIFT: number;
|
|
83
|
+
OP_BOOLAND: number;
|
|
84
|
+
OP_BOOLOR: number;
|
|
85
|
+
OP_NUMEQUAL: number;
|
|
86
|
+
OP_NUMEQUALVERIFY: number;
|
|
87
|
+
OP_NUMNOTEQUAL: number;
|
|
88
|
+
OP_LESSTHAN: number;
|
|
89
|
+
OP_GREATERTHAN: number;
|
|
90
|
+
OP_LESSTHANOREQUAL: number;
|
|
91
|
+
OP_GREATERTHANOREQUAL: number;
|
|
92
|
+
OP_MIN: number;
|
|
93
|
+
OP_MAX: number;
|
|
94
|
+
OP_WITHIN: number;
|
|
95
|
+
OP_RIPEMD160: number;
|
|
96
|
+
OP_SHA1: number;
|
|
97
|
+
OP_SHA256: number;
|
|
98
|
+
OP_HASH160: number;
|
|
99
|
+
OP_HASH256: number;
|
|
100
|
+
OP_CODESEPARATOR: number;
|
|
101
|
+
OP_CHECKSIG: number;
|
|
102
|
+
OP_CHECKSIGVERIFY: number;
|
|
103
|
+
OP_CHECKMULTISIG: number;
|
|
104
|
+
OP_CHECKMULTISIGVERIFY: number;
|
|
105
|
+
OP_CHECKLOCKTIMEVERIFY: number;
|
|
106
|
+
OP_CHECKSEQUENCEVERIFY: number;
|
|
107
|
+
OP_CHECKSIGADD: number;
|
|
108
|
+
OP_NOP1: number;
|
|
109
|
+
OP_NOP2: number;
|
|
110
|
+
OP_NOP3: number;
|
|
111
|
+
OP_NOP4: number;
|
|
112
|
+
OP_NOP5: number;
|
|
113
|
+
OP_NOP6: number;
|
|
114
|
+
OP_NOP7: number;
|
|
115
|
+
OP_NOP8: number;
|
|
116
|
+
OP_NOP9: number;
|
|
117
|
+
OP_NOP10: number;
|
|
118
|
+
OP_PUBKEYHASH: number;
|
|
119
|
+
OP_PUBKEY: number;
|
|
120
|
+
OP_INVALIDOPCODE: number;
|
|
121
|
+
}
|
|
122
|
+
declare const OPS: Opcodes;
|
|
123
|
+
declare const REVERSE_OPS: {
|
|
124
|
+
[key: number]: string;
|
|
125
|
+
};
|
|
126
|
+
export { OPS, REVERSE_OPS };
|
package/src/ops.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.REVERSE_OPS = exports.OPS = void 0;
|
|
4
|
+
const OPS = {
|
|
5
|
+
OP_FALSE: 0,
|
|
6
|
+
OP_0: 0,
|
|
7
|
+
OP_PUSHDATA1: 76,
|
|
8
|
+
OP_PUSHDATA2: 77,
|
|
9
|
+
OP_PUSHDATA4: 78,
|
|
10
|
+
OP_1NEGATE: 79,
|
|
11
|
+
OP_RESERVED: 80,
|
|
12
|
+
OP_TRUE: 81,
|
|
13
|
+
OP_1: 81,
|
|
14
|
+
OP_2: 82,
|
|
15
|
+
OP_3: 83,
|
|
16
|
+
OP_4: 84,
|
|
17
|
+
OP_5: 85,
|
|
18
|
+
OP_6: 86,
|
|
19
|
+
OP_7: 87,
|
|
20
|
+
OP_8: 88,
|
|
21
|
+
OP_9: 89,
|
|
22
|
+
OP_10: 90,
|
|
23
|
+
OP_11: 91,
|
|
24
|
+
OP_12: 92,
|
|
25
|
+
OP_13: 93,
|
|
26
|
+
OP_14: 94,
|
|
27
|
+
OP_15: 95,
|
|
28
|
+
OP_16: 96,
|
|
29
|
+
OP_NOP: 97,
|
|
30
|
+
OP_VER: 98,
|
|
31
|
+
OP_IF: 99,
|
|
32
|
+
OP_NOTIF: 100,
|
|
33
|
+
OP_VERIF: 101,
|
|
34
|
+
OP_VERNOTIF: 102,
|
|
35
|
+
OP_ELSE: 103,
|
|
36
|
+
OP_ENDIF: 104,
|
|
37
|
+
OP_VERIFY: 105,
|
|
38
|
+
OP_RETURN: 106,
|
|
39
|
+
OP_TOALTSTACK: 107,
|
|
40
|
+
OP_FROMALTSTACK: 108,
|
|
41
|
+
OP_2DROP: 109,
|
|
42
|
+
OP_2DUP: 110,
|
|
43
|
+
OP_3DUP: 111,
|
|
44
|
+
OP_2OVER: 112,
|
|
45
|
+
OP_2ROT: 113,
|
|
46
|
+
OP_2SWAP: 114,
|
|
47
|
+
OP_IFDUP: 115,
|
|
48
|
+
OP_DEPTH: 116,
|
|
49
|
+
OP_DROP: 117,
|
|
50
|
+
OP_DUP: 118,
|
|
51
|
+
OP_NIP: 119,
|
|
52
|
+
OP_OVER: 120,
|
|
53
|
+
OP_PICK: 121,
|
|
54
|
+
OP_ROLL: 122,
|
|
55
|
+
OP_ROT: 123,
|
|
56
|
+
OP_SWAP: 124,
|
|
57
|
+
OP_TUCK: 125,
|
|
58
|
+
OP_CAT: 126,
|
|
59
|
+
OP_SUBSTR: 127,
|
|
60
|
+
OP_LEFT: 128,
|
|
61
|
+
OP_RIGHT: 129,
|
|
62
|
+
OP_SIZE: 130,
|
|
63
|
+
OP_INVERT: 131,
|
|
64
|
+
OP_AND: 132,
|
|
65
|
+
OP_OR: 133,
|
|
66
|
+
OP_XOR: 134,
|
|
67
|
+
OP_EQUAL: 135,
|
|
68
|
+
OP_EQUALVERIFY: 136,
|
|
69
|
+
OP_RESERVED1: 137,
|
|
70
|
+
OP_RESERVED2: 138,
|
|
71
|
+
OP_1ADD: 139,
|
|
72
|
+
OP_1SUB: 140,
|
|
73
|
+
OP_2MUL: 141,
|
|
74
|
+
OP_2DIV: 142,
|
|
75
|
+
OP_NEGATE: 143,
|
|
76
|
+
OP_ABS: 144,
|
|
77
|
+
OP_NOT: 145,
|
|
78
|
+
OP_0NOTEQUAL: 146,
|
|
79
|
+
OP_ADD: 147,
|
|
80
|
+
OP_SUB: 148,
|
|
81
|
+
OP_MUL: 149,
|
|
82
|
+
OP_DIV: 150,
|
|
83
|
+
OP_MOD: 151,
|
|
84
|
+
OP_LSHIFT: 152,
|
|
85
|
+
OP_RSHIFT: 153,
|
|
86
|
+
OP_BOOLAND: 154,
|
|
87
|
+
OP_BOOLOR: 155,
|
|
88
|
+
OP_NUMEQUAL: 156,
|
|
89
|
+
OP_NUMEQUALVERIFY: 157,
|
|
90
|
+
OP_NUMNOTEQUAL: 158,
|
|
91
|
+
OP_LESSTHAN: 159,
|
|
92
|
+
OP_GREATERTHAN: 160,
|
|
93
|
+
OP_LESSTHANOREQUAL: 161,
|
|
94
|
+
OP_GREATERTHANOREQUAL: 162,
|
|
95
|
+
OP_MIN: 163,
|
|
96
|
+
OP_MAX: 164,
|
|
97
|
+
OP_WITHIN: 165,
|
|
98
|
+
OP_RIPEMD160: 166,
|
|
99
|
+
OP_SHA1: 167,
|
|
100
|
+
OP_SHA256: 168,
|
|
101
|
+
OP_HASH160: 169,
|
|
102
|
+
OP_HASH256: 170,
|
|
103
|
+
OP_CODESEPARATOR: 171,
|
|
104
|
+
OP_CHECKSIG: 172,
|
|
105
|
+
OP_CHECKSIGVERIFY: 173,
|
|
106
|
+
OP_CHECKMULTISIG: 174,
|
|
107
|
+
OP_CHECKMULTISIGVERIFY: 175,
|
|
108
|
+
OP_NOP1: 176,
|
|
109
|
+
OP_NOP2: 177,
|
|
110
|
+
OP_CHECKLOCKTIMEVERIFY: 177,
|
|
111
|
+
OP_NOP3: 178,
|
|
112
|
+
OP_CHECKSEQUENCEVERIFY: 178,
|
|
113
|
+
OP_NOP4: 179,
|
|
114
|
+
OP_NOP5: 180,
|
|
115
|
+
OP_NOP6: 181,
|
|
116
|
+
OP_NOP7: 182,
|
|
117
|
+
OP_NOP8: 183,
|
|
118
|
+
OP_NOP9: 184,
|
|
119
|
+
OP_NOP10: 185,
|
|
120
|
+
OP_CHECKSIGADD: 186,
|
|
121
|
+
OP_PUBKEYHASH: 253,
|
|
122
|
+
OP_PUBKEY: 254,
|
|
123
|
+
OP_INVALIDOPCODE: 255,
|
|
124
|
+
};
|
|
125
|
+
exports.OPS = OPS;
|
|
126
|
+
const REVERSE_OPS = {};
|
|
127
|
+
exports.REVERSE_OPS = REVERSE_OPS;
|
|
128
|
+
for (const op of Object.keys(OPS)) {
|
|
129
|
+
const code = OPS[op];
|
|
130
|
+
REVERSE_OPS[code] = op;
|
|
131
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Tapleaf, Taptree } from '../types';
|
|
3
|
+
export declare const LEAF_VERSION_TAPSCRIPT = 192;
|
|
4
|
+
export declare const MAX_TAPTREE_DEPTH = 128;
|
|
5
|
+
interface HashLeaf {
|
|
6
|
+
hash: Buffer;
|
|
7
|
+
}
|
|
8
|
+
interface HashBranch {
|
|
9
|
+
hash: Buffer;
|
|
10
|
+
left: HashTree;
|
|
11
|
+
right: HashTree;
|
|
12
|
+
}
|
|
13
|
+
interface TweakedPublicKey {
|
|
14
|
+
parity: number;
|
|
15
|
+
x: Buffer;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Binary tree representing leaf, branch, and root node hashes of a Taptree.
|
|
19
|
+
* Each node contains a hash, and potentially left and right branch hashes.
|
|
20
|
+
* This tree is used for 2 purposes: Providing the root hash for tweaking,
|
|
21
|
+
* and calculating merkle inclusion proofs when constructing a control block.
|
|
22
|
+
*/
|
|
23
|
+
export type HashTree = HashLeaf | HashBranch;
|
|
24
|
+
/**
|
|
25
|
+
* Calculates the root hash from a given control block and leaf hash.
|
|
26
|
+
* @param controlBlock - The control block buffer.
|
|
27
|
+
* @param leafHash - The leaf hash buffer.
|
|
28
|
+
* @returns The root hash buffer.
|
|
29
|
+
* @throws {TypeError} If the control block length is less than 33.
|
|
30
|
+
*/
|
|
31
|
+
export declare function rootHashFromPath(controlBlock: Buffer, leafHash: Buffer): Buffer;
|
|
32
|
+
/**
|
|
33
|
+
* Build a hash tree of merkle nodes from the scripts binary tree.
|
|
34
|
+
* @param scriptTree - the tree of scripts to pairwise hash.
|
|
35
|
+
*/
|
|
36
|
+
export declare function toHashTree(scriptTree: Taptree): HashTree;
|
|
37
|
+
/**
|
|
38
|
+
* Given a HashTree, finds the path from a particular hash to the root.
|
|
39
|
+
* @param node - the root of the tree
|
|
40
|
+
* @param hash - the hash to search for
|
|
41
|
+
* @returns - array of sibling hashes, from leaf (inclusive) to root
|
|
42
|
+
* (exclusive) needed to prove inclusion of the specified hash. undefined if no
|
|
43
|
+
* path is found
|
|
44
|
+
*/
|
|
45
|
+
export declare function findScriptPath(node: HashTree, hash: Buffer): Buffer[] | undefined;
|
|
46
|
+
export declare function tapleafHash(leaf: Tapleaf): Buffer;
|
|
47
|
+
export declare function tapTweakHash(pubKey: Buffer, h: Buffer | undefined): Buffer;
|
|
48
|
+
export declare function tweakKey(pubKey: Buffer, h: Buffer | undefined): TweakedPublicKey | null;
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.tweakKey =
|
|
4
|
+
exports.tapTweakHash =
|
|
5
|
+
exports.tapleafHash =
|
|
6
|
+
exports.findScriptPath =
|
|
7
|
+
exports.toHashTree =
|
|
8
|
+
exports.rootHashFromPath =
|
|
9
|
+
exports.MAX_TAPTREE_DEPTH =
|
|
10
|
+
exports.LEAF_VERSION_TAPSCRIPT =
|
|
11
|
+
void 0;
|
|
12
|
+
const buffer_1 = require('buffer');
|
|
13
|
+
const ecc_lib_1 = require('../ecc_lib');
|
|
14
|
+
const bcrypto = require('../crypto');
|
|
15
|
+
const bufferutils_1 = require('../bufferutils');
|
|
16
|
+
const types_1 = require('../types');
|
|
17
|
+
exports.LEAF_VERSION_TAPSCRIPT = 0xc0;
|
|
18
|
+
exports.MAX_TAPTREE_DEPTH = 128;
|
|
19
|
+
const isHashBranch = ht => 'left' in ht && 'right' in ht;
|
|
20
|
+
/**
|
|
21
|
+
* Calculates the root hash from a given control block and leaf hash.
|
|
22
|
+
* @param controlBlock - The control block buffer.
|
|
23
|
+
* @param leafHash - The leaf hash buffer.
|
|
24
|
+
* @returns The root hash buffer.
|
|
25
|
+
* @throws {TypeError} If the control block length is less than 33.
|
|
26
|
+
*/
|
|
27
|
+
function rootHashFromPath(controlBlock, leafHash) {
|
|
28
|
+
if (controlBlock.length < 33)
|
|
29
|
+
throw new TypeError(
|
|
30
|
+
`The control-block length is too small. Got ${controlBlock.length}, expected min 33.`,
|
|
31
|
+
);
|
|
32
|
+
const m = (controlBlock.length - 33) / 32;
|
|
33
|
+
let kj = leafHash;
|
|
34
|
+
for (let j = 0; j < m; j++) {
|
|
35
|
+
const ej = controlBlock.slice(33 + 32 * j, 65 + 32 * j);
|
|
36
|
+
if (kj.compare(ej) < 0) {
|
|
37
|
+
kj = tapBranchHash(kj, ej);
|
|
38
|
+
} else {
|
|
39
|
+
kj = tapBranchHash(ej, kj);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return kj;
|
|
43
|
+
}
|
|
44
|
+
exports.rootHashFromPath = rootHashFromPath;
|
|
45
|
+
/**
|
|
46
|
+
* Build a hash tree of merkle nodes from the scripts binary tree.
|
|
47
|
+
* @param scriptTree - the tree of scripts to pairwise hash.
|
|
48
|
+
*/
|
|
49
|
+
function toHashTree(scriptTree) {
|
|
50
|
+
if ((0, types_1.isTapleaf)(scriptTree))
|
|
51
|
+
return { hash: tapleafHash(scriptTree) };
|
|
52
|
+
const hashes = [toHashTree(scriptTree[0]), toHashTree(scriptTree[1])];
|
|
53
|
+
hashes.sort((a, b) => a.hash.compare(b.hash));
|
|
54
|
+
const [left, right] = hashes;
|
|
55
|
+
return {
|
|
56
|
+
hash: tapBranchHash(left.hash, right.hash),
|
|
57
|
+
left,
|
|
58
|
+
right,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
exports.toHashTree = toHashTree;
|
|
62
|
+
/**
|
|
63
|
+
* Given a HashTree, finds the path from a particular hash to the root.
|
|
64
|
+
* @param node - the root of the tree
|
|
65
|
+
* @param hash - the hash to search for
|
|
66
|
+
* @returns - array of sibling hashes, from leaf (inclusive) to root
|
|
67
|
+
* (exclusive) needed to prove inclusion of the specified hash. undefined if no
|
|
68
|
+
* path is found
|
|
69
|
+
*/
|
|
70
|
+
function findScriptPath(node, hash) {
|
|
71
|
+
if (isHashBranch(node)) {
|
|
72
|
+
const leftPath = findScriptPath(node.left, hash);
|
|
73
|
+
if (leftPath !== undefined) return [...leftPath, node.right.hash];
|
|
74
|
+
const rightPath = findScriptPath(node.right, hash);
|
|
75
|
+
if (rightPath !== undefined) return [...rightPath, node.left.hash];
|
|
76
|
+
} else if (node.hash.equals(hash)) {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
exports.findScriptPath = findScriptPath;
|
|
82
|
+
function tapleafHash(leaf) {
|
|
83
|
+
const version = leaf.version || exports.LEAF_VERSION_TAPSCRIPT;
|
|
84
|
+
return bcrypto.taggedHash(
|
|
85
|
+
'TapLeaf',
|
|
86
|
+
buffer_1.Buffer.concat([
|
|
87
|
+
buffer_1.Buffer.from([version]),
|
|
88
|
+
serializeScript(leaf.output),
|
|
89
|
+
]),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
exports.tapleafHash = tapleafHash;
|
|
93
|
+
function tapTweakHash(pubKey, h) {
|
|
94
|
+
return bcrypto.taggedHash(
|
|
95
|
+
'TapTweak',
|
|
96
|
+
buffer_1.Buffer.concat(h ? [pubKey, h] : [pubKey]),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
exports.tapTweakHash = tapTweakHash;
|
|
100
|
+
function tweakKey(pubKey, h) {
|
|
101
|
+
if (!buffer_1.Buffer.isBuffer(pubKey)) return null;
|
|
102
|
+
if (pubKey.length !== 32) return null;
|
|
103
|
+
if (h && h.length !== 32) return null;
|
|
104
|
+
const tweakHash = tapTweakHash(pubKey, h);
|
|
105
|
+
const res = (0, ecc_lib_1.getEccLib)().xOnlyPointAddTweak(
|
|
106
|
+
pubKey,
|
|
107
|
+
tweakHash,
|
|
108
|
+
);
|
|
109
|
+
if (!res || res.xOnlyPubkey === null) return null;
|
|
110
|
+
return {
|
|
111
|
+
parity: res.parity,
|
|
112
|
+
x: buffer_1.Buffer.from(res.xOnlyPubkey),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
exports.tweakKey = tweakKey;
|
|
116
|
+
function tapBranchHash(a, b) {
|
|
117
|
+
return bcrypto.taggedHash('TapBranch', buffer_1.Buffer.concat([a, b]));
|
|
118
|
+
}
|
|
119
|
+
function serializeScript(s) {
|
|
120
|
+
const varintLen = bufferutils_1.varuint.encodingLength(s.length);
|
|
121
|
+
const buffer = buffer_1.Buffer.allocUnsafe(varintLen); // better
|
|
122
|
+
bufferutils_1.varuint.encode(s.length, buffer);
|
|
123
|
+
return buffer_1.Buffer.concat([buffer, s]);
|
|
124
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Payment, PaymentOpts } from './index';
|
|
2
|
+
/**
|
|
3
|
+
* Embeds data in a Bitcoin payment.
|
|
4
|
+
* @param a - The payment object.
|
|
5
|
+
* @param opts - Optional payment options.
|
|
6
|
+
* @returns The modified payment object.
|
|
7
|
+
* @throws {TypeError} If there is not enough data or if the output is invalid.
|
|
8
|
+
*/
|
|
9
|
+
export declare function p2data(a: Payment, opts?: PaymentOpts): Payment;
|