@did-btcr2/keypair 0.9.1 → 0.11.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.
@@ -1,8 +1,9 @@
1
- import { BIP340_PUBLIC_KEY_MULTIBASE_PREFIX, BIP340_PUBLIC_KEY_MULTIBASE_PREFIX_HASH, CURVE, PublicKeyError } from '@did-btcr2/common';
2
- import { sha256 } from '@noble/hashes/sha2';
3
- import { base58btc } from 'multiformats/bases/base58';
4
- import * as tinysecp from 'tiny-secp256k1';
5
- import { Secp256k1SecretKey } from './secret.js';
1
+ import { PublicKeyError } from '@did-btcr2/common';
2
+ /** Fixed public key header bytes per the Data Integrity BIP340 Cryptosuite spec: [0xe7, 0x01] */
3
+ export const BIP340_PUBLIC_KEY_MULTIBASE_PREFIX = new Uint8Array([0xe7, 0x01]);
4
+ import { secp256k1, schnorr } from '@noble/curves/secp256k1.js';
5
+ import { timingSafeEqual } from 'crypto';
6
+ import { base58 } from '@scure/base';
6
7
  /**
7
8
  * Encapsulates a secp256k1 public key compliant to BIP-340 BIP schnorr signature scheme.
8
9
  * Provides get methods for different formats (compressed, x-only, multibase).
@@ -11,9 +12,13 @@ import { Secp256k1SecretKey } from './secret.js';
11
12
  * @type {CompressedSecp256k1PublicKey}
12
13
  */
13
14
  export class CompressedSecp256k1PublicKey {
14
- /** @type {KeyBytes} The public key bytes */
15
+ /**
16
+ * The public key bytes
17
+ **/
15
18
  #bytes;
16
- /** @type {MultibaseObject} The public key as a MultibaseObject */
19
+ /**
20
+ * The public key as a MultibaseObject
21
+ */
17
22
  #multibase = {
18
23
  prefix: BIP340_PUBLIC_KEY_MULTIBASE_PREFIX,
19
24
  key: [],
@@ -34,11 +39,11 @@ export class CompressedSecp256k1PublicKey {
34
39
  throw new PublicKeyError('Invalid argument: byte length must be 33 (compressed)', 'CONSTRUCTOR_ERROR', { keyBytes });
35
40
  }
36
41
  // Validate the point is on curve and in compressed form
37
- if (!tinysecp.isPoint(keyBytes)) {
42
+ if (!secp256k1.utils.isValidPublicKey(keyBytes)) {
38
43
  throw new PublicKeyError('Invalid argument: not a valid secp256k1 compressed point', 'CONSTRUCTOR_ERROR', { keyBytes });
39
44
  }
40
- // Set the bytes
41
- this.#bytes = keyBytes;
45
+ // Defensive copy — caller cannot mutate internal state
46
+ this.#bytes = new Uint8Array(keyBytes);
42
47
  // Set multibase
43
48
  this.#multibase.encoded = this.encode();
44
49
  this.#multibase.key = [...this.#multibase.prefix, ...this.compressed];
@@ -57,8 +62,7 @@ export class CompressedSecp256k1PublicKey {
57
62
  * @returns {Uint8Array} The 65-byte uncompressed public key (0x04, x, y).
58
63
  */
59
64
  get uncompressed() {
60
- const uncompressed = this.liftX();
61
- return uncompressed;
65
+ return secp256k1.Point.fromBytes(this.compressed).toBytes(false);
62
66
  }
63
67
  /**
64
68
  * X-only (32-byte) view of the public key per BIP-340.
@@ -107,8 +111,11 @@ export class CompressedSecp256k1PublicKey {
107
111
  * @returns {MultibaseObject} An object containing the multibase bytes, address and prefix.
108
112
  */
109
113
  get multibase() {
110
- const multibase = this.#multibase;
111
- return multibase;
114
+ return {
115
+ prefix: new Uint8Array(this.#multibase.prefix),
116
+ key: [...this.#multibase.key],
117
+ encoded: this.#multibase.encoded
118
+ };
112
119
  }
113
120
  /**
114
121
  * Returns the raw public key as a hex string.
@@ -140,40 +147,17 @@ export class CompressedSecp256k1PublicKey {
140
147
  * @returns {KeyBytes} The decoded public key: prefix and public key bytes
141
148
  */
142
149
  decode() {
143
- // Decode the public key multibase string
144
- const decoded = base58btc.decode(this.multibase.encoded);
145
- // If the public key bytes are not 35 bytes, throw an error
146
- if (decoded.length !== 35) {
147
- throw new PublicKeyError('Invalid argument: must be 35 byte publicKeyMultibase', 'DECODE_MULTIBASE_ERROR');
148
- }
149
- // Grab the prefix bytes
150
- const prefix = decoded.slice(0, 2);
151
- // Compute the prefix hash
152
- const prefixHash = Buffer.from(sha256(prefix)).toString('hex');
153
- // If the prefix hash does not equal the BIP340 prefix hash, throw an error
154
- if (prefixHash !== BIP340_PUBLIC_KEY_MULTIBASE_PREFIX_HASH) {
155
- throw new PublicKeyError(`Invalid prefix: malformed multibase prefix ${prefix}`, 'DECODE_MULTIBASE_ERROR');
156
- }
157
- // Return the decoded public key bytes
158
- return decoded;
150
+ return base58.decode(this.multibase.encoded.slice(1));
159
151
  }
160
152
  /**
161
153
  * Encodes compressed secp256k1 public key from bytes to BIP340 multibase format.
162
154
  * @returns {string} The public key encoded in base-58-btc multibase format.
163
155
  */
164
156
  encode() {
165
- // Convert public key bytes to an array
166
157
  const pk = Array.from(this.compressed);
167
- // Ensure the public key is 33-byte secp256k1 compressed public key
168
- if (pk.length !== 33) {
169
- throw new PublicKeyError('Invalid argument: must be 33-byte (compressed) public key', 'ENCODE_MULTIBASE_ERROR');
170
- }
171
- // Convert prefix to an array
172
158
  const publicKeyMultibase = Array.from(BIP340_PUBLIC_KEY_MULTIBASE_PREFIX);
173
- // Push the public key bytes at the end of the prefix
174
159
  publicKeyMultibase.push(...pk);
175
- // Encode the bytes in base58btc format and return
176
- return base58btc.encode(Uint8Array.from(publicKeyMultibase));
160
+ return 'z' + base58.encode(Uint8Array.from(publicKeyMultibase));
177
161
  }
178
162
  /**
179
163
  * Verify a signature using schnorr or ecdsa.
@@ -184,29 +168,30 @@ export class CompressedSecp256k1PublicKey {
184
168
  * @returns {boolean} If the signature is valid against the public key.
185
169
  */
186
170
  verify(signature, data, opts) {
171
+ // Default to schnorr scheme
187
172
  opts ??= { scheme: 'schnorr' };
188
- // Verify the signature depending on the scheme and return the result
189
173
  if (opts.scheme === 'ecdsa') {
190
- return tinysecp.verify(data, this.compressed, signature);
174
+ return secp256k1.verify(signature, data, this.compressed);
191
175
  }
192
176
  else if (opts.scheme === 'schnorr') {
193
- return tinysecp.verifySchnorr(data, this.x, signature);
177
+ return schnorr.verify(signature, data, this.x);
194
178
  }
179
+ // If scheme is neither ecdsa nor schnorr, throw an error
195
180
  throw new PublicKeyError(`Invalid scheme: ${opts.scheme}.`, 'VERIFY_SIGNATURE_ERROR', opts);
196
181
  }
197
182
  /**
198
183
  * Compares this public key to another public key.
199
- * @param {CompressedSecp256k1PublicKey} other The other public key to compare
184
+ * @param {PublicKey} other The other public key to compare
200
185
  * @returns {boolean} True if the public keys are equal, false otherwise.
201
186
  */
202
187
  equals(other) {
203
- return this.hex === other.hex;
188
+ return timingSafeEqual(this.compressed, other.compressed);
204
189
  }
205
190
  /**
206
191
  * JSON representation of a CompressedSecp256k1PublicKey object.
207
192
  * @returns {PublicKeyObject} The CompressedSecp256k1PublicKey as a JSON object.
208
193
  */
209
- json() {
194
+ toJSON() {
210
195
  return {
211
196
  hex: this.hex,
212
197
  multibase: this.multibase,
@@ -217,61 +202,6 @@ export class CompressedSecp256k1PublicKey {
217
202
  },
218
203
  };
219
204
  }
220
- /**
221
- * Computes modular exponentiation: (base^exp) % mod.
222
- * Used for computing modular square roots.
223
- * @param {bigint} base The base value
224
- * @param {bigint} exp The exponent value
225
- * @param {bigint} mod The modulus value
226
- * @returns {bigint} The result of the modular exponentiation
227
- */
228
- modPow(base, exp, mod) {
229
- let result = 1n;
230
- while (exp > 0n) {
231
- if (exp & 1n)
232
- result = (result * base) % mod;
233
- base = (base * base) % mod;
234
- exp >>= 1n;
235
- }
236
- return result;
237
- }
238
- ;
239
- /**
240
- * Computes `sqrt(a) mod p` using Tonelli-Shanks algorithm.
241
- * This finds `y` such that `y^2 ≡ a mod p`.
242
- * @param {bigint} a The value to find the square root of
243
- * @param {bigint} p The prime modulus
244
- * @returns {bigint} The square root of `a` mod `p`
245
- */
246
- sqrtMod(a, p) {
247
- return this.modPow(a, (p + 1n) >> 2n, p);
248
- }
249
- ;
250
- /**
251
- * Lifts a 32-byte x-only coordinate into a full secp256k1 point (x, y).
252
- * @param xBytes 32-byte x-coordinate
253
- * @returns {Uint8Array} 65-byte uncompressed public key (starts with `0x04`)
254
- */
255
- liftX() {
256
- // Ensure x-coordinate is 32 bytes
257
- if (this.x.length !== 32) {
258
- throw new PublicKeyError('Invalid argument: x-coordinate length must be 32 bytes', 'LIFT_X_ERROR');
259
- }
260
- // Convert x from Uint8Array → BigInt
261
- const x = BigInt('0x' + Buffer.from(this.x).toString('hex'));
262
- if (x <= 0n || x >= CURVE.p) {
263
- throw new PublicKeyError('Invalid conversion: x out of range as BigInt', 'LIFT_X_ERROR');
264
- }
265
- // Compute y² = x³ + 7 mod p
266
- const ySquared = BigInt((x ** 3n + CURVE.b) % CURVE.p);
267
- // Compute y (do not enforce parity)
268
- const y = this.sqrtMod(ySquared, CURVE.p);
269
- // Convert x and y to Uint8Array
270
- const yBytes = Buffer.from(y.toString(16).padStart(64, '0'), 'hex');
271
- // Return 65-byte uncompressed public key: `0x04 || x || y`
272
- return new Uint8Array(Buffer.concat([Buffer.from([0x04]), Buffer.from(this.x), yBytes]));
273
- }
274
- ;
275
205
  /**
276
206
  * Static method to validate a public key.
277
207
  * @param {Hex} pk The public key in hex (Uint8Array or string) format.
@@ -286,53 +216,13 @@ export class CompressedSecp256k1PublicKey {
286
216
  return false;
287
217
  }
288
218
  }
289
- /**
290
- * Returns the point of the public key.
291
- * @param {Hex} pk The public key in hex (Uint8Array or string) format.
292
- * @returns {Point} The point of the public key.
293
- * @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
294
- */
295
- static point(pk) {
296
- // If the public key is a hex string, convert it to a CompressedSecp256k1PublicKey object and return the point
297
- if (typeof pk === 'string' && /^[0-9a-fA-F]+$/.test(pk)) {
298
- const publicKey = new CompressedSecp256k1PublicKey(Buffer.from(pk, 'hex'));
299
- return publicKey.point;
300
- }
301
- // If the public key is a byte array or ArrayBuffer, convert it to a CompressedSecp256k1PublicKey object and return the point
302
- if (pk instanceof Uint8Array || ArrayBuffer.isView(pk)) {
303
- const publicKey = new CompressedSecp256k1PublicKey(pk);
304
- return publicKey.point;
305
- }
306
- // If the public key is neither a hex string nor a byte array, throw an error
307
- throw new PublicKeyError('Invalid publicKey: must be a hex string or byte array', 'POINT_ERROR', { publicKey: pk });
308
- }
309
219
  /**
310
220
  * Creates a CompressedSecp256k1PublicKey object from a JSON representation.
311
221
  * @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
312
222
  * @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
313
223
  */
314
224
  static fromJSON(json) {
315
- json.point.x.unshift(json.point.parity);
316
- return new CompressedSecp256k1PublicKey(Uint8Array.from(json.point.x));
317
- }
318
- /**
319
- * Computes the deterministic public key for a given secret key.
320
- * @param {Secp256k1SecretKey | KeyBytes} sk The Secp256k1SecretKey object or the secret key bytes
321
- * @returns {CompressedSecp256k1PublicKey} A new CompressedSecp256k1PublicKey object
322
- */
323
- static fromSecretKey(sk) {
324
- // If the secret key is a Secp256k1SecretKey object, get the raw bytes else use the bytes
325
- const bytes = sk instanceof Secp256k1SecretKey ? sk.bytes : sk;
326
- // Throw error if the secret key is not 32 bytes
327
- if (bytes.length !== 32) {
328
- throw new PublicKeyError('Invalid arg: must be 32 byte secret key', 'FROM_SECRET_KEY_ERROR');
329
- }
330
- // Compute the public key from the secret key
331
- const secret = sk instanceof Secp256k1SecretKey
332
- ? sk
333
- : new Secp256k1SecretKey(sk);
334
- // Return a new CompressedSecp256k1PublicKey object
335
- return secret.computePublicKey();
225
+ return new CompressedSecp256k1PublicKey(Uint8Array.from([json.point.parity, ...json.point.x]));
336
226
  }
337
227
  }
338
228
  //# sourceMappingURL=public.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"public.js","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kCAAkC,EAClC,uCAAuC,EAEvC,KAAK,EAIL,cAAc,EAEf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;A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AAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACjG,CAAC;CAEF"}
@@ -1,10 +1,14 @@
1
- import { BIP340_SECRET_KEY_MULTIBASE_PREFIX, BIP340_SECRET_KEY_MULTIBASE_PREFIX_HASH, CURVE, SecretKeyError } from '@did-btcr2/common';
1
+ import { SecretKeyError } from '@did-btcr2/common';
2
2
  import { sha256 } from '@noble/hashes/sha2';
3
- import { getRandomValues, randomBytes } from 'crypto';
4
- import { base58btc } from 'multiformats/bases/base58';
5
- import * as tinysecp from 'tiny-secp256k1';
6
- import { SchnorrKeyPair } from './pair.js';
3
+ import { bytesToHex } from '@noble/hashes/utils';
4
+ import { secp256k1, schnorr } from '@noble/curves/secp256k1.js';
5
+ import { getRandomValues, timingSafeEqual } from 'crypto';
6
+ import { base58 } from '@scure/base';
7
7
  import { CompressedSecp256k1PublicKey } from './public.js';
8
+ /** Fixed secret key header bytes per the Data Integrity BIP340 Cryptosuite spec: [0x81, 0x26] */
9
+ const BIP340_SECRET_KEY_MULTIBASE_PREFIX = new Uint8Array([0x81, 0x26]);
10
+ /** Hash of the BIP-340 Multikey secret key prefix */
11
+ const BIP340_SECRET_KEY_MULTIBASE_PREFIX_HASH = bytesToHex(sha256(BIP340_SECRET_KEY_MULTIBASE_PREFIX));
8
12
  /**
9
13
  * Encapsulates a secp256k1 secret key
10
14
  * Provides get methods for different formats (raw, secret, point).
@@ -32,25 +36,35 @@ export class Secp256k1SecretKey {
32
36
  if (!isBytes && !isSecret) {
33
37
  throw new SecretKeyError('Invalid entropy: must be a valid byte array (32) or bigint', 'CONSTRUCTOR_ERROR');
34
38
  }
35
- // If bytes and bytes are not length 32
39
+ // If bytes and length is 32, defensive-copy and derive seed
36
40
  if (isBytes && entropy.length === 32) {
37
- this.#bytes = entropy;
38
- this.#seed = Secp256k1SecretKey.toSecret(entropy);
41
+ this.#bytes = new Uint8Array(entropy);
42
+ this.#seed = Secp256k1SecretKey.toSecret(this.#bytes);
39
43
  }
40
- // If secret and secret is not a valid bigint, throw error
41
- if (isSecret && !(entropy < 1n || entropy >= CURVE.n)) {
44
+ // If bigint in valid range [1, n), convert to bytes
45
+ if (isSecret && entropy >= 1n && entropy < secp256k1.Point.Fn.ORDER) {
42
46
  this.#bytes = Secp256k1SecretKey.toBytes(entropy);
43
47
  this.#seed = entropy;
44
48
  }
45
49
  if (!this.#bytes || this.#bytes.length !== 32) {
46
50
  throw new SecretKeyError('Invalid bytes: must be a valid 32-byte secret key', 'CONSTRUCTOR_ERROR');
47
51
  }
48
- if (!this.#seed || (this.#seed < 1n || this.#seed >= CURVE.n)) {
49
- throw new SecretKeyError('Invalid seed: must must be valid bigint', 'CONSTRUCTOR_ERROR');
52
+ if (!this.#seed || this.#seed < 1n || this.#seed >= secp256k1.Point.Fn.ORDER) {
53
+ throw new SecretKeyError('Invalid seed: must be valid bigint', 'CONSTRUCTOR_ERROR');
50
54
  }
51
55
  // Set the secret key multibase
52
56
  this.#multibase = this.encode();
53
57
  }
58
+ /**
59
+ * Zeros out secret key material from memory.
60
+ * The instance should not be used after calling this method.
61
+ */
62
+ destroy() {
63
+ if (this.#bytes)
64
+ this.#bytes.fill(0);
65
+ this.#seed = undefined;
66
+ this.#multibase = '';
67
+ }
54
68
  /**
55
69
  * Get the secret key entropy as a byte array.
56
70
  * @returns {KeyBytes} The secret key bytes as a Uint8Array
@@ -90,75 +104,71 @@ export class Secp256k1SecretKey {
90
104
  * @returns {string} The secret key in BIP340 multibase format.
91
105
  */
92
106
  encode() {
93
- // Convert Uint8Array bytes to an Array
94
107
  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
108
  const mbaseBytes = Array.from(BIP340_SECRET_KEY_MULTIBASE_PREFIX);
100
- // Push the secret key bytes at the end of the prefix
101
109
  mbaseBytes.push(...secretKeyBytes);
102
- // Encode the bytes in base58btc format and return
103
- return base58btc.encode(Uint8Array.from(mbaseBytes));
110
+ return 'z' + base58.encode(Uint8Array.from(mbaseBytes));
104
111
  }
105
112
  /**
106
113
  * Checks if this secret key is equal to another.
107
- * @param {Secp256k1SecretKey} other The other secret key
114
+ * @param {SecretKey} other The other secret key
108
115
  * @returns {boolean} True if the private keys are equal, false otherwise
109
116
  */
110
117
  equals(other) {
111
- // Compare the hex strings of the private keys
112
- return this.hex === other.hex;
118
+ return timingSafeEqual(this.bytes, other.bytes);
113
119
  }
114
120
  /**
115
121
  * Computes the public key from the secret key bytes.
116
122
  * @returns {CompressedSecp256k1PublicKey} The computed public key
117
123
  */
118
124
  computePublicKey() {
119
- // Derive the public key from the secret key
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);
125
+ return new CompressedSecp256k1PublicKey(secp256k1.getPublicKey(this.bytes));
130
126
  }
131
127
  /**
132
- * Converts the secret key to a JSON object.
128
+ * Safe JSON representation. Does not expose secret material.
129
+ * Called implicitly by JSON.stringify(). Use exportJSON() for full serialization.
130
+ */
131
+ toJSON() {
132
+ return { type: 'Secp256k1SecretKey' };
133
+ }
134
+ /**
135
+ * Exports the secret key as a JSON object. Contains sensitive material.
133
136
  * @returns {SecretKeyObject} The secret key as a JSON object
134
137
  */
135
- json() {
138
+ exportJSON() {
136
139
  return {
137
140
  bytes: Array.from(this.bytes),
138
141
  seed: this.seed.toString(),
139
142
  hex: this.hex,
140
143
  };
141
144
  }
145
+ /** @override Prevents secret material from appearing in console.log */
146
+ toString() {
147
+ return '[Secp256k1SecretKey]';
148
+ }
149
+ /** @override Prevents secret material from appearing in Node.js inspect */
150
+ [Symbol.for('nodejs.util.inspect.custom')]() {
151
+ return '[Secp256k1SecretKey]';
152
+ }
142
153
  /**
143
154
  * Checks if the secret key is valid.
144
155
  * @returns {boolean} True if the secret key is valid, false otherwise
145
156
  */
146
157
  isValid() {
147
- return tinysecp.isPrivate(this.bytes);
158
+ return secp256k1.utils.isValidSecretKey(this.bytes);
148
159
  }
149
160
  /**
150
161
  * Checks if the public key is a valid secp256k1 point.
151
162
  * @returns {boolean} True if the public key is valid, false otherwise
152
163
  */
153
164
  hasValidPublicKey() {
154
- // Compute the public key from the secret key and compress it
155
- const pk = this.computePublicKey();
156
- // If the public key is not valid, return false
157
- if (!tinysecp.isPoint(pk.compressed)) {
165
+ try {
166
+ this.computePublicKey();
167
+ return true;
168
+ }
169
+ catch {
158
170
  return false;
159
171
  }
160
- // Return true if the computed public key equals the provided public key
161
- return true;
162
172
  }
163
173
  /**
164
174
  * Produce a signature over arbitrary data using schnorr or ecdsa.
@@ -171,13 +181,11 @@ export class Secp256k1SecretKey {
171
181
  sign(data, opts) {
172
182
  // Set default options if not provided
173
183
  opts ??= { scheme: 'schnorr' };
174
- // Sign ecdsa and return
175
184
  if (opts.scheme === 'ecdsa') {
176
- return tinysecp.sign(data, this.bytes);
185
+ return secp256k1.sign(data, this.bytes);
177
186
  }
178
- // Sign schnorr and return
179
187
  if (opts.scheme === 'schnorr') {
180
- return tinysecp.signSchnorr(data, this.bytes, randomBytes(32));
188
+ return schnorr.sign(data, this.bytes);
181
189
  }
182
190
  throw new SecretKeyError(`Invalid scheme: ${opts.scheme}.`, 'SIGN_ERROR', opts);
183
191
  }
@@ -188,7 +196,7 @@ export class Secp256k1SecretKey {
188
196
  */
189
197
  static decode(multibase) {
190
198
  // Decode the public key multibase string
191
- const decoded = base58btc.decode(multibase);
199
+ const decoded = base58.decode(multibase.slice(1));
192
200
  // If the public key bytes are not 35 bytes, throw an error
193
201
  if (decoded.length !== 34) {
194
202
  throw new SecretKeyError('Invalid argument: must be 34 byte secretKeyMultibase', 'DECODE_MULTIBASE_ERROR');
@@ -212,20 +220,6 @@ export class Secp256k1SecretKey {
212
220
  static fromJSON(json) {
213
221
  return new Secp256k1SecretKey(new Uint8Array(json.bytes));
214
222
  }
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
223
  /**
230
224
  * Convert a bigint secret to secret key bytes.
231
225
  * @param {KeyBytes} bytes The secret key bytes
@@ -243,42 +237,30 @@ export class Secp256k1SecretKey {
243
237
  // Ensure it’s a valid 32-byte value in [1, n-1] and convert bigint to Uint8Array
244
238
  const bytes = Uint8Array.from({ length: 32 }, (_, i) => Number(secret >> BigInt(8 * (31 - i)) & BigInt(0xff)));
245
239
  // If bytes are not a valid secp256k1 secret key, throw error
246
- if (!tinysecp.isPrivate(bytes)) {
240
+ if (!secp256k1.utils.isValidSecretKey(bytes)) {
247
241
  throw new SecretKeyError('Invalid secret key: secret out of valid range', 'SET_PRIVATE_KEY_ERROR');
248
242
  }
249
243
  return new Uint8Array(bytes);
250
244
  }
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
245
  /**
261
246
  * Creates a new Secp256k1SecretKey object from a bigint secret.
262
247
  * @param {bigint} bint The secret bigint
263
248
  * @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
264
249
  */
265
250
  static fromBigInt(bint) {
266
- // Convert the secret bigint to a hex string
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);
251
+ return new Secp256k1SecretKey(Secp256k1SecretKey.toBytes(bint));
272
252
  }
273
253
  /**
274
254
  * Generates random secret key bytes.
275
255
  * @returns {KeyBytes} Uint8Array of 32 random bytes.
276
256
  */
277
257
  static random() {
278
- // Generate empty 32-byte array
279
258
  const byteArray = new Uint8Array(32);
280
- // Use the getRandomValues function to fill the byteArray with random values
281
- return getRandomValues(byteArray);
259
+ // Retry until bytes fall in valid scalar range [1, n)
260
+ do {
261
+ getRandomValues(byteArray);
262
+ } while (!secp256k1.utils.isValidSecretKey(byteArray));
263
+ return byteArray;
282
264
  }
283
265
  /**
284
266
  * Creates a new Secp256k1SecretKey from random secret key bytes.
@@ -1 +1 @@
1
- {"version":3,"file":"secret.js","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kCAAkC,EAClC,uCAAuC,EAEvC,KAAK,EAIL,cAAc,EAGf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,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,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,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;QACrC,sDAAsD;QACtD,GAAG,CAAC;YACF,eAAe,CAAC,SAAS,CAAC,CAAC;QAC7B,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"}
@@ -2,3 +2,4 @@ export * from './pair.js';
2
2
  export * from './secret.js';
3
3
  export * from './public.js';
4
4
  export * from './types.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -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 of a Keys.
71
- * @returns {SchnorrKeyPairObject} The Keys as a JSON object
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
- json(): SchnorrKeyPairObject;
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