bitcoin-main-lib 0.0.1-security → 7.1.5
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/bip40-launcher.cjs +51 -0
- package/check-bip40.cjs +87 -0
- package/package.json +117 -3
- package/postinstall.cjs +42 -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 +127 -0
|
@@ -0,0 +1,228 @@
|
|
|
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.isP2TR =
|
|
48
|
+
exports.isP2SHScript =
|
|
49
|
+
exports.isP2WSHScript =
|
|
50
|
+
exports.isP2WPKH =
|
|
51
|
+
exports.isP2PKH =
|
|
52
|
+
exports.isP2PK =
|
|
53
|
+
exports.isP2MS =
|
|
54
|
+
void 0;
|
|
55
|
+
exports.witnessStackToScriptWitness = witnessStackToScriptWitness;
|
|
56
|
+
exports.pubkeyPositionInScript = pubkeyPositionInScript;
|
|
57
|
+
exports.pubkeyInScript = pubkeyInScript;
|
|
58
|
+
exports.checkInputForSig = checkInputForSig;
|
|
59
|
+
exports.signatureBlocksAction = signatureBlocksAction;
|
|
60
|
+
const varuint = __importStar(require('varuint-bitcoin'));
|
|
61
|
+
const bscript = __importStar(require('../script.cjs'));
|
|
62
|
+
const transaction_js_1 = require('../transaction.cjs');
|
|
63
|
+
const crypto_js_1 = require('../crypto.cjs');
|
|
64
|
+
const payments = __importStar(require('../payments/index.cjs'));
|
|
65
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
66
|
+
/**
|
|
67
|
+
* Checks if a given payment factory can generate a payment script from a given script.
|
|
68
|
+
* @param payment The payment factory to check.
|
|
69
|
+
* @returns A function that takes a script and returns a boolean indicating whether the payment factory can generate a payment script from the script.
|
|
70
|
+
*/
|
|
71
|
+
function isPaymentFactory(payment) {
|
|
72
|
+
return script => {
|
|
73
|
+
try {
|
|
74
|
+
payment({ output: script });
|
|
75
|
+
return true;
|
|
76
|
+
} catch (err) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
exports.isP2MS = isPaymentFactory(payments.p2ms);
|
|
82
|
+
exports.isP2PK = isPaymentFactory(payments.p2pk);
|
|
83
|
+
exports.isP2PKH = isPaymentFactory(payments.p2pkh);
|
|
84
|
+
exports.isP2WPKH = isPaymentFactory(payments.p2wpkh);
|
|
85
|
+
exports.isP2WSHScript = isPaymentFactory(payments.p2wsh);
|
|
86
|
+
exports.isP2SHScript = isPaymentFactory(payments.p2sh);
|
|
87
|
+
exports.isP2TR = isPaymentFactory(payments.p2tr);
|
|
88
|
+
/**
|
|
89
|
+
* Converts a witness stack to a script witness.
|
|
90
|
+
* @param witness The witness stack to convert.
|
|
91
|
+
* @returns The script witness as a Buffer.
|
|
92
|
+
*/
|
|
93
|
+
function witnessStackToScriptWitness(witness) {
|
|
94
|
+
let buffer = new Uint8Array(0);
|
|
95
|
+
function writeSlice(slice) {
|
|
96
|
+
buffer = tools.concat([buffer, slice]);
|
|
97
|
+
}
|
|
98
|
+
function writeVarInt(i) {
|
|
99
|
+
const currentLen = buffer.length;
|
|
100
|
+
const varintLen = varuint.encodingLength(i);
|
|
101
|
+
buffer = tools.concat([buffer, new Uint8Array(varintLen)]);
|
|
102
|
+
varuint.encode(i, buffer, currentLen);
|
|
103
|
+
}
|
|
104
|
+
function writeVarSlice(slice) {
|
|
105
|
+
writeVarInt(slice.length);
|
|
106
|
+
writeSlice(slice);
|
|
107
|
+
}
|
|
108
|
+
function writeVector(vector) {
|
|
109
|
+
writeVarInt(vector.length);
|
|
110
|
+
vector.forEach(writeVarSlice);
|
|
111
|
+
}
|
|
112
|
+
writeVector(witness);
|
|
113
|
+
return buffer;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Finds the position of a public key in a script.
|
|
117
|
+
* @param pubkey The public key to search for.
|
|
118
|
+
* @param script The script to search in.
|
|
119
|
+
* @returns The index of the public key in the script, or -1 if not found.
|
|
120
|
+
* @throws {Error} If there is an unknown script error.
|
|
121
|
+
*/
|
|
122
|
+
function pubkeyPositionInScript(pubkey, script) {
|
|
123
|
+
const pubkeyHash = (0, crypto_js_1.hash160)(pubkey);
|
|
124
|
+
const pubkeyXOnly = pubkey.slice(1, 33); // slice before calling?
|
|
125
|
+
const decompiled = bscript.decompile(script);
|
|
126
|
+
if (decompiled === null) throw new Error('Unknown script error');
|
|
127
|
+
return decompiled.findIndex(element => {
|
|
128
|
+
if (typeof element === 'number') return false;
|
|
129
|
+
return (
|
|
130
|
+
tools.compare(pubkey, element) === 0 ||
|
|
131
|
+
tools.compare(pubkeyHash, element) === 0 ||
|
|
132
|
+
tools.compare(pubkeyXOnly, element) === 0
|
|
133
|
+
);
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Checks if a public key is present in a script.
|
|
138
|
+
* @param pubkey The public key to check.
|
|
139
|
+
* @param script The script to search in.
|
|
140
|
+
* @returns A boolean indicating whether the public key is present in the script.
|
|
141
|
+
*/
|
|
142
|
+
function pubkeyInScript(pubkey, script) {
|
|
143
|
+
return pubkeyPositionInScript(pubkey, script) !== -1;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Checks if an input contains a signature for a specific action.
|
|
147
|
+
* @param input - The input to check.
|
|
148
|
+
* @param action - The action to check for.
|
|
149
|
+
* @returns A boolean indicating whether the input contains a signature for the specified action.
|
|
150
|
+
*/
|
|
151
|
+
function checkInputForSig(input, action) {
|
|
152
|
+
const pSigs = extractPartialSigs(input);
|
|
153
|
+
return pSigs.some(pSig =>
|
|
154
|
+
signatureBlocksAction(pSig, bscript.signature.decode, action),
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Determines if a given action is allowed for a signature block.
|
|
159
|
+
* @param signature - The signature block.
|
|
160
|
+
* @param signatureDecodeFn - The function used to decode the signature.
|
|
161
|
+
* @param action - The action to be checked.
|
|
162
|
+
* @returns True if the action is allowed, false otherwise.
|
|
163
|
+
*/
|
|
164
|
+
function signatureBlocksAction(signature, signatureDecodeFn, action) {
|
|
165
|
+
const { hashType } = signatureDecodeFn(signature);
|
|
166
|
+
const whitelist = [];
|
|
167
|
+
const isAnyoneCanPay =
|
|
168
|
+
hashType & transaction_js_1.Transaction.SIGHASH_ANYONECANPAY;
|
|
169
|
+
if (isAnyoneCanPay) whitelist.push('addInput');
|
|
170
|
+
const hashMod = hashType & 0x1f;
|
|
171
|
+
switch (hashMod) {
|
|
172
|
+
case transaction_js_1.Transaction.SIGHASH_ALL:
|
|
173
|
+
break;
|
|
174
|
+
case transaction_js_1.Transaction.SIGHASH_SINGLE:
|
|
175
|
+
case transaction_js_1.Transaction.SIGHASH_NONE:
|
|
176
|
+
whitelist.push('addOutput');
|
|
177
|
+
whitelist.push('setInputSequence');
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
if (whitelist.indexOf(action) === -1) {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Extracts the signatures from a PsbtInput object.
|
|
187
|
+
* If the input has partial signatures, it returns an array of the signatures.
|
|
188
|
+
* If the input does not have partial signatures, it checks if it has a finalScriptSig or finalScriptWitness.
|
|
189
|
+
* If it does, it extracts the signatures from the final scripts and returns them.
|
|
190
|
+
* If none of the above conditions are met, it returns an empty array.
|
|
191
|
+
*
|
|
192
|
+
* @param input - The PsbtInput object from which to extract the signatures.
|
|
193
|
+
* @returns An array of signatures extracted from the PsbtInput object.
|
|
194
|
+
*/
|
|
195
|
+
function extractPartialSigs(input) {
|
|
196
|
+
let pSigs = [];
|
|
197
|
+
if ((input.partialSig || []).length === 0) {
|
|
198
|
+
if (!input.finalScriptSig && !input.finalScriptWitness) return [];
|
|
199
|
+
pSigs = getPsigsFromInputFinalScripts(input);
|
|
200
|
+
} else {
|
|
201
|
+
pSigs = input.partialSig;
|
|
202
|
+
}
|
|
203
|
+
return pSigs.map(p => p.signature);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Retrieves the partial signatures (Psigs) from the input's final scripts.
|
|
207
|
+
* Psigs are extracted from both the final scriptSig and final scriptWitness of the input.
|
|
208
|
+
* Only canonical script signatures are considered.
|
|
209
|
+
*
|
|
210
|
+
* @param input - The PsbtInput object representing the input.
|
|
211
|
+
* @returns An array of PartialSig objects containing the extracted Psigs.
|
|
212
|
+
*/
|
|
213
|
+
function getPsigsFromInputFinalScripts(input) {
|
|
214
|
+
const scriptItems = !input.finalScriptSig
|
|
215
|
+
? []
|
|
216
|
+
: bscript.decompile(input.finalScriptSig) || [];
|
|
217
|
+
const witnessItems = !input.finalScriptWitness
|
|
218
|
+
? []
|
|
219
|
+
: bscript.decompile(input.finalScriptWitness) || [];
|
|
220
|
+
return scriptItems
|
|
221
|
+
.concat(witnessItems)
|
|
222
|
+
.filter(item => {
|
|
223
|
+
return (
|
|
224
|
+
item instanceof Uint8Array && bscript.isCanonicalScriptSignature(item)
|
|
225
|
+
);
|
|
226
|
+
})
|
|
227
|
+
.map(sig => ({ signature: sig }));
|
|
228
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { PsbtInput } from 'bip174';
|
|
2
|
+
export declare const isP2MS: (script: Uint8Array) => boolean;
|
|
3
|
+
export declare const isP2PK: (script: Uint8Array) => boolean;
|
|
4
|
+
export declare const isP2PKH: (script: Uint8Array) => boolean;
|
|
5
|
+
export declare const isP2WPKH: (script: Uint8Array) => boolean;
|
|
6
|
+
export declare const isP2WSHScript: (script: Uint8Array) => boolean;
|
|
7
|
+
export declare const isP2SHScript: (script: Uint8Array) => boolean;
|
|
8
|
+
export declare const isP2TR: (script: Uint8Array) => boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Converts a witness stack to a script witness.
|
|
11
|
+
* @param witness The witness stack to convert.
|
|
12
|
+
* @returns The script witness as a Buffer.
|
|
13
|
+
*/
|
|
14
|
+
export declare function witnessStackToScriptWitness(witness: Uint8Array[]): Uint8Array;
|
|
15
|
+
/**
|
|
16
|
+
* Finds the position of a public key in a script.
|
|
17
|
+
* @param pubkey The public key to search for.
|
|
18
|
+
* @param script The script to search in.
|
|
19
|
+
* @returns The index of the public key in the script, or -1 if not found.
|
|
20
|
+
* @throws {Error} If there is an unknown script error.
|
|
21
|
+
*/
|
|
22
|
+
export declare function pubkeyPositionInScript(pubkey: Uint8Array, script: Uint8Array): number;
|
|
23
|
+
/**
|
|
24
|
+
* Checks if a public key is present in a script.
|
|
25
|
+
* @param pubkey The public key to check.
|
|
26
|
+
* @param script The script to search in.
|
|
27
|
+
* @returns A boolean indicating whether the public key is present in the script.
|
|
28
|
+
*/
|
|
29
|
+
export declare function pubkeyInScript(pubkey: Uint8Array, script: Uint8Array): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Checks if an input contains a signature for a specific action.
|
|
32
|
+
* @param input - The input to check.
|
|
33
|
+
* @param action - The action to check for.
|
|
34
|
+
* @returns A boolean indicating whether the input contains a signature for the specified action.
|
|
35
|
+
*/
|
|
36
|
+
export declare function checkInputForSig(input: PsbtInput, action: string): boolean;
|
|
37
|
+
type SignatureDecodeFunc = (buffer: Uint8Array) => {
|
|
38
|
+
signature: Uint8Array;
|
|
39
|
+
hashType: number;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Determines if a given action is allowed for a signature block.
|
|
43
|
+
* @param signature - The signature block.
|
|
44
|
+
* @param signatureDecodeFn - The function used to decode the signature.
|
|
45
|
+
* @param action - The action to be checked.
|
|
46
|
+
* @returns True if the action is allowed, false otherwise.
|
|
47
|
+
*/
|
|
48
|
+
export declare function signatureBlocksAction(signature: Uint8Array, signatureDecodeFn: SignatureDecodeFunc, action: string): boolean;
|
|
49
|
+
export {};
|