@did-btcr2/keypair 0.10.0 → 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.
- package/dist/cjs/pair.js +43 -49
- package/dist/cjs/pair.js.map +1 -1
- package/dist/cjs/public.js +22 -139
- package/dist/cjs/public.js.map +1 -1
- package/dist/cjs/secret.js +63 -81
- 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 +22 -139
- package/dist/esm/public.js.map +1 -1
- package/dist/esm/secret.js +63 -81
- 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 +8 -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 +4 -4
- package/src/pair.ts +51 -56
- package/src/public.ts +26 -176
- package/src/secret.ts +70 -110
- package/src/types.ts +2 -2
package/dist/types/public.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Bytes, Hex, KeyBytes, MultibaseObject, PublicKeyObject } from '@did-btcr2/common';
|
|
2
|
-
|
|
2
|
+
/** Fixed public key header bytes per the Data Integrity BIP340 Cryptosuite spec: [0xe7, 0x01] */
|
|
3
|
+
export declare const BIP340_PUBLIC_KEY_MULTIBASE_PREFIX: Bytes;
|
|
3
4
|
import { CryptoOptions } from './types.js';
|
|
4
5
|
/**
|
|
5
6
|
* Point Interface representing an (x, y) coordinate on the secp256k1 curve.
|
|
@@ -77,11 +78,11 @@ export interface PublicKey {
|
|
|
77
78
|
*/
|
|
78
79
|
encode(): string;
|
|
79
80
|
/**
|
|
80
|
-
*
|
|
81
|
-
* @param {
|
|
81
|
+
* Public key equality check.
|
|
82
|
+
* @param {PublicKey} other The public key to compare.
|
|
82
83
|
* @returns {boolean} True if the public keys are equal.
|
|
83
84
|
*/
|
|
84
|
-
equals(other:
|
|
85
|
+
equals(other: PublicKey): boolean;
|
|
85
86
|
}
|
|
86
87
|
/**
|
|
87
88
|
* Encapsulates a secp256k1 public key compliant to BIP-340 BIP schnorr signature scheme.
|
|
@@ -174,61 +175,26 @@ export declare class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
174
175
|
verify(signature: Bytes, data: Bytes, opts?: CryptoOptions): boolean;
|
|
175
176
|
/**
|
|
176
177
|
* Compares this public key to another public key.
|
|
177
|
-
* @param {
|
|
178
|
+
* @param {PublicKey} other The other public key to compare
|
|
178
179
|
* @returns {boolean} True if the public keys are equal, false otherwise.
|
|
179
180
|
*/
|
|
180
|
-
equals(other:
|
|
181
|
+
equals(other: PublicKey): boolean;
|
|
181
182
|
/**
|
|
182
183
|
* JSON representation of a CompressedSecp256k1PublicKey object.
|
|
183
184
|
* @returns {PublicKeyObject} The CompressedSecp256k1PublicKey as a JSON object.
|
|
184
185
|
*/
|
|
185
186
|
toJSON(): PublicKeyObject;
|
|
186
|
-
/**
|
|
187
|
-
* Computes modular exponentiation: (base^exp) % mod.
|
|
188
|
-
* Used for computing modular square roots.
|
|
189
|
-
* @param {bigint} base The base value
|
|
190
|
-
* @param {bigint} exp The exponent value
|
|
191
|
-
* @param {bigint} mod The modulus value
|
|
192
|
-
* @returns {bigint} The result of the modular exponentiation
|
|
193
|
-
*/
|
|
194
|
-
modPow(base: bigint, exp: bigint, mod: bigint): bigint;
|
|
195
|
-
/**
|
|
196
|
-
* Computes `sqrt(a) mod p` using Tonelli-Shanks algorithm.
|
|
197
|
-
* This finds `y` such that `y^2 ≡ a mod p`.
|
|
198
|
-
* @param {bigint} a The value to find the square root of
|
|
199
|
-
* @param {bigint} p The prime modulus
|
|
200
|
-
* @returns {bigint} The square root of `a` mod `p`
|
|
201
|
-
*/
|
|
202
|
-
sqrtMod(a: bigint, p: bigint): bigint;
|
|
203
|
-
/**
|
|
204
|
-
* Lifts a 32-byte x-only coordinate into a full secp256k1 point (x, y).
|
|
205
|
-
* @param xBytes 32-byte x-coordinate
|
|
206
|
-
* @returns {Uint8Array} 65-byte uncompressed public key (starts with `0x04`)
|
|
207
|
-
*/
|
|
208
|
-
liftX(): Uint8Array;
|
|
209
187
|
/**
|
|
210
188
|
* Static method to validate a public key.
|
|
211
189
|
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
212
190
|
* @returns {boolean} True if the public key is valid, false otherwise.
|
|
213
191
|
*/
|
|
214
192
|
static isValid(pk: Hex): boolean;
|
|
215
|
-
/**
|
|
216
|
-
* Returns the point of the public key.
|
|
217
|
-
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
218
|
-
* @returns {Point} The point of the public key.
|
|
219
|
-
* @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
|
|
220
|
-
*/
|
|
221
|
-
static point(pk: Hex): Point;
|
|
222
193
|
/**
|
|
223
194
|
* Creates a CompressedSecp256k1PublicKey object from a JSON representation.
|
|
224
195
|
* @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
|
|
225
196
|
* @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
|
|
226
197
|
*/
|
|
227
198
|
static fromJSON(json: PublicKeyObject): CompressedSecp256k1PublicKey;
|
|
228
|
-
/**
|
|
229
|
-
* Computes the deterministic public key for a given secret key.
|
|
230
|
-
* @param {Secp256k1SecretKey | KeyBytes} sk The Secp256k1SecretKey object or the secret key bytes
|
|
231
|
-
* @returns {CompressedSecp256k1PublicKey} A new CompressedSecp256k1PublicKey object
|
|
232
|
-
*/
|
|
233
|
-
static fromSecretKey(sk: Secp256k1SecretKey | KeyBytes): CompressedSecp256k1PublicKey;
|
|
234
199
|
}
|
|
200
|
+
//# sourceMappingURL=public.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,GAAG,EACH,QAAQ,EACR,eAAe,EAEf,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAE3B,iGAAiG;AACjG,eAAO,MAAM,kCAAkC,EAAE,KAAoC,CAAC;AAItF,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C;;;;GAIG;AACH,MAAM,WAAW,KAAK;IACpB,CAAC,EAAE,QAAQ,CAAC;IACZ,CAAC,EAAE,QAAQ,CAAC;CACb;AAED;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB;;;OAGG;IACH,UAAU,EAAE,QAAQ,CAAC;IAErB;;;OAGG;IACH,YAAY,EAAE,QAAQ,CAAC;IAEvB;;;OAGG;IACH,KAAK,EAAE,QAAQ,CAAC;IAEhB;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;;OAGG;IACH,MAAM,EAAE,OAAO,CAAC;IAEhB;;;OAGG;IACH,CAAC,EAAE,QAAQ,CAAC;IAEZ;;;OAGG;IACH,CAAC,EAAE,QAAQ,CAAC;IAEZ;;;OAGG;IACH,SAAS,EAAE,eAAe,CAAC;IAE3B;;;OAGG;IACH,GAAG,EAAE,GAAG,CAAC;IAET;;;OAGG;IACH,KAAK,EAAE,KAAK,CAAC;IAEb;;;OAGG;IACH,MAAM,IAAI,QAAQ,CAAC;IAEnB;;;OAGG;IACH,MAAM,IAAI,MAAM,CAAC;IAEjB;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC;CACnC;AAED;;;;;;GAMG;AACH,qBAAa,4BAA6B,YAAW,SAAS;;IAe5D;;;;OAIG;gBACS,YAAY,EAAE,GAAG;IA6B7B;;;OAGG;IACH,IAAI,UAAU,IAAI,QAAQ,CAGzB;IAED;;;OAGG;IACH,IAAI,YAAY,IAAI,QAAQ,CAE3B;IAED;;OAEG;IACH,IAAI,KAAK,IAAI,QAAQ,CAGpB;IAED;;;;OAIG;IACH,IAAI,MAAM,IAAI,IAAI,GAAG,IAAI,CASxB;IAED;;;OAGG;IACH,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED;;;OAGG;IACH,IAAI,CAAC,IAAI,QAAQ,CAGhB;IAED;;;OAGG;IACH,IAAI,CAAC,IAAI,QAAQ,CAGhB;IAED;;;OAGG;IACH,IAAI,SAAS,IAAI,eAAe,CAM/B;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;IAIlB;;;OAGG;IACH,MAAM,IAAI,MAAM;IAOhB;;;;;;;OAOG;IACH,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,aAAa,GAAG,OAAO;IAepE;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO;IAIjC;;;OAGG;IACH,MAAM,IAAI,eAAe;IAYzB;;;;OAIG;IACH,MAAM,CAAC,OAAO,CAAC,EAAE,EAAE,GAAG,GAAG,OAAO;IAShC;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,4BAA4B;CAIrE"}
|
package/dist/types/secret.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Bytes, Hex, HexString, KeyBytes, SecretKeyObject, SignatureBytes } from '@did-btcr2/common';
|
|
2
|
-
import { SchnorrKeyPair } from './pair.js';
|
|
3
2
|
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
4
3
|
import { CryptoOptions } from './types.js';
|
|
5
4
|
/**
|
|
@@ -26,10 +25,10 @@ export interface SecretKey {
|
|
|
26
25
|
hex: Hex;
|
|
27
26
|
/**
|
|
28
27
|
* Checks if this secret key is equal to another secret key.
|
|
29
|
-
* @param {
|
|
28
|
+
* @param {SecretKey} other The other secret key to compare.
|
|
30
29
|
* @returns {boolean} True if the private keys are equal.
|
|
31
30
|
*/
|
|
32
|
-
equals(other:
|
|
31
|
+
equals(other: SecretKey): boolean;
|
|
33
32
|
/**
|
|
34
33
|
* Uses the secret key to compute the corresponding public key.
|
|
35
34
|
* @returns {CompressedSecp256k1PublicKey} The computed public key bytes.
|
|
@@ -57,6 +56,11 @@ export declare class Secp256k1SecretKey implements SecretKey {
|
|
|
57
56
|
* @throws {SecretKeyError} If entropy is not provided, not a valid 32-byte secret key or not a valid bigint seed
|
|
58
57
|
*/
|
|
59
58
|
constructor(entropy: Bytes | bigint);
|
|
59
|
+
/**
|
|
60
|
+
* Zeros out secret key material from memory.
|
|
61
|
+
* The instance should not be used after calling this method.
|
|
62
|
+
*/
|
|
63
|
+
destroy(): void;
|
|
60
64
|
/**
|
|
61
65
|
* Get the secret key entropy as a byte array.
|
|
62
66
|
* @returns {KeyBytes} The secret key bytes as a Uint8Array
|
|
@@ -84,20 +88,29 @@ export declare class Secp256k1SecretKey implements SecretKey {
|
|
|
84
88
|
encode(): string;
|
|
85
89
|
/**
|
|
86
90
|
* Checks if this secret key is equal to another.
|
|
87
|
-
* @param {
|
|
91
|
+
* @param {SecretKey} other The other secret key
|
|
88
92
|
* @returns {boolean} True if the private keys are equal, false otherwise
|
|
89
93
|
*/
|
|
90
|
-
equals(other:
|
|
94
|
+
equals(other: SecretKey): boolean;
|
|
91
95
|
/**
|
|
92
96
|
* Computes the public key from the secret key bytes.
|
|
93
97
|
* @returns {CompressedSecp256k1PublicKey} The computed public key
|
|
94
98
|
*/
|
|
95
99
|
computePublicKey(): CompressedSecp256k1PublicKey;
|
|
96
100
|
/**
|
|
97
|
-
*
|
|
101
|
+
* Safe JSON representation. Does not expose secret material.
|
|
102
|
+
* Called implicitly by JSON.stringify(). Use exportJSON() for full serialization.
|
|
103
|
+
*/
|
|
104
|
+
toJSON(): {
|
|
105
|
+
type: string;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Exports the secret key as a JSON object. Contains sensitive material.
|
|
98
109
|
* @returns {SecretKeyObject} The secret key as a JSON object
|
|
99
110
|
*/
|
|
100
|
-
|
|
111
|
+
exportJSON(): SecretKeyObject;
|
|
112
|
+
/** @override Prevents secret material from appearing in console.log */
|
|
113
|
+
toString(): string;
|
|
101
114
|
/**
|
|
102
115
|
* Checks if the secret key is valid.
|
|
103
116
|
* @returns {boolean} True if the secret key is valid, false otherwise
|
|
@@ -129,13 +142,6 @@ export declare class Secp256k1SecretKey implements SecretKey {
|
|
|
129
142
|
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
130
143
|
*/
|
|
131
144
|
static fromJSON(json: SecretKeyObject): Secp256k1SecretKey;
|
|
132
|
-
/**
|
|
133
|
-
* Converts a Secp256k1SecretKey or KeyBytes to a SchnorrKeyPair.
|
|
134
|
-
* @param {KeyBytes} bytes The secret key bytes
|
|
135
|
-
* @returns {SchnorrKeyPair} The SchnorrKeyPair object containing the public and private keys
|
|
136
|
-
* @throws {SecretKeyError} If the secret key is not valid
|
|
137
|
-
*/
|
|
138
|
-
static toKeyPair(bytes: KeyBytes): SchnorrKeyPair;
|
|
139
145
|
/**
|
|
140
146
|
* Convert a bigint secret to secret key bytes.
|
|
141
147
|
* @param {KeyBytes} bytes The secret key bytes
|
|
@@ -148,12 +154,6 @@ export declare class Secp256k1SecretKey implements SecretKey {
|
|
|
148
154
|
* @returns {KeyBytes} The secret key secret as secret key bytes.
|
|
149
155
|
*/
|
|
150
156
|
static toBytes(secret: bigint): KeyBytes;
|
|
151
|
-
/**
|
|
152
|
-
* Creates a new Secp256k1SecretKey object from random bytes.
|
|
153
|
-
* @param {KeyBytes} bytes The secret key bytes
|
|
154
|
-
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
155
|
-
*/
|
|
156
|
-
static fromBytes(bytes: KeyBytes): Secp256k1SecretKey;
|
|
157
157
|
/**
|
|
158
158
|
* Creates a new Secp256k1SecretKey object from a bigint secret.
|
|
159
159
|
* @param {bigint} bint The secret bigint
|
|
@@ -177,3 +177,4 @@ export declare class Secp256k1SecretKey implements SecretKey {
|
|
|
177
177
|
*/
|
|
178
178
|
static getPublicKey(bytes: KeyBytes): CompressedSecp256k1PublicKey;
|
|
179
179
|
}
|
|
180
|
+
//# sourceMappingURL=secret.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secret.d.ts","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"secret.d.ts","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,GAAG,EACH,SAAS,EACT,QAAQ,EAER,eAAe,EACf,cAAc,EACf,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAO3C;;;;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,SAAS,GAAG,OAAO,CAAC;IAElC;;;OAGG;IACH,gBAAgB,IAAI,4BAA4B,CAAC;IAEjD;;;OAGG;IACH,OAAO,IAAI,OAAO,CAAC;CACpB;AAED;;;;;;;GAOG;AACH,qBAAa,kBAAmB,YAAW,SAAS;;IAUlD;;;;OAIG;gBACS,OAAO,EAAE,KAAK,GAAG,MAAM;IAyCnC;;;OAGG;IACI,OAAO,IAAI,IAAI;IAMtB;;;OAGG;IACH,IAAI,KAAK,IAAI,UAAU,CAItB;IAED;;;OAGG;IACH,IAAI,IAAI,IAAI,MAAM,CAIjB;IAED;;;OAGG;IACH,IAAI,GAAG,IAAI,SAAS,CAGnB;IAGD;;;OAGG;IACH,IAAI,SAAS,IAAI,MAAM,CAGtB;IAED;;;OAGG;IACI,MAAM,IAAI,MAAM;IAOvB;;;;OAIG;IACI,MAAM,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO;IAIxC;;;OAGG;IACI,gBAAgB,IAAI,4BAA4B;IAIvD;;;OAGG;IACI,MAAM,IAAI;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE;IAIjC;;;OAGG;IACI,UAAU,IAAI,eAAe;IAQpC,uEAAuE;IAChE,QAAQ,IAAI,MAAM;IASzB;;;OAGG;IACI,OAAO,IAAI,OAAO;IAIzB;;;OAGG;IACI,iBAAiB,IAAI,OAAO;IASnC;;;;;;;OAOG;IACI,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,aAAa,GAAG,cAAc;IAe9D;;;;OAIG;WACW,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,KAAK;IA8B9C;;;;OAIG;WACW,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,kBAAkB;IAIjE;;;;OAIG;WACW,QAAQ,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM;IAI/C;;;;OAIG;WACW,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ;IAiB/C;;;;OAIG;WACW,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,kBAAkB;IAI1D;;;OAGG;WACW,MAAM,IAAI,QAAQ;IAShC;;;OAGG;WACW,QAAQ,IAAI,kBAAkB;IAQ5C;;;;OAIG;WACW,YAAY,CAAC,KAAK,EAAE,QAAQ,GAAG,4BAA4B;CAI1E"}
|
package/dist/types/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Hex, KeyBytes } from '@did-btcr2/common';
|
|
2
|
-
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
3
|
-
import { Secp256k1SecretKey } from './secret.js';
|
|
2
|
+
import type { CompressedSecp256k1PublicKey } from './public.js';
|
|
3
|
+
import type { Secp256k1SecretKey } from './secret.js';
|
|
4
4
|
export type CryptoOptions = {
|
|
5
5
|
scheme: 'ecdsa' | 'schnorr';
|
|
6
6
|
};
|
|
@@ -21,3 +21,4 @@ export interface MultibaseKeys {
|
|
|
21
21
|
publicKeyMultibase: string;
|
|
22
22
|
secretKeyMultibase: string;
|
|
23
23
|
}
|
|
24
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtD,MAAM,MAAM,aAAa,GAAG;IAAE,MAAM,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,CAAA;AAE3D,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,EAAE,QAAQ,CAAC;IACjB,MAAM,CAAC,EAAE,QAAQ,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,EAAE,GAAG,CAAC;IACZ,MAAM,CAAC,EAAE,GAAG,CAAA;CACb,CAAA;AAED,wDAAwD;AACxD,MAAM,WAAW,oBAAoB;IACnC,SAAS,CAAC,EAAE,kBAAkB,GAAG,QAAQ,CAAC;IAC1C,SAAS,CAAC,EAAE,4BAA4B,GAAG,QAAQ,CAAC;CACrD;AAED,MAAM,WAAW,aAAa;IAC5B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,kBAAkB,EAAE,MAAM,CAAA;CAC3B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@did-btcr2/keypair",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.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",
|
|
@@ -59,10 +59,10 @@
|
|
|
59
59
|
"bip340 key pair"
|
|
60
60
|
],
|
|
61
61
|
"dependencies": {
|
|
62
|
+
"@noble/curves": "^2.0.1",
|
|
62
63
|
"@noble/hashes": "^1.7.1",
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"@did-btcr2/common": "5.0.0"
|
|
64
|
+
"@scure/base": "^2.0.0",
|
|
65
|
+
"@did-btcr2/common": "7.0.0"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@eslint/js": "^9.20.0",
|
package/src/pair.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
HexString,
|
|
4
4
|
KeyBytes,
|
|
5
5
|
KeyPairError,
|
|
6
|
+
PublicKeyObject,
|
|
6
7
|
SchnorrKeyPairObject
|
|
7
8
|
} from '@did-btcr2/common';
|
|
8
9
|
import { CompressedSecp256k1PublicKey, PublicKey } from './public.js';
|
|
@@ -32,26 +33,9 @@ export interface KeyPair {
|
|
|
32
33
|
* @type {SchnorrKeyPair}
|
|
33
34
|
*/
|
|
34
35
|
export class SchnorrKeyPair implements KeyPair {
|
|
35
|
-
/**
|
|
36
|
-
* The secret key objec
|
|
37
|
-
*/
|
|
38
36
|
#secretKey?: Secp256k1SecretKey;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* The public key object
|
|
42
|
-
*/;
|
|
43
37
|
#publicKey: CompressedSecp256k1PublicKey;
|
|
44
38
|
|
|
45
|
-
/**
|
|
46
|
-
* The public key in multibase forma
|
|
47
|
-
*/
|
|
48
|
-
#publicKeyMultibase: string;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The secret key in multibase forma
|
|
52
|
-
*/
|
|
53
|
-
#secretKeyMultibase: string;
|
|
54
|
-
|
|
55
39
|
/**
|
|
56
40
|
* Creates an instance of Keys. Must provide a at least a secret key.
|
|
57
41
|
* Can optionally provide both a secret and public key, but must be a valid pair.
|
|
@@ -83,8 +67,13 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
83
67
|
this.#publicKey = this.#secretKey!.computePublicKey();
|
|
84
68
|
}
|
|
85
69
|
|
|
86
|
-
|
|
87
|
-
|
|
70
|
+
// Validate that an explicitly provided public key matches the secret key
|
|
71
|
+
if (this.#secretKey && params.publicKey) {
|
|
72
|
+
const derived = this.#secretKey.computePublicKey();
|
|
73
|
+
if (!this.#publicKey.equals(derived)) {
|
|
74
|
+
throw new KeyPairError('Public key does not match secret key', 'CONSTRUCTOR_ERROR');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
88
77
|
}
|
|
89
78
|
|
|
90
79
|
/**
|
|
@@ -112,18 +101,13 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
112
101
|
* @throws {KeyPairError} If the public key is not a valid pair with the secret key.
|
|
113
102
|
*/
|
|
114
103
|
set publicKey(publicKey: CompressedSecp256k1PublicKey) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if(!
|
|
118
|
-
throw new KeyPairError('
|
|
104
|
+
if(this.#secretKey) {
|
|
105
|
+
const derived = this.#secretKey.computePublicKey();
|
|
106
|
+
if(!publicKey.equals(derived)) {
|
|
107
|
+
throw new KeyPairError('Public key does not match secret key', 'PUBLIC_KEY_ERROR');
|
|
119
108
|
}
|
|
120
|
-
const cPk = this.secretKey.computePublicKey();
|
|
121
|
-
if(!publicKey.equals(cPk))
|
|
122
|
-
throw new KeyPairError('Public key is not a valid pair with the secret key', 'PUBLIC_KEY_ERROR');
|
|
123
109
|
}
|
|
124
110
|
this.#publicKey = publicKey;
|
|
125
|
-
this.#publicKeyMultibase = publicKey.multibase.encoded;
|
|
126
|
-
this.#secretKeyMultibase = this.#secretKey ? this.#secretKey.multibase : '';
|
|
127
111
|
}
|
|
128
112
|
|
|
129
113
|
/**
|
|
@@ -131,8 +115,15 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
131
115
|
* @returns {CompressedSecp256k1PublicKey} The CompressedSecp256k1PublicKey object
|
|
132
116
|
*/
|
|
133
117
|
get publicKey(): CompressedSecp256k1PublicKey {
|
|
134
|
-
|
|
135
|
-
|
|
118
|
+
return this.#publicKey;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Whether this key pair contains a secret key.
|
|
123
|
+
* @returns {boolean} True if the secret key is present.
|
|
124
|
+
*/
|
|
125
|
+
get hasSecretKey(): boolean {
|
|
126
|
+
return !!this.#secretKey;
|
|
136
127
|
}
|
|
137
128
|
|
|
138
129
|
/**
|
|
@@ -142,7 +133,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
142
133
|
get raw(): RawSchnorrKeyPair {
|
|
143
134
|
return {
|
|
144
135
|
public : this.publicKey.x,
|
|
145
|
-
secret : this
|
|
136
|
+
secret : this.#secretKey ? this.#secretKey.bytes : undefined
|
|
146
137
|
};
|
|
147
138
|
}
|
|
148
139
|
|
|
@@ -153,7 +144,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
153
144
|
get hex(): HexSchnorrKeyPair {
|
|
154
145
|
return {
|
|
155
146
|
public : this.publicKey.hex,
|
|
156
|
-
secret : this.#secretKey ? this
|
|
147
|
+
secret : this.#secretKey ? this.#secretKey.hex : undefined
|
|
157
148
|
};
|
|
158
149
|
}
|
|
159
150
|
|
|
@@ -163,22 +154,43 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
163
154
|
*/
|
|
164
155
|
get multibase(): MultibaseKeys {
|
|
165
156
|
return {
|
|
166
|
-
publicKeyMultibase : this.#
|
|
167
|
-
secretKeyMultibase : this.#
|
|
157
|
+
publicKeyMultibase : this.#publicKey.multibase.encoded,
|
|
158
|
+
secretKeyMultibase : this.#secretKey ? this.#secretKey.multibase : '',
|
|
168
159
|
};
|
|
169
160
|
}
|
|
170
161
|
|
|
171
162
|
/**
|
|
172
|
-
* JSON representation
|
|
173
|
-
*
|
|
163
|
+
* Safe JSON representation. Only includes the public key.
|
|
164
|
+
* Called implicitly by JSON.stringify(). Use exportJSON() for full serialization.
|
|
165
|
+
* @returns {{ publicKey: PublicKeyObject }} The JSON representation of the public key
|
|
174
166
|
*/
|
|
175
|
-
toJSON():
|
|
167
|
+
toJSON(): { publicKey: PublicKeyObject } {
|
|
168
|
+
return { publicKey: this.publicKey.toJSON() };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Exports the full key pair as a JSON object. Contains sensitive material.
|
|
173
|
+
* @returns {SchnorrKeyPairObject} The key pair as a JSON object
|
|
174
|
+
* @throws {KeyPairError} If the secret key is not available
|
|
175
|
+
*/
|
|
176
|
+
exportJSON(): SchnorrKeyPairObject {
|
|
177
|
+
if (!this.#secretKey) {
|
|
178
|
+
throw new KeyPairError(
|
|
179
|
+
'Cannot export: secret key required. Use publicKey.toJSON() for public-key-only pairs.',
|
|
180
|
+
'SERIALIZE_ERROR'
|
|
181
|
+
);
|
|
182
|
+
}
|
|
176
183
|
return {
|
|
177
|
-
secretKey : this
|
|
184
|
+
secretKey : this.#secretKey.exportJSON(),
|
|
178
185
|
publicKey : this.publicKey.toJSON()
|
|
179
186
|
};
|
|
180
187
|
}
|
|
181
188
|
|
|
189
|
+
/** @override Prevents secret material from appearing in Node.js inspect */
|
|
190
|
+
[Symbol.for('nodejs.util.inspect.custom')](): string {
|
|
191
|
+
return `[SchnorrKeyPair ${this.publicKey.hex}]`;
|
|
192
|
+
}
|
|
193
|
+
|
|
182
194
|
/**
|
|
183
195
|
* Static method creates a new Keys from a JSON object.
|
|
184
196
|
* @param {SchnorrKeyPairObject} keys The JSON object to initialize the Keys.
|
|
@@ -245,24 +257,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
245
257
|
* @returns {boolean} True if the public key and secret key are equal, false otherwise.
|
|
246
258
|
*/
|
|
247
259
|
static equals(kp: SchnorrKeyPair, otherKp: SchnorrKeyPair): boolean {
|
|
248
|
-
|
|
249
|
-
const pk = kp.publicKey;
|
|
250
|
-
const otherPk = otherKp.publicKey;
|
|
251
|
-
|
|
252
|
-
// If publicKeys present, use to compare as hex strings.
|
|
253
|
-
if(pk && otherPk) {
|
|
254
|
-
return pk.hex === otherPk.hex;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// Deconstruct the secret keys from the key pairs
|
|
258
|
-
const sk = kp.secretKey;
|
|
259
|
-
const otherSk = otherKp.secretKey;
|
|
260
|
-
if(sk && otherSk) {
|
|
261
|
-
// Get the public key hex strings for both key pair publicKeys
|
|
262
|
-
return sk.hex === otherSk.hex;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
throw new KeyPairError('Cannot compare invalid key pair(s)', 'KEYPAIR_EQUALS_ERROR');
|
|
260
|
+
return kp.publicKey.equals(otherKp.publicKey);
|
|
266
261
|
}
|
|
267
262
|
|
|
268
263
|
/**
|