@scure/btc-signer 2.0.1 → 2.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/utils.d.ts CHANGED
@@ -1,41 +1,312 @@
1
- import { schnorr } from '@noble/curves/secp256k1.js';
2
- import { sha256 } from '@noble/hashes/sha2.js';
1
+ import { sha256 as nobleSha256 } from '@noble/hashes/sha2.js';
2
+ import { type TArg, type TRet } from '@noble/hashes/utils.js';
3
+ export { abytes, validateObject as vld } from '@noble/curves/utils.js';
4
+ export { type TArg, type TRet } from '@noble/hashes/utils.js';
5
+ /** Hex-like input accepted by helpers in this module. */
3
6
  export type Hex = string | Uint8Array;
7
+ /** Byte array alias used across the library. */
4
8
  export type Bytes = Uint8Array;
9
+ /**
10
+ * Validates that a value is a non-negative bigint.
11
+ * @param n - Value to validate.
12
+ * @param title - Label included in thrown errors.
13
+ * @returns The same bigint.
14
+ * @throws On wrong argument types. {@link TypeError}
15
+ * @example
16
+ * Validate a satoshi amount before transaction encoding.
17
+ * ```ts
18
+ * abigint(1n, 'amount');
19
+ * ```
20
+ */
21
+ export declare function abigint(n: unknown, title?: string): bigint;
22
+ export declare function aarray<T>(item: unknown, title: string, inner?: (elm: T, title: string) => void): T[];
23
+ /**
24
+ * Asserts something is a string.
25
+ * @param value - Value to validate.
26
+ * @param title - Label included in thrown errors.
27
+ * @returns The validated string.
28
+ * @throws On wrong argument types. {@link TypeError}
29
+ * @example
30
+ * Validate a label string.
31
+ *
32
+ * ```ts
33
+ * astring('example', 'label');
34
+ * ```
35
+ */
36
+ export declare function astring(value: unknown, title?: string): string;
37
+ export declare function validateObject(object: Record<string, any>, fields?: Record<string, string>, optFields?: Record<string, string>, _title?: string): void;
38
+ /**
39
+ * Checks whether a curve y-coordinate is even.
40
+ * @param y - y-coordinate to inspect
41
+ * @returns `true` when the coordinate is even.
42
+ * @example
43
+ * Check whether a point coordinate has even parity.
44
+ * ```ts
45
+ * hasEven(2n);
46
+ * ```
47
+ */
5
48
  export declare const hasEven: (y: bigint) => boolean;
