@btc-vision/bitcoin 6.3.1 → 6.3.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/.babelrc +4 -0
- package/.gitattributes +2 -0
- package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
- package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
- package/.nyc_output/processinfo/index.json +1 -0
- package/.prettierrc.json +12 -0
- package/CHANGELOG.md +403 -0
- package/CONTRIBUTING.md +83 -0
- package/browser/address.d.ts +16 -0
- package/{src → browser}/bip66.d.ts +6 -7
- package/{src → browser}/block.d.ts +29 -30
- package/{src → browser}/bufferutils.d.ts +34 -54
- package/browser/crypto/crypto.d.ts +1 -0
- package/{src → browser}/crypto.d.ts +13 -18
- package/browser/ecc_lib.d.ts +3 -0
- package/browser/hooks/AdvancedSignatureManager.d.ts +16 -0
- package/{src → browser}/hooks/HookedSigner.d.ts +4 -4
- package/browser/hooks/SignatureManager.d.ts +13 -0
- package/browser/index.d.ts +58 -0
- package/browser/index.js +2 -0
- package/browser/index.js.LICENSE.txt +14 -0
- package/browser/merkle.d.ts +1 -0
- package/browser/networks.d.ts +23 -0
- package/{src → browser}/ops.d.ts +126 -126
- package/browser/payments/bip341.d.ts +23 -0
- package/browser/payments/embed.d.ts +2 -0
- package/browser/payments/index.d.ts +41 -0
- package/{src → browser}/payments/lazy.d.ts +2 -2
- package/browser/payments/p2ms.d.ts +2 -0
- package/browser/payments/p2pk.d.ts +2 -0
- package/browser/payments/p2pkh.d.ts +2 -0
- package/browser/payments/p2sh.d.ts +2 -0
- package/browser/payments/p2tr.d.ts +2 -0
- package/browser/payments/p2wpkh.d.ts +2 -0
- package/browser/payments/p2wsh.d.ts +2 -0
- package/browser/psbt/bip371.d.ts +16 -0
- package/browser/psbt/psbtutils.d.ts +26 -0
- package/{src → browser}/psbt.d.ts +167 -238
- package/browser/push_data.d.ts +7 -0
- package/browser/script.d.ts +17 -0
- package/browser/script_number.d.ts +2 -0
- package/browser/script_signature.d.ts +7 -0
- package/{src → browser}/transaction.d.ts +48 -60
- package/{src → browser}/types.d.ts +37 -54
- package/build/address.d.ts +16 -0
- package/build/address.js +148 -0
- package/build/bip66.d.ts +6 -0
- package/build/bip66.js +99 -0
- package/build/block.d.ts +29 -0
- package/build/block.js +181 -0
- package/build/bufferutils.d.ts +34 -0
- package/build/bufferutils.js +141 -0
- package/build/crypto/crypto.d.ts +1 -0
- package/build/crypto/crypto.js +1 -0
- package/build/crypto.d.ts +13 -0
- package/build/crypto.js +87 -0
- package/build/ecc_lib.d.ts +3 -0
- package/build/ecc_lib.js +61 -0
- package/build/hooks/AdvancedSignatureManager.d.ts +16 -0
- package/build/hooks/AdvancedSignatureManager.js +52 -0
- package/build/hooks/HookedSigner.d.ts +4 -0
- package/build/hooks/HookedSigner.js +64 -0
- package/build/hooks/SignatureManager.d.ts +13 -0
- package/build/hooks/SignatureManager.js +45 -0
- package/build/index.d.ts +58 -0
- package/build/index.js +32 -0
- package/build/merkle.d.ts +1 -0
- package/build/merkle.js +19 -0
- package/build/networks.d.ts +23 -0
- package/build/networks.js +121 -0
- package/build/ops.d.ts +126 -0
- package/{src → build}/ops.js +127 -131
- package/build/payments/bip341.d.ts +23 -0
- package/build/payments/bip341.js +82 -0
- package/build/payments/embed.d.ts +2 -0
- package/build/payments/embed.js +39 -0
- package/build/payments/index.d.ts +41 -0
- package/build/payments/index.js +10 -0
- package/build/payments/lazy.d.ts +2 -0
- package/{src → build}/payments/lazy.js +28 -32
- package/build/payments/p2ms.d.ts +2 -0
- package/{src → build}/payments/p2ms.js +128 -158
- package/build/payments/p2pk.d.ts +2 -0
- package/build/payments/p2pk.js +68 -0
- package/build/payments/p2pkh.d.ts +2 -0
- package/build/payments/p2pkh.js +135 -0
- package/build/payments/p2sh.d.ts +2 -0
- package/build/payments/p2sh.js +175 -0
- package/build/payments/p2tr.d.ts +2 -0
- package/build/payments/p2tr.js +254 -0
- package/build/payments/p2wpkh.d.ts +2 -0
- package/build/payments/p2wpkh.js +130 -0
- package/build/payments/p2wsh.d.ts +2 -0
- package/build/payments/p2wsh.js +180 -0
- package/build/psbt/bip371.d.ts +16 -0
- package/build/psbt/bip371.js +246 -0
- package/build/psbt/psbtutils.d.ts +26 -0
- package/build/psbt/psbtutils.js +170 -0
- package/build/psbt.d.ts +167 -0
- package/build/psbt.js +1305 -0
- package/build/push_data.d.ts +7 -0
- package/build/push_data.js +57 -0
- package/build/script.d.ts +17 -0
- package/build/script.js +167 -0
- package/build/script_number.d.ts +2 -0
- package/build/script_number.js +49 -0
- package/build/script_signature.d.ts +7 -0
- package/build/script_signature.js +49 -0
- package/build/transaction.d.ts +48 -0
- package/build/transaction.js +445 -0
- package/build/types.d.ts +37 -0
- package/build/types.js +73 -0
- package/cjs/package.json +3 -0
- package/eslint.config.js +56 -0
- package/gulpfile.js +42 -0
- package/package.json +105 -50
- package/src/{address.js → address.ts} +93 -73
- package/src/{bip66.js → bip66.ts} +23 -19
- package/src/{block.js → block.ts} +114 -105
- package/src/{bufferutils.js → bufferutils.ts} +65 -67
- package/src/crypto/crypto-browser.js +75 -0
- package/src/crypto/crypto.ts +1 -0
- package/src/crypto.ts +108 -0
- package/src/{ecc_lib.js → ecc_lib.ts} +25 -53
- package/src/hooks/{AdvancedSignatureManager.js → AdvancedSignatureManager.ts} +34 -18
- package/src/hooks/HookedSigner.ts +108 -0
- package/src/hooks/{SignatureManager.js → SignatureManager.ts} +26 -14
- package/src/index.ts +86 -0
- package/src/{merkle.js → merkle.ts} +8 -7
- package/src/{networks.js → networks.ts} +44 -29
- package/src/ops.ts +282 -0
- package/src/payments/bip341.ts +140 -0
- package/src/payments/embed.ts +55 -0
- package/src/payments/{index.d.ts → index.ts} +20 -10
- package/src/payments/lazy.ts +28 -0
- package/src/payments/p2ms.ts +150 -0
- package/src/payments/{p2pk.js → p2pk.ts} +32 -29
- package/src/payments/{p2pkh.js → p2pkh.ts} +53 -47
- package/src/payments/{p2sh.js → p2sh.ts} +72 -71
- package/src/payments/{p2tr.js → p2tr.ts} +114 -125
- package/src/payments/{p2wpkh.js → p2wpkh.ts} +51 -56
- package/src/payments/{p2wsh.js → p2wsh.ts} +69 -81
- package/src/psbt/{bip371.js → bip371.ts} +191 -174
- package/src/psbt/psbtutils.ts +299 -0
- package/src/{psbt.js → psbt.ts} +1025 -679
- package/src/{push_data.js → push_data.ts} +35 -21
- package/src/{script.js → script.ts} +93 -77
- package/src/{script_number.js → script_number.ts} +15 -21
- package/src/{script_signature.js → script_signature.ts} +26 -14
- package/src/{transaction.js → transaction.ts} +247 -167
- package/src/types.ts +122 -0
- package/test/address.spec.js +124 -0
- package/test/address.spec.ts +177 -0
- package/test/bitcoin.core.spec.js +170 -0
- package/test/bitcoin.core.spec.ts +234 -0
- package/test/block.spec.js +141 -0
- package/test/block.spec.ts +194 -0
- package/test/bufferutils.spec.js +427 -0
- package/test/bufferutils.spec.ts +513 -0
- package/test/crypto.spec.js +41 -0
- package/test/crypto.spec.ts +55 -0
- package/test/fixtures/address.json +329 -0
- package/test/fixtures/block.json +148 -0
- package/test/fixtures/bufferutils.json +102 -0
- package/test/fixtures/core/README.md +26 -0
- package/test/fixtures/core/base58_encode_decode.json +50 -0
- package/test/fixtures/core/base58_keys_invalid.json +152 -0
- package/test/fixtures/core/base58_keys_valid.json +452 -0
- package/test/fixtures/core/blocks.json +27 -0
- package/test/fixtures/core/sig_canonical.json +7 -0
- package/test/fixtures/core/sig_noncanonical.json +33 -0
- package/test/fixtures/core/sighash.json +3505 -0
- package/test/fixtures/core/tx_valid.json +2023 -0
- package/test/fixtures/crypto.json +43 -0
- package/test/fixtures/ecdsa.json +217 -0
- package/test/fixtures/ecpair.json +141 -0
- package/test/fixtures/embed.json +108 -0
- package/test/fixtures/p2ms.json +434 -0
- package/test/fixtures/p2pk.json +179 -0
- package/test/fixtures/p2pkh.json +276 -0
- package/test/fixtures/p2sh.json +508 -0
- package/test/fixtures/p2tr.json +1198 -0
- package/test/fixtures/p2wpkh.json +290 -0
- package/test/fixtures/p2wsh.json +489 -0
- package/test/fixtures/psbt.json +924 -0
- package/test/fixtures/script.json +465 -0
- package/test/fixtures/script_number.json +225 -0
- package/test/fixtures/signature.json +140 -0
- package/test/fixtures/transaction.json +916 -0
- package/test/integration/_regtest.js +7 -0
- package/test/integration/_regtest.ts +6 -0
- package/test/integration/addresses.spec.js +116 -0
- package/test/integration/addresses.spec.ts +154 -0
- package/test/integration/bip32.spec.js +85 -0
- package/test/integration/bip32.spec.ts +151 -0
- package/test/integration/blocks.spec.js +26 -0
- package/test/integration/blocks.spec.ts +28 -0
- package/test/integration/cltv.spec.js +199 -0
- package/test/integration/cltv.spec.ts +283 -0
- package/test/integration/csv.spec.js +362 -0
- package/test/integration/csv.spec.ts +527 -0
- package/test/integration/payments.spec.js +98 -0
- package/test/integration/payments.spec.ts +135 -0
- package/test/integration/taproot.spec.js +532 -0
- package/test/integration/taproot.spec.ts +707 -0
- package/test/integration/transactions.spec.js +561 -0
- package/test/integration/transactions.spec.ts +769 -0
- package/test/payments.spec.js +97 -0
- package/test/payments.spec.ts +125 -0
- package/test/payments.utils.js +190 -0
- package/test/payments.utils.ts +208 -0
- package/test/psbt.spec.js +1044 -0
- package/test/psbt.spec.ts +1414 -0
- package/test/script.spec.js +151 -0
- package/test/script.spec.ts +210 -0
- package/test/script_number.spec.js +24 -0
- package/test/script_number.spec.ts +29 -0
- package/test/script_signature.spec.js +52 -0
- package/test/script_signature.spec.ts +66 -0
- package/test/transaction.spec.js +269 -0
- package/test/transaction.spec.ts +387 -0
- package/test/ts-node-register.js +5 -0
- package/test/tsconfig.json +45 -0
- package/test/types.spec.js +46 -0
- package/test/types.spec.ts +58 -0
- package/tsconfig.base.json +27 -0
- package/tsconfig.json +19 -0
- package/tsconfig.webpack.json +18 -0
- package/webpack.config.js +79 -0
- package/src/address.d.ts +0 -42
- package/src/crypto.js +0 -128
- package/src/ecc_lib.d.ts +0 -17
- package/src/hooks/AdvancedSignatureManager.d.ts +0 -44
- package/src/hooks/HookedSigner.js +0 -90
- package/src/hooks/SignatureManager.d.ts +0 -35
- package/src/index.d.ts +0 -42
- package/src/index.js +0 -87
- package/src/merkle.d.ts +0 -10
- package/src/networks.d.ts +0 -83
- package/src/payments/bip341.d.ts +0 -49
- package/src/payments/bip341.js +0 -124
- package/src/payments/embed.d.ts +0 -9
- package/src/payments/embed.js +0 -54
- package/src/payments/index.js +0 -69
- package/src/payments/p2ms.d.ts +0 -9
- package/src/payments/p2pk.d.ts +0 -10
- package/src/payments/p2pkh.d.ts +0 -10
- package/src/payments/p2sh.d.ts +0 -10
- package/src/payments/p2tr.d.ts +0 -10
- package/src/payments/p2wpkh.d.ts +0 -10
- package/src/payments/p2wsh.d.ts +0 -10
- package/src/psbt/bip371.d.ts +0 -42
- package/src/psbt/psbtutils.d.ts +0 -64
- package/src/psbt/psbtutils.js +0 -191
- package/src/push_data.d.ts +0 -29
- package/src/script.d.ts +0 -42
- package/src/script_number.d.ts +0 -19
- package/src/script_signature.d.ts +0 -21
- package/src/types.js +0 -106
|
@@ -1,23 +1,30 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
// https://en.bitcoin.it/wiki/List_of_address_prefixes
|
|
3
2
|
// Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a Bitcoin network configuration,including messagePrefix, bech32, bip32, pubKeyHash, scriptHash, wif.
|
|
6
|
+
* Support bitcoin、bitcoin testnet and bitcoin regtest.
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface Bip32 {
|
|
11
|
+
public: number;
|
|
12
|
+
private: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Network {
|
|
16
|
+
wif: number;
|
|
17
|
+
bip32: Bip32;
|
|
18
|
+
messagePrefix: string;
|
|
19
|
+
bech32: string;
|
|
20
|
+
pubKeyHash: number;
|
|
21
|
+
scriptHash: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
17
24
|
/**
|
|
18
25
|
* Represents the Bitcoin network configuration.
|
|
19
26
|
*/
|
|
20
|
-
|
|
27
|
+
export const bitcoin: Network = {
|
|
21
28
|
/**
|
|
22
29
|
* The message prefix used for signing Bitcoin messages.
|
|
23
30
|
*/
|
|
@@ -55,7 +62,7 @@ exports.bitcoin = {
|
|
|
55
62
|
/**
|
|
56
63
|
* Represents the regtest network configuration.
|
|
57
64
|
*/
|
|
58
|
-
|
|
65
|
+
export const regtest: Network = {
|
|
59
66
|
messagePrefix: '\x18Bitcoin Signed Message:\n',
|
|
60
67
|
bech32: 'bcrt',
|
|
61
68
|
bip32: {
|
|
@@ -69,7 +76,7 @@ exports.regtest = {
|
|
|
69
76
|
/**
|
|
70
77
|
* Represents the testnet network configuration.
|
|
71
78
|
*/
|
|
72
|
-
|
|
79
|
+
export const testnet: Network = {
|
|
73
80
|
messagePrefix: '\x18Bitcoin Signed Message:\n',
|
|
74
81
|
bech32: 'tb',
|
|
75
82
|
bip32: {
|
|
@@ -80,6 +87,7 @@ exports.testnet = {
|
|
|
80
87
|
scriptHash: 0xc4,
|
|
81
88
|
wif: 0xef,
|
|
82
89
|
};
|
|
90
|
+
|
|
83
91
|
/**
|
|
84
92
|
* Represents the Dogecoin mainnet configuration.
|
|
85
93
|
*
|
|
@@ -93,9 +101,9 @@ exports.testnet = {
|
|
|
93
101
|
* Message prefix:
|
|
94
102
|
* - Dogecoin uses "\x19Dogecoin Signed Message:\n"
|
|
95
103
|
*/
|
|
96
|
-
|
|
104
|
+
export const dogecoin: Network = {
|
|
97
105
|
messagePrefix: '\x19Dogecoin Signed Message:\n',
|
|
98
|
-
bech32: '',
|
|
106
|
+
bech32: '', // Dogecoin does not currently use Bech32
|
|
99
107
|
bip32: {
|
|
100
108
|
public: 0x02facafd,
|
|
101
109
|
private: 0x02fac398,
|
|
@@ -104,6 +112,7 @@ exports.dogecoin = {
|
|
|
104
112
|
scriptHash: 0x16,
|
|
105
113
|
wif: 0x9e,
|
|
106
114
|
};
|
|
115
|
+
|
|
107
116
|
/**
|
|
108
117
|
* Represents the Dogecoin testnet configuration.
|
|
109
118
|
*
|
|
@@ -117,9 +126,9 @@ exports.dogecoin = {
|
|
|
117
126
|
* Message prefix:
|
|
118
127
|
* - Same as mainnet: "\x19Dogecoin Signed Message:\n"
|
|
119
128
|
*/
|
|
120
|
-
|
|
129
|
+
export const dogecoinTestnet: Network = {
|
|
121
130
|
messagePrefix: '\x19Dogecoin Signed Message:\n',
|
|
122
|
-
bech32: '',
|
|
131
|
+
bech32: '', // Dogecoin testnet does not currently use Bech32
|
|
123
132
|
bip32: {
|
|
124
133
|
public: 0x0432a9a8,
|
|
125
134
|
private: 0x0432a243,
|
|
@@ -128,10 +137,11 @@ exports.dogecoinTestnet = {
|
|
|
128
137
|
scriptHash: 0xc4,
|
|
129
138
|
wif: 0xf1,
|
|
130
139
|
};
|
|
140
|
+
|
|
131
141
|
/**
|
|
132
142
|
* Litecoin mainnet configuration.
|
|
133
143
|
*/
|
|
134
|
-
|
|
144
|
+
export const litecoin: Network = {
|
|
135
145
|
messagePrefix: '\x19Litecoin Signed Message:\n',
|
|
136
146
|
bech32: 'ltc',
|
|
137
147
|
bip32: {
|
|
@@ -142,10 +152,11 @@ exports.litecoin = {
|
|
|
142
152
|
scriptHash: 0x32,
|
|
143
153
|
wif: 0xb0,
|
|
144
154
|
};
|
|
155
|
+
|
|
145
156
|
/**
|
|
146
157
|
* Litecoin testnet configuration.
|
|
147
158
|
*/
|
|
148
|
-
|
|
159
|
+
export const litecoinTestnet: Network = {
|
|
149
160
|
messagePrefix: '\x19Litecoin Signed Message:\n',
|
|
150
161
|
bech32: 'tltc',
|
|
151
162
|
bip32: {
|
|
@@ -156,12 +167,13 @@ exports.litecoinTestnet = {
|
|
|
156
167
|
scriptHash: 0x3a,
|
|
157
168
|
wif: 0xef,
|
|
158
169
|
};
|
|
170
|
+
|
|
159
171
|
/**
|
|
160
172
|
* Bitcoin Cash mainnet configuration (legacy).
|
|
161
173
|
* Note: Bitcoin Cash uses Cashaddr starting with 'q' or 'p',
|
|
162
174
|
* but we retain the legacy prefixes for compatibility.
|
|
163
175
|
*/
|
|
164
|
-
|
|
176
|
+
export const bitcoinCash: Network = {
|
|
165
177
|
messagePrefix: '\x18Bitcoin Signed Message:\n',
|
|
166
178
|
// Cashaddr prefix differs from bech32 for general usage, but we can set it similarly.
|
|
167
179
|
// Actual cashaddr prefix is "bitcoincash", but this field is for bech32 which BCH doesn't fully use for segwit (it doesn't have segwit).
|
|
@@ -174,10 +186,11 @@ exports.bitcoinCash = {
|
|
|
174
186
|
scriptHash: 0x05,
|
|
175
187
|
wif: 0x80,
|
|
176
188
|
};
|
|
189
|
+
|
|
177
190
|
/**
|
|
178
191
|
* Bitcoin Cash testnet configuration (legacy).
|
|
179
192
|
*/
|
|
180
|
-
|
|
193
|
+
export const bitcoinCashTestnet: Network = {
|
|
181
194
|
messagePrefix: '\x18Bitcoin Signed Message:\n',
|
|
182
195
|
bech32: 'bchtest',
|
|
183
196
|
bip32: {
|
|
@@ -188,14 +201,15 @@ exports.bitcoinCashTestnet = {
|
|
|
188
201
|
scriptHash: 0xc4,
|
|
189
202
|
wif: 0xef,
|
|
190
203
|
};
|
|
204
|
+
|
|
191
205
|
/**
|
|
192
206
|
* Dash mainnet configuration.
|
|
193
207
|
*/
|
|
194
|
-
|
|
208
|
+
export const dash: Network = {
|
|
195
209
|
// Historically Dash used DarkCoin message prefix, and most implementations use this:
|
|
196
210
|
// As of Dash Core 0.17, this has not changed in code.
|
|
197
211
|
messagePrefix: '\x19DarkCoin Signed Message:\n',
|
|
198
|
-
bech32: '',
|
|
212
|
+
bech32: '', // Dash does not use Bech32
|
|
199
213
|
bip32: {
|
|
200
214
|
public: 0x02fe52cc,
|
|
201
215
|
private: 0x02fe52f8,
|
|
@@ -204,12 +218,13 @@ exports.dash = {
|
|
|
204
218
|
scriptHash: 0x10,
|
|
205
219
|
wif: 0xcc,
|
|
206
220
|
};
|
|
221
|
+
|
|
207
222
|
/**
|
|
208
223
|
* Dash testnet configuration.
|
|
209
224
|
*/
|
|
210
|
-
|
|
225
|
+
export const dashTestnet: Network = {
|
|
211
226
|
messagePrefix: '\x19DarkCoin Signed Message:\n',
|
|
212
|
-
bech32: '',
|
|
227
|
+
bech32: '', // Dash testnet does not use Bech32
|
|
213
228
|
bip32: {
|
|
214
229
|
public: 0x3a8061a0,
|
|
215
230
|
private: 0x3a805837,
|
package/src/ops.ts
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
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
|
+
|
|
27
|
+
// control
|
|
28
|
+
OP_NOP: number;
|
|
29
|
+
OP_VER: number;
|
|
30
|
+
OP_IF: number;
|
|
31
|
+
OP_NOTIF: number;
|
|
32
|
+
OP_VERIF: number;
|
|
33
|
+
OP_VERNOTIF: number;
|
|
34
|
+
OP_ELSE: number;
|
|
35
|
+
OP_ENDIF: number;
|
|
36
|
+
OP_VERIFY: number;
|
|
37
|
+
OP_RETURN: number;
|
|
38
|
+
|
|
39
|
+
// stack ops
|
|
40
|
+
OP_TOALTSTACK: number;
|
|
41
|
+
OP_FROMALTSTACK: number;
|
|
42
|
+
OP_2DROP: number;
|
|
43
|
+
OP_2DUP: number;
|
|
44
|
+
OP_3DUP: number;
|
|
45
|
+
OP_2OVER: number;
|
|
46
|
+
OP_2ROT: number;
|
|
47
|
+
OP_2SWAP: number;
|
|
48
|
+
OP_IFDUP: number;
|
|
49
|
+
OP_DEPTH: number;
|
|
50
|
+
OP_DROP: number;
|
|
51
|
+
OP_DUP: number;
|
|
52
|
+
OP_NIP: number;
|
|
53
|
+
OP_OVER: number;
|
|
54
|
+
OP_PICK: number;
|
|
55
|
+
OP_ROLL: number;
|
|
56
|
+
OP_ROT: number;
|
|
57
|
+
OP_SWAP: number;
|
|
58
|
+
OP_TUCK: number;
|
|
59
|
+
|
|
60
|
+
// splice ops
|
|
61
|
+
OP_CAT: number;
|
|
62
|
+
OP_SUBSTR: number;
|
|
63
|
+
OP_LEFT: number;
|
|
64
|
+
OP_RIGHT: number;
|
|
65
|
+
OP_SIZE: number;
|
|
66
|
+
|
|
67
|
+
// bit logic
|
|
68
|
+
OP_INVERT: number;
|
|
69
|
+
OP_AND: number;
|
|
70
|
+
OP_OR: number;
|
|
71
|
+
OP_XOR: number;
|
|
72
|
+
OP_EQUAL: number;
|
|
73
|
+
OP_EQUALVERIFY: number;
|
|
74
|
+
OP_RESERVED1: number;
|
|
75
|
+
OP_RESERVED2: number;
|
|
76
|
+
|
|
77
|
+
// numeric
|
|
78
|
+
OP_1ADD: number;
|
|
79
|
+
OP_1SUB: number;
|
|
80
|
+
OP_2MUL: number;
|
|
81
|
+
OP_2DIV: number;
|
|
82
|
+
OP_NEGATE: number;
|
|
83
|
+
OP_ABS: number;
|
|
84
|
+
OP_NOT: number;
|
|
85
|
+
OP_0NOTEQUAL: number;
|
|
86
|
+
OP_ADD: number;
|
|
87
|
+
OP_SUB: number;
|
|
88
|
+
OP_MUL: number;
|
|
89
|
+
OP_DIV: number;
|
|
90
|
+
OP_MOD: number;
|
|
91
|
+
OP_LSHIFT: number;
|
|
92
|
+
OP_RSHIFT: number;
|
|
93
|
+
OP_BOOLAND: number;
|
|
94
|
+
OP_BOOLOR: number;
|
|
95
|
+
OP_NUMEQUAL: number;
|
|
96
|
+
OP_NUMEQUALVERIFY: number;
|
|
97
|
+
OP_NUMNOTEQUAL: number;
|
|
98
|
+
OP_LESSTHAN: number;
|
|
99
|
+
OP_GREATERTHAN: number;
|
|
100
|
+
OP_LESSTHANOREQUAL: number;
|
|
101
|
+
OP_GREATERTHANOREQUAL: number;
|
|
102
|
+
OP_MIN: number;
|
|
103
|
+
OP_MAX: number;
|
|
104
|
+
OP_WITHIN: number;
|
|
105
|
+
|
|
106
|
+
// crypto
|
|
107
|
+
OP_RIPEMD160: number;
|
|
108
|
+
OP_SHA1: number;
|
|
109
|
+
OP_SHA256: number;
|
|
110
|
+
OP_HASH160: number;
|
|
111
|
+
OP_HASH256: number;
|
|
112
|
+
OP_CODESEPARATOR: number;
|
|
113
|
+
OP_CHECKSIG: number;
|
|
114
|
+
OP_CHECKSIGVERIFY: number;
|
|
115
|
+
OP_CHECKMULTISIG: number;
|
|
116
|
+
OP_CHECKMULTISIGVERIFY: number;
|
|
117
|
+
OP_CHECKLOCKTIMEVERIFY: number;
|
|
118
|
+
OP_CHECKSEQUENCEVERIFY: number;
|
|
119
|
+
|
|
120
|
+
OP_CHECKSIGADD: number;
|
|
121
|
+
|
|
122
|
+
// expansion
|
|
123
|
+
OP_NOP1: number;
|
|
124
|
+
OP_NOP2: number;
|
|
125
|
+
OP_NOP3: number;
|
|
126
|
+
OP_NOP4: number;
|
|
127
|
+
OP_NOP5: number;
|
|
128
|
+
OP_NOP6: number;
|
|
129
|
+
OP_NOP7: number;
|
|
130
|
+
OP_NOP8: number;
|
|
131
|
+
OP_NOP9: number;
|
|
132
|
+
OP_NOP10: number;
|
|
133
|
+
|
|
134
|
+
// template matching params
|
|
135
|
+
OP_PUBKEYHASH: number;
|
|
136
|
+
OP_PUBKEY: number;
|
|
137
|
+
OP_INVALIDOPCODE: number;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const OPS: Opcodes = {
|
|
141
|
+
OP_FALSE: 0,
|
|
142
|
+
OP_0: 0,
|
|
143
|
+
OP_PUSHDATA1: 76,
|
|
144
|
+
OP_PUSHDATA2: 77,
|
|
145
|
+
OP_PUSHDATA4: 78,
|
|
146
|
+
OP_1NEGATE: 79,
|
|
147
|
+
OP_RESERVED: 80,
|
|
148
|
+
OP_TRUE: 81,
|
|
149
|
+
OP_1: 81,
|
|
150
|
+
OP_2: 82,
|
|
151
|
+
OP_3: 83,
|
|
152
|
+
OP_4: 84,
|
|
153
|
+
OP_5: 85,
|
|
154
|
+
OP_6: 86,
|
|
155
|
+
OP_7: 87,
|
|
156
|
+
OP_8: 88,
|
|
157
|
+
OP_9: 89,
|
|
158
|
+
OP_10: 90,
|
|
159
|
+
OP_11: 91,
|
|
160
|
+
OP_12: 92,
|
|
161
|
+
OP_13: 93,
|
|
162
|
+
OP_14: 94,
|
|
163
|
+
OP_15: 95,
|
|
164
|
+
OP_16: 96,
|
|
165
|
+
|
|
166
|
+
OP_NOP: 97,
|
|
167
|
+
OP_VER: 98,
|
|
168
|
+
OP_IF: 99,
|
|
169
|
+
OP_NOTIF: 100,
|
|
170
|
+
OP_VERIF: 101,
|
|
171
|
+
OP_VERNOTIF: 102,
|
|
172
|
+
OP_ELSE: 103,
|
|
173
|
+
OP_ENDIF: 104,
|
|
174
|
+
OP_VERIFY: 105,
|
|
175
|
+
OP_RETURN: 106,
|
|
176
|
+
|
|
177
|
+
OP_TOALTSTACK: 107,
|
|
178
|
+
OP_FROMALTSTACK: 108,
|
|
179
|
+
OP_2DROP: 109,
|
|
180
|
+
OP_2DUP: 110,
|
|
181
|
+
OP_3DUP: 111,
|
|
182
|
+
OP_2OVER: 112,
|
|
183
|
+
OP_2ROT: 113,
|
|
184
|
+
OP_2SWAP: 114,
|
|
185
|
+
OP_IFDUP: 115,
|
|
186
|
+
OP_DEPTH: 116,
|
|
187
|
+
OP_DROP: 117,
|
|
188
|
+
OP_DUP: 118,
|
|
189
|
+
OP_NIP: 119,
|
|
190
|
+
OP_OVER: 120,
|
|
191
|
+
OP_PICK: 121,
|
|
192
|
+
OP_ROLL: 122,
|
|
193
|
+
OP_ROT: 123,
|
|
194
|
+
OP_SWAP: 124,
|
|
195
|
+
OP_TUCK: 125,
|
|
196
|
+
|
|
197
|
+
OP_CAT: 126,
|
|
198
|
+
OP_SUBSTR: 127,
|
|
199
|
+
OP_LEFT: 128,
|
|
200
|
+
OP_RIGHT: 129,
|
|
201
|
+
OP_SIZE: 130,
|
|
202
|
+
|
|
203
|
+
OP_INVERT: 131,
|
|
204
|
+
OP_AND: 132,
|
|
205
|
+
OP_OR: 133,
|
|
206
|
+
OP_XOR: 134,
|
|
207
|
+
OP_EQUAL: 135,
|
|
208
|
+
OP_EQUALVERIFY: 136,
|
|
209
|
+
OP_RESERVED1: 137,
|
|
210
|
+
OP_RESERVED2: 138,
|
|
211
|
+
|
|
212
|
+
OP_1ADD: 139,
|
|
213
|
+
OP_1SUB: 140,
|
|
214
|
+
OP_2MUL: 141,
|
|
215
|
+
OP_2DIV: 142,
|
|
216
|
+
OP_NEGATE: 143,
|
|
217
|
+
OP_ABS: 144,
|
|
218
|
+
OP_NOT: 145,
|
|
219
|
+
OP_0NOTEQUAL: 146,
|
|
220
|
+
OP_ADD: 147,
|
|
221
|
+
OP_SUB: 148,
|
|
222
|
+
OP_MUL: 149,
|
|
223
|
+
OP_DIV: 150,
|
|
224
|
+
OP_MOD: 151,
|
|
225
|
+
OP_LSHIFT: 152,
|
|
226
|
+
OP_RSHIFT: 153,
|
|
227
|
+
|
|
228
|
+
OP_BOOLAND: 154,
|
|
229
|
+
OP_BOOLOR: 155,
|
|
230
|
+
OP_NUMEQUAL: 156,
|
|
231
|
+
OP_NUMEQUALVERIFY: 157,
|
|
232
|
+
OP_NUMNOTEQUAL: 158,
|
|
233
|
+
OP_LESSTHAN: 159,
|
|
234
|
+
OP_GREATERTHAN: 160,
|
|
235
|
+
OP_LESSTHANOREQUAL: 161,
|
|
236
|
+
OP_GREATERTHANOREQUAL: 162,
|
|
237
|
+
OP_MIN: 163,
|
|
238
|
+
OP_MAX: 164,
|
|
239
|
+
|
|
240
|
+
OP_WITHIN: 165,
|
|
241
|
+
|
|
242
|
+
OP_RIPEMD160: 166,
|
|
243
|
+
OP_SHA1: 167,
|
|
244
|
+
OP_SHA256: 168,
|
|
245
|
+
OP_HASH160: 169,
|
|
246
|
+
OP_HASH256: 170,
|
|
247
|
+
OP_CODESEPARATOR: 171,
|
|
248
|
+
OP_CHECKSIG: 172,
|
|
249
|
+
OP_CHECKSIGVERIFY: 173,
|
|
250
|
+
OP_CHECKMULTISIG: 174,
|
|
251
|
+
OP_CHECKMULTISIGVERIFY: 175,
|
|
252
|
+
|
|
253
|
+
OP_NOP1: 176,
|
|
254
|
+
|
|
255
|
+
OP_NOP2: 177,
|
|
256
|
+
OP_CHECKLOCKTIMEVERIFY: 177,
|
|
257
|
+
|
|
258
|
+
OP_NOP3: 178,
|
|
259
|
+
OP_CHECKSEQUENCEVERIFY: 178,
|
|
260
|
+
|
|
261
|
+
OP_NOP4: 179,
|
|
262
|
+
OP_NOP5: 180,
|
|
263
|
+
OP_NOP6: 181,
|
|
264
|
+
OP_NOP7: 182,
|
|
265
|
+
OP_NOP8: 183,
|
|
266
|
+
OP_NOP9: 184,
|
|
267
|
+
OP_NOP10: 185,
|
|
268
|
+
|
|
269
|
+
OP_CHECKSIGADD: 186,
|
|
270
|
+
|
|
271
|
+
OP_PUBKEYHASH: 253,
|
|
272
|
+
OP_PUBKEY: 254,
|
|
273
|
+
OP_INVALIDOPCODE: 255,
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const REVERSE_OPS: { [key: number]: string } = {};
|
|
277
|
+
for (const op of Object.keys(OPS)) {
|
|
278
|
+
const code = OPS[op as keyof Opcodes];
|
|
279
|
+
REVERSE_OPS[code] = op;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export { OPS, REVERSE_OPS };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { Buffer as NBuffer } from 'buffer';
|
|
2
|
+
import * as bcrypto from '../crypto.js';
|
|
3
|
+
import { getEccLib } from '../ecc_lib.js';
|
|
4
|
+
|
|
5
|
+
import { varuint } from '../bufferutils.js';
|
|
6
|
+
import { isTapleaf, Tapleaf, Taptree } from '../types.js';
|
|
7
|
+
|
|
8
|
+
export const LEAF_VERSION_TAPSCRIPT = 0xc0;
|
|
9
|
+
export const MAX_TAPTREE_DEPTH = 128;
|
|
10
|
+
|
|
11
|
+
interface HashLeaf {
|
|
12
|
+
hash: Buffer;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface HashBranch {
|
|
16
|
+
hash: Buffer;
|
|
17
|
+
left: HashTree;
|
|
18
|
+
right: HashTree;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface TweakedPublicKey {
|
|
22
|
+
parity: number;
|
|
23
|
+
x: Buffer;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const isHashBranch = (ht: HashTree): ht is HashBranch => 'left' in ht && 'right' in ht;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Binary tree representing leaf, branch, and root node hashes of a Taptree.
|
|
30
|
+
* Each node contains a hash, and potentially left and right branch hashes.
|
|
31
|
+
* This tree is used for 2 purposes: Providing the root hash for tweaking,
|
|
32
|
+
* and calculating merkle inclusion proofs when constructing a control block.
|
|
33
|
+
*/
|
|
34
|
+
export type HashTree = HashLeaf | HashBranch;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Calculates the root hash from a given control block and leaf hash.
|
|
38
|
+
* @param controlBlock - The control block buffer.
|
|
39
|
+
* @param leafHash - The leaf hash buffer.
|
|
40
|
+
* @returns The root hash buffer.
|
|
41
|
+
* @throws {TypeError} If the control block length is less than 33.
|
|
42
|
+
*/
|
|
43
|
+
export function rootHashFromPath(controlBlock: Buffer, leafHash: Buffer): Buffer {
|
|
44
|
+
if (controlBlock.length < 33)
|
|
45
|
+
throw new TypeError(
|
|
46
|
+
`The control-block length is too small. Got ${controlBlock.length}, expected min 33.`,
|
|
47
|
+
);
|
|
48
|
+
const m = (controlBlock.length - 33) / 32;
|
|
49
|
+
|
|
50
|
+
let kj = leafHash;
|
|
51
|
+
for (let j = 0; j < m; j++) {
|
|
52
|
+
const ej = controlBlock.slice(33 + 32 * j, 65 + 32 * j);
|
|
53
|
+
if (kj.compare(ej) < 0) {
|
|
54
|
+
kj = tapBranchHash(kj, ej);
|
|
55
|
+
} else {
|
|
56
|
+
kj = tapBranchHash(ej, kj);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return kj;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Build a hash tree of merkle nodes from the scripts binary tree.
|
|
65
|
+
* @param scriptTree - the tree of scripts to pairwise hash.
|
|
66
|
+
*/
|
|
67
|
+
export function toHashTree(scriptTree: Taptree): HashTree {
|
|
68
|
+
if (isTapleaf(scriptTree)) return { hash: tapleafHash(scriptTree) };
|
|
69
|
+
|
|
70
|
+
const hashes = [toHashTree(scriptTree[0]), toHashTree(scriptTree[1])];
|
|
71
|
+
hashes.sort((a, b) => a.hash.compare(b.hash));
|
|
72
|
+
const [left, right] = hashes;
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
hash: tapBranchHash(left.hash, right.hash),
|
|
76
|
+
left,
|
|
77
|
+
right,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Given a HashTree, finds the path from a particular hash to the root.
|
|
83
|
+
* @param node - the root of the tree
|
|
84
|
+
* @param hash - the hash to search for
|
|
85
|
+
* @returns - array of sibling hashes, from leaf (inclusive) to root
|
|
86
|
+
* (exclusive) needed to prove inclusion of the specified hash. undefined if no
|
|
87
|
+
* path is found
|
|
88
|
+
*/
|
|
89
|
+
export function findScriptPath(node: HashTree, hash: Buffer): Buffer[] | undefined {
|
|
90
|
+
if (isHashBranch(node)) {
|
|
91
|
+
const leftPath = findScriptPath(node.left, hash);
|
|
92
|
+
if (leftPath !== undefined) return [...leftPath, node.right.hash];
|
|
93
|
+
|
|
94
|
+
const rightPath = findScriptPath(node.right, hash);
|
|
95
|
+
if (rightPath !== undefined) return [...rightPath, node.left.hash];
|
|
96
|
+
} else if (node.hash.equals(hash)) {
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function tapleafHash(leaf: Tapleaf): Buffer {
|
|
104
|
+
const version = leaf.version || LEAF_VERSION_TAPSCRIPT;
|
|
105
|
+
return bcrypto.taggedHash(
|
|
106
|
+
'TapLeaf',
|
|
107
|
+
NBuffer.concat([NBuffer.from([version]), serializeScript(leaf.output)]),
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function tapTweakHash(pubKey: Buffer, h: Buffer | undefined): Buffer {
|
|
112
|
+
return bcrypto.taggedHash('TapTweak', NBuffer.concat(h ? [pubKey, h] : [pubKey]));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function tweakKey(pubKey: Buffer, h: Buffer | undefined): TweakedPublicKey | null {
|
|
116
|
+
if (!NBuffer.isBuffer(pubKey)) return null;
|
|
117
|
+
if (pubKey.length !== 32) return null;
|
|
118
|
+
if (h && h.length !== 32) return null;
|
|
119
|
+
|
|
120
|
+
const tweakHash = tapTweakHash(pubKey, h);
|
|
121
|
+
|
|
122
|
+
const res = getEccLib().xOnlyPointAddTweak(pubKey, tweakHash);
|
|
123
|
+
if (!res || res.xOnlyPubkey === null) return null;
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
parity: res.parity,
|
|
127
|
+
x: NBuffer.from(res.xOnlyPubkey),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function tapBranchHash(a: Buffer, b: Buffer): Buffer {
|
|
132
|
+
return bcrypto.taggedHash('TapBranch', NBuffer.concat([a, b]));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function serializeScript(s: Buffer): Buffer {
|
|
136
|
+
const varintLen = varuint.encodingLength(s.length);
|
|
137
|
+
const buffer = NBuffer.allocUnsafe(varintLen); // better
|
|
138
|
+
varuint.encode(s.length, buffer);
|
|
139
|
+
return NBuffer.concat([buffer, s]);
|
|
140
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
|
|
2
|
+
import * as bscript from '../script.js';
|
|
3
|
+
import { stacksEqual, typeforce as typef } from '../types.js';
|
|
4
|
+
import { Payment, PaymentOpts, Stack } from './index.js';
|
|
5
|
+
import * as lazy from './lazy.js';
|
|
6
|
+
|
|
7
|
+
const OPS = bscript.OPS;
|
|
8
|
+
|
|
9
|
+
// output: OP_RETURN ...
|
|
10
|
+
/**
|
|
11
|
+
* Embeds data in a Bitcoin payment.
|
|
12
|
+
* @param a - The payment object.
|
|
13
|
+
* @param opts - Optional payment options.
|
|
14
|
+
* @returns The modified payment object.
|
|
15
|
+
* @throws {TypeError} If there is not enough data or if the output is invalid.
|
|
16
|
+
*/
|
|
17
|
+
export function p2data(a: Payment, opts?: PaymentOpts): Payment {
|
|
18
|
+
if (!a.data && !a.output) throw new TypeError('Not enough data');
|
|
19
|
+
opts = Object.assign({ validate: true }, opts || {});
|
|
20
|
+
|
|
21
|
+
typef(
|
|
22
|
+
{
|
|
23
|
+
network: typef.maybe(typef.Object),
|
|
24
|
+
output: typef.maybe(typef.Buffer),
|
|
25
|
+
data: typef.maybe(typef.arrayOf(typef.Buffer)),
|
|
26
|
+
},
|
|
27
|
+
a,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const network = a.network || BITCOIN_NETWORK;
|
|
31
|
+
const o = { name: 'embed', network } as Payment;
|
|
32
|
+
|
|
33
|
+
lazy.prop(o, 'output', () => {
|
|
34
|
+
if (!a.data) return;
|
|
35
|
+
return bscript.compile(([OPS.OP_RETURN] as Stack).concat(a.data));
|
|
36
|
+
});
|
|
37
|
+
lazy.prop(o, 'data', () => {
|
|
38
|
+
if (!a.output) return;
|
|
39
|
+
return bscript.decompile(a.output)!.slice(1);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// extended validation
|
|
43
|
+
if (opts.validate) {
|
|
44
|
+
if (a.output) {
|
|
45
|
+
const chunks = bscript.decompile(a.output);
|
|
46
|
+
if (chunks![0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid');
|
|
47
|
+
if (!chunks!.slice(1).every(typef.Buffer)) throw new TypeError('Output is invalid');
|
|
48
|
+
|
|
49
|
+
if (a.data && !stacksEqual(a.data, o.data as Buffer[]))
|
|
50
|
+
throw new TypeError('Data mismatch');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return Object.assign(o, a);
|
|
55
|
+
}
|