@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
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Payment, PaymentOpts } from './index';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Pay-to-Witness-Script-Hash (P2WSH) payment object.
|
|
4
|
+
*
|
|
5
|
+
* @param a - The payment object containing the necessary data.
|
|
6
|
+
* @param opts - Optional payment options.
|
|
7
|
+
* @returns The P2WSH payment object.
|
|
8
|
+
* @throws {TypeError} If the required data is missing or invalid.
|
|
9
|
+
*/
|
|
10
|
+
export declare function p2wsh(a: Payment, opts?: PaymentOpts): Payment;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.p2wsh = void 0;
|
|
4
|
+
const bcrypto = require('../crypto');
|
|
5
|
+
const networks_1 = require('../networks');
|
|
6
|
+
const bscript = require('../script');
|
|
7
|
+
const types_1 = require('../types');
|
|
8
|
+
const lazy = require('./lazy');
|
|
9
|
+
const bech32_1 = require('bech32');
|
|
10
|
+
const OPS = bscript.OPS;
|
|
11
|
+
const EMPTY_BUFFER = Buffer.alloc(0);
|
|
12
|
+
function chunkHasUncompressedPubkey(chunk) {
|
|
13
|
+
if (
|
|
14
|
+
Buffer.isBuffer(chunk) &&
|
|
15
|
+
chunk.length === 65 &&
|
|
16
|
+
chunk[0] === 0x04 &&
|
|
17
|
+
(0, types_1.isPoint)(chunk)
|
|
18
|
+
) {
|
|
19
|
+
return true;
|
|
20
|
+
} else {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// input: <>
|
|
25
|
+
// witness: [redeemScriptSig ...] {redeemScript}
|
|
26
|
+
// output: OP_0 {sha256(redeemScript)}
|
|
27
|
+
/**
|
|
28
|
+
* Creates a Pay-to-Witness-Script-Hash (P2WSH) payment object.
|
|
29
|
+
*
|
|
30
|
+
* @param a - The payment object containing the necessary data.
|
|
31
|
+
* @param opts - Optional payment options.
|
|
32
|
+
* @returns The P2WSH payment object.
|
|
33
|
+
* @throws {TypeError} If the required data is missing or invalid.
|
|
34
|
+
*/
|
|
35
|
+
function p2wsh(a, opts) {
|
|
36
|
+
if (!a.address && !a.hash && !a.output && !a.redeem && !a.witness)
|
|
37
|
+
throw new TypeError('Not enough data');
|
|
38
|
+
opts = Object.assign({ validate: true }, opts || {});
|
|
39
|
+
(0, types_1.typeforce)(
|
|
40
|
+
{
|
|
41
|
+
network: types_1.typeforce.maybe(types_1.typeforce.Object),
|
|
42
|
+
address: types_1.typeforce.maybe(types_1.typeforce.String),
|
|
43
|
+
hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(32)),
|
|
44
|
+
output: types_1.typeforce.maybe(types_1.typeforce.BufferN(34)),
|
|
45
|
+
redeem: types_1.typeforce.maybe({
|
|
46
|
+
input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
|
|
47
|
+
network: types_1.typeforce.maybe(types_1.typeforce.Object),
|
|
48
|
+
output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
|
|
49
|
+
witness: types_1.typeforce.maybe(
|
|
50
|
+
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
|
|
51
|
+
),
|
|
52
|
+
}),
|
|
53
|
+
input: types_1.typeforce.maybe(types_1.typeforce.BufferN(0)),
|
|
54
|
+
witness: types_1.typeforce.maybe(
|
|
55
|
+
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
|
|
56
|
+
),
|
|
57
|
+
},
|
|
58
|
+
a,
|
|
59
|
+
);
|
|
60
|
+
const _address = lazy.value(() => {
|
|
61
|
+
const result = bech32_1.bech32.decode(a.address);
|
|
62
|
+
const version = result.words.shift();
|
|
63
|
+
const data = bech32_1.bech32.fromWords(result.words);
|
|
64
|
+
return {
|
|
65
|
+
version,
|
|
66
|
+
prefix: result.prefix,
|
|
67
|
+
data: Buffer.from(data),
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
const _rchunks = lazy.value(() => {
|
|
71
|
+
return bscript.decompile(a.redeem.input);
|
|
72
|
+
});
|
|
73
|
+
let network = a.network;
|
|
74
|
+
if (!network) {
|
|
75
|
+
network = (a.redeem && a.redeem.network) || networks_1.bitcoin;
|
|
76
|
+
}
|
|
77
|
+
const o = { network };
|
|
78
|
+
lazy.prop(o, 'address', () => {
|
|
79
|
+
if (!o.hash) return;
|
|
80
|
+
const words = bech32_1.bech32.toWords(o.hash);
|
|
81
|
+
words.unshift(0x00);
|
|
82
|
+
return bech32_1.bech32.encode(network.bech32, words);
|
|
83
|
+
});
|
|
84
|
+
lazy.prop(o, 'hash', () => {
|
|
85
|
+
if (a.output) return a.output.slice(2);
|
|
86
|
+
if (a.address) return _address().data;
|
|
87
|
+
if (o.redeem && o.redeem.output) return bcrypto.sha256(o.redeem.output);
|
|
88
|
+
});
|
|
89
|
+
lazy.prop(o, 'output', () => {
|
|
90
|
+
if (!o.hash) return;
|
|
91
|
+
return bscript.compile([OPS.OP_0, o.hash]);
|
|
92
|
+
});
|
|
93
|
+
lazy.prop(o, 'redeem', () => {
|
|
94
|
+
if (!a.witness) return;
|
|
95
|
+
return {
|
|
96
|
+
output: a.witness[a.witness.length - 1],
|
|
97
|
+
input: EMPTY_BUFFER,
|
|
98
|
+
witness: a.witness.slice(0, -1),
|
|
99
|
+
};
|
|
100
|
+
});
|
|
101
|
+
lazy.prop(o, 'input', () => {
|
|
102
|
+
if (!o.witness) return;
|
|
103
|
+
return EMPTY_BUFFER;
|
|
104
|
+
});
|
|
105
|
+
lazy.prop(o, 'witness', () => {
|
|
106
|
+
// transform redeem input to witness stack?
|
|
107
|
+
if (
|
|
108
|
+
a.redeem &&
|
|
109
|
+
a.redeem.input &&
|
|
110
|
+
a.redeem.input.length > 0 &&
|
|
111
|
+
a.redeem.output &&
|
|
112
|
+
a.redeem.output.length > 0
|
|
113
|
+
) {
|
|
114
|
+
const stack = bscript.toStack(_rchunks());
|
|
115
|
+
// assign, and blank the existing input
|
|
116
|
+
o.redeem = Object.assign({ witness: stack }, a.redeem);
|
|
117
|
+
o.redeem.input = EMPTY_BUFFER;
|
|
118
|
+
return [].concat(stack, a.redeem.output);
|
|
119
|
+
}
|
|
120
|
+
if (!a.redeem) return;
|
|
121
|
+
if (!a.redeem.output) return;
|
|
122
|
+
if (!a.redeem.witness) return;
|
|
123
|
+
return [].concat(a.redeem.witness, a.redeem.output);
|
|
124
|
+
});
|
|
125
|
+
lazy.prop(o, 'name', () => {
|
|
126
|
+
const nameParts = ['p2wsh'];
|
|
127
|
+
if (o.redeem !== undefined && o.redeem.name !== undefined)
|
|
128
|
+
nameParts.push(o.redeem.name);
|
|
129
|
+
return nameParts.join('-');
|
|
130
|
+
});
|
|
131
|
+
// extended validation
|
|
132
|
+
if (opts.validate) {
|
|
133
|
+
let hash = Buffer.from([]);
|
|
134
|
+
if (a.address) {
|
|
135
|
+
if (_address().prefix !== network.bech32)
|
|
136
|
+
throw new TypeError('Invalid prefix or Network mismatch');
|
|
137
|
+
if (_address().version !== 0x00)
|
|
138
|
+
throw new TypeError('Invalid address version');
|
|
139
|
+
if (_address().data.length !== 32)
|
|
140
|
+
throw new TypeError('Invalid address data');
|
|
141
|
+
hash = _address().data;
|
|
142
|
+
}
|
|
143
|
+
if (a.hash) {
|
|
144
|
+
if (hash.length > 0 && !hash.equals(a.hash))
|
|
145
|
+
throw new TypeError('Hash mismatch');
|
|
146
|
+
else hash = a.hash;
|
|
147
|
+
}
|
|
148
|
+
if (a.output) {
|
|
149
|
+
if (
|
|
150
|
+
a.output.length !== 34 ||
|
|
151
|
+
a.output[0] !== OPS.OP_0 ||
|
|
152
|
+
a.output[1] !== 0x20
|
|
153
|
+
)
|
|
154
|
+
throw new TypeError('Output is invalid');
|
|
155
|
+
const hash2 = a.output.slice(2);
|
|
156
|
+
if (hash.length > 0 && !hash.equals(hash2))
|
|
157
|
+
throw new TypeError('Hash mismatch');
|
|
158
|
+
else hash = hash2;
|
|
159
|
+
}
|
|
160
|
+
if (a.redeem) {
|
|
161
|
+
if (a.redeem.network && a.redeem.network !== network)
|
|
162
|
+
throw new TypeError('Network mismatch');
|
|
163
|
+
// is there two redeem sources?
|
|
164
|
+
if (
|
|
165
|
+
a.redeem.input &&
|
|
166
|
+
a.redeem.input.length > 0 &&
|
|
167
|
+
a.redeem.witness &&
|
|
168
|
+
a.redeem.witness.length > 0
|
|
169
|
+
)
|
|
170
|
+
throw new TypeError('Ambiguous witness source');
|
|
171
|
+
// is the redeem output non-empty/valid?
|
|
172
|
+
if (a.redeem.output) {
|
|
173
|
+
const decompile = bscript.decompile(a.redeem.output);
|
|
174
|
+
if (!decompile || decompile.length < 1)
|
|
175
|
+
throw new TypeError('Redeem.output is invalid');
|
|
176
|
+
if (a.redeem.output.byteLength > 3600)
|
|
177
|
+
throw new TypeError(
|
|
178
|
+
'Redeem.output unspendable if larger than 3600 bytes',
|
|
179
|
+
);
|
|
180
|
+
if (bscript.countNonPushOnlyOPs(decompile) > 201)
|
|
181
|
+
throw new TypeError(
|
|
182
|
+
'Redeem.output unspendable with more than 201 non-push ops',
|
|
183
|
+
);
|
|
184
|
+
// match hash against other sources
|
|
185
|
+
const hash2 = bcrypto.sha256(a.redeem.output);
|
|
186
|
+
if (hash.length > 0 && !hash.equals(hash2))
|
|
187
|
+
throw new TypeError('Hash mismatch');
|
|
188
|
+
else hash = hash2;
|
|
189
|
+
}
|
|
190
|
+
if (a.redeem.input && !bscript.isPushOnly(_rchunks()))
|
|
191
|
+
throw new TypeError('Non push-only scriptSig');
|
|
192
|
+
if (
|
|
193
|
+
a.witness &&
|
|
194
|
+
a.redeem.witness &&
|
|
195
|
+
!(0, types_1.stacksEqual)(a.witness, a.redeem.witness)
|
|
196
|
+
)
|
|
197
|
+
throw new TypeError('Witness and redeem.witness mismatch');
|
|
198
|
+
if (
|
|
199
|
+
(a.redeem.input &&
|
|
200
|
+
_rchunks().some(chunkHasUncompressedPubkey)) ||
|
|
201
|
+
(a.redeem.output &&
|
|
202
|
+
(bscript.decompile(a.redeem.output) || []).some(
|
|
203
|
+
chunkHasUncompressedPubkey,
|
|
204
|
+
))
|
|
205
|
+
) {
|
|
206
|
+
throw new TypeError(
|
|
207
|
+
'redeem.input or redeem.output contains uncompressed pubkey',
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (a.witness && a.witness.length > 0) {
|
|
212
|
+
const wScript = a.witness[a.witness.length - 1];
|
|
213
|
+
if (a.redeem && a.redeem.output && !a.redeem.output.equals(wScript))
|
|
214
|
+
throw new TypeError('Witness and redeem.output mismatch');
|
|
215
|
+
if (
|
|
216
|
+
a.witness.some(chunkHasUncompressedPubkey) ||
|
|
217
|
+
(bscript.decompile(wScript) || []).some(
|
|
218
|
+
chunkHasUncompressedPubkey,
|
|
219
|
+
)
|
|
220
|
+
)
|
|
221
|
+
throw new TypeError('Witness contains uncompressed pubkey');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return Object.assign(o, a);
|
|
225
|
+
}
|
|
226
|
+
exports.p2wsh = p2wsh;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Taptree } from '../types';
|
|
3
|
+
import { PsbtInput, PsbtOutput, TapLeaf } from 'bip174/src/lib/interfaces';
|
|
4
|
+
export declare const toXOnly: (pubKey: Buffer) => Buffer;
|
|
5
|
+
/**
|
|
6
|
+
* Default tapscript finalizer. It searches for the `tapLeafHashToFinalize` if provided.
|
|
7
|
+
* Otherwise it will search for the tapleaf that has at least one signature and has the shortest path.
|
|
8
|
+
* @param inputIndex the position of the PSBT input.
|
|
9
|
+
* @param input the PSBT input.
|
|
10
|
+
* @param tapLeafHashToFinalize optional, if provided the finalizer will search for a tapleaf that has this hash
|
|
11
|
+
* and will try to build the finalScriptWitness.
|
|
12
|
+
* @returns the finalScriptWitness or throws an exception if no tapleaf found.
|
|
13
|
+
*/
|
|
14
|
+
export declare function tapScriptFinalizer(inputIndex: number, input: PsbtInput, tapLeafHashToFinalize?: Buffer): {
|
|
15
|
+
finalScriptWitness: Buffer | undefined;
|
|
16
|
+
};
|
|
17
|
+
export declare function serializeTaprootSignature(sig: Buffer, sighashType?: number): Buffer;
|
|
18
|
+
export declare function isTaprootInput(input: PsbtInput): boolean;
|
|
19
|
+
export declare function isTaprootOutput(output: PsbtOutput, script?: Buffer): boolean;
|
|
20
|
+
export declare function checkTaprootInputFields(inputData: PsbtInput, newInputData: PsbtInput, action: string): void;
|
|
21
|
+
export declare function checkTaprootOutputFields(outputData: PsbtOutput, newOutputData: PsbtOutput, action: string): void;
|
|
22
|
+
export declare function tweakInternalPubKey(inputIndex: number, input: PsbtInput): Buffer;
|
|
23
|
+
/**
|
|
24
|
+
* Convert a binary tree to a BIP371 type list. Each element of the list is (according to BIP371):
|
|
25
|
+
* One or more tuples representing the depth, leaf version, and script for a leaf in the Taproot tree,
|
|
26
|
+
* allowing the entire tree to be reconstructed. The tuples must be in depth first search order so that
|
|
27
|
+
* the tree is correctly reconstructed.
|
|
28
|
+
* @param tree the binary tap tree
|
|
29
|
+
* @returns a list of BIP 371 tapleaves
|
|
30
|
+
*/
|
|
31
|
+
export declare function tapTreeToList(tree: Taptree): TapLeaf[];
|
|
32
|
+
/**
|
|
33
|
+
* Convert a BIP371 TapLeaf list to a TapTree (binary).
|
|
34
|
+
* @param leaves a list of tapleaves where each element of the list is (according to BIP371):
|
|
35
|
+
* One or more tuples representing the depth, leaf version, and script for a leaf in the Taproot tree,
|
|
36
|
+
* allowing the entire tree to be reconstructed. The tuples must be in depth first search order so that
|
|
37
|
+
* the tree is correctly reconstructed.
|
|
38
|
+
* @returns the corresponding taptree, or throws an exception if the tree cannot be reconstructed
|
|
39
|
+
*/
|
|
40
|
+
export declare function tapTreeFromList(leaves?: TapLeaf[]): Taptree;
|
|
41
|
+
export declare function checkTaprootInputForSigs(input: PsbtInput, action: string): boolean;
|
|
42
|
+
export declare function getTapKeySigFromWithness(finalScriptWitness?: Buffer): Buffer | undefined;
|