6
- declare const isBytes: (a: unknown) => a is Uint8Array;
7
- declare const concatBytes: (...arrays: Uint8Array[]) => Uint8Array;
8
- declare const equalBytes: (a: Uint8Array, b: Uint8Array) => boolean;
9
- export { concatBytes, equalBytes, isBytes, sha256 };
10
- export declare const hash160: (msg: Uint8Array) => Uint8Array;
11
- export declare const sha256x2: (...msgs: Uint8Array[]) => Uint8Array;
12
- export declare const randomPrivateKeyBytes: () => Uint8Array;
13
- export declare const pubSchnorr: (priv: string | Uint8Array) => Uint8Array;
14
- export declare const pubECDSA: (privateKey: Uint8Array, isCompressed?: boolean) => Uint8Array;
15
- export declare function signECDSA(hash: Bytes, privateKey: Bytes, lowR?: boolean): Bytes;
16
- export declare const signSchnorr: typeof schnorr.sign;
17
- export declare const tagSchnorr: typeof schnorr.utils.taggedHash;
18
- export declare const PubT: {
19
- ecdsa: number;
20
- schnorr: number;
21
- };
49
+ /**
50
+ * Checks whether a value is a Uint8Array.
51
+ * @param a - value to inspect
52
+ * @returns `true` when the value is a Uint8Array.
53
+ * @example
54
+ * Check whether an unknown value is already bytes.
55
+ * ```ts
56
+ * isBytes(new Uint8Array([1]));
57
+ * ```
58
+ */
59
+ export declare const isBytes: (a: unknown) => a is Uint8Array;
60
+ /**
61
+ * Concatenates byte arrays into a single Uint8Array.
62
+ * @param arrays - byte arrays to concatenate
63
+ * @returns Concatenated byte array.
64
+ * @example
65
+ * Join several byte chunks before hashing or signing them.
66
+ * ```ts
67
+ * concatBytes(new Uint8Array([1]), new Uint8Array([2]));
68
+ * ```
69
+ */
70
+ export declare const concatBytes: (...arrays: TArg<Uint8Array[]>) => TRet<Uint8Array>;
71
+ /**
72
+ * Compares two byte arrays for equality.
73
+ * @param a - first byte array
74
+ * @param b - second byte array
75
+ * @returns `true` when both arrays contain the same bytes.
76
+ * @example
77
+ * Compare two serialized values without converting them first.
78
+ * ```ts
79
+ * equalBytes(new Uint8Array([1]), new Uint8Array([1]));
80
+ * ```
81
+ */
82
+ export declare const equalBytes: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => boolean;
83
+ /**
84
+ * SHA-256 hash function.
85
+ * @param msg - bytes to hash
86
+ * @returns SHA-256 digest.
87
+ * @example
88
+ * Hash a byte array with SHA-256.
89
+ * ```ts
90
+ * sha256(new Uint8Array([1, 2, 3]));
91
+ * ```
92
+ */
93
+ export declare const sha256: typeof nobleSha256;
94
+ /**
95
+ * HASH160 helper used by classic Bitcoin addresses.
96
+ * @param msg - bytes to hash
97
+ * @returns RIPEMD160(SHA256(msg)).
98
+ * @example
99
+ * Derive the HASH160 used by legacy address formats.
100
+ * ```ts
101
+ * hash160(new Uint8Array([1, 2, 3]));
102
+ * ```
103
+ */
104
+ export declare const hash160: (msg: TArg<Uint8Array>) => TRet<Uint8Array>;
105
+ /**
106
+ * Double-SHA256 helper used by Bitcoin transaction ids.
107
+ * @param msgs - message parts to concatenate and hash
108
+ * @returns SHA256(SHA256(concat(msgs))).
109
+ * @example
110
+ * Compute the double-SHA256 used by txids and sighashes.
111
+ * ```ts
112
+ * sha256x2(new Uint8Array([1]), new Uint8Array([2]));
113
+ * ```
114
+ */
115
+ export declare const sha256x2: (...msgs: TArg<Uint8Array[]>) => TRet<Uint8Array>;
116
+ /**
117
+ * Generates a random secp256k1 private key.
118
+ * @returns Random 32-byte private key.
119
+ * @example
120
+ * Generate a fresh secp256k1 private key for signing.
121
+ * ```ts
122
+ * const privKey = randomPrivateKeyBytes();
123
+ * ```
124
+ */
125
+ export declare const randomPrivateKeyBytes: () => TRet<Uint8Array>;
126
+ /**
127
+ * Derives a BIP340 Schnorr public key from a private key.
128
+ * @param priv - private key bytes
129
+ * @returns X-only public key bytes.
130
+ * @example
131
+ * Derive the x-only public key used by Schnorr and Taproot.
132
+ * ```ts
133
+ * import { pubSchnorr, randomPrivateKeyBytes } from '@scure/btc-signer/utils.js';
134
+ * pubSchnorr(randomPrivateKeyBytes());
135
+ * ```
136
+ */
137
+ export declare const pubSchnorr: (priv: TArg<Uint8Array>) => TRet<Uint8Array>;
138
+ /**
139
+ * Derives a secp256k1 ECDSA public key from a private key.
140
+ * @param privateKey - private key bytes
141
+ * @param isCompressed - whether to return the compressed form
142
+ * @returns Serialized public key bytes.
143
+ * @example
144
+ * Derive the normal secp256k1 public key for legacy or SegWit scripts.
145
+ * ```ts
146
+ * import { pubECDSA, randomPrivateKeyBytes } from '@scure/btc-signer/utils.js';
147
+ * pubECDSA(randomPrivateKeyBytes());
148
+ * ```
149
+ */
150
+ export declare const pubECDSA: (privateKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
151
+ /**
152
+ * Signs a 32-byte hash with ECDSA and returns DER encoding.
153
+ * @param hash - message hash to sign
154
+ * @param privateKey - signer private key
155
+ * @param lowR - whether to grind for low-R signatures
156
+ * @returns DER-encoded signature bytes.
157
+ * @throws If low-R grinding overflows or ECDSA signing fails validation. {@link Error}
158
+ * @example
159
+ * Hash a message first, then create the DER-encoded ECDSA signature.
160
+ * ```ts
161
+ * import { randomPrivateKeyBytes, sha256, signECDSA } from '@scure/btc-signer/utils.js';
162
+ * signECDSA(sha256(new Uint8Array([1, 2, 3])), randomPrivateKeyBytes());
163
+ * ```
164
+ */
165
+ export declare function signECDSA(hash: TArg<Bytes>, privateKey: TArg<Bytes>, lowR?: boolean): TRet<Bytes>;
166
+ /**
167
+ * BIP340 Schnorr signing function.
168
+ * @param message - 32-byte message digest
169
+ * @param secretKey - signer private key
170
+ * @param auxRand - optional auxiliary randomness
171
+ * @returns Schnorr signature bytes.
172
+ * @example
173
+ * Sign a 32-byte digest with the built-in BIP340 helper.
174
+ * ```ts
175
+ * import { randomPrivateKeyBytes, sha256, signSchnorr } from '@scure/btc-signer/utils.js';
176
+ * const msg = sha256(new Uint8Array([1, 2, 3]));
177
+ * signSchnorr(msg, randomPrivateKeyBytes());
178
+ * ```
179
+ */
180
+ export declare const signSchnorr: (message: TArg<Uint8Array>, secretKey: TArg<Uint8Array>, auxRand?: TArg<Uint8Array>) => TRet<Uint8Array>;
181
+ /**
182
+ * Tagged-hash helper used by Schnorr and taproot constructions.
183
+ * @param tag - tagged-hash domain separator
184
+ * @param messages - message parts hashed under the tag
185
+ * @returns Tagged SHA-256 digest.
186
+ * @example
187
+ * Build the tagged hash used by Taproot leaves or tweaks.
188
+ * ```ts
189
+ * import { tagSchnorr } from '@scure/btc-signer/utils.js';
190
+ * tagSchnorr('TapLeaf', Uint8Array.of(0xc0), Uint8Array.of(0x51));
191
+ * ```
192
+ */
193
+ export declare const tagSchnorr: (tag: string, ...messages: TArg<Uint8Array[]>) => TRet<Uint8Array>;
194
+ /** Public key format tags used by validation helpers. */
195
+ export declare const PubT: Readonly<{
196
+ ecdsa: 0;
197
+ schnorr: 1;
198
+ }>;
199
+ /** Numeric public key format tag from {@link PubT}. */
22
200
  export type PubT = ValueOf<typeof PubT>;
23
- export declare function validatePubkey(pub: Bytes, type: PubT): Bytes;
24
- export declare function tapTweak(a: Bytes, b: Bytes): bigint;
25
- export declare function taprootTweakPrivKey(privKey: Bytes, merkleRoot?: Bytes): Bytes;
26
- export declare function taprootTweakPubkey(pubKey: Bytes, h: Bytes): [Bytes, number];
27
- export declare const TAPROOT_UNSPENDABLE_KEY: Bytes;
201
+ /**
202
+ * Validates a public key against the expected Bitcoin key encoding.
203
+ * @param pub - public key bytes to validate
204
+ * @param type - expected public key format
205
+ * @returns The validated public key bytes.
206
+ * @throws On wrong argument types. {@link TypeError}
207
+ * @throws On wrong argument ranges or values. {@link RangeError}
208
+ * @example
209
+ * Reject keys that do not match the encoding required by the current script path.
210
+ * ```ts
211
+ * import {
212
+ * PubT,
213
+ * pubECDSA,
214
+ * randomPrivateKeyBytes,
215
+ * validatePubkey,
216
+ * } from '@scure/btc-signer/utils.js';
217
+ * validatePubkey(pubECDSA(randomPrivateKeyBytes()), PubT.ecdsa);
218
+ * ```
219
+ */
220
+ export declare function validatePubkey(pub: TArg<Bytes>, type: PubT): TRet<Bytes>;
221
+ /**
222
+ * Computes the Taproot tweak scalar from an internal key and merkle root.
223
+ * @param a - internal key bytes
224
+ * @param b - optional merkle root bytes
225
+ * @returns Taproot tweak scalar.
226
+ * @throws If the tweak scalar is outside the curve order. {@link Error}
227
+ * @example
228
+ * Combine the internal key and Merkle root into the Taproot tweak scalar.
229
+ * ```ts
230
+ * import { pubSchnorr, randomPrivateKeyBytes, tapTweak } from '@scure/btc-signer/utils.js';
231
+ * tapTweak(pubSchnorr(randomPrivateKeyBytes()), new Uint8Array());
232
+ * ```
233
+ */
234
+ export declare function tapTweak(a: TArg<Bytes>, b: TArg<Bytes>): bigint;
235
+ /**
236
+ * Tweaks a private key for Taproot key-path spending.
237
+ * @param privKey - internal private key bytes
238
+ * @param merkleRoot - optional taproot merkle root
239
+ * @returns Tweaked private key bytes.
240
+ * @throws If the Taproot tweak scalar is outside the curve order. {@link Error}
241
+ * @example
242
+ * Derive the tweaked Taproot key-path secret from the internal private key.
243
+ * ```ts
244
+ * import { randomPrivateKeyBytes, taprootTweakPrivKey } from '@scure/btc-signer/utils.js';
245
+ * taprootTweakPrivKey(randomPrivateKeyBytes());
246
+ * ```
247
+ */
248
+ export declare function taprootTweakPrivKey(privKey: TArg<Bytes>, merkleRoot?: TArg<Bytes>): TRet<Bytes>;
249
+ /**
250
+ * Tweaks a Schnorr public key for Taproot key-path spending.
251
+ * @param pubKey - x-only internal public key
252
+ * @param h - taproot merkle root
253
+ * @returns Tweaked public key and output-key parity.
254
+ * @throws If the Taproot tweak scalar is outside the curve order. {@link Error}
255
+ * @example
256
+ * Derive the final Taproot output key from the internal key and Merkle root.
257
+ * ```ts
258
+ * import {
259
+ * pubSchnorr,
260
+ * randomPrivateKeyBytes,
261
+ * taprootTweakPubkey,
262
+ * } from '@scure/btc-signer/utils.js';
263
+ * taprootTweakPubkey(pubSchnorr(randomPrivateKeyBytes()), new Uint8Array());
264
+ * ```
265
+ */
266
+ export declare function taprootTweakPubkey(pubKey: TArg<Bytes>, h: TArg<Bytes>): TRet<[Bytes, number]>;
267
+ /** Standard unspendable internal key used for script-only Taproot outputs. */
268
+ export declare const TAPROOT_UNSPENDABLE_KEY: TRet<Bytes>;
269
+ /** Bitcoin network parameters. */
28
270
  export type BTC_NETWORK = {
271
+ /** Human-readable prefix used by Bech32 and Bech32m addresses. */
29
272
  bech32: string;
273
+ /** Base58 version byte for pay-to-public-key-hash addresses. */
30
274
  pubKeyHash: number;
275
+ /** Base58 version byte for pay-to-script-hash addresses. */
31
276
  scriptHash: number;
277
+ /** Base58 version byte for wallet-import-format private keys. */
32
278
  wif: number;
33
279
  };
280
+ /** Bitcoin mainnet network parameters. */
34
281
  export declare const NETWORK: BTC_NETWORK;
282
+ /** Bitcoin testnet network parameters. */
35
283
  export declare const TEST_NETWORK: BTC_NETWORK;
36
- export declare function compareBytes(a: Bytes, b: Bytes): number;
284
+ /**
285
+ * Lexicographically compares two byte arrays.
286
+ * @param a - first byte array
287
+ * @param b - second byte array
288
+ * @returns `-1`, `0`, or `1` depending on the ordering.
289
+ * @throws On wrong argument types. {@link TypeError}
290
+ * @example
291
+ * Compare two serialized keys using Bitcoin's byte ordering.
292
+ * ```ts
293
+ * compareBytes(new Uint8Array([1]), new Uint8Array([2]));
294
+ * ```
295
+ */
296
+ export declare function compareBytes(a: TArg<Bytes>, b: TArg<Bytes>): number;
297
+ /**
298
+ * Reverses an object's keys and values.
299
+ * @param obj - object to reverse
300
+ * @returns Object with original values mapped back to keys.
301
+ * @throws If duplicate values would collide while reversing the object. {@link Error}
302
+ * @example
303
+ * Flip a lookup table so the values become keys.
304
+ * ```ts
305
+ * reverseObject({ a: 1, b: 2 });
306
+ * ```
307
+ */
37
308
  export declare function reverseObject<T extends Record<string, string | number>>(obj: T): {
38
309
  [K in T[keyof T]]: Extract<keyof T, string>;
39
310
  };
311
+ /** Union of all value types in an object type. */
40
312
  export type ValueOf<T> = T[keyof T];
41
- //# sourceMappingURL=utils.d.ts.map