@scure/btc-signer 2.0.0 → 2.2.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/README.md +189 -39
- package/index.d.ts +15 -5
- package/index.d.ts.map +1 -1
- package/index.js +16 -6
- package/index.js.map +1 -1
- package/musig2.d.ts +202 -64
- package/musig2.d.ts.map +1 -1
- package/musig2.js +324 -87
- package/musig2.js.map +1 -1
- package/p2p.d.ts +17 -7
- package/p2p.d.ts.map +1 -1
- package/p2p.js +40 -4
- package/p2p.js.map +1 -1
- package/package.json +25 -10
- package/payment.d.ts +407 -38
- package/payment.d.ts.map +1 -1
- package/payment.js +504 -57
- package/payment.js.map +1 -1
- package/psbt.d.ts +2958 -559
- package/psbt.d.ts.map +1 -1
- package/psbt.js +462 -118
- package/psbt.js.map +1 -1
- package/script.d.ts +311 -132
- package/script.d.ts.map +1 -1
- package/script.js +246 -35
- package/script.js.map +1 -1
- package/src/index.ts +34 -11
- package/src/musig2.ts +387 -145
- package/src/p2p.ts +54 -18
- package/src/payment.ts +823 -226
- package/src/psbt.ts +633 -228
- package/src/script.ts +353 -117
- package/src/transaction.ts +593 -169
- package/src/utils.ts +322 -43
- package/src/utxo.ts +154 -51
- package/transaction.d.ts +242 -31
- package/transaction.d.ts.map +1 -1
- package/transaction.js +460 -100
- package/transaction.js.map +1 -1
- package/utils.d.ts +266 -24
- package/utils.d.ts.map +1 -1
- package/utils.js +278 -27
- package/utils.js.map +1 -1
- package/utxo.d.ts +438 -75
- package/utxo.d.ts.map +1 -1
- package/utxo.js +123 -36
- package/utxo.js.map +1 -1
package/utils.js
CHANGED
|
@@ -1,27 +1,151 @@
|
|
|
1
1
|
import { schnorr, secp256k1 as secp } from '@noble/curves/secp256k1.js';
|
|
2
|
-
import { bytesToNumberBE, numberToBytesBE } from '@noble/curves/utils.js';
|
|
2
|
+
import { abytes, bytesToNumberBE, numberToBytesBE } from '@noble/curves/utils.js';
|
|
3
3
|
import { ripemd160 } from '@noble/hashes/legacy.js';
|
|
4
|
-
import { sha256 } from '@noble/hashes/sha2.js';
|
|
4
|
+
import { sha256 as nobleSha256 } from '@noble/hashes/sha2.js';
|
|
5
|
+
import {} from '@noble/hashes/utils.js';
|
|
5
6
|
import { utils as packedUtils, U32LE } from 'micro-packed';
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
7
|
+
export {} from '@noble/hashes/utils.js';
|
|
8
|
+
const Point = /* @__PURE__ */ (() => secp.Point)();
|
|
9
|
+
const Fn = /* @__PURE__ */ (() => Point.Fn)();
|
|
10
|
+
const CURVE_ORDER = /* @__PURE__ */ (() => Point.Fn.ORDER)();
|
|
11
|
+
/**
|
|
12
|
+
* Checks whether a curve y-coordinate is even.
|
|
13
|
+
* @param y - y-coordinate to inspect
|
|
14
|
+
* @returns `true` when the coordinate is even.
|
|
15
|
+
* @example
|
|
16
|
+
* Check whether a point coordinate has even parity.
|
|
17
|
+
* ```ts
|
|
18
|
+
* hasEven(2n);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
9
21
|
export const hasEven = (y) => y % 2n === 0n;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Checks whether a value is a Uint8Array.
|
|
24
|
+
* @param a - value to inspect
|
|
25
|
+
* @returns `true` when the value is a Uint8Array.
|
|
26
|
+
* @example
|
|
27
|
+
* Check whether an unknown value is already bytes.
|
|
28
|
+
* ```ts
|
|
29
|
+
* isBytes(new Uint8Array([1]));
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export const isBytes = /* @__PURE__ */ (() => packedUtils.isBytes)();
|
|
33
|
+
/**
|
|
34
|
+
* Concatenates byte arrays into a single Uint8Array.
|
|
35
|
+
* @param arrays - byte arrays to concatenate
|
|
36
|
+
* @returns Concatenated byte array.
|
|
37
|
+
* @example
|
|
38
|
+
* Join several byte chunks before hashing or signing them.
|
|
39
|
+
* ```ts
|
|
40
|
+
* concatBytes(new Uint8Array([1]), new Uint8Array([2]));
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export const concatBytes =
|
|
44
|
+
/* @__PURE__ */ (() => packedUtils.concatBytes)();
|
|
45
|
+
/**
|
|
46
|
+
* Compares two byte arrays for equality.
|
|
47
|
+
* @param a - first byte array
|
|
48
|
+
* @param b - second byte array
|
|
49
|
+
* @returns `true` when both arrays contain the same bytes.
|
|
50
|
+
* @example
|
|
51
|
+
* Compare two serialized values without converting them first.
|
|
52
|
+
* ```ts
|
|
53
|
+
* equalBytes(new Uint8Array([1]), new Uint8Array([1]));
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export const equalBytes =
|
|
57
|
+
/* @__PURE__ */ (() => packedUtils.equalBytes)();
|
|
58
|
+
/**
|
|
59
|
+
* SHA-256 hash function.
|
|
60
|
+
* @param msg - bytes to hash
|
|
61
|
+
* @returns SHA-256 digest.
|
|
62
|
+
* @example
|
|
63
|
+
* Hash a byte array with SHA-256.
|
|
64
|
+
* ```ts
|
|
65
|
+
* sha256(new Uint8Array([1, 2, 3]));
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export const sha256 = /* @__PURE__ */ (() => nobleSha256)();
|
|
69
|
+
/**
|
|
70
|
+
* HASH160 helper used by classic Bitcoin addresses.
|
|
71
|
+
* @param msg - bytes to hash
|
|
72
|
+
* @returns RIPEMD160(SHA256(msg)).
|
|
73
|
+
* @example
|
|
74
|
+
* Derive the HASH160 used by legacy address formats.
|
|
75
|
+
* ```ts
|
|
76
|
+
* hash160(new Uint8Array([1, 2, 3]));
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
14
79
|
export const hash160 = (msg) => ripemd160(sha256(msg));
|
|
80
|
+
/**
|
|
81
|
+
* Double-SHA256 helper used by Bitcoin transaction ids.
|
|
82
|
+
* @param msgs - message parts to concatenate and hash
|
|
83
|
+
* @returns SHA256(SHA256(concat(msgs))).
|
|
84
|
+
* @example
|
|
85
|
+
* Compute the double-SHA256 used by txids and sighashes.
|
|
86
|
+
* ```ts
|
|
87
|
+
* sha256x2(new Uint8Array([1]), new Uint8Array([2]));
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
15
90
|
export const sha256x2 = (...msgs) => sha256(sha256(concatBytes(...msgs)));
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Generates a random secp256k1 private key.
|
|
93
|
+
* @returns Random 32-byte private key.
|
|
94
|
+
* @example
|
|
95
|
+
* Generate a fresh secp256k1 private key for signing.
|
|
96
|
+
* ```ts
|
|
97
|
+
* const privKey = randomPrivateKeyBytes();
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export const randomPrivateKeyBytes = () => schnorr.utils.randomSecretKey();
|
|
101
|
+
/**
|
|
102
|
+
* Derives a BIP340 Schnorr public key from a private key.
|
|
103
|
+
* @param priv - private key bytes
|
|
104
|
+
* @returns X-only public key bytes.
|
|
105
|
+
* @example
|
|
106
|
+
* Derive the x-only public key used by Schnorr and Taproot.
|
|
107
|
+
* ```ts
|
|
108
|
+
* import { pubSchnorr, randomPrivateKeyBytes } from '@scure/btc-signer/utils.js';
|
|
109
|
+
* pubSchnorr(randomPrivateKeyBytes());
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export const pubSchnorr = (priv) => schnorr.getPublicKey(priv);
|
|
113
|
+
/**
|
|
114
|
+
* Derives a secp256k1 ECDSA public key from a private key.
|
|
115
|
+
* @param privateKey - private key bytes
|
|
116
|
+
* @param isCompressed - whether to return the compressed form
|
|
117
|
+
* @returns Serialized public key bytes.
|
|
118
|
+
* @example
|
|
119
|
+
* Derive the normal secp256k1 public key for legacy or SegWit scripts.
|
|
120
|
+
* ```ts
|
|
121
|
+
* import { pubECDSA, randomPrivateKeyBytes } from '@scure/btc-signer/utils.js';
|
|
122
|
+
* pubECDSA(randomPrivateKeyBytes());
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export const pubECDSA = (privateKey, isCompressed) => secp.getPublicKey(privateKey, isCompressed);
|
|
19
126
|
// low-r signature grinding. Used to reduce tx size by 1 byte.
|
|
20
127
|
// noble/secp256k1 does not support the feature: it is not used outside of BTC.
|
|
21
128
|
// We implement it manually, because in BTC it's common.
|
|
22
129
|
// Not best way, but closest to bitcoin implementation (easier to check)
|
|
23
130
|
const hasLowR = (sig) => sig.r < CURVE_ORDER / 2n;
|
|
131
|
+
/**
|
|
132
|
+
* Signs a 32-byte hash with ECDSA and returns DER encoding.
|
|
133
|
+
* @param hash - message hash to sign
|
|
134
|
+
* @param privateKey - signer private key
|
|
135
|
+
* @param lowR - whether to grind for low-R signatures
|
|
136
|
+
* @returns DER-encoded signature bytes.
|
|
137
|
+
* @throws If low-R grinding overflows or ECDSA signing fails validation. {@link Error}
|
|
138
|
+
* @example
|
|
139
|
+
* Hash a message first, then create the DER-encoded ECDSA signature.
|
|
140
|
+
* ```ts
|
|
141
|
+
* import { randomPrivateKeyBytes, sha256, signECDSA } from '@scure/btc-signer/utils.js';
|
|
142
|
+
* signECDSA(sha256(new Uint8Array([1, 2, 3])), randomPrivateKeyBytes());
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
24
145
|
export function signECDSA(hash, privateKey, lowR = false) {
|
|
146
|
+
// signECDSA is the 32-byte sighash wrapper for BTC callers, so reject arbitrary-length
|
|
147
|
+
// messages here instead of silently signing them with prehash disabled.
|
|
148
|
+
abytes(hash, 32, 'hash');
|
|
25
149
|
let sig = secp.Signature.fromBytes(secp.sign(hash, privateKey, { prehash: false }));
|
|
26
150
|
if (lowR && !hasLowR(sig)) {
|
|
27
151
|
const extraEntropy = new Uint8Array(32);
|
|
@@ -35,30 +159,89 @@ export function signECDSA(hash, privateKey, lowR = false) {
|
|
|
35
159
|
}
|
|
36
160
|
return sig.toBytes('der');
|
|
37
161
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
162
|
+
/**
|
|
163
|
+
* BIP340 Schnorr signing function.
|
|
164
|
+
* @param message - 32-byte message digest
|
|
165
|
+
* @param secretKey - signer private key
|
|
166
|
+
* @param auxRand - optional auxiliary randomness
|
|
167
|
+
* @returns Schnorr signature bytes.
|
|
168
|
+
* @example
|
|
169
|
+
* Sign a 32-byte digest with the built-in BIP340 helper.
|
|
170
|
+
* ```ts
|
|
171
|
+
* import { randomPrivateKeyBytes, sha256, signSchnorr } from '@scure/btc-signer/utils.js';
|
|
172
|
+
* const msg = sha256(new Uint8Array([1, 2, 3]));
|
|
173
|
+
* signSchnorr(msg, randomPrivateKeyBytes());
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
export const signSchnorr = (message, secretKey, auxRand) => schnorr.sign(message, secretKey, auxRand);
|
|
177
|
+
/**
|
|
178
|
+
* Tagged-hash helper used by Schnorr and taproot constructions.
|
|
179
|
+
* @param tag - tagged-hash domain separator
|
|
180
|
+
* @param messages - message parts hashed under the tag
|
|
181
|
+
* @returns Tagged SHA-256 digest.
|
|
182
|
+
* @example
|
|
183
|
+
* Build the tagged hash used by Taproot leaves or tweaks.
|
|
184
|
+
* ```ts
|
|
185
|
+
* import { tagSchnorr } from '@scure/btc-signer/utils.js';
|
|
186
|
+
* tagSchnorr('TapLeaf', Uint8Array.of(0xc0), Uint8Array.of(0x51));
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export const tagSchnorr = (tag, ...messages) => schnorr.utils.taggedHash(tag, ...messages);
|
|
190
|
+
/** Public key format tags used by validation helpers. */
|
|
191
|
+
export const PubT = /* @__PURE__ */ (() => Object.freeze({
|
|
41
192
|
ecdsa: 0,
|
|
42
193
|
schnorr: 1,
|
|
43
|
-
};
|
|
194
|
+
}))();
|
|
195
|
+
/**
|
|
196
|
+
* Validates a public key against the expected Bitcoin key encoding.
|
|
197
|
+
* @param pub - public key bytes to validate
|
|
198
|
+
* @param type - expected public key format
|
|
199
|
+
* @returns The validated public key bytes.
|
|
200
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
201
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
202
|
+
* @example
|
|
203
|
+
* Reject keys that do not match the encoding required by the current script path.
|
|
204
|
+
* ```ts
|
|
205
|
+
* import {
|
|
206
|
+
* PubT,
|
|
207
|
+
* pubECDSA,
|
|
208
|
+
* randomPrivateKeyBytes,
|
|
209
|
+
* validatePubkey,
|
|
210
|
+
* } from '@scure/btc-signer/utils.js';
|
|
211
|
+
* validatePubkey(pubECDSA(randomPrivateKeyBytes()), PubT.ecdsa);
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
44
214
|
export function validatePubkey(pub, type) {
|
|
45
215
|
const len = pub.length;
|
|
46
216
|
if (type === PubT.ecdsa) {
|
|
47
217
|
if (len === 32)
|
|
48
|
-
throw new
|
|
218
|
+
throw new RangeError('Expected non-Schnorr key');
|
|
49
219
|
Point.fromBytes(pub); // does assertValidity
|
|
50
220
|
return pub;
|
|
51
221
|
}
|
|
52
222
|
else if (type === PubT.schnorr) {
|
|
53
223
|
if (len !== 32)
|
|
54
|
-
throw new
|
|
224
|
+
throw new RangeError('Expected 32-byte Schnorr key');
|
|
55
225
|
schnorr.utils.lift_x(bytesToNumberBE(pub));
|
|
56
226
|
return pub;
|
|
57
227
|
}
|
|
58
228
|
else {
|
|
59
|
-
throw new
|
|
229
|
+
throw new TypeError('Unknown key type');
|
|
60
230
|
}
|
|
61
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Computes the Taproot tweak scalar from an internal key and merkle root.
|
|
234
|
+
* @param a - internal key bytes
|
|
235
|
+
* @param b - optional merkle root bytes
|
|
236
|
+
* @returns Taproot tweak scalar.
|
|
237
|
+
* @throws If the tweak scalar is outside the curve order. {@link Error}
|
|
238
|
+
* @example
|
|
239
|
+
* Combine the internal key and Merkle root into the Taproot tweak scalar.
|
|
240
|
+
* ```ts
|
|
241
|
+
* import { pubSchnorr, randomPrivateKeyBytes, tapTweak } from '@scure/btc-signer/utils.js';
|
|
242
|
+
* tapTweak(pubSchnorr(randomPrivateKeyBytes()), new Uint8Array());
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
62
245
|
export function tapTweak(a, b) {
|
|
63
246
|
const u = schnorr.utils;
|
|
64
247
|
const t = u.taggedHash('TapTweak', a, b);
|
|
@@ -67,8 +250,24 @@ export function tapTweak(a, b) {
|
|
|
67
250
|
throw new Error('tweak higher than curve order');
|
|
68
251
|
return tn;
|
|
69
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Tweaks a private key for Taproot key-path spending.
|
|
255
|
+
* @param privKey - internal private key bytes
|
|
256
|
+
* @param merkleRoot - optional taproot merkle root
|
|
257
|
+
* @returns Tweaked private key bytes.
|
|
258
|
+
* @throws If the Taproot tweak scalar is outside the curve order. {@link Error}
|
|
259
|
+
* @example
|
|
260
|
+
* Derive the tweaked Taproot key-path secret from the internal private key.
|
|
261
|
+
* ```ts
|
|
262
|
+
* import { randomPrivateKeyBytes, taprootTweakPrivKey } from '@scure/btc-signer/utils.js';
|
|
263
|
+
* taprootTweakPrivKey(randomPrivateKeyBytes());
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
70
266
|
export function taprootTweakPrivKey(privKey, merkleRoot = Uint8Array.of()) {
|
|
71
267
|
const u = schnorr.utils;
|
|
268
|
+
// BIP341 taproot_tweak_seckey starts with `seckey0 = int_from_bytes(seckey0)`, and
|
|
269
|
+
// BIP340 defines `int(x)` only for `x` as a 32-byte array, so reject other widths here.
|
|
270
|
+
abytes(privKey, 32, 'privKey');
|
|
72
271
|
const seckey0 = bytesToNumberBE(privKey); // seckey0 = int_from_bytes(seckey0)
|
|
73
272
|
const P = Point.BASE.multiply(seckey0); // P = point_mul(G, seckey0)
|
|
74
273
|
// seckey = seckey0 if has_even_y(P) else SECP256K1_ORDER - seckey0
|
|
@@ -79,8 +278,28 @@ export function taprootTweakPrivKey(privKey, merkleRoot = Uint8Array.of()) {
|
|
|
79
278
|
// bytes_from_int((seckey + t) % SECP256K1_ORDER)
|
|
80
279
|
return numberToBytesBE(Fn.add(seckey, t), 32);
|
|
81
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Tweaks a Schnorr public key for Taproot key-path spending.
|
|
283
|
+
* @param pubKey - x-only internal public key
|
|
284
|
+
* @param h - taproot merkle root
|
|
285
|
+
* @returns Tweaked public key and output-key parity.
|
|
286
|
+
* @throws If the Taproot tweak scalar is outside the curve order. {@link Error}
|
|
287
|
+
* @example
|
|
288
|
+
* Derive the final Taproot output key from the internal key and Merkle root.
|
|
289
|
+
* ```ts
|
|
290
|
+
* import {
|
|
291
|
+
* pubSchnorr,
|
|
292
|
+
* randomPrivateKeyBytes,
|
|
293
|
+
* taprootTweakPubkey,
|
|
294
|
+
* } from '@scure/btc-signer/utils.js';
|
|
295
|
+
* taprootTweakPubkey(pubSchnorr(randomPrivateKeyBytes()), new Uint8Array());
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
82
298
|
export function taprootTweakPubkey(pubKey, h) {
|
|
83
299
|
const u = schnorr.utils;
|
|
300
|
+
// BIP341 taproot_tweak_pubkey feeds `pubkey` into `int_from_bytes(pubkey)`, and
|
|
301
|
+
// BIP340 defines `int(x)` only for `x` as a 32-byte array, so reject other widths here.
|
|
302
|
+
abytes(pubKey, 32, 'pubKey');
|
|
84
303
|
const t = tapTweak(pubKey, h); // t = int_from_bytes(tagged_hash("TapTweak", pubkey + h))
|
|
85
304
|
const P = u.lift_x(bytesToNumberBE(pubKey)); // P = lift_x(int_from_bytes(pubkey))
|
|
86
305
|
const Q = P.add(Point.BASE.multiply(t)); // Q = point_add(P, point_mul(G, t))
|
|
@@ -89,27 +308,46 @@ export function taprootTweakPubkey(pubKey, h) {
|
|
|
89
308
|
}
|
|
90
309
|
// Another stupid decision, where lack of standard affects security.
|
|
91
310
|
// Multisig needs to be generated with some key.
|
|
92
|
-
// We are using
|
|
311
|
+
// We are using the BIP 341/bitcoinjs-lib approach:
|
|
312
|
+
// SHA256(uncompressedDER(SECP256K1_GENERATOR_POINT))
|
|
93
313
|
// It is possible to switch SECP256K1_GENERATOR_POINT with some random point;
|
|
94
314
|
// but it's too complex to prove.
|
|
95
315
|
// Also used by bitcoin-core and bitcoinjs-lib
|
|
96
|
-
|
|
97
|
-
|
|
316
|
+
// This is the fixed BIP 341 H example, not the privacy-preserving H + rG variant.
|
|
317
|
+
// Downstream helpers use exact-byte equality with it to recognize
|
|
318
|
+
// library-generated script-only outputs.
|
|
319
|
+
/** Standard unspendable internal key used for script-only Taproot outputs. */
|
|
320
|
+
export const TAPROOT_UNSPENDABLE_KEY = /* @__PURE__ */ (() => sha256(Point.BASE.toBytes(false)))();
|
|
321
|
+
/** Bitcoin mainnet network parameters. */
|
|
322
|
+
export const NETWORK = /* @__PURE__ */ Object.freeze({
|
|
98
323
|
bech32: 'bc',
|
|
99
324
|
pubKeyHash: 0x00,
|
|
100
325
|
scriptHash: 0x05,
|
|
101
326
|
wif: 0x80,
|
|
102
|
-
};
|
|
103
|
-
|
|
327
|
+
});
|
|
328
|
+
/** Bitcoin testnet network parameters. */
|
|
329
|
+
export const TEST_NETWORK = /* @__PURE__ */ Object.freeze({
|
|
104
330
|
bech32: 'tb',
|
|
105
331
|
pubKeyHash: 0x6f,
|
|
106
332
|
scriptHash: 0xc4,
|
|
107
333
|
wif: 0xef,
|
|
108
|
-
};
|
|
334
|
+
});
|
|
109
335
|
// Exported for tests, internal method
|
|
336
|
+
/**
|
|
337
|
+
* Lexicographically compares two byte arrays.
|
|
338
|
+
* @param a - first byte array
|
|
339
|
+
* @param b - second byte array
|
|
340
|
+
* @returns `-1`, `0`, or `1` depending on the ordering.
|
|
341
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
342
|
+
* @example
|
|
343
|
+
* Compare two serialized keys using Bitcoin's byte ordering.
|
|
344
|
+
* ```ts
|
|
345
|
+
* compareBytes(new Uint8Array([1]), new Uint8Array([2]));
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
110
348
|
export function compareBytes(a, b) {
|
|
111
349
|
if (!isBytes(a) || !isBytes(b))
|
|
112
|
-
throw new
|
|
350
|
+
throw new TypeError(`cmp: wrong type a=${typeof a} b=${typeof b}`);
|
|
113
351
|
// -1 -> a<b, 0 -> a==b, 1 -> a>b
|
|
114
352
|
const len = Math.min(a.length, b.length);
|
|
115
353
|
for (let i = 0; i < len; i++)
|
|
@@ -118,8 +356,21 @@ export function compareBytes(a, b) {
|
|
|
118
356
|
return Math.sign(a.length - b.length);
|
|
119
357
|
}
|
|
120
358
|
// Reverses key<->values
|
|
359
|
+
/**
|
|
360
|
+
* Reverses an object's keys and values.
|
|
361
|
+
* @param obj - object to reverse
|
|
362
|
+
* @returns Object with original values mapped back to keys.
|
|
363
|
+
* @throws If duplicate values would collide while reversing the object. {@link Error}
|
|
364
|
+
* @example
|
|
365
|
+
* Flip a lookup table so the values become keys.
|
|
366
|
+
* ```ts
|
|
367
|
+
* reverseObject({ a: 1, b: 2 });
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
121
370
|
export function reverseObject(obj) {
|
|
122
|
-
|
|
371
|
+
// Keep a raw dictionary shape so enum-like tables can reverse values like
|
|
372
|
+
// `toString` without colliding with inherited Object prototype properties.
|
|
373
|
+
const res = Object.create(null);
|
|
123
374
|
for (const k in obj) {
|
|
124
375
|
if (res[obj[k]] !== undefined)
|
|
125
376
|
throw new Error('duplicate key');
|
package/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,SAAS,IAAI,IAAI,EAAE,MAAM,4BAA4B,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,SAAS,IA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