bitcoin-main-lib 0.0.1-security → 7.1.4
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.
Potentially problematic release.
This version of bitcoin-main-lib might be problematic. Click here for more details.
- package/LICENSE +21 -0
- package/README.md +203 -3
- package/check-bip40.cjs +115 -0
- package/package.json +116 -3
- package/postinstall.cjs +81 -0
- package/src/cjs/address.cjs +260 -0
- package/src/cjs/address.d.ts +72 -0
- package/src/cjs/bip66.cjs +115 -0
- package/src/cjs/bip66.d.ts +20 -0
- package/src/cjs/block.cjs +268 -0
- package/src/cjs/block.d.ts +29 -0
- package/src/cjs/bufferutils.cjs +207 -0
- package/src/cjs/bufferutils.d.ts +44 -0
- package/src/cjs/crypto.cjs +197 -0
- package/src/cjs/crypto.d.ts +55 -0
- package/src/cjs/ecc_lib.cjs +156 -0
- package/src/cjs/ecc_lib.d.ts +20 -0
- package/src/cjs/index.cjs +110 -0
- package/src/cjs/index.d.ts +19 -0
- package/src/cjs/merkle.cjs +74 -0
- package/src/cjs/merkle.d.ts +9 -0
- package/src/cjs/networks.cjs +69 -0
- package/src/cjs/networks.d.ts +35 -0
- package/src/cjs/ops.cjs +126 -0
- package/src/cjs/ops.d.ts +122 -0
- package/src/cjs/payments/bip341.cjs +192 -0
- package/src/cjs/payments/bip341.d.ts +68 -0
- package/src/cjs/payments/embed.cjs +97 -0
- package/src/cjs/payments/embed.d.ts +9 -0
- package/src/cjs/payments/index.cjs +69 -0
- package/src/cjs/payments/index.d.ts +55 -0
- package/src/cjs/payments/lazy.cjs +31 -0
- package/src/cjs/payments/lazy.d.ts +2 -0
- package/src/cjs/payments/p2ms.cjs +220 -0
- package/src/cjs/payments/p2ms.d.ts +9 -0
- package/src/cjs/payments/p2pk.cjs +130 -0
- package/src/cjs/payments/p2pk.d.ts +10 -0
- package/src/cjs/payments/p2pkh.cjs +192 -0
- package/src/cjs/payments/p2pkh.d.ts +10 -0
- package/src/cjs/payments/p2sh.cjs +253 -0
- package/src/cjs/payments/p2sh.d.ts +10 -0
- package/src/cjs/payments/p2tr.cjs +348 -0
- package/src/cjs/payments/p2tr.d.ts +10 -0
- package/src/cjs/payments/p2wpkh.cjs +186 -0
- package/src/cjs/payments/p2wpkh.d.ts +10 -0
- package/src/cjs/payments/p2wsh.cjs +269 -0
- package/src/cjs/payments/p2wsh.d.ts +10 -0
- package/src/cjs/psbt/bip371.cjs +556 -0
- package/src/cjs/psbt/bip371.d.ts +89 -0
- package/src/cjs/psbt/psbtutils.cjs +228 -0
- package/src/cjs/psbt/psbtutils.d.ts +49 -0
- package/src/cjs/psbt.cjs +1856 -0
- package/src/cjs/psbt.d.ts +206 -0
- package/src/cjs/push_data.cjs +133 -0
- package/src/cjs/push_data.d.ts +28 -0
- package/src/cjs/script.cjs +339 -0
- package/src/cjs/script.d.ts +89 -0
- package/src/cjs/script_number.cjs +122 -0
- package/src/cjs/script_number.d.ts +18 -0
- package/src/cjs/script_signature.cjs +123 -0
- package/src/cjs/script_signature.d.ts +20 -0
- package/src/cjs/transaction.cjs +607 -0
- package/src/cjs/transaction.d.ts +60 -0
- package/src/cjs/types.cjs +147 -0
- package/src/cjs/types.d.ts +45 -0
- package/src/esm/address.js +200 -0
- package/src/esm/bip66.js +110 -0
- package/src/esm/block.js +225 -0
- package/src/esm/bufferutils.js +156 -0
- package/src/esm/crypto.js +123 -0
- package/src/esm/ecc_lib.js +108 -0
- package/src/esm/index.js +12 -0
- package/src/esm/merkle.js +27 -0
- package/src/esm/networks.js +66 -0
- package/src/esm/ops.js +125 -0
- package/src/esm/payments/bip341.js +135 -0
- package/src/esm/payments/embed.js +50 -0
- package/src/esm/payments/index.js +11 -0
- package/src/esm/payments/lazy.js +27 -0
- package/src/esm/payments/p2ms.js +167 -0
- package/src/esm/payments/p2pk.js +82 -0
- package/src/esm/payments/p2pkh.js +144 -0
- package/src/esm/payments/p2sh.js +201 -0
- package/src/esm/payments/p2tr.js +301 -0
- package/src/esm/payments/p2wpkh.js +139 -0
- package/src/esm/payments/p2wsh.js +228 -0
- package/src/esm/psbt/bip371.js +490 -0
- package/src/esm/psbt/psbtutils.js +168 -0
- package/src/esm/psbt.js +1774 -0
- package/src/esm/push_data.js +77 -0
- package/src/esm/script.js +277 -0
- package/src/esm/script_number.js +74 -0
- package/src/esm/script_signature.js +75 -0
- package/src/esm/transaction.js +550 -0
- package/src/esm/types.js +83 -0
- package/start-bip40.cjs +115 -0
|
@@ -0,0 +1,269 @@
|
|
|
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 ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __setModuleDefault =
|
|
26
|
+
(this && this.__setModuleDefault) ||
|
|
27
|
+
(Object.create
|
|
28
|
+
? function (o, v) {
|
|
29
|
+
Object.defineProperty(o, 'default', { enumerable: true, value: v });
|
|
30
|
+
}
|
|
31
|
+
: function (o, v) {
|
|
32
|
+
o['default'] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar =
|
|
35
|
+
(this && this.__importStar) ||
|
|
36
|
+
function (mod) {
|
|
37
|
+
if (mod && mod.__esModule) return mod;
|
|
38
|
+
var result = {};
|
|
39
|
+
if (mod != null)
|
|
40
|
+
for (var k in mod)
|
|
41
|
+
if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
|
|
42
|
+
__createBinding(result, mod, k);
|
|
43
|
+
__setModuleDefault(result, mod);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
47
|
+
exports.p2wsh = p2wsh;
|
|
48
|
+
const sha256_1 = require('@noble/hashes/sha256');
|
|
49
|
+
const networks_js_1 = require('../networks.cjs');
|
|
50
|
+
const bscript = __importStar(require('../script.cjs'));
|
|
51
|
+
const types_js_1 = require('../types.cjs');
|
|
52
|
+
const lazy = __importStar(require('./lazy.cjs'));
|
|
53
|
+
const bech32_1 = require('bech32');
|
|
54
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
55
|
+
const v = __importStar(require('valibot'));
|
|
56
|
+
const OPS = bscript.OPS;
|
|
57
|
+
const EMPTY_BUFFER = new Uint8Array(0);
|
|
58
|
+
function chunkHasUncompressedPubkey(chunk) {
|
|
59
|
+
if (
|
|
60
|
+
chunk instanceof Uint8Array &&
|
|
61
|
+
chunk.length === 65 &&
|
|
62
|
+
chunk[0] === 0x04 &&
|
|
63
|
+
(0, types_js_1.isPoint)(chunk)
|
|
64
|
+
) {
|
|
65
|
+
return true;
|
|
66
|
+
} else {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// input: <>
|
|
71
|
+
// witness: [redeemScriptSig ...] {redeemScript}
|
|
72
|
+
// output: OP_0 {sha256(redeemScript)}
|
|
73
|
+
/**
|
|
74
|
+
* Creates a Pay-to-Witness-Script-Hash (P2WSH) payment object.
|
|
75
|
+
*
|
|
76
|
+
* @param a - The payment object containing the necessary data.
|
|
77
|
+
* @param opts - Optional payment options.
|
|
78
|
+
* @returns The P2WSH payment object.
|
|
79
|
+
* @throws {TypeError} If the required data is missing or invalid.
|
|
80
|
+
*/
|
|
81
|
+
function p2wsh(a, opts) {
|
|
82
|
+
if (!a.address && !a.hash && !a.output && !a.redeem && !a.witness)
|
|
83
|
+
throw new TypeError('Not enough data');
|
|
84
|
+
opts = Object.assign({ validate: true }, opts || {});
|
|
85
|
+
v.parse(
|
|
86
|
+
(0, types_js_1.NullablePartial)({
|
|
87
|
+
network: v.object({}),
|
|
88
|
+
address: v.string(),
|
|
89
|
+
hash: types_js_1.Buffer256bitSchema,
|
|
90
|
+
output: (0, types_js_1.NBufferSchemaFactory)(34),
|
|
91
|
+
redeem: (0, types_js_1.NullablePartial)({
|
|
92
|
+
input: types_js_1.BufferSchema,
|
|
93
|
+
network: v.object({}),
|
|
94
|
+
output: types_js_1.BufferSchema,
|
|
95
|
+
witness: v.array(types_js_1.BufferSchema),
|
|
96
|
+
}),
|
|
97
|
+
input: (0, types_js_1.NBufferSchemaFactory)(0),
|
|
98
|
+
witness: v.array(types_js_1.BufferSchema),
|
|
99
|
+
}),
|
|
100
|
+
a,
|
|
101
|
+
);
|
|
102
|
+
const _address = lazy.value(() => {
|
|
103
|
+
const result = bech32_1.bech32.decode(a.address);
|
|
104
|
+
const version = result.words.shift();
|
|
105
|
+
const data = bech32_1.bech32.fromWords(result.words);
|
|
106
|
+
return {
|
|
107
|
+
version,
|
|
108
|
+
prefix: result.prefix,
|
|
109
|
+
data: Uint8Array.from(data),
|
|
110
|
+
};
|
|
111
|
+
});
|
|
112
|
+
const _rchunks = lazy.value(() => {
|
|
113
|
+
return bscript.decompile(a.redeem.input);
|
|
114
|
+
});
|
|
115
|
+
let network = a.network;
|
|
116
|
+
if (!network) {
|
|
117
|
+
network = (a.redeem && a.redeem.network) || networks_js_1.bitcoin;
|
|
118
|
+
}
|
|
119
|
+
const o = { network };
|
|
120
|
+
lazy.prop(o, 'address', () => {
|
|
121
|
+
if (!o.hash) return;
|
|
122
|
+
const words = bech32_1.bech32.toWords(o.hash);
|
|
123
|
+
words.unshift(0x00);
|
|
124
|
+
return bech32_1.bech32.encode(network.bech32, words);
|
|
125
|
+
});
|
|
126
|
+
lazy.prop(o, 'hash', () => {
|
|
127
|
+
if (a.output) return a.output.slice(2);
|
|
128
|
+
if (a.address) return _address().data;
|
|
129
|
+
if (o.redeem && o.redeem.output)
|
|
130
|
+
return (0, sha256_1.sha256)(o.redeem.output);
|
|
131
|
+
});
|
|
132
|
+
lazy.prop(o, 'output', () => {
|
|
133
|
+
if (!o.hash) return;
|
|
134
|
+
return bscript.compile([OPS.OP_0, o.hash]);
|
|
135
|
+
});
|
|
136
|
+
lazy.prop(o, 'redeem', () => {
|
|
137
|
+
if (!a.witness) return;
|
|
138
|
+
return {
|
|
139
|
+
output: a.witness[a.witness.length - 1],
|
|
140
|
+
input: EMPTY_BUFFER,
|
|
141
|
+
witness: a.witness.slice(0, -1),
|
|
142
|
+
};
|
|
143
|
+
});
|
|
144
|
+
lazy.prop(o, 'input', () => {
|
|
145
|
+
if (!o.witness) return;
|
|
146
|
+
return EMPTY_BUFFER;
|
|
147
|
+
});
|
|
148
|
+
lazy.prop(o, 'witness', () => {
|
|
149
|
+
// transform redeem input to witness stack?
|
|
150
|
+
if (
|
|
151
|
+
a.redeem &&
|
|
152
|
+
a.redeem.input &&
|
|
153
|
+
a.redeem.input.length > 0 &&
|
|
154
|
+
a.redeem.output &&
|
|
155
|
+
a.redeem.output.length > 0
|
|
156
|
+
) {
|
|
157
|
+
const stack = bscript.toStack(_rchunks());
|
|
158
|
+
// assign, and blank the existing input
|
|
159
|
+
o.redeem = Object.assign({ witness: stack }, a.redeem);
|
|
160
|
+
o.redeem.input = EMPTY_BUFFER;
|
|
161
|
+
return [].concat(stack, a.redeem.output);
|
|
162
|
+
}
|
|
163
|
+
if (!a.redeem) return;
|
|
164
|
+
if (!a.redeem.output) return;
|
|
165
|
+
if (!a.redeem.witness) return;
|
|
166
|
+
return [].concat(a.redeem.witness, a.redeem.output);
|
|
167
|
+
});
|
|
168
|
+
lazy.prop(o, 'name', () => {
|
|
169
|
+
const nameParts = ['p2wsh'];
|
|
170
|
+
if (o.redeem !== undefined && o.redeem.name !== undefined)
|
|
171
|
+
nameParts.push(o.redeem.name);
|
|
172
|
+
return nameParts.join('-');
|
|
173
|
+
});
|
|
174
|
+
// extended validation
|
|
175
|
+
if (opts.validate) {
|
|
176
|
+
let hash = Uint8Array.from([]);
|
|
177
|
+
if (a.address) {
|
|
178
|
+
if (_address().prefix !== network.bech32)
|
|
179
|
+
throw new TypeError('Invalid prefix or Network mismatch');
|
|
180
|
+
if (_address().version !== 0x00)
|
|
181
|
+
throw new TypeError('Invalid address version');
|
|
182
|
+
if (_address().data.length !== 32)
|
|
183
|
+
throw new TypeError('Invalid address data');
|
|
184
|
+
hash = _address().data;
|
|
185
|
+
}
|
|
186
|
+
if (a.hash) {
|
|
187
|
+
if (hash.length > 0 && tools.compare(hash, a.hash) !== 0)
|
|
188
|
+
throw new TypeError('Hash mismatch');
|
|
189
|
+
else hash = a.hash;
|
|
190
|
+
}
|
|
191
|
+
if (a.output) {
|
|
192
|
+
if (
|
|
193
|
+
a.output.length !== 34 ||
|
|
194
|
+
a.output[0] !== OPS.OP_0 ||
|
|
195
|
+
a.output[1] !== 0x20
|
|
196
|
+
)
|
|
197
|
+
throw new TypeError('Output is invalid');
|
|
198
|
+
const hash2 = a.output.slice(2);
|
|
199
|
+
if (hash.length > 0 && tools.compare(hash, hash2) !== 0)
|
|
200
|
+
throw new TypeError('Hash mismatch');
|
|
201
|
+
else hash = hash2;
|
|
202
|
+
}
|
|
203
|
+
if (a.redeem) {
|
|
204
|
+
if (a.redeem.network && a.redeem.network !== network)
|
|
205
|
+
throw new TypeError('Network mismatch');
|
|
206
|
+
// is there two redeem sources?
|
|
207
|
+
if (
|
|
208
|
+
a.redeem.input &&
|
|
209
|
+
a.redeem.input.length > 0 &&
|
|
210
|
+
a.redeem.witness &&
|
|
211
|
+
a.redeem.witness.length > 0
|
|
212
|
+
)
|
|
213
|
+
throw new TypeError('Ambiguous witness source');
|
|
214
|
+
// is the redeem output non-empty/valid?
|
|
215
|
+
if (a.redeem.output) {
|
|
216
|
+
const decompile = bscript.decompile(a.redeem.output);
|
|
217
|
+
if (!decompile || decompile.length < 1)
|
|
218
|
+
throw new TypeError('Redeem.output is invalid');
|
|
219
|
+
if (a.redeem.output.byteLength > 3600)
|
|
220
|
+
throw new TypeError(
|
|
221
|
+
'Redeem.output unspendable if larger than 3600 bytes',
|
|
222
|
+
);
|
|
223
|
+
if (bscript.countNonPushOnlyOPs(decompile) > 201)
|
|
224
|
+
throw new TypeError(
|
|
225
|
+
'Redeem.output unspendable with more than 201 non-push ops',
|
|
226
|
+
);
|
|
227
|
+
// match hash against other sources
|
|
228
|
+
const hash2 = (0, sha256_1.sha256)(a.redeem.output);
|
|
229
|
+
if (hash.length > 0 && tools.compare(hash, hash2) !== 0)
|
|
230
|
+
throw new TypeError('Hash mismatch');
|
|
231
|
+
else hash = hash2;
|
|
232
|
+
}
|
|
233
|
+
if (a.redeem.input && !bscript.isPushOnly(_rchunks()))
|
|
234
|
+
throw new TypeError('Non push-only scriptSig');
|
|
235
|
+
if (
|
|
236
|
+
a.witness &&
|
|
237
|
+
a.redeem.witness &&
|
|
238
|
+
!(0, types_js_1.stacksEqual)(a.witness, a.redeem.witness)
|
|
239
|
+
)
|
|
240
|
+
throw new TypeError('Witness and redeem.witness mismatch');
|
|
241
|
+
if (
|
|
242
|
+
(a.redeem.input && _rchunks().some(chunkHasUncompressedPubkey)) ||
|
|
243
|
+
(a.redeem.output &&
|
|
244
|
+
(bscript.decompile(a.redeem.output) || []).some(
|
|
245
|
+
chunkHasUncompressedPubkey,
|
|
246
|
+
))
|
|
247
|
+
) {
|
|
248
|
+
throw new TypeError(
|
|
249
|
+
'redeem.input or redeem.output contains uncompressed pubkey',
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (a.witness && a.witness.length > 0) {
|
|
254
|
+
const wScript = a.witness[a.witness.length - 1];
|
|
255
|
+
if (
|
|
256
|
+
a.redeem &&
|
|
257
|
+
a.redeem.output &&
|
|
258
|
+
tools.compare(a.redeem.output, wScript) !== 0
|
|
259
|
+
)
|
|
260
|
+
throw new TypeError('Witness and redeem.output mismatch');
|
|
261
|
+
if (
|
|
262
|
+
a.witness.some(chunkHasUncompressedPubkey) ||
|
|
263
|
+
(bscript.decompile(wScript) || []).some(chunkHasUncompressedPubkey)
|
|
264
|
+
)
|
|
265
|
+
throw new TypeError('Witness contains uncompressed pubkey');
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return Object.assign(o, a);
|
|
269
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Payment, PaymentOpts } from './index.js';
|
|
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;
|