@did-btcr2/keypair 0.5.1 → 0.7.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/src/secret.ts CHANGED
@@ -14,14 +14,14 @@ import { getRandomValues } from 'crypto';
14
14
  import { base58btc } from 'multiformats/bases/base58';
15
15
  import * as tinysecp from 'tiny-secp256k1';
16
16
  import { SchnorrKeyPair } from './pair.js';
17
- import { PublicKey } from './public.js';
17
+ import { CompressedSecp256k1PublicKey } from './public.js';
18
18
 
19
19
  /**
20
- * Interface for the SecretKey class.
21
- * @interface ISecretKey
22
- * @type {ISecretKey}
20
+ * General SecretKey interface for the Secp256k1SecretKey class.
21
+ * @interface SecretKey
22
+ * @type {SecretKey}
23
23
  */
24
- export interface ISecretKey {
24
+ export interface SecretKey {
25
25
  /**
26
26
  * Get the secret key bytes.
27
27
  * @readonly @type {KeyBytes} The secret key bytes.
@@ -43,16 +43,16 @@ export interface ISecretKey {
43
43
 
44
44
  /**
45
45
  * Checks if this secret key is equal to another secret key.
46
- *
46
+ * @param {Secp256k1SecretKey} other The other secret key to compare.
47
47
  * @returns {boolean} True if the private keys are equal.
48
48
  */
49
- equals(other: SecretKey): boolean;
49
+ equals(other: Secp256k1SecretKey): boolean;
50
50
 
51
51
  /**
52
52
  * Uses the secret key to compute the corresponding public key.
53
- * @returns {KeyBytes} A new PublicKey object.
53
+ * @returns {CompressedSecp256k1PublicKey} The computed public key bytes.
54
54
  */
55
- computePublicKey(): KeyBytes;
55
+ computePublicKey(): CompressedSecp256k1PublicKey;
56
56
 
57
57
  /**
58
58
  * Checks if the secret key is valid.
@@ -62,8 +62,8 @@ export interface ISecretKey {
62
62
 
63
63
 
64
64
  /**
65
- * JSON representation of a SecretKey object.
66
- * @returns {SecretKeyObject} The SecretKey as a JSON object.
65
+ * JSON representation of a Secp256k1SecretKey object.
66
+ * @returns {SecretKeyObject} The Secp256k1SecretKey as a JSON object.
67
67
  */
68
68
  json(): SecretKeyObject;
69
69
  }
@@ -72,11 +72,11 @@ export interface ISecretKey {
72
72
  * Encapsulates a secp256k1 secret key
73
73
  * Provides get methods for different formats (raw, secret, point).
74
74
  * Provides helpers methods for comparison, serialization and publicKey generation.
75
- * @class SecretKey
76
- * @type {SecretKey}
77
- *
75
+ * @class Secp256k1SecretKey
76
+ * @type {Secp256k1SecretKey}
77
+ * @implements {SecretKey}
78
78
  */
79
- export class SecretKey implements ISecretKey {
79
+ export class Secp256k1SecretKey implements SecretKey {
80
80
  /** @type {KeyBytes} The entropy for the secret key as a byte array */
81
81
  private _bytes?: KeyBytes;
82
82
 
@@ -87,7 +87,7 @@ export class SecretKey implements ISecretKey {
87
87
  private _multibase: string;
88
88
 
89
89
  /**
90
- * Instantiates an instance of SecretKey.
90
+ * Instantiates an instance of Secp256k1SecretKey.
91
91
  * @param {Entropy} entropy bytes (Uint8Array) or secret (bigint)
92
92
  * @throws {SecretKeyError} If entropy is not provided, not a valid 32-byte secret key or not a valid bigint seed
93
93
  */
@@ -105,12 +105,12 @@ export class SecretKey implements ISecretKey {
105
105
  // If bytes and bytes are not length 32
106
106
  if (isBytes && entropy.length === 32) {
107
107
  this._bytes = entropy;
108
- this._seed = SecretKey.toSecret(entropy);
108
+ this._seed = Secp256k1SecretKey.toSecret(entropy);
109
109
  }
110
110
 
111
111
  // If secret and secret is not a valid bigint, throw error
112
112
  if (isSecret && !(entropy < 1n || entropy >= CURVE.n)) {
113
- this._bytes = SecretKey.toBytes(entropy);
113
+ this._bytes = Secp256k1SecretKey.toBytes(entropy);
114
114
  this._seed = entropy;
115
115
  }
116
116
 
@@ -197,19 +197,19 @@ export class SecretKey implements ISecretKey {
197
197
 
198
198
  /**
199
199
  * Checks if this secret key is equal to another.
200
- * @param {SecretKey} other The other secret key
200
+ * @param {Secp256k1SecretKey} other The other secret key
201
201
  * @returns {boolean} True if the private keys are equal, false otherwise
202
202
  */
203
- public equals(other: SecretKey): boolean {
203
+ public equals(other: Secp256k1SecretKey): boolean {
204
204
  // Compare the hex strings of the private keys
205
205
  return this.hex === other.hex;
206
206
  }
207
207
 
208
208
  /**
209
209
  * Computes the public key from the secret key bytes.
210
- * @returns {KeyBytes} The computed public key
210
+ * @returns {CompressedSecp256k1PublicKey} The computed public key
211
211
  */
212
- public computePublicKey(): KeyBytes {
212
+ public computePublicKey(): CompressedSecp256k1PublicKey {
213
213
  // Derive the public key from the secret key
214
214
  const publicKeyBytes = tinysecp.pointFromScalar(this.bytes, true);
215
215
 
@@ -229,7 +229,7 @@ export class SecretKey implements ISecretKey {
229
229
  );
230
230
  }
231
231
 
232
- return publicKeyBytes;
232
+ return new CompressedSecp256k1PublicKey(publicKeyBytes);
233
233
  }
234
234
 
235
235
  /**
@@ -254,16 +254,18 @@ export class SecretKey implements ISecretKey {
254
254
 
255
255
  /**
256
256
  * Checks if the public key is a valid secp256k1 point.
257
- * @param {PublicKey} pk The public key to validate
258
257
  * @returns {boolean} True if the public key is valid, false otherwise
259
258
  */
260
- public isValidPair(pk: PublicKey): boolean {
259
+ public hasValidPublicKey(): boolean {
260
+ // Compute the public key from the secret key and compress it
261
+ const pk = this.computePublicKey();
262
+
261
263
  // If the public key is not valid, return false
262
264
  if (!tinysecp.isPoint(pk.compressed)) {
263
265
  return false;
264
266
  }
265
267
 
266
- // Else return true
268
+ // Return true if the computed public key equals the provided public key
267
269
  return true;
268
270
  }
269
271
 
@@ -303,23 +305,23 @@ export class SecretKey implements ISecretKey {
303
305
  }
304
306
 
305
307
  /**
306
- * Creates a SecretKey object from a JSON object.
308
+ * Creates a Secp256k1SecretKey object from a JSON object.
307
309
  * @param {SecretKeyObject} json The JSON object containing the secret key bytes
308
- * @returns {SecretKey} A new SecretKey object
310
+ * @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
309
311
  */
310
- public static fromJSON(json: SecretKeyObject): SecretKey {
311
- return new SecretKey(new Uint8Array(json.bytes));
312
+ public static fromJSON(json: SecretKeyObject): Secp256k1SecretKey {
313
+ return new Secp256k1SecretKey(new Uint8Array(json.bytes));
312
314
  }
313
315
 
314
316
  /**
315
- * Converts a SecretKey or KeyBytes to a Pair.
316
- * @param {KeyBytes} bytes
317
+ * Converts a Secp256k1SecretKey or KeyBytes to a SchnorrKeyPair.
318
+ * @param {KeyBytes} bytes The secret key bytes
317
319
  * @returns {SchnorrKeyPair} The SchnorrKeyPair object containing the public and private keys
318
320
  * @throws {SecretKeyError} If the secret key is not valid
319
321
  */
320
322
  public static toKeyPair(bytes: KeyBytes): SchnorrKeyPair {
321
- // Create a new SecretKey from the bytes
322
- const secretKey = new SecretKey(bytes);
323
+ // Create a new Secp256k1SecretKey from the bytes
324
+ const secretKey = new Secp256k1SecretKey(bytes);
323
325
 
324
326
  // Compute the public key from the secret key
325
327
  const publicKey = secretKey.computePublicKey();
@@ -360,17 +362,17 @@ export class SecretKey implements ISecretKey {
360
362
  }
361
363
 
362
364
  /**
363
- * Creates a new SecretKey object from a bigint secret.
364
- * @param {bigint} secret The secret bigint
365
- * @returns {SecretKey} A new SecretKey object
365
+ * Creates a new Secp256k1SecretKey object from a bigint secret.
366
+ * @param {bigint} entropy The secret bigint
367
+ * @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
366
368
  */
367
- public static fromSecret(secret: bigint): SecretKey {
369
+ public static fromEntropy(entropy: bigint): Secp256k1SecretKey {
368
370
  // Convert the secret bigint to a hex string
369
- const hexsecret = secret.toString(16).padStart(64, '0');
371
+ const hexsecret = entropy.toString(16).padStart(64, '0');
370
372
  // Convert the hex string to a Uint8Array
371
373
  const privateKeyBytes = new Uint8Array(hexsecret.match(/.{2}/g)!.map(byte => parseInt(byte, 16)));
372
- // Return a new SecretKey object
373
- return new SecretKey(privateKeyBytes);
374
+ // Return a new Secp256k1SecretKey object
375
+ return new Secp256k1SecretKey(privateKeyBytes);
374
376
  }
375
377
 
376
378
  /**
@@ -387,24 +389,24 @@ export class SecretKey implements ISecretKey {
387
389
 
388
390
 
389
391
  /**
390
- * Creates a new SecretKey from random secret key bytes.
391
- * @returns {SecretKey} A new SecretKey object
392
+ * Creates a new Secp256k1SecretKey from random secret key bytes.
393
+ * @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
392
394
  */
393
- public static generate(): SecretKey {
395
+ public static generate(): Secp256k1SecretKey {
394
396
  // Generate empty 32-byte array
395
397
  const randomBytes = this.random();
396
398
 
397
399
  // Use the getRandomValues function to fill the byteArray with random values
398
- return new SecretKey(randomBytes);
400
+ return new Secp256k1SecretKey(randomBytes);
399
401
  }
400
402
 
401
403
  /**
402
404
  * Generates a public key from the given secret key bytes.
403
405
  * @param {KeyBytes} bytes The secret key bytes
404
- * @returns {KeyBytes} The computed public key bytes
406
+ * @returns {CompressedSecp256k1PublicKey} The computed public key bytes
405
407
  */
406
- public static getPublicKey(bytes: KeyBytes): KeyBytes {
407
- // Create a new SecretKey from the bytes and compute the public key
408
- return new SecretKey(bytes).computePublicKey();
408
+ public static getPublicKey(bytes: KeyBytes): CompressedSecp256k1PublicKey {
409
+ // Create a new Secp256k1SecretKey from the bytes and compute the public key
410
+ return new Secp256k1SecretKey(bytes).computePublicKey();
409
411
  }
410
412
  }
package/src/types.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { KeyBytes } from '@did-btcr2/common';
2
+ import { CompressedSecp256k1PublicKey } from './public.js';
3
+ import { Secp256k1SecretKey } from './secret.js';
4
+
5
+ export type RawSchnorrKeyPair = {
6
+ public: KeyBytes;
7
+ secret?: KeyBytes
8
+ }
9
+
10
+ /** Params for the {@link SchnorrKeyPair} constructor */
11
+ export interface SchnorrKeyPairParams {
12
+ secretKey?: Secp256k1SecretKey | KeyBytes;
13
+ publicKey?: CompressedSecp256k1PublicKey | KeyBytes;
14
+ }
15
+
16
+ export interface MultibaseKeys {
17
+ publicKeyMultibase: string;
18
+ secretKeyMultibase: string
19
+ }