@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/README.md +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/pair.js +76 -65
- package/dist/cjs/pair.js.map +1 -1
- package/dist/cjs/public.js +64 -47
- package/dist/cjs/public.js.map +1 -1
- package/dist/cjs/secret.js +35 -33
- package/dist/cjs/secret.js.map +1 -1
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/pair.js +76 -65
- package/dist/esm/pair.js.map +1 -1
- package/dist/esm/public.js +64 -47
- package/dist/esm/public.js.map +1 -1
- package/dist/esm/secret.js +35 -33
- package/dist/esm/secret.js.map +1 -1
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/pair.d.ts +42 -51
- package/dist/types/pair.d.ts.map +1 -1
- package/dist/types/public.d.ts +68 -44
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/secret.d.ts +35 -36
- package/dist/types/secret.d.ts.map +1 -1
- package/dist/types/types.d.ts +16 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +3 -4
- package/src/index.ts +2 -1
- package/src/pair.ts +89 -93
- package/src/public.ts +115 -72
- package/src/secret.ts +51 -49
- package/src/types.ts +19 -0
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 {
|
|
17
|
+
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
21
|
-
* @interface
|
|
22
|
-
* @type {
|
|
20
|
+
* General SecretKey interface for the Secp256k1SecretKey class.
|
|
21
|
+
* @interface SecretKey
|
|
22
|
+
* @type {SecretKey}
|
|
23
23
|
*/
|
|
24
|
-
export interface
|
|
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:
|
|
49
|
+
equals(other: Secp256k1SecretKey): boolean;
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
52
|
* Uses the secret key to compute the corresponding public key.
|
|
53
|
-
* @returns {
|
|
53
|
+
* @returns {CompressedSecp256k1PublicKey} The computed public key bytes.
|
|
54
54
|
*/
|
|
55
|
-
computePublicKey():
|
|
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
|
|
66
|
-
* @returns {SecretKeyObject} The
|
|
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
|
|
76
|
-
* @type {
|
|
77
|
-
*
|
|
75
|
+
* @class Secp256k1SecretKey
|
|
76
|
+
* @type {Secp256k1SecretKey}
|
|
77
|
+
* @implements {SecretKey}
|
|
78
78
|
*/
|
|
79
|
-
export class
|
|
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
|
|
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 =
|
|
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 =
|
|
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 {
|
|
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:
|
|
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 {
|
|
210
|
+
* @returns {CompressedSecp256k1PublicKey} The computed public key
|
|
211
211
|
*/
|
|
212
|
-
public computePublicKey():
|
|
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
|
|
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
|
-
//
|
|
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
|
|
308
|
+
* Creates a Secp256k1SecretKey object from a JSON object.
|
|
307
309
|
* @param {SecretKeyObject} json The JSON object containing the secret key bytes
|
|
308
|
-
* @returns {
|
|
310
|
+
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
309
311
|
*/
|
|
310
|
-
public static fromJSON(json: SecretKeyObject):
|
|
311
|
-
return new
|
|
312
|
+
public static fromJSON(json: SecretKeyObject): Secp256k1SecretKey {
|
|
313
|
+
return new Secp256k1SecretKey(new Uint8Array(json.bytes));
|
|
312
314
|
}
|
|
313
315
|
|
|
314
316
|
/**
|
|
315
|
-
* Converts a
|
|
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
|
|
322
|
-
const secretKey = new
|
|
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
|
|
364
|
-
* @param {bigint}
|
|
365
|
-
* @returns {
|
|
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
|
|
369
|
+
public static fromEntropy(entropy: bigint): Secp256k1SecretKey {
|
|
368
370
|
// Convert the secret bigint to a hex string
|
|
369
|
-
const hexsecret =
|
|
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
|
|
373
|
-
return new
|
|
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
|
|
391
|
-
* @returns {
|
|
392
|
+
* Creates a new Secp256k1SecretKey from random secret key bytes.
|
|
393
|
+
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
392
394
|
*/
|
|
393
|
-
public static generate():
|
|
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
|
|
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 {
|
|
406
|
+
* @returns {CompressedSecp256k1PublicKey} The computed public key bytes
|
|
405
407
|
*/
|
|
406
|
-
public static getPublicKey(bytes: KeyBytes):
|
|
407
|
-
// Create a new
|
|
408
|
-
return new
|
|
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
|
+
}
|