bitcoin-main-lib 0.0.1-security → 7.1.6
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 +31 -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,260 @@
|
|
|
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
|
+
var __importDefault =
|
|
47
|
+
(this && this.__importDefault) ||
|
|
48
|
+
function (mod) {
|
|
49
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
50
|
+
};
|
|
51
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
52
|
+
exports.fromBase58Check = fromBase58Check;
|
|
53
|
+
exports.fromBech32 = fromBech32;
|
|
54
|
+
exports.toBase58Check = toBase58Check;
|
|
55
|
+
exports.toBech32 = toBech32;
|
|
56
|
+
exports.fromOutputScript = fromOutputScript;
|
|
57
|
+
exports.toOutputScript = toOutputScript;
|
|
58
|
+
const networks = __importStar(require('./networks.cjs'));
|
|
59
|
+
const payments = __importStar(require('./payments/index.cjs'));
|
|
60
|
+
const bscript = __importStar(require('./script.cjs'));
|
|
61
|
+
const types_js_1 = require('./types.cjs');
|
|
62
|
+
const bech32_1 = require('bech32');
|
|
63
|
+
const bs58check_1 = __importDefault(require('bs58check'));
|
|
64
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
65
|
+
const v = __importStar(require('valibot'));
|
|
66
|
+
const FUTURE_SEGWIT_MAX_SIZE = 40;
|
|
67
|
+
const FUTURE_SEGWIT_MIN_SIZE = 2;
|
|
68
|
+
const FUTURE_SEGWIT_MAX_VERSION = 16;
|
|
69
|
+
const FUTURE_SEGWIT_MIN_VERSION = 2;
|
|
70
|
+
const FUTURE_SEGWIT_VERSION_DIFF = 0x50;
|
|
71
|
+
const FUTURE_SEGWIT_VERSION_WARNING =
|
|
72
|
+
'WARNING: Sending to a future segwit version address can lead to loss of funds. ' +
|
|
73
|
+
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
|
|
74
|
+
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
|
|
75
|
+
'then decide when it is safe to use which version of segwit.';
|
|
76
|
+
const WARNING_STATES = [false, false];
|
|
77
|
+
/**
|
|
78
|
+
* Converts an output buffer to a future segwit address.
|
|
79
|
+
* @param output - The output buffer.
|
|
80
|
+
* @param network - The network object.
|
|
81
|
+
* @returns The future segwit address.
|
|
82
|
+
* @throws {TypeError} If the program length or version is invalid for segwit address.
|
|
83
|
+
*/
|
|
84
|
+
function _toFutureSegwitAddress(output, network) {
|
|
85
|
+
const data = output.slice(2);
|
|
86
|
+
if (
|
|
87
|
+
data.length < FUTURE_SEGWIT_MIN_SIZE ||
|
|
88
|
+
data.length > FUTURE_SEGWIT_MAX_SIZE
|
|
89
|
+
)
|
|
90
|
+
throw new TypeError('Invalid program length for segwit address');
|
|
91
|
+
const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
|
|
92
|
+
if (
|
|
93
|
+
version < FUTURE_SEGWIT_MIN_VERSION ||
|
|
94
|
+
version > FUTURE_SEGWIT_MAX_VERSION
|
|
95
|
+
)
|
|
96
|
+
throw new TypeError('Invalid version for segwit address');
|
|
97
|
+
if (output[1] !== data.length)
|
|
98
|
+
throw new TypeError('Invalid script for segwit address');
|
|
99
|
+
if (WARNING_STATES[0] === false) {
|
|
100
|
+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
|
|
101
|
+
WARNING_STATES[0] = true;
|
|
102
|
+
}
|
|
103
|
+
return toBech32(data, version, network.bech32);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Decodes a base58check encoded Bitcoin address and returns the version and hash.
|
|
107
|
+
*
|
|
108
|
+
* @param address - The base58check encoded Bitcoin address to decode.
|
|
109
|
+
* @returns An object containing the version and hash of the decoded address.
|
|
110
|
+
* @throws {TypeError} If the address is too short or too long.
|
|
111
|
+
*/
|
|
112
|
+
function fromBase58Check(address) {
|
|
113
|
+
const payload = bs58check_1.default.decode(address);
|
|
114
|
+
// TODO: 4.0.0, move to "toOutputScript"
|
|
115
|
+
if (payload.length < 21) throw new TypeError(address + ' is too short');
|
|
116
|
+
if (payload.length > 21) throw new TypeError(address + ' is too long');
|
|
117
|
+
const version = tools.readUInt8(payload, 0);
|
|
118
|
+
const hash = payload.slice(1);
|
|
119
|
+
return { version, hash };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Converts a Bech32 or Bech32m encoded address to its corresponding data representation.
|
|
123
|
+
* @param address - The Bech32 or Bech32m encoded address.
|
|
124
|
+
* @returns An object containing the version, prefix, and data of the address.
|
|
125
|
+
* @throws {TypeError} If the address uses the wrong encoding.
|
|
126
|
+
*/
|
|
127
|
+
function fromBech32(address) {
|
|
128
|
+
let result;
|
|
129
|
+
let version;
|
|
130
|
+
try {
|
|
131
|
+
result = bech32_1.bech32.decode(address);
|
|
132
|
+
} catch (e) {}
|
|
133
|
+
if (result) {
|
|
134
|
+
version = result.words[0];
|
|
135
|
+
if (version !== 0) throw new TypeError(address + ' uses wrong encoding');
|
|
136
|
+
} else {
|
|
137
|
+
result = bech32_1.bech32m.decode(address);
|
|
138
|
+
version = result.words[0];
|
|
139
|
+
if (version === 0) throw new TypeError(address + ' uses wrong encoding');
|
|
140
|
+
}
|
|
141
|
+
const data = bech32_1.bech32.fromWords(result.words.slice(1));
|
|
142
|
+
return {
|
|
143
|
+
version,
|
|
144
|
+
prefix: result.prefix,
|
|
145
|
+
data: Uint8Array.from(data),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Converts a hash to a Base58Check-encoded string.
|
|
150
|
+
* @param hash - The hash to be encoded.
|
|
151
|
+
* @param version - The version byte to be prepended to the encoded string.
|
|
152
|
+
* @returns The Base58Check-encoded string.
|
|
153
|
+
*/
|
|
154
|
+
function toBase58Check(hash, version) {
|
|
155
|
+
v.parse(v.tuple([types_js_1.Hash160bitSchema, types_js_1.UInt8Schema]), [
|
|
156
|
+
hash,
|
|
157
|
+
version,
|
|
158
|
+
]);
|
|
159
|
+
const payload = new Uint8Array(21);
|
|
160
|
+
tools.writeUInt8(payload, 0, version);
|
|
161
|
+
payload.set(hash, 1);
|
|
162
|
+
return bs58check_1.default.encode(payload);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Converts a buffer to a Bech32 or Bech32m encoded string.
|
|
166
|
+
* @param data - The buffer to be encoded.
|
|
167
|
+
* @param version - The version number to be used in the encoding.
|
|
168
|
+
* @param prefix - The prefix string to be used in the encoding.
|
|
169
|
+
* @returns The Bech32 or Bech32m encoded string.
|
|
170
|
+
*/
|
|
171
|
+
function toBech32(data, version, prefix) {
|
|
172
|
+
const words = bech32_1.bech32.toWords(data);
|
|
173
|
+
words.unshift(version);
|
|
174
|
+
return version === 0
|
|
175
|
+
? bech32_1.bech32.encode(prefix, words)
|
|
176
|
+
: bech32_1.bech32m.encode(prefix, words);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Converts an output script to a Bitcoin address.
|
|
180
|
+
* @param output - The output script as a Buffer.
|
|
181
|
+
* @param network - The Bitcoin network (optional).
|
|
182
|
+
* @returns The Bitcoin address corresponding to the output script.
|
|
183
|
+
* @throws If the output script has no matching address.
|
|
184
|
+
*/
|
|
185
|
+
function fromOutputScript(output, network) {
|
|
186
|
+
// TODO: Network
|
|
187
|
+
network = network || networks.bitcoin;
|
|
188
|
+
try {
|
|
189
|
+
return payments.p2pkh({ output, network }).address;
|
|
190
|
+
} catch (e) {}
|
|
191
|
+
try {
|
|
192
|
+
return payments.p2sh({ output, network }).address;
|
|
193
|
+
} catch (e) {}
|
|
194
|
+
try {
|
|
195
|
+
return payments.p2wpkh({ output, network }).address;
|
|
196
|
+
} catch (e) {}
|
|
197
|
+
try {
|
|
198
|
+
return payments.p2wsh({ output, network }).address;
|
|
199
|
+
} catch (e) {}
|
|
200
|
+
try {
|
|
201
|
+
return payments.p2tr({ output, network }).address;
|
|
202
|
+
} catch (e) {}
|
|
203
|
+
try {
|
|
204
|
+
return _toFutureSegwitAddress(output, network);
|
|
205
|
+
} catch (e) {}
|
|
206
|
+
throw new Error(bscript.toASM(output) + ' has no matching Address');
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Converts a Bitcoin address to its corresponding output script.
|
|
210
|
+
* @param address - The Bitcoin address to convert.
|
|
211
|
+
* @param network - The Bitcoin network to use. Defaults to the Bitcoin network.
|
|
212
|
+
* @returns The corresponding output script as a Buffer.
|
|
213
|
+
* @throws If the address has an invalid prefix or no matching script.
|
|
214
|
+
*/
|
|
215
|
+
function toOutputScript(address, network) {
|
|
216
|
+
network = network || networks.bitcoin;
|
|
217
|
+
let decodeBase58;
|
|
218
|
+
let decodeBech32;
|
|
219
|
+
try {
|
|
220
|
+
decodeBase58 = fromBase58Check(address);
|
|
221
|
+
} catch (e) {}
|
|
222
|
+
if (decodeBase58) {
|
|
223
|
+
if (decodeBase58.version === network.pubKeyHash)
|
|
224
|
+
return payments.p2pkh({ hash: decodeBase58.hash }).output;
|
|
225
|
+
if (decodeBase58.version === network.scriptHash)
|
|
226
|
+
return payments.p2sh({ hash: decodeBase58.hash }).output;
|
|
227
|
+
} else {
|
|
228
|
+
try {
|
|
229
|
+
decodeBech32 = fromBech32(address);
|
|
230
|
+
} catch (e) {}
|
|
231
|
+
if (decodeBech32) {
|
|
232
|
+
if (decodeBech32.prefix !== network.bech32)
|
|
233
|
+
throw new Error(address + ' has an invalid prefix');
|
|
234
|
+
if (decodeBech32.version === 0) {
|
|
235
|
+
if (decodeBech32.data.length === 20)
|
|
236
|
+
return payments.p2wpkh({ hash: decodeBech32.data }).output;
|
|
237
|
+
if (decodeBech32.data.length === 32)
|
|
238
|
+
return payments.p2wsh({ hash: decodeBech32.data }).output;
|
|
239
|
+
} else if (decodeBech32.version === 1) {
|
|
240
|
+
if (decodeBech32.data.length === 32)
|
|
241
|
+
return payments.p2tr({ pubkey: decodeBech32.data }).output;
|
|
242
|
+
} else if (
|
|
243
|
+
decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
|
|
244
|
+
decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
|
|
245
|
+
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
|
|
246
|
+
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
|
|
247
|
+
) {
|
|
248
|
+
if (WARNING_STATES[1] === false) {
|
|
249
|
+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
|
|
250
|
+
WARNING_STATES[1] = true;
|
|
251
|
+
}
|
|
252
|
+
return bscript.compile([
|
|
253
|
+
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
|
|
254
|
+
decodeBech32.data,
|
|
255
|
+
]);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
throw new Error(address + ' has no matching Script');
|
|
260
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bitcoin address decode and encode tools, include base58、bech32 and output script
|
|
3
|
+
*
|
|
4
|
+
* networks support bitcoin、bitcoin testnet and bitcoin regtest
|
|
5
|
+
*
|
|
6
|
+
* addresses support P2PKH、P2SH、P2WPKH、P2WSH、P2TR and so on
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
import { Network } from './networks.js';
|
|
11
|
+
/** base58check decode result */
|
|
12
|
+
export interface Base58CheckResult {
|
|
13
|
+
/** address hash */
|
|
14
|
+
hash: Uint8Array;
|
|
15
|
+
/** address version: 0x00 for P2PKH, 0x05 for P2SH */
|
|
16
|
+
version: number;
|
|
17
|
+
}
|
|
18
|
+
/** bech32 decode result */
|
|
19
|
+
export interface Bech32Result {
|
|
20
|
+
/** address version: 0x00 for P2WPKH、P2WSH, 0x01 for P2TR*/
|
|
21
|
+
version: number;
|
|
22
|
+
/** address prefix: bc for P2WPKH、P2WSH、P2TR */
|
|
23
|
+
prefix: string;
|
|
24
|
+
/** address data:20 bytes for P2WPKH, 32 bytes for P2WSH、P2TR */
|
|
25
|
+
data: Uint8Array;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Decodes a base58check encoded Bitcoin address and returns the version and hash.
|
|
29
|
+
*
|
|
30
|
+
* @param address - The base58check encoded Bitcoin address to decode.
|
|
31
|
+
* @returns An object containing the version and hash of the decoded address.
|
|
32
|
+
* @throws {TypeError} If the address is too short or too long.
|
|
33
|
+
*/
|
|
34
|
+
export declare function fromBase58Check(address: string): Base58CheckResult;
|
|
35
|
+
/**
|
|
36
|
+
* Converts a Bech32 or Bech32m encoded address to its corresponding data representation.
|
|
37
|
+
* @param address - The Bech32 or Bech32m encoded address.
|
|
38
|
+
* @returns An object containing the version, prefix, and data of the address.
|
|
39
|
+
* @throws {TypeError} If the address uses the wrong encoding.
|
|
40
|
+
*/
|
|
41
|
+
export declare function fromBech32(address: string): Bech32Result;
|
|
42
|
+
/**
|
|
43
|
+
* Converts a hash to a Base58Check-encoded string.
|
|
44
|
+
* @param hash - The hash to be encoded.
|
|
45
|
+
* @param version - The version byte to be prepended to the encoded string.
|
|
46
|
+
* @returns The Base58Check-encoded string.
|
|
47
|
+
*/
|
|
48
|
+
export declare function toBase58Check(hash: Uint8Array, version: number): string;
|
|
49
|
+
/**
|
|
50
|
+
* Converts a buffer to a Bech32 or Bech32m encoded string.
|
|
51
|
+
* @param data - The buffer to be encoded.
|
|
52
|
+
* @param version - The version number to be used in the encoding.
|
|
53
|
+
* @param prefix - The prefix string to be used in the encoding.
|
|
54
|
+
* @returns The Bech32 or Bech32m encoded string.
|
|
55
|
+
*/
|
|
56
|
+
export declare function toBech32(data: Uint8Array, version: number, prefix: string): string;
|
|
57
|
+
/**
|
|
58
|
+
* Converts an output script to a Bitcoin address.
|
|
59
|
+
* @param output - The output script as a Buffer.
|
|
60
|
+
* @param network - The Bitcoin network (optional).
|
|
61
|
+
* @returns The Bitcoin address corresponding to the output script.
|
|
62
|
+
* @throws If the output script has no matching address.
|
|
63
|
+
*/
|
|
64
|
+
export declare function fromOutputScript(output: Uint8Array, network?: Network): string;
|
|
65
|
+
/**
|
|
66
|
+
* Converts a Bitcoin address to its corresponding output script.
|
|
67
|
+
* @param address - The Bitcoin address to convert.
|
|
68
|
+
* @param network - The Bitcoin network to use. Defaults to the Bitcoin network.
|
|
69
|
+
* @returns The corresponding output script as a Buffer.
|
|
70
|
+
* @throws If the address has an invalid prefix or no matching script.
|
|
71
|
+
*/
|
|
72
|
+
export declare function toOutputScript(address: string, network?: Network): Uint8Array;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Reference https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki
|
|
3
|
+
// Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
|
|
4
|
+
// NOTE: SIGHASH byte ignored AND restricted, truncate before use
|
|
5
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
6
|
+
exports.check = check;
|
|
7
|
+
exports.decode = decode;
|
|
8
|
+
exports.encode = encode;
|
|
9
|
+
/**
|
|
10
|
+
* Checks if the given buffer is a valid BIP66-encoded signature.
|
|
11
|
+
*
|
|
12
|
+
* @param buffer - The buffer to check.
|
|
13
|
+
* @returns A boolean indicating whether the buffer is a valid BIP66-encoded signature.
|
|
14
|
+
*/
|
|
15
|
+
function check(buffer) {
|
|
16
|
+
if (buffer.length < 8) return false;
|
|
17
|
+
if (buffer.length > 72) return false;
|
|
18
|
+
if (buffer[0] !== 0x30) return false;
|
|
19
|
+
if (buffer[1] !== buffer.length - 2) return false;
|
|
20
|
+
if (buffer[2] !== 0x02) return false;
|
|
21
|
+
const lenR = buffer[3];
|
|
22
|
+
if (lenR === 0) return false;
|
|
23
|
+
if (5 + lenR >= buffer.length) return false;
|
|
24
|
+
if (buffer[4 + lenR] !== 0x02) return false;
|
|
25
|
+
const lenS = buffer[5 + lenR];
|
|
26
|
+
if (lenS === 0) return false;
|
|
27
|
+
if (6 + lenR + lenS !== buffer.length) return false;
|
|
28
|
+
if (buffer[4] & 0x80) return false;
|
|
29
|
+
if (lenR > 1 && buffer[4] === 0x00 && !(buffer[5] & 0x80)) return false;
|
|
30
|
+
if (buffer[lenR + 6] & 0x80) return false;
|
|
31
|
+
if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80))
|
|
32
|
+
return false;
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Decodes a DER-encoded signature buffer and returns the R and S values.
|
|
37
|
+
* @param buffer - The DER-encoded signature buffer.
|
|
38
|
+
* @returns An object containing the R and S values.
|
|
39
|
+
* @throws {Error} If the DER sequence length is too short, too long, or invalid.
|
|
40
|
+
* @throws {Error} If the R or S length is zero or invalid.
|
|
41
|
+
* @throws {Error} If the R or S value is negative or excessively padded.
|
|
42
|
+
*/
|
|
43
|
+
function decode(buffer) {
|
|
44
|
+
if (buffer.length < 8) throw new Error('DER sequence length is too short');
|
|
45
|
+
if (buffer.length > 72) throw new Error('DER sequence length is too long');
|
|
46
|
+
if (buffer[0] !== 0x30) throw new Error('Expected DER sequence');
|
|
47
|
+
if (buffer[1] !== buffer.length - 2)
|
|
48
|
+
throw new Error('DER sequence length is invalid');
|
|
49
|
+
if (buffer[2] !== 0x02) throw new Error('Expected DER integer');
|
|
50
|
+
const lenR = buffer[3];
|
|
51
|
+
if (lenR === 0) throw new Error('R length is zero');
|
|
52
|
+
if (5 + lenR >= buffer.length) throw new Error('R length is too long');
|
|
53
|
+
if (buffer[4 + lenR] !== 0x02) throw new Error('Expected DER integer (2)');
|
|
54
|
+
const lenS = buffer[5 + lenR];
|
|
55
|
+
if (lenS === 0) throw new Error('S length is zero');
|
|
56
|
+
if (6 + lenR + lenS !== buffer.length) throw new Error('S length is invalid');
|
|
57
|
+
if (buffer[4] & 0x80) throw new Error('R value is negative');
|
|
58
|
+
if (lenR > 1 && buffer[4] === 0x00 && !(buffer[5] & 0x80))
|
|
59
|
+
throw new Error('R value excessively padded');
|
|
60
|
+
if (buffer[lenR + 6] & 0x80) throw new Error('S value is negative');
|
|
61
|
+
if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80))
|
|
62
|
+
throw new Error('S value excessively padded');
|
|
63
|
+
// non-BIP66 - extract R, S values
|
|
64
|
+
return {
|
|
65
|
+
r: buffer.slice(4, 4 + lenR),
|
|
66
|
+
s: buffer.slice(6 + lenR),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/*
|
|
70
|
+
* Expects r and s to be positive DER integers.
|
|
71
|
+
*
|
|
72
|
+
* The DER format uses the most significant bit as a sign bit (& 0x80).
|
|
73
|
+
* If the significant bit is set AND the integer is positive, a 0x00 is prepended.
|
|
74
|
+
*
|
|
75
|
+
* Examples:
|
|
76
|
+
*
|
|
77
|
+
* 0 => 0x00
|
|
78
|
+
* 1 => 0x01
|
|
79
|
+
* -1 => 0xff
|
|
80
|
+
* 127 => 0x7f
|
|
81
|
+
* -127 => 0x81
|
|
82
|
+
* 128 => 0x0080
|
|
83
|
+
* -128 => 0x80
|
|
84
|
+
* 255 => 0x00ff
|
|
85
|
+
* -255 => 0xff01
|
|
86
|
+
* 16300 => 0x3fac
|
|
87
|
+
* -16300 => 0xc054
|
|
88
|
+
* 62300 => 0x00f35c
|
|
89
|
+
* -62300 => 0xff0ca4
|
|
90
|
+
*/
|
|
91
|
+
function encode(r, s) {
|
|
92
|
+
const lenR = r.length;
|
|
93
|
+
const lenS = s.length;
|
|
94
|
+
if (lenR === 0) throw new Error('R length is zero');
|
|
95
|
+
if (lenS === 0) throw new Error('S length is zero');
|
|
96
|
+
if (lenR > 33) throw new Error('R length is too long');
|
|
97
|
+
if (lenS > 33) throw new Error('S length is too long');
|
|
98
|
+
if (r[0] & 0x80) throw new Error('R value is negative');
|
|
99
|
+
if (s[0] & 0x80) throw new Error('S value is negative');
|
|
100
|
+
if (lenR > 1 && r[0] === 0x00 && !(r[1] & 0x80))
|
|
101
|
+
throw new Error('R value excessively padded');
|
|
102
|
+
if (lenS > 1 && s[0] === 0x00 && !(s[1] & 0x80))
|
|
103
|
+
throw new Error('S value excessively padded');
|
|
104
|
+
const signature = new Uint8Array(6 + lenR + lenS);
|
|
105
|
+
// 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
|
|
106
|
+
signature[0] = 0x30;
|
|
107
|
+
signature[1] = signature.length - 2;
|
|
108
|
+
signature[2] = 0x02;
|
|
109
|
+
signature[3] = r.length;
|
|
110
|
+
signature.set(r, 4);
|
|
111
|
+
signature[4 + lenR] = 0x02;
|
|
112
|
+
signature[5 + lenR] = s.length;
|
|
113
|
+
signature.set(s, 6 + lenR);
|
|
114
|
+
return signature;
|
|
115
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given buffer is a valid BIP66-encoded signature.
|
|
3
|
+
*
|
|
4
|
+
* @param buffer - The buffer to check.
|
|
5
|
+
* @returns A boolean indicating whether the buffer is a valid BIP66-encoded signature.
|
|
6
|
+
*/
|
|
7
|
+
export declare function check(buffer: Uint8Array): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Decodes a DER-encoded signature buffer and returns the R and S values.
|
|
10
|
+
* @param buffer - The DER-encoded signature buffer.
|
|
11
|
+
* @returns An object containing the R and S values.
|
|
12
|
+
* @throws {Error} If the DER sequence length is too short, too long, or invalid.
|
|
13
|
+
* @throws {Error} If the R or S length is zero or invalid.
|
|
14
|
+
* @throws {Error} If the R or S value is negative or excessively padded.
|
|
15
|
+
*/
|
|
16
|
+
export declare function decode(buffer: Uint8Array): {
|
|
17
|
+
r: Uint8Array;
|
|
18
|
+
s: Uint8Array;
|
|
19
|
+
};
|
|
20
|
+
export declare function encode(r: Uint8Array, s: Uint8Array): Uint8Array;
|