@did-btcr2/keypair 0.10.0 → 0.11.1
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/dist/cjs/pair.js +43 -49
- package/dist/cjs/pair.js.map +1 -1
- package/dist/cjs/public.js +21 -139
- package/dist/cjs/public.js.map +1 -1
- package/dist/cjs/secret.js +65 -82
- package/dist/cjs/secret.js.map +1 -1
- package/dist/esm/pair.js +43 -49
- package/dist/esm/pair.js.map +1 -1
- package/dist/esm/public.js +21 -139
- package/dist/esm/public.js.map +1 -1
- package/dist/esm/secret.js +65 -82
- package/dist/esm/secret.js.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/pair.d.ts +19 -4
- package/dist/types/pair.d.ts.map +1 -1
- package/dist/types/public.d.ts +7 -42
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/secret.d.ts +21 -20
- package/dist/types/secret.d.ts.map +1 -1
- package/dist/types/types.d.ts +3 -2
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/pair.ts +51 -56
- package/src/public.ts +25 -176
- package/src/secret.ts +72 -111
- package/src/types.ts +2 -2
package/dist/esm/public.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
|
|
1
|
+
import { PublicKeyError } from '@did-btcr2/common';
|
|
2
|
+
import { secp256k1, schnorr } from '@noble/curves/secp256k1.js';
|
|
3
|
+
import { equalBytes } from '@noble/curves/utils.js';
|
|
4
|
+
import { base58 } from '@scure/base';
|
|
5
|
+
export const BIP340_PUBLIC_KEY_MULTIBASE_PREFIX = new Uint8Array([0xe7, 0x01]);
|
|
6
6
|
/**
|
|
7
7
|
* Encapsulates a secp256k1 public key compliant to BIP-340 BIP schnorr signature scheme.
|
|
8
8
|
* Provides get methods for different formats (compressed, x-only, multibase).
|
|
@@ -38,11 +38,11 @@ export class CompressedSecp256k1PublicKey {
|
|
|
38
38
|
throw new PublicKeyError('Invalid argument: byte length must be 33 (compressed)', 'CONSTRUCTOR_ERROR', { keyBytes });
|
|
39
39
|
}
|
|
40
40
|
// Validate the point is on curve and in compressed form
|
|
41
|
-
if (!
|
|
41
|
+
if (!secp256k1.utils.isValidPublicKey(keyBytes)) {
|
|
42
42
|
throw new PublicKeyError('Invalid argument: not a valid secp256k1 compressed point', 'CONSTRUCTOR_ERROR', { keyBytes });
|
|
43
43
|
}
|
|
44
|
-
//
|
|
45
|
-
this.#bytes = keyBytes;
|
|
44
|
+
// Defensive copy — caller cannot mutate internal state
|
|
45
|
+
this.#bytes = new Uint8Array(keyBytes);
|
|
46
46
|
// Set multibase
|
|
47
47
|
this.#multibase.encoded = this.encode();
|
|
48
48
|
this.#multibase.key = [...this.#multibase.prefix, ...this.compressed];
|
|
@@ -61,8 +61,7 @@ export class CompressedSecp256k1PublicKey {
|
|
|
61
61
|
* @returns {Uint8Array} The 65-byte uncompressed public key (0x04, x, y).
|
|
62
62
|
*/
|
|
63
63
|
get uncompressed() {
|
|
64
|
-
|
|
65
|
-
return uncompressed;
|
|
64
|
+
return secp256k1.Point.fromBytes(this.compressed).toBytes(false);
|
|
66
65
|
}
|
|
67
66
|
/**
|
|
68
67
|
* X-only (32-byte) view of the public key per BIP-340.
|
|
@@ -111,8 +110,11 @@ export class CompressedSecp256k1PublicKey {
|
|
|
111
110
|
* @returns {MultibaseObject} An object containing the multibase bytes, address and prefix.
|
|
112
111
|
*/
|
|
113
112
|
get multibase() {
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
return {
|
|
114
|
+
prefix: new Uint8Array(this.#multibase.prefix),
|
|
115
|
+
key: [...this.#multibase.key],
|
|
116
|
+
encoded: this.#multibase.encoded
|
|
117
|
+
};
|
|
116
118
|
}
|
|
117
119
|
/**
|
|
118
120
|
* Returns the raw public key as a hex string.
|
|
@@ -144,40 +146,17 @@ export class CompressedSecp256k1PublicKey {
|
|
|
144
146
|
* @returns {KeyBytes} The decoded public key: prefix and public key bytes
|
|
145
147
|
*/
|
|
146
148
|
decode() {
|
|
147
|
-
|
|
148
|
-
const decoded = base58btc.decode(this.multibase.encoded);
|
|
149
|
-
// If the public key bytes are not 35 bytes, throw an error
|
|
150
|
-
if (decoded.length !== 35) {
|
|
151
|
-
throw new PublicKeyError('Invalid argument: must be 35 byte publicKeyMultibase', 'DECODE_MULTIBASE_ERROR');
|
|
152
|
-
}
|
|
153
|
-
// Grab the prefix bytes
|
|
154
|
-
const prefix = decoded.slice(0, 2);
|
|
155
|
-
// Compute the prefix hash
|
|
156
|
-
const prefixHash = Buffer.from(sha256(prefix)).toString('hex');
|
|
157
|
-
// If the prefix hash does not equal the BIP340 prefix hash, throw an error
|
|
158
|
-
if (prefixHash !== BIP340_PUBLIC_KEY_MULTIBASE_PREFIX_HASH) {
|
|
159
|
-
throw new PublicKeyError(`Invalid prefix: malformed multibase prefix ${prefix}`, 'DECODE_MULTIBASE_ERROR');
|
|
160
|
-
}
|
|
161
|
-
// Return the decoded public key bytes
|
|
162
|
-
return decoded;
|
|
149
|
+
return base58.decode(this.multibase.encoded.slice(1));
|
|
163
150
|
}
|
|
164
151
|
/**
|
|
165
152
|
* Encodes compressed secp256k1 public key from bytes to BIP340 multibase format.
|
|
166
153
|
* @returns {string} The public key encoded in base-58-btc multibase format.
|
|
167
154
|
*/
|
|
168
155
|
encode() {
|
|
169
|
-
// Convert public key bytes to an array
|
|
170
156
|
const pk = Array.from(this.compressed);
|
|
171
|
-
// Ensure the public key is 33-byte secp256k1 compressed public key
|
|
172
|
-
if (pk.length !== 33) {
|
|
173
|
-
throw new PublicKeyError('Invalid argument: must be 33-byte (compressed) public key', 'ENCODE_MULTIBASE_ERROR');
|
|
174
|
-
}
|
|
175
|
-
// Convert prefix to an array
|
|
176
157
|
const publicKeyMultibase = Array.from(BIP340_PUBLIC_KEY_MULTIBASE_PREFIX);
|
|
177
|
-
// Push the public key bytes at the end of the prefix
|
|
178
158
|
publicKeyMultibase.push(...pk);
|
|
179
|
-
|
|
180
|
-
return base58btc.encode(Uint8Array.from(publicKeyMultibase));
|
|
159
|
+
return 'z' + base58.encode(Uint8Array.from(publicKeyMultibase));
|
|
181
160
|
}
|
|
182
161
|
/**
|
|
183
162
|
* Verify a signature using schnorr or ecdsa.
|
|
@@ -190,24 +169,22 @@ export class CompressedSecp256k1PublicKey {
|
|
|
190
169
|
verify(signature, data, opts) {
|
|
191
170
|
// Default to schnorr scheme
|
|
192
171
|
opts ??= { scheme: 'schnorr' };
|
|
193
|
-
// If scheme is ecdsa, verify using ecdsa
|
|
194
172
|
if (opts.scheme === 'ecdsa') {
|
|
195
|
-
return
|
|
173
|
+
return secp256k1.verify(signature, data, this.compressed);
|
|
196
174
|
}
|
|
197
|
-
// If scheme is schnorr, verify using schnorr
|
|
198
175
|
else if (opts.scheme === 'schnorr') {
|
|
199
|
-
return
|
|
176
|
+
return schnorr.verify(signature, data, this.x);
|
|
200
177
|
}
|
|
201
178
|
// If scheme is neither ecdsa nor schnorr, throw an error
|
|
202
179
|
throw new PublicKeyError(`Invalid scheme: ${opts.scheme}.`, 'VERIFY_SIGNATURE_ERROR', opts);
|
|
203
180
|
}
|
|
204
181
|
/**
|
|
205
182
|
* Compares this public key to another public key.
|
|
206
|
-
* @param {
|
|
183
|
+
* @param {PublicKey} other The other public key to compare
|
|
207
184
|
* @returns {boolean} True if the public keys are equal, false otherwise.
|
|
208
185
|
*/
|
|
209
186
|
equals(other) {
|
|
210
|
-
return this.
|
|
187
|
+
return equalBytes(this.compressed, other.compressed);
|
|
211
188
|
}
|
|
212
189
|
/**
|
|
213
190
|
* JSON representation of a CompressedSecp256k1PublicKey object.
|
|
@@ -224,61 +201,6 @@ export class CompressedSecp256k1PublicKey {
|
|
|
224
201
|
},
|
|
225
202
|
};
|
|
226
203
|
}
|
|
227
|
-
/**
|
|
228
|
-
* Computes modular exponentiation: (base^exp) % mod.
|
|
229
|
-
* Used for computing modular square roots.
|
|
230
|
-
* @param {bigint} base The base value
|
|
231
|
-
* @param {bigint} exp The exponent value
|
|
232
|
-
* @param {bigint} mod The modulus value
|
|
233
|
-
* @returns {bigint} The result of the modular exponentiation
|
|
234
|
-
*/
|
|
235
|
-
modPow(base, exp, mod) {
|
|
236
|
-
let result = 1n;
|
|
237
|
-
while (exp > 0n) {
|
|
238
|
-
if (exp & 1n)
|
|
239
|
-
result = (result * base) % mod;
|
|
240
|
-
base = (base * base) % mod;
|
|
241
|
-
exp >>= 1n;
|
|
242
|
-
}
|
|
243
|
-
return result;
|
|
244
|
-
}
|
|
245
|
-
;
|
|
246
|
-
/**
|
|
247
|
-
* Computes `sqrt(a) mod p` using Tonelli-Shanks algorithm.
|
|
248
|
-
* This finds `y` such that `y^2 ≡ a mod p`.
|
|
249
|
-
* @param {bigint} a The value to find the square root of
|
|
250
|
-
* @param {bigint} p The prime modulus
|
|
251
|
-
* @returns {bigint} The square root of `a` mod `p`
|
|
252
|
-
*/
|
|
253
|
-
sqrtMod(a, p) {
|
|
254
|
-
return this.modPow(a, (p + 1n) >> 2n, p);
|
|
255
|
-
}
|
|
256
|
-
;
|
|
257
|
-
/**
|
|
258
|
-
* Lifts a 32-byte x-only coordinate into a full secp256k1 point (x, y).
|
|
259
|
-
* @param xBytes 32-byte x-coordinate
|
|
260
|
-
* @returns {Uint8Array} 65-byte uncompressed public key (starts with `0x04`)
|
|
261
|
-
*/
|
|
262
|
-
liftX() {
|
|
263
|
-
// Ensure x-coordinate is 32 bytes
|
|
264
|
-
if (this.x.length !== 32) {
|
|
265
|
-
throw new PublicKeyError('Invalid argument: x-coordinate length must be 32 bytes', 'LIFT_X_ERROR');
|
|
266
|
-
}
|
|
267
|
-
// Convert x from Uint8Array → BigInt
|
|
268
|
-
const x = BigInt('0x' + Buffer.from(this.x).toString('hex'));
|
|
269
|
-
if (x <= 0n || x >= CURVE.p) {
|
|
270
|
-
throw new PublicKeyError('Invalid conversion: x out of range as BigInt', 'LIFT_X_ERROR');
|
|
271
|
-
}
|
|
272
|
-
// Compute y² = x³ + 7 mod p
|
|
273
|
-
const ySquared = BigInt((x ** 3n + CURVE.b) % CURVE.p);
|
|
274
|
-
// Compute y (do not enforce parity)
|
|
275
|
-
const y = this.sqrtMod(ySquared, CURVE.p);
|
|
276
|
-
// Convert x and y to Uint8Array
|
|
277
|
-
const yBytes = Buffer.from(y.toString(16).padStart(64, '0'), 'hex');
|
|
278
|
-
// Return 65-byte uncompressed public key: `0x04 || x || y`
|
|
279
|
-
return new Uint8Array(Buffer.concat([Buffer.from([0x04]), Buffer.from(this.x), yBytes]));
|
|
280
|
-
}
|
|
281
|
-
;
|
|
282
204
|
/**
|
|
283
205
|
* Static method to validate a public key.
|
|
284
206
|
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
@@ -293,53 +215,13 @@ export class CompressedSecp256k1PublicKey {
|
|
|
293
215
|
return false;
|
|
294
216
|
}
|
|
295
217
|
}
|
|
296
|
-
/**
|
|
297
|
-
* Returns the point of the public key.
|
|
298
|
-
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
299
|
-
* @returns {Point} The point of the public key.
|
|
300
|
-
* @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
|
|
301
|
-
*/
|
|
302
|
-
static point(pk) {
|
|
303
|
-
// If the public key is a hex string, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
304
|
-
if (typeof pk === 'string' && /^[0-9a-fA-F]+$/.test(pk)) {
|
|
305
|
-
const publicKey = new CompressedSecp256k1PublicKey(Buffer.from(pk, 'hex'));
|
|
306
|
-
return publicKey.point;
|
|
307
|
-
}
|
|
308
|
-
// If the public key is a byte array or ArrayBuffer, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
309
|
-
if (pk instanceof Uint8Array || ArrayBuffer.isView(pk)) {
|
|
310
|
-
const publicKey = new CompressedSecp256k1PublicKey(pk);
|
|
311
|
-
return publicKey.point;
|
|
312
|
-
}
|
|
313
|
-
// If the public key is neither a hex string nor a byte array, throw an error
|
|
314
|
-
throw new PublicKeyError('Invalid publicKey: must be a hex string or byte array', 'POINT_ERROR', { publicKey: pk });
|
|
315
|
-
}
|
|
316
218
|
/**
|
|
317
219
|
* Creates a CompressedSecp256k1PublicKey object from a JSON representation.
|
|
318
220
|
* @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
|
|
319
221
|
* @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
|
|
320
222
|
*/
|
|
321
223
|
static fromJSON(json) {
|
|
322
|
-
json.point.
|
|
323
|
-
return new CompressedSecp256k1PublicKey(Uint8Array.from(json.point.x));
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Computes the deterministic public key for a given secret key.
|
|
327
|
-
* @param {Secp256k1SecretKey | KeyBytes} sk The Secp256k1SecretKey object or the secret key bytes
|
|
328
|
-
* @returns {CompressedSecp256k1PublicKey} A new CompressedSecp256k1PublicKey object
|
|
329
|
-
*/
|
|
330
|
-
static fromSecretKey(sk) {
|
|
331
|
-
// If the secret key is a Secp256k1SecretKey object, get the raw bytes else use the bytes
|
|
332
|
-
const bytes = sk instanceof Secp256k1SecretKey ? sk.bytes : sk;
|
|
333
|
-
// Throw error if the secret key is not 32 bytes
|
|
334
|
-
if (bytes.length !== 32) {
|
|
335
|
-
throw new PublicKeyError('Invalid arg: must be 32 byte secret key', 'FROM_SECRET_KEY_ERROR');
|
|
336
|
-
}
|
|
337
|
-
// Compute the public key from the secret key
|
|
338
|
-
const secret = sk instanceof Secp256k1SecretKey
|
|
339
|
-
? sk
|
|
340
|
-
: new Secp256k1SecretKey(sk);
|
|
341
|
-
// Return a new CompressedSecp256k1PublicKey object
|
|
342
|
-
return secret.computePublicKey();
|
|
224
|
+
return new CompressedSecp256k1PublicKey(Uint8Array.from([json.point.parity, ...json.point.x]));
|
|
343
225
|
}
|
|
344
226
|
}
|
|
345
227
|
//# sourceMappingURL=public.js.map
|
package/dist/esm/public.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public.js","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"public.js","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,cAAc,EAEf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,MAAM,CAAC,MAAM,kCAAkC,GAAU,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAkGtF;;;;;;GAMG;AACH,MAAM,OAAO,4BAA4B;IACvC;;QAEI;IACK,MAAM,CAAW;IAE1B;;OAEG;IACM,UAAU,GAAoB;QACrC,MAAM,EAAI,kCAAkC;QAC5C,GAAG,EAAO,EAAE;QACZ,OAAO,EAAG,EAAE;KACb,CAAC;IAEF;;;;OAIG;IACH,YAAY,YAAiB;QAC3B,gDAAgD;QAChD,MAAM,QAAQ,GAAG,YAAY,YAAY,UAAU;YACjD,CAAC,CAAC,YAAY;YACd,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC,CAAC;QAEtD,+CAA+C;QAC/C,IAAG,CAAC,QAAQ,IAAI,QAAQ,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;YACvC,MAAM,IAAI,cAAc,CACtB,uDAAuD,EACvD,mBAAmB,EAAE,EAAE,QAAQ,EAAE,CAClC,CAAC;QACJ,CAAC;QAED,wDAAwD;QACxD,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,gBAAgB,CAAC,QAAQ,CAAC,EAAE,CAAC;YAChD,MAAM,IAAI,cAAc,CACtB,0DAA0D,EAC1D,mBAAm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IACH,MAAM,CAAC,OAAO,CAAC,EAAO;QACpB,IAAI,CAAC;YACH,IAAI,4BAA4B,CAAC,EAAE,CAAC,CAAC;YACrC,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,IAAqB;QACnC,OAAO,IAAI,4BAA4B,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACjG,CAAC;CAEF"}
|
package/dist/esm/secret.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SecretKeyError } from '@did-btcr2/common';
|
|
2
2
|
import { sha256 } from '@noble/hashes/sha2';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
3
|
+
import { bytesToHex } from '@noble/hashes/utils';
|
|
4
|
+
import { secp256k1, schnorr } from '@noble/curves/secp256k1.js';
|
|
5
|
+
import { randomBytes } from '@noble/hashes/utils';
|
|
6
|
+
import { base58 } from '@scure/base';
|
|
7
7
|
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
8
|
+
import { equalBytes } from '@noble/curves/utils.js';
|
|
9
|
+
/** Fixed secret key header bytes per the Data Integrity BIP340 Cryptosuite spec: [0x81, 0x26] */
|
|
10
|
+
const BIP340_SECRET_KEY_MULTIBASE_PREFIX = new Uint8Array([0x81, 0x26]);
|
|
11
|
+
/** Hash of the BIP-340 Multikey secret key prefix */
|
|
12
|
+
const BIP340_SECRET_KEY_MULTIBASE_PREFIX_HASH = bytesToHex(sha256(BIP340_SECRET_KEY_MULTIBASE_PREFIX));
|
|
8
13
|
/**
|
|
9
14
|
* Encapsulates a secp256k1 secret key
|
|
10
15
|
* Provides get methods for different formats (raw, secret, point).
|
|
@@ -32,25 +37,35 @@ export class Secp256k1SecretKey {
|
|
|
32
37
|
if (!isBytes && !isSecret) {
|
|
33
38
|
throw new SecretKeyError('Invalid entropy: must be a valid byte array (32) or bigint', 'CONSTRUCTOR_ERROR');
|
|
34
39
|
}
|
|
35
|
-
// If bytes and
|
|
40
|
+
// If bytes and length is 32, defensive-copy and derive seed
|
|
36
41
|
if (isBytes && entropy.length === 32) {
|
|
37
|
-
this.#bytes = entropy;
|
|
38
|
-
this.#seed = Secp256k1SecretKey.toSecret(
|
|
42
|
+
this.#bytes = new Uint8Array(entropy);
|
|
43
|
+
this.#seed = Secp256k1SecretKey.toSecret(this.#bytes);
|
|
39
44
|
}
|
|
40
|
-
// If
|
|
41
|
-
if (isSecret &&
|
|
45
|
+
// If bigint in valid range [1, n), convert to bytes
|
|
46
|
+
if (isSecret && entropy >= 1n && entropy < secp256k1.Point.Fn.ORDER) {
|
|
42
47
|
this.#bytes = Secp256k1SecretKey.toBytes(entropy);
|
|
43
48
|
this.#seed = entropy;
|
|
44
49
|
}
|
|
45
50
|
if (!this.#bytes || this.#bytes.length !== 32) {
|
|
46
51
|
throw new SecretKeyError('Invalid bytes: must be a valid 32-byte secret key', 'CONSTRUCTOR_ERROR');
|
|
47
52
|
}
|
|
48
|
-
if (!this.#seed ||
|
|
49
|
-
throw new SecretKeyError('Invalid seed: must
|
|
53
|
+
if (!this.#seed || this.#seed < 1n || this.#seed >= secp256k1.Point.Fn.ORDER) {
|
|
54
|
+
throw new SecretKeyError('Invalid seed: must be valid bigint', 'CONSTRUCTOR_ERROR');
|
|
50
55
|
}
|
|
51
56
|
// Set the secret key multibase
|
|
52
57
|
this.#multibase = this.encode();
|
|
53
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Zeros out secret key material from memory.
|
|
61
|
+
* The instance should not be used after calling this method.
|
|
62
|
+
*/
|
|
63
|
+
destroy() {
|
|
64
|
+
if (this.#bytes)
|
|
65
|
+
this.#bytes.fill(0);
|
|
66
|
+
this.#seed = undefined;
|
|
67
|
+
this.#multibase = '';
|
|
68
|
+
}
|
|
54
69
|
/**
|
|
55
70
|
* Get the secret key entropy as a byte array.
|
|
56
71
|
* @returns {KeyBytes} The secret key bytes as a Uint8Array
|
|
@@ -90,75 +105,71 @@ export class Secp256k1SecretKey {
|
|
|
90
105
|
* @returns {string} The secret key in BIP340 multibase format.
|
|
91
106
|
*/
|
|
92
107
|
encode() {
|
|
93
|
-
// Convert Uint8Array bytes to an Array
|
|
94
108
|
const secretKeyBytes = Array.from(this.bytes);
|
|
95
|
-
if (secretKeyBytes.length !== 32) {
|
|
96
|
-
throw new SecretKeyError('Invalid secret key: must be a valid 32-byte secret key', 'ENCODE_MULTIBASE_ERROR');
|
|
97
|
-
}
|
|
98
|
-
// Convert prefix to an array
|
|
99
109
|
const mbaseBytes = Array.from(BIP340_SECRET_KEY_MULTIBASE_PREFIX);
|
|
100
|
-
// Push the secret key bytes at the end of the prefix
|
|
101
110
|
mbaseBytes.push(...secretKeyBytes);
|
|
102
|
-
|
|
103
|
-
return base58btc.encode(Uint8Array.from(mbaseBytes));
|
|
111
|
+
return 'z' + base58.encode(Uint8Array.from(mbaseBytes));
|
|
104
112
|
}
|
|
105
113
|
/**
|
|
106
114
|
* Checks if this secret key is equal to another.
|
|
107
|
-
* @param {
|
|
115
|
+
* @param {SecretKey} other The other secret key
|
|
108
116
|
* @returns {boolean} True if the private keys are equal, false otherwise
|
|
109
117
|
*/
|
|
110
118
|
equals(other) {
|
|
111
|
-
|
|
112
|
-
return this.hex === other.hex;
|
|
119
|
+
return equalBytes(this.bytes, other.bytes);
|
|
113
120
|
}
|
|
114
121
|
/**
|
|
115
122
|
* Computes the public key from the secret key bytes.
|
|
116
123
|
* @returns {CompressedSecp256k1PublicKey} The computed public key
|
|
117
124
|
*/
|
|
118
125
|
computePublicKey() {
|
|
119
|
-
|
|
120
|
-
const publicKeyBytes = tinysecp.pointFromScalar(this.bytes, true);
|
|
121
|
-
// If no public key, throw error
|
|
122
|
-
if (!publicKeyBytes) {
|
|
123
|
-
throw new SecretKeyError('Invalid compute: failed to derive public key', 'COMPUTE_PUBLIC_KEY_ERROR');
|
|
124
|
-
}
|
|
125
|
-
// If public key is not compressed, throw error
|
|
126
|
-
if (publicKeyBytes.length !== 33) {
|
|
127
|
-
throw new SecretKeyError('Invalid compute: public key not compressed format', 'COMPUTE_PUBLIC_KEY_ERROR');
|
|
128
|
-
}
|
|
129
|
-
return new CompressedSecp256k1PublicKey(publicKeyBytes);
|
|
126
|
+
return new CompressedSecp256k1PublicKey(secp256k1.getPublicKey(this.bytes));
|
|
130
127
|
}
|
|
131
128
|
/**
|
|
132
|
-
*
|
|
133
|
-
*
|
|
129
|
+
* Safe JSON representation. Does not expose secret material.
|
|
130
|
+
* Called implicitly by JSON.stringify(). Use exportJSON() for full serialization.
|
|
134
131
|
*/
|
|
135
132
|
toJSON() {
|
|
133
|
+
return { type: 'Secp256k1SecretKey' };
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Exports the secret key as a JSON object. Contains sensitive material.
|
|
137
|
+
* @returns {SecretKeyObject} The secret key as a JSON object
|
|
138
|
+
*/
|
|
139
|
+
exportJSON() {
|
|
136
140
|
return {
|
|
137
141
|
bytes: Array.from(this.bytes),
|
|
138
142
|
seed: this.seed.toString(),
|
|
139
143
|
hex: this.hex,
|
|
140
144
|
};
|
|
141
145
|
}
|
|
146
|
+
/** @override Prevents secret material from appearing in console.log */
|
|
147
|
+
toString() {
|
|
148
|
+
return '[Secp256k1SecretKey]';
|
|
149
|
+
}
|
|
150
|
+
/** @override Prevents secret material from appearing in Node.js inspect */
|
|
151
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
152
|
+
return '[Secp256k1SecretKey]';
|
|
153
|
+
}
|
|
142
154
|
/**
|
|
143
155
|
* Checks if the secret key is valid.
|
|
144
156
|
* @returns {boolean} True if the secret key is valid, false otherwise
|
|
145
157
|
*/
|
|
146
158
|
isValid() {
|
|
147
|
-
return
|
|
159
|
+
return secp256k1.utils.isValidSecretKey(this.bytes);
|
|
148
160
|
}
|
|
149
161
|
/**
|
|
150
162
|
* Checks if the public key is a valid secp256k1 point.
|
|
151
163
|
* @returns {boolean} True if the public key is valid, false otherwise
|
|
152
164
|
*/
|
|
153
165
|
hasValidPublicKey() {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
166
|
+
try {
|
|
167
|
+
this.computePublicKey();
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
158
171
|
return false;
|
|
159
172
|
}
|
|
160
|
-
// Return true if the computed public key equals the provided public key
|
|
161
|
-
return true;
|
|
162
173
|
}
|
|
163
174
|
/**
|
|
164
175
|
* Produce a signature over arbitrary data using schnorr or ecdsa.
|
|
@@ -171,13 +182,11 @@ export class Secp256k1SecretKey {
|
|
|
171
182
|
sign(data, opts) {
|
|
172
183
|
// Set default options if not provided
|
|
173
184
|
opts ??= { scheme: 'schnorr' };
|
|
174
|
-
// Sign ecdsa and return
|
|
175
185
|
if (opts.scheme === 'ecdsa') {
|
|
176
|
-
return
|
|
186
|
+
return secp256k1.sign(data, this.bytes);
|
|
177
187
|
}
|
|
178
|
-
// Sign schnorr and return
|
|
179
188
|
if (opts.scheme === 'schnorr') {
|
|
180
|
-
return
|
|
189
|
+
return schnorr.sign(data, this.bytes);
|
|
181
190
|
}
|
|
182
191
|
throw new SecretKeyError(`Invalid scheme: ${opts.scheme}.`, 'SIGN_ERROR', opts);
|
|
183
192
|
}
|
|
@@ -188,7 +197,7 @@ export class Secp256k1SecretKey {
|
|
|
188
197
|
*/
|
|
189
198
|
static decode(multibase) {
|
|
190
199
|
// Decode the public key multibase string
|
|
191
|
-
const decoded =
|
|
200
|
+
const decoded = base58.decode(multibase.slice(1));
|
|
192
201
|
// If the public key bytes are not 35 bytes, throw an error
|
|
193
202
|
if (decoded.length !== 34) {
|
|
194
203
|
throw new SecretKeyError('Invalid argument: must be 34 byte secretKeyMultibase', 'DECODE_MULTIBASE_ERROR');
|
|
@@ -212,20 +221,6 @@ export class Secp256k1SecretKey {
|
|
|
212
221
|
static fromJSON(json) {
|
|
213
222
|
return new Secp256k1SecretKey(new Uint8Array(json.bytes));
|
|
214
223
|
}
|
|
215
|
-
/**
|
|
216
|
-
* Converts a Secp256k1SecretKey or KeyBytes to a SchnorrKeyPair.
|
|
217
|
-
* @param {KeyBytes} bytes The secret key bytes
|
|
218
|
-
* @returns {SchnorrKeyPair} The SchnorrKeyPair object containing the public and private keys
|
|
219
|
-
* @throws {SecretKeyError} If the secret key is not valid
|
|
220
|
-
*/
|
|
221
|
-
static toKeyPair(bytes) {
|
|
222
|
-
// Create a new Secp256k1SecretKey from the bytes
|
|
223
|
-
const secretKey = new Secp256k1SecretKey(bytes);
|
|
224
|
-
// Compute the public key from the secret key
|
|
225
|
-
const publicKey = secretKey.computePublicKey();
|
|
226
|
-
// Create a new Pair from the public key and secret key
|
|
227
|
-
return new SchnorrKeyPair({ publicKey, secretKey });
|
|
228
|
-
}
|
|
229
224
|
/**
|
|
230
225
|
* Convert a bigint secret to secret key bytes.
|
|
231
226
|
* @param {KeyBytes} bytes The secret key bytes
|
|
@@ -243,42 +238,30 @@ export class Secp256k1SecretKey {
|
|
|
243
238
|
// Ensure it’s a valid 32-byte value in [1, n-1] and convert bigint to Uint8Array
|
|
244
239
|
const bytes = Uint8Array.from({ length: 32 }, (_, i) => Number(secret >> BigInt(8 * (31 - i)) & BigInt(0xff)));
|
|
245
240
|
// If bytes are not a valid secp256k1 secret key, throw error
|
|
246
|
-
if (!
|
|
241
|
+
if (!secp256k1.utils.isValidSecretKey(bytes)) {
|
|
247
242
|
throw new SecretKeyError('Invalid secret key: secret out of valid range', 'SET_PRIVATE_KEY_ERROR');
|
|
248
243
|
}
|
|
249
244
|
return new Uint8Array(bytes);
|
|
250
245
|
}
|
|
251
|
-
/**
|
|
252
|
-
* Creates a new Secp256k1SecretKey object from random bytes.
|
|
253
|
-
* @param {KeyBytes} bytes The secret key bytes
|
|
254
|
-
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
255
|
-
*/
|
|
256
|
-
static fromBytes(bytes) {
|
|
257
|
-
// Return a new Secp256k1SecretKey object
|
|
258
|
-
return new Secp256k1SecretKey(bytes);
|
|
259
|
-
}
|
|
260
246
|
/**
|
|
261
247
|
* Creates a new Secp256k1SecretKey object from a bigint secret.
|
|
262
248
|
* @param {bigint} bint The secret bigint
|
|
263
249
|
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
264
250
|
*/
|
|
265
251
|
static fromBigInt(bint) {
|
|
266
|
-
|
|
267
|
-
const hexsecret = bint.toString(16).padStart(64, '0');
|
|
268
|
-
// Convert the hex string to a Uint8Array
|
|
269
|
-
const bytes = new Uint8Array(hexsecret.match(/.{2}/g).map(byte => parseInt(byte, 16)));
|
|
270
|
-
// Return a new Secp256k1SecretKey object
|
|
271
|
-
return new Secp256k1SecretKey(bytes);
|
|
252
|
+
return new Secp256k1SecretKey(Secp256k1SecretKey.toBytes(bint));
|
|
272
253
|
}
|
|
273
254
|
/**
|
|
274
255
|
* Generates random secret key bytes.
|
|
275
256
|
* @returns {KeyBytes} Uint8Array of 32 random bytes.
|
|
276
257
|
*/
|
|
277
258
|
static random() {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
259
|
+
let byteArray;
|
|
260
|
+
// Retry until bytes fall in valid scalar range [1, n)
|
|
261
|
+
do {
|
|
262
|
+
byteArray = randomBytes(32);
|
|
263
|
+
} while (!secp256k1.utils.isValidSecretKey(byteArray));
|
|
264
|
+
return byteArray;
|
|
282
265
|
}
|
|
283
266
|
/**
|
|
284
267
|
* Creates a new Secp256k1SecretKey from random secret key bytes.
|
package/dist/esm/secret.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secret.js","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":"AAAA,OAAO,
|
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C,CAAC,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAChE,CAAC;QAEF,6DAA6D;QAC7D,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;YAC7C,MAAM,IAAI,cAAc,CACtB,+CAA+C,EAC/C,uBAAuB,CACxB,CAAC;QACJ,CAAC;QACD,OAAO,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,UAAU,CAAC,IAAY;QACnC,OAAO,IAAI,kBAAkB,CAAC,kBAAkB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IAClE,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM;QAClB,IAAI,SAAqB,CAAC;QAC1B,sDAAsD;QACtD,GAAG,CAAC;YACF,SAAS,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;QAC9B,CAAC,QAAQ,CAAC,SAAS,CAAC,KAAK,CAAC,gBAAgB,CAAC,SAAS,CAAC,EAAE;QACvD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,QAAQ;QACpB,+BAA+B;QAC/B,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QAElC,4EAA4E;QAC5E,OAAO,IAAI,kBAAkB,CAAC,WAAW,CAAC,CAAC;IAC7C,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,YAAY,CAAC,KAAe;QACxC,4EAA4E;QAC5E,OAAO,IAAI,kBAAkB,CAAC,KAAK,CAAC,CAAC,gBAAgB,EAAE,CAAC;IAC1D,CAAC;CACF"}
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/pair.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Hex, HexString, KeyBytes, SchnorrKeyPairObject } from '@did-btcr2/common';
|
|
1
|
+
import { Hex, HexString, KeyBytes, PublicKeyObject, SchnorrKeyPairObject } from '@did-btcr2/common';
|
|
2
2
|
import { CompressedSecp256k1PublicKey, PublicKey } from './public.js';
|
|
3
3
|
import { Secp256k1SecretKey, SecretKey } from './secret.js';
|
|
4
4
|
import { HexSchnorrKeyPair, MultibaseKeys, RawSchnorrKeyPair, SchnorrKeyPairParams } from './types.js';
|
|
@@ -51,6 +51,11 @@ export declare class SchnorrKeyPair implements KeyPair {
|
|
|
51
51
|
* @returns {CompressedSecp256k1PublicKey} The CompressedSecp256k1PublicKey object
|
|
52
52
|
*/
|
|
53
53
|
get publicKey(): CompressedSecp256k1PublicKey;
|
|
54
|
+
/**
|
|
55
|
+
* Whether this key pair contains a secret key.
|
|
56
|
+
* @returns {boolean} True if the secret key is present.
|
|
57
|
+
*/
|
|
58
|
+
get hasSecretKey(): boolean;
|
|
54
59
|
/**
|
|
55
60
|
* Get the `raw` bytes of each key in the SchnorrKeyPair.
|
|
56
61
|
* @returns {RawSchnorrKeyPair} JSON object with the SchnorrKeyPair raw bytes.
|
|
@@ -67,10 +72,19 @@ export declare class SchnorrKeyPair implements KeyPair {
|
|
|
67
72
|
*/
|
|
68
73
|
get multibase(): MultibaseKeys;
|
|
69
74
|
/**
|
|
70
|
-
* JSON representation
|
|
71
|
-
*
|
|
75
|
+
* Safe JSON representation. Only includes the public key.
|
|
76
|
+
* Called implicitly by JSON.stringify(). Use exportJSON() for full serialization.
|
|
77
|
+
* @returns {{ publicKey: PublicKeyObject }} The JSON representation of the public key
|
|
78
|
+
*/
|
|
79
|
+
toJSON(): {
|
|
80
|
+
publicKey: PublicKeyObject;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Exports the full key pair as a JSON object. Contains sensitive material.
|
|
84
|
+
* @returns {SchnorrKeyPairObject} The key pair as a JSON object
|
|
85
|
+
* @throws {KeyPairError} If the secret key is not available
|
|
72
86
|
*/
|
|
73
|
-
|
|
87
|
+
exportJSON(): SchnorrKeyPairObject;
|
|
74
88
|
/**
|
|
75
89
|
* Static method creates a new Keys from a JSON object.
|
|
76
90
|
* @param {SchnorrKeyPairObject} keys The JSON object to initialize the Keys.
|
|
@@ -108,3 +122,4 @@ export declare class SchnorrKeyPair implements KeyPair {
|
|
|
108
122
|
*/
|
|
109
123
|
static generate(): SchnorrKeyPair;
|
|
110
124
|
}
|
|
125
|
+
//# sourceMappingURL=pair.d.ts.map
|
package/dist/types/pair.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pair.d.ts","sourceRoot":"","sources":["../../src/pair.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,SAAS,EACT,QAAQ,EAER,oBAAoB,EACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,4BAA4B,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEvG;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;IAE9B;;OAEG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;CAChC;AAED;;;;GAIG;AACH,qBAAa,cAAe,YAAW,OAAO;;
|
|
1
|
+
{"version":3,"file":"pair.d.ts","sourceRoot":"","sources":["../../src/pair.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,SAAS,EACT,QAAQ,EAER,eAAe,EACf,oBAAoB,EACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,4BAA4B,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,iBAAiB,EAAE,oBAA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