@did-btcr2/keypair 0.7.2 → 0.9.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/dist/cjs/pair.js +50 -40
- package/dist/cjs/pair.js.map +1 -1
- package/dist/cjs/public.js +79 -64
- package/dist/cjs/public.js.map +1 -1
- package/dist/cjs/secret.js +19 -10
- package/dist/cjs/secret.js.map +1 -1
- package/dist/esm/pair.js +50 -40
- package/dist/esm/pair.js.map +1 -1
- package/dist/esm/public.js +79 -64
- package/dist/esm/public.js.map +1 -1
- package/dist/esm/secret.js +19 -10
- package/dist/esm/secret.js.map +1 -1
- package/dist/types/pair.d.ts +12 -24
- package/dist/types/pair.d.ts.map +1 -1
- package/dist/types/public.d.ts +27 -21
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/secret.d.ts +11 -5
- package/dist/types/secret.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/pair.ts +64 -60
- package/src/public.ts +99 -84
- package/src/secret.ts +21 -12
package/dist/types/public.d.ts
CHANGED
|
@@ -99,10 +99,10 @@ export declare class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
99
99
|
#private;
|
|
100
100
|
/**
|
|
101
101
|
* Creates a CompressedSecp256k1PublicKey instance.
|
|
102
|
-
* @param {Hex}
|
|
102
|
+
* @param {Hex} initialBytes The public key byte array.
|
|
103
103
|
* @throws {PublicKeyError} if the byte length is not 32 (x-only) or 33 (compressed)
|
|
104
104
|
*/
|
|
105
|
-
constructor(
|
|
105
|
+
constructor(initialBytes: Hex);
|
|
106
106
|
/**
|
|
107
107
|
* Get the compressed public key.
|
|
108
108
|
* @returns {KeyBytes} The 33-byte compressed public key (0x02 or 0x03, x).
|
|
@@ -158,13 +158,6 @@ export declare class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
158
158
|
* @returns {KeyBytes} The BIP-340 (x-only) representation of the public key.
|
|
159
159
|
*/
|
|
160
160
|
bip340(): KeyBytes;
|
|
161
|
-
/**
|
|
162
|
-
* Returns the point of the public key.
|
|
163
|
-
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
164
|
-
* @returns {Point} The point of the public key.
|
|
165
|
-
* @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
|
|
166
|
-
*/
|
|
167
|
-
static point(pk: Hex): Point;
|
|
168
161
|
/**
|
|
169
162
|
* Decodes the multibase string to the 35-byte corresponding public key (2 byte prefix + 32 byte public key).
|
|
170
163
|
* @returns {KeyBytes} The decoded public key: prefix and public key bytes
|
|
@@ -195,18 +188,6 @@ export declare class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
195
188
|
* @returns {PublicKeyObject} The CompressedSecp256k1PublicKey as a JSON object.
|
|
196
189
|
*/
|
|
197
190
|
json(): PublicKeyObject;
|
|
198
|
-
/**
|
|
199
|
-
* Creates a CompressedSecp256k1PublicKey object from a JSON representation.
|
|
200
|
-
* @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
|
|
201
|
-
* @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
|
|
202
|
-
*/
|
|
203
|
-
static fromJSON(json: PublicKeyObject): CompressedSecp256k1PublicKey;
|
|
204
|
-
/**
|
|
205
|
-
* Computes the deterministic public key for a given secret key.
|
|
206
|
-
* @param {Secp256k1SecretKey | KeyBytes} sk The Secp256k1SecretKey object or the secret key bytes
|
|
207
|
-
* @returns {CompressedSecp256k1PublicKey} A new CompressedSecp256k1PublicKey object
|
|
208
|
-
*/
|
|
209
|
-
static fromSecretKey(sk: Secp256k1SecretKey | KeyBytes): CompressedSecp256k1PublicKey;
|
|
210
191
|
/**
|
|
211
192
|
* Computes modular exponentiation: (base^exp) % mod.
|
|
212
193
|
* Used for computing modular square roots.
|
|
@@ -230,4 +211,29 @@ export declare class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
230
211
|
* @returns {Uint8Array} 65-byte uncompressed public key (starts with `0x04`)
|
|
231
212
|
*/
|
|
232
213
|
liftX(): Uint8Array;
|
|
214
|
+
/**
|
|
215
|
+
* Static method to validate a public key.
|
|
216
|
+
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
217
|
+
* @returns {boolean} True if the public key is valid, false otherwise.
|
|
218
|
+
*/
|
|
219
|
+
static isValid(pk: Hex): boolean;
|
|
220
|
+
/**
|
|
221
|
+
* Returns the point of the public key.
|
|
222
|
+
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
223
|
+
* @returns {Point} The point of the public key.
|
|
224
|
+
* @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
|
|
225
|
+
*/
|
|
226
|
+
static point(pk: Hex): Point;
|
|
227
|
+
/**
|
|
228
|
+
* Creates a CompressedSecp256k1PublicKey object from a JSON representation.
|
|
229
|
+
* @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
|
|
230
|
+
* @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
|
|
231
|
+
*/
|
|
232
|
+
static fromJSON(json: PublicKeyObject): CompressedSecp256k1PublicKey;
|
|
233
|
+
/**
|
|
234
|
+
* Computes the deterministic public key for a given secret key.
|
|
235
|
+
* @param {Secp256k1SecretKey | KeyBytes} sk The Secp256k1SecretKey object or the secret key bytes
|
|
236
|
+
* @returns {CompressedSecp256k1PublicKey} A new CompressedSecp256k1PublicKey object
|
|
237
|
+
*/
|
|
238
|
+
static fromSecretKey(sk: Secp256k1SecretKey | KeyBytes): CompressedSecp256k1PublicKey;
|
|
233
239
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,EAEL,GAAG,EACH,QAAQ,EACR,eAAe,EAEf,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAI3B,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,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IAED;;;OAGG;IACH,IAAI,GAAG,IAAI,MAAM,CAGhB;IAED;;;OAGG;IACH,IAAI,KAAK,IAAI,KAAK,CAKjB;IAED;;;OAGG;IACH,MAAM,IAAI,QAAQ;IAIlB;;;OAGG;IACH,MAAM,IAAI,QAAQ;IA8BlB;;;OAGG;IACH,MAAM,IAAI,MAAM;IAsBhB;;;;;;;OAOG;IACH,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,aAAa,GAAG,OAAO;IAYpE;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,4BAA4B,GAAG,OAAO;IAIpD;;;OAGG;IACH,IAAI,IAAI,eAAe;IAYvB;;;;;;;OAOG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM;IAUtD;;;;;;OAMG;IACH,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM;IAIrC;;;;OAIG;IACH,KAAK,IAAI,UAAU;IAyBnB;;;;OAIG;IACH,MAAM,CAAC,OAAO,CAAC,EAAE,EAAE,GAAG,GAAG,OAAO;IAShC;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,GAAG,GAAG,KAAK;IAoB5B;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,4BAA4B;IAKpE;;;;OAIG;IACH,MAAM,CAAC,aAAa,CAAC,EAAE,EAAE,kBAAkB,GAAG,QAAQ,GAAG,4BAA4B;CAiBtF"}
|
package/dist/types/secret.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bytes,
|
|
1
|
+
import { Bytes, Hex, KeyBytes, SecretKeyObject, SignatureBytes } from '@did-btcr2/common';
|
|
2
2
|
import { SchnorrKeyPair } from './pair.js';
|
|
3
3
|
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
4
4
|
import { CryptoOptions } from './types.js';
|
|
@@ -58,10 +58,10 @@ export declare class Secp256k1SecretKey implements SecretKey {
|
|
|
58
58
|
#private;
|
|
59
59
|
/**
|
|
60
60
|
* Instantiates an instance of Secp256k1SecretKey.
|
|
61
|
-
* @param {
|
|
61
|
+
* @param {Bytes | bigint} entropy bytes (Uint8Array) or secret (bigint)
|
|
62
62
|
* @throws {SecretKeyError} If entropy is not provided, not a valid 32-byte secret key or not a valid bigint seed
|
|
63
63
|
*/
|
|
64
|
-
constructor(entropy:
|
|
64
|
+
constructor(entropy: Bytes | bigint);
|
|
65
65
|
/**
|
|
66
66
|
* Get the secret key entropy as a byte array.
|
|
67
67
|
* @returns {KeyBytes} The secret key bytes as a Uint8Array
|
|
@@ -153,12 +153,18 @@ export declare class Secp256k1SecretKey implements SecretKey {
|
|
|
153
153
|
* @returns {KeyBytes} The secret key secret as secret key bytes.
|
|
154
154
|
*/
|
|
155
155
|
static toBytes(secret: bigint): KeyBytes;
|
|
156
|
+
/**
|
|
157
|
+
* Creates a new Secp256k1SecretKey object from random bytes.
|
|
158
|
+
* @param {KeyBytes} bytes The secret key bytes
|
|
159
|
+
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
160
|
+
*/
|
|
161
|
+
static fromBytes(bytes: KeyBytes): Secp256k1SecretKey;
|
|
156
162
|
/**
|
|
157
163
|
* Creates a new Secp256k1SecretKey object from a bigint secret.
|
|
158
|
-
* @param {bigint}
|
|
164
|
+
* @param {bigint} bint The secret bigint
|
|
159
165
|
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
160
166
|
*/
|
|
161
|
-
static
|
|
167
|
+
static fromBigInt(bint: bigint): Secp256k1SecretKey;
|
|
162
168
|
/**
|
|
163
169
|
* Generates random secret key bytes.
|
|
164
170
|
* @returns {KeyBytes} Uint8Array of 32 random bytes.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secret.d.ts","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,EAEL,
|
|
1
|
+
{"version":3,"file":"secret.d.ts","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,EAEL,GAAG,EACH,QAAQ,EAER,eAAe,EACf,cAAc,EACf,MAAM,mBAAmB,CAAC;AAK3B,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB;;;OAGG;IACH,KAAK,EAAE,QAAQ,CAAC;IAEhB;;;;OAIG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;OAGG;IACH,GAAG,EAAE,GAAG,CAAC;IAET;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC;IAE3C;;;OAGG;IACH,gBAAgB,IAAI,4BAA4B,CAAC;IAEjD;;;OAGG;IACH,OAAO,IAAI,OAAO,CAAC;IAEnB;;;OAGG;IACH,IAAI,IAAI,eAAe,CAAC;CACzB;AAED;;;;;;;GAOG;AACH,qBAAa,kBAAmB,YAAW,SAAS;;IAUlD;;;;OAIG;gBACS,OAAO,EAAE,KAAK,GAAG,MAAM;IAyCnC;;;OAGG;IACH,IAAI,KAAK,IAAI,UAAU,CAItB;IAED;;;OAGG;IACH,IAAI,IAAI,IAAI,MAAM,CAIjB;IAED;;;OAGG;IACH,IAAI,GAAG,IAAI,GAAG,CAGb;IAGD;;;OAGG;IACH,IAAI,SAAS,IAAI,MAAM,CAGtB;IAED;;;OAGG;IACI,MAAM,IAAI,MAAM;IAoBvB;;;;OAIG;IACI,MAAM,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO;IAKjD;;;OAGG;IACI,gBAAgB,IAAI,4BAA4B;IAuBvD;;;OAGG;IACI,IAAI,IAAI,eAAe;IAQ9B;;;OAGG;IACI,OAAO,IAAI,OAAO;IAIzB;;;OAGG;IACI,iBAAiB,IAAI,OAAO;IAanC;;;;;;;OAOG;IACI,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,aAAa,GAAG,cAAc;IAiB9D;;;;OAIG;WACW,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,KAAK;IA8B9C;;;;OAIG;WACW,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,kBAAkB;IAIjE;;;;;OAKG;WACW,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,cAAc;IAWxD;;;;OAIG;WACW,QAAQ,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM;IAI/C;;;;OAIG;WACW,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ;IAiB/C;;;;OAIG;WACW,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,kBAAkB;IAK5D;;;;OAIG;WACW,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,kBAAkB;IAS1D;;;OAGG;WACW,MAAM,IAAI,QAAQ;IAQhC;;;OAGG;WACW,QAAQ,IAAI,kBAAkB;IAQ5C;;;;OAIG;WACW,YAAY,CAAC,KAAK,EAAE,QAAQ,GAAG,4BAA4B;CAI1E"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@did-btcr2/keypair",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "JavaScript/TypeScript implementation of secp256k1 public/private key pairs with BIP340 schnorr signatures. Used by various parts of the did-btcr2-js monorepo.",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"@noble/hashes": "^1.7.1",
|
|
63
63
|
"multiformats": "^13.3.2",
|
|
64
64
|
"tiny-secp256k1": "^2.2.3",
|
|
65
|
-
"@did-btcr2/common": "
|
|
65
|
+
"@did-btcr2/common": "3.1.0"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@eslint/js": "^9.20.0",
|
package/src/pair.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Hex,
|
|
3
|
+
HexString,
|
|
3
4
|
KeyBytes,
|
|
4
5
|
KeyPairError,
|
|
5
6
|
SchnorrKeyPairObject
|
|
6
7
|
} from '@did-btcr2/common';
|
|
7
|
-
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
8
|
-
import { Secp256k1SecretKey } from './secret.js';
|
|
8
|
+
import { CompressedSecp256k1PublicKey, PublicKey } from './public.js';
|
|
9
|
+
import { Secp256k1SecretKey, SecretKey } from './secret.js';
|
|
9
10
|
import { HexSchnorrKeyPair, MultibaseKeys, RawSchnorrKeyPair, SchnorrKeyPairParams } from './types.js';
|
|
10
11
|
|
|
11
12
|
/**
|
|
@@ -15,40 +16,41 @@ import { HexSchnorrKeyPair, MultibaseKeys, RawSchnorrKeyPair, SchnorrKeyPairPara
|
|
|
15
16
|
*/
|
|
16
17
|
export interface KeyPair {
|
|
17
18
|
/**
|
|
18
|
-
*
|
|
19
|
+
* The public key associated with the SchnorrKeyPair (required).
|
|
19
20
|
*/
|
|
20
|
-
readonly publicKey:
|
|
21
|
+
readonly publicKey: PublicKey;
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
|
-
*
|
|
24
|
-
* @throws {KeyPairError} If the secret key is not available.
|
|
24
|
+
* The secret key associated with the SchnorrKeyPair (optional).
|
|
25
25
|
*/
|
|
26
|
-
readonly secretKey?:
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* JSON representation of the SchnorrKeyPair object.
|
|
30
|
-
* @returns {SchnorrKeyPairObject} The SchnorrKeyPair as a JSON object.
|
|
31
|
-
*/
|
|
32
|
-
json(): SchnorrKeyPairObject;
|
|
26
|
+
readonly secretKey?: SecretKey;
|
|
33
27
|
}
|
|
34
28
|
|
|
35
29
|
/**
|
|
36
|
-
* Encapsulates
|
|
30
|
+
* Encapsulates paired CompressedSecp256k1PublicKey and Secp256k1SecretKey objects as a single SchnorrKeyPair object.
|
|
37
31
|
* @class SchnorrKeyPair
|
|
38
32
|
* @type {SchnorrKeyPair}
|
|
39
33
|
*/
|
|
40
34
|
export class SchnorrKeyPair implements KeyPair {
|
|
41
|
-
/**
|
|
42
|
-
|
|
35
|
+
/**
|
|
36
|
+
* The secret key objec
|
|
37
|
+
*/
|
|
38
|
+
#secretKey?: Secp256k1SecretKey;
|
|
43
39
|
|
|
44
|
-
/**
|
|
45
|
-
|
|
40
|
+
/**
|
|
41
|
+
* The public key object
|
|
42
|
+
*/;
|
|
43
|
+
#publicKey: CompressedSecp256k1PublicKey;
|
|
46
44
|
|
|
47
|
-
/**
|
|
48
|
-
|
|
45
|
+
/**
|
|
46
|
+
* The public key in multibase forma
|
|
47
|
+
*/
|
|
48
|
+
#publicKeyMultibase: string;
|
|
49
49
|
|
|
50
|
-
/**
|
|
51
|
-
|
|
50
|
+
/**
|
|
51
|
+
* The secret key in multibase forma
|
|
52
|
+
*/
|
|
53
|
+
#secretKeyMultibase: string;
|
|
52
54
|
|
|
53
55
|
/**
|
|
54
56
|
* Creates an instance of Keys. Must provide a at least a secret key.
|
|
@@ -67,22 +69,22 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
67
69
|
|
|
68
70
|
// Set the secretKey
|
|
69
71
|
if(params.secretKey instanceof Uint8Array) {
|
|
70
|
-
this
|
|
72
|
+
this.#secretKey = new Secp256k1SecretKey(params.secretKey);
|
|
71
73
|
} else if (params.secretKey instanceof Secp256k1SecretKey) {
|
|
72
|
-
this
|
|
74
|
+
this.#secretKey = params.secretKey;
|
|
73
75
|
}
|
|
74
76
|
|
|
75
77
|
// Set the publicKey
|
|
76
78
|
if(params.publicKey instanceof CompressedSecp256k1PublicKey) {
|
|
77
|
-
this
|
|
79
|
+
this.#publicKey = params.publicKey;
|
|
78
80
|
} else if (params.publicKey instanceof Uint8Array) {
|
|
79
|
-
this
|
|
81
|
+
this.#publicKey = new CompressedSecp256k1PublicKey(params.publicKey);
|
|
80
82
|
} else {
|
|
81
|
-
this
|
|
83
|
+
this.#publicKey = this.#secretKey!.computePublicKey();
|
|
82
84
|
}
|
|
83
85
|
|
|
84
|
-
this
|
|
85
|
-
this
|
|
86
|
+
this.#publicKeyMultibase = this.#publicKey.multibase.encoded;
|
|
87
|
+
this.#secretKeyMultibase = this.#secretKey ? this.#secretKey.multibase : '';
|
|
86
88
|
}
|
|
87
89
|
|
|
88
90
|
/**
|
|
@@ -92,15 +94,15 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
92
94
|
*/
|
|
93
95
|
get secretKey(): Secp256k1SecretKey {
|
|
94
96
|
// If the secret key is not available, throw an error
|
|
95
|
-
if(!this
|
|
97
|
+
if(!this.#secretKey) {
|
|
96
98
|
throw new KeyPairError('Secret key not available', 'SECRET_KEY_ERROR');
|
|
97
99
|
}
|
|
98
100
|
// If the secret key is not valid, throw an error
|
|
99
|
-
if(!this.
|
|
101
|
+
if(!this.#secretKey.isValid()) {
|
|
100
102
|
throw new KeyPairError('Secret key is not valid', 'SECRET_KEY_ERROR');
|
|
101
103
|
}
|
|
102
104
|
// Return a copy of the secret key
|
|
103
|
-
const secret = this
|
|
105
|
+
const secret = this.#secretKey;
|
|
104
106
|
return secret;
|
|
105
107
|
}
|
|
106
108
|
|
|
@@ -119,9 +121,9 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
119
121
|
if(!publicKey.equals(cPk))
|
|
120
122
|
throw new KeyPairError('Public key is not a valid pair with the secret key', 'PUBLIC_KEY_ERROR');
|
|
121
123
|
}
|
|
122
|
-
this
|
|
123
|
-
this
|
|
124
|
-
this
|
|
124
|
+
this.#publicKey = publicKey;
|
|
125
|
+
this.#publicKeyMultibase = publicKey.multibase.encoded;
|
|
126
|
+
this.#secretKeyMultibase = this.#secretKey ? this.#secretKey.multibase : '';
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
/**
|
|
@@ -129,7 +131,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
129
131
|
* @returns {CompressedSecp256k1PublicKey} The CompressedSecp256k1PublicKey object
|
|
130
132
|
*/
|
|
131
133
|
get publicKey(): CompressedSecp256k1PublicKey {
|
|
132
|
-
const publicKey = this
|
|
134
|
+
const publicKey = this.#publicKey;
|
|
133
135
|
return publicKey;
|
|
134
136
|
}
|
|
135
137
|
|
|
@@ -151,7 +153,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
151
153
|
get hex(): HexSchnorrKeyPair {
|
|
152
154
|
return {
|
|
153
155
|
public : this.publicKey.hex,
|
|
154
|
-
secret : this
|
|
156
|
+
secret : this.#secretKey ? this.secretKey.hex : undefined
|
|
155
157
|
};
|
|
156
158
|
}
|
|
157
159
|
|
|
@@ -161,8 +163,8 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
161
163
|
*/
|
|
162
164
|
get multibase(): MultibaseKeys {
|
|
163
165
|
return {
|
|
164
|
-
publicKeyMultibase
|
|
165
|
-
secretKeyMultibase : this
|
|
166
|
+
publicKeyMultibase : this.#publicKeyMultibase,
|
|
167
|
+
secretKeyMultibase : this.#secretKeyMultibase,
|
|
166
168
|
};
|
|
167
169
|
}
|
|
168
170
|
|
|
@@ -170,7 +172,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
170
172
|
* JSON representation of a Keys.
|
|
171
173
|
* @returns {SchnorrKeyPairObject} The Keys as a JSON object
|
|
172
174
|
*/
|
|
173
|
-
|
|
175
|
+
json(): SchnorrKeyPairObject {
|
|
174
176
|
return {
|
|
175
177
|
secretKey : this.secretKey.json(),
|
|
176
178
|
publicKey : this.publicKey.json()
|
|
@@ -182,7 +184,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
182
184
|
* @param {SchnorrKeyPairObject} keys The JSON object to initialize the Keys.
|
|
183
185
|
* @returns {SchnorrKeyPair} The initialized Keys object.
|
|
184
186
|
*/
|
|
185
|
-
|
|
187
|
+
static fromJSON(keys: SchnorrKeyPairObject): SchnorrKeyPair {
|
|
186
188
|
return new SchnorrKeyPair({
|
|
187
189
|
secretKey : Secp256k1SecretKey.fromJSON(keys.secretKey),
|
|
188
190
|
publicKey : CompressedSecp256k1PublicKey.fromJSON(keys.publicKey)
|
|
@@ -194,33 +196,35 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
194
196
|
* @param {Secp256k1SecretKey | KeyBytes} data The secret key bytes
|
|
195
197
|
* @returns {SchnorrKeyPair} A new SchnorrKeyPair object
|
|
196
198
|
*/
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
// If the
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
199
|
+
static fromSecret(data: KeyBytes | HexString): SchnorrKeyPair {
|
|
200
|
+
|
|
201
|
+
// If the data is Secp256k1SecretKey object, get the raw bytes
|
|
202
|
+
// Else if data is string, convert to byte array
|
|
203
|
+
// Else must be bytes, use them
|
|
204
|
+
const secret = typeof data === 'string'
|
|
205
|
+
? Buffer.from(data, 'hex')
|
|
206
|
+
: data;
|
|
207
|
+
|
|
208
|
+
// Check the lenth
|
|
209
|
+
if(secret.length !== 32) {
|
|
210
|
+
throw new KeyPairError('Invalid arg: must be 32 byte secret key', 'FROM_SECRET_KEY_ERROR');
|
|
205
211
|
}
|
|
206
212
|
|
|
207
213
|
// If pk Uint8Array, construct Secp256k1SecretKey object else use the object
|
|
208
|
-
const
|
|
214
|
+
const secretKey = new Secp256k1SecretKey(secret);
|
|
215
|
+
const publicKey = secretKey.computePublicKey();
|
|
209
216
|
|
|
210
217
|
// Return a new Keys object
|
|
211
|
-
return new SchnorrKeyPair({
|
|
212
|
-
secretKey : data instanceof Uint8Array ? new Secp256k1SecretKey(data) : data,
|
|
213
|
-
publicKey : secret.computePublicKey()
|
|
214
|
-
});
|
|
218
|
+
return new SchnorrKeyPair({ secretKey, publicKey });
|
|
215
219
|
}
|
|
216
220
|
|
|
217
221
|
/**
|
|
218
222
|
* Static method creates a new Keys (Secp256k1SecretKey/CompressedSecp256k1PublicKey) from bigint entropy.
|
|
219
|
-
* @param {bigint}
|
|
223
|
+
* @param {bigint} bint The entropy in bigint form
|
|
220
224
|
* @returns {SchnorrKeyPair} A new SchnorrKeyPair object
|
|
221
225
|
*/
|
|
222
|
-
|
|
223
|
-
const secretKey = Secp256k1SecretKey.
|
|
226
|
+
static fromBigInt(bint: bigint): SchnorrKeyPair {
|
|
227
|
+
const secretKey = Secp256k1SecretKey.fromBigInt(bint);
|
|
224
228
|
const publicKey = secretKey.computePublicKey();
|
|
225
229
|
return new SchnorrKeyPair({ secretKey, publicKey });
|
|
226
230
|
}
|
|
@@ -230,7 +234,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
230
234
|
* @param {KeyBytes} keyBytes The key bytes (secret or public).
|
|
231
235
|
* @returns {Hex} The key bytes as a hex string.
|
|
232
236
|
*/
|
|
233
|
-
|
|
237
|
+
static toHex(keyBytes: KeyBytes): Hex {
|
|
234
238
|
return Buffer.from(keyBytes).toString('hex');
|
|
235
239
|
}
|
|
236
240
|
|
|
@@ -240,7 +244,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
240
244
|
* @param {SchnorrKeyPair} otherKp The other keys to compare.
|
|
241
245
|
* @returns {boolean} True if the public key and secret key are equal, false otherwise.
|
|
242
246
|
*/
|
|
243
|
-
|
|
247
|
+
static equals(kp: SchnorrKeyPair, otherKp: SchnorrKeyPair): boolean {
|
|
244
248
|
// Deconstruct the public keys from the key pairs
|
|
245
249
|
const pk = kp.publicKey;
|
|
246
250
|
const otherPk = otherKp.publicKey;
|
|
@@ -265,7 +269,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
265
269
|
* Static method to generate a new random SchnorrKeyPair instance.
|
|
266
270
|
* @returns {SchnorrKeyPair} A new Secp256k1SecretKey object.
|
|
267
271
|
*/
|
|
268
|
-
|
|
272
|
+
static generate(): SchnorrKeyPair {
|
|
269
273
|
// Generate random secret key bytes
|
|
270
274
|
const sk = Secp256k1SecretKey.random();
|
|
271
275
|
|