@did-btcr2/keypair 0.7.2 → 0.8.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 +34 -32
- 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 +4 -4
- package/dist/cjs/secret.js.map +1 -1
- package/dist/esm/pair.js +34 -32
- 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 +4 -4
- package/dist/esm/secret.js.map +1 -1
- package/dist/types/pair.d.ts +9 -20
- 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/package.json +2 -2
- package/src/pair.ts +43 -46
- package/src/public.ts +99 -84
- package/src/secret.ts +4 -4
package/dist/cjs/pair.js
CHANGED
|
@@ -8,13 +8,13 @@ import { Secp256k1SecretKey } from './secret.js';
|
|
|
8
8
|
*/
|
|
9
9
|
export class SchnorrKeyPair {
|
|
10
10
|
/** @type {Secp256k1SecretKey} The secret key object */
|
|
11
|
-
|
|
11
|
+
#secretKey;
|
|
12
12
|
/** @type {CompressedSecp256k1PublicKey} The public key object */ ;
|
|
13
|
-
|
|
13
|
+
#publicKey;
|
|
14
14
|
/** @type {string} The public key in multibase format */
|
|
15
|
-
|
|
15
|
+
#publicKeyMultibase;
|
|
16
16
|
/** @type {string} The secret key in multibase format */
|
|
17
|
-
|
|
17
|
+
#secretKeyMultibase;
|
|
18
18
|
/**
|
|
19
19
|
* Creates an instance of Keys. Must provide a at least a secret key.
|
|
20
20
|
* Can optionally provide both a secret and public key, but must be a valid pair.
|
|
@@ -31,23 +31,23 @@ export class SchnorrKeyPair {
|
|
|
31
31
|
}
|
|
32
32
|
// Set the secretKey
|
|
33
33
|
if (params.secretKey instanceof Uint8Array) {
|
|
34
|
-
this
|
|
34
|
+
this.#secretKey = new Secp256k1SecretKey(params.secretKey);
|
|
35
35
|
}
|
|
36
36
|
else if (params.secretKey instanceof Secp256k1SecretKey) {
|
|
37
|
-
this
|
|
37
|
+
this.#secretKey = params.secretKey;
|
|
38
38
|
}
|
|
39
39
|
// Set the publicKey
|
|
40
40
|
if (params.publicKey instanceof CompressedSecp256k1PublicKey) {
|
|
41
|
-
this
|
|
41
|
+
this.#publicKey = params.publicKey;
|
|
42
42
|
}
|
|
43
43
|
else if (params.publicKey instanceof Uint8Array) {
|
|
44
|
-
this
|
|
44
|
+
this.#publicKey = new CompressedSecp256k1PublicKey(params.publicKey);
|
|
45
45
|
}
|
|
46
46
|
else {
|
|
47
|
-
this
|
|
47
|
+
this.#publicKey = this.#secretKey.computePublicKey();
|
|
48
48
|
}
|
|
49
|
-
this
|
|
50
|
-
this
|
|
49
|
+
this.#publicKeyMultibase = this.#publicKey.multibase.encoded;
|
|
50
|
+
this.#secretKeyMultibase = this.#secretKey ? this.#secretKey.multibase : '';
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
53
|
* Get the Secp256k1SecretKey.
|
|
@@ -56,15 +56,15 @@ export class SchnorrKeyPair {
|
|
|
56
56
|
*/
|
|
57
57
|
get secretKey() {
|
|
58
58
|
// If the secret key is not available, throw an error
|
|
59
|
-
if (!this
|
|
59
|
+
if (!this.#secretKey) {
|
|
60
60
|
throw new KeyPairError('Secret key not available', 'SECRET_KEY_ERROR');
|
|
61
61
|
}
|
|
62
62
|
// If the secret key is not valid, throw an error
|
|
63
|
-
if (!this.
|
|
63
|
+
if (!this.#secretKey.isValid()) {
|
|
64
64
|
throw new KeyPairError('Secret key is not valid', 'SECRET_KEY_ERROR');
|
|
65
65
|
}
|
|
66
66
|
// Return a copy of the secret key
|
|
67
|
-
const secret = this
|
|
67
|
+
const secret = this.#secretKey;
|
|
68
68
|
return secret;
|
|
69
69
|
}
|
|
70
70
|
/**
|
|
@@ -82,16 +82,16 @@ export class SchnorrKeyPair {
|
|
|
82
82
|
if (!publicKey.equals(cPk))
|
|
83
83
|
throw new KeyPairError('Public key is not a valid pair with the secret key', 'PUBLIC_KEY_ERROR');
|
|
84
84
|
}
|
|
85
|
-
this
|
|
86
|
-
this
|
|
87
|
-
this
|
|
85
|
+
this.#publicKey = publicKey;
|
|
86
|
+
this.#publicKeyMultibase = publicKey.multibase.encoded;
|
|
87
|
+
this.#secretKeyMultibase = this.#secretKey ? this.#secretKey.multibase : '';
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
90
|
* Get the CompressedSecp256k1PublicKey.
|
|
91
91
|
* @returns {CompressedSecp256k1PublicKey} The CompressedSecp256k1PublicKey object
|
|
92
92
|
*/
|
|
93
93
|
get publicKey() {
|
|
94
|
-
const publicKey = this
|
|
94
|
+
const publicKey = this.#publicKey;
|
|
95
95
|
return publicKey;
|
|
96
96
|
}
|
|
97
97
|
/**
|
|
@@ -111,7 +111,7 @@ export class SchnorrKeyPair {
|
|
|
111
111
|
get hex() {
|
|
112
112
|
return {
|
|
113
113
|
public: this.publicKey.hex,
|
|
114
|
-
secret: this
|
|
114
|
+
secret: this.#secretKey ? this.secretKey.hex : undefined
|
|
115
115
|
};
|
|
116
116
|
}
|
|
117
117
|
/**
|
|
@@ -120,8 +120,8 @@ export class SchnorrKeyPair {
|
|
|
120
120
|
*/
|
|
121
121
|
get multibase() {
|
|
122
122
|
return {
|
|
123
|
-
publicKeyMultibase: this
|
|
124
|
-
secretKeyMultibase: this
|
|
123
|
+
publicKeyMultibase: this.#publicKeyMultibase,
|
|
124
|
+
secretKeyMultibase: this.#secretKeyMultibase,
|
|
125
125
|
};
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
@@ -150,20 +150,22 @@ export class SchnorrKeyPair {
|
|
|
150
150
|
* @param {Secp256k1SecretKey | KeyBytes} data The secret key bytes
|
|
151
151
|
* @returns {SchnorrKeyPair} A new SchnorrKeyPair object
|
|
152
152
|
*/
|
|
153
|
-
static
|
|
154
|
-
// If the
|
|
155
|
-
|
|
156
|
-
//
|
|
157
|
-
|
|
158
|
-
|
|
153
|
+
static fromSecret(data) {
|
|
154
|
+
// If the data is Secp256k1SecretKey object, get the raw bytes
|
|
155
|
+
// Else if data is string, convert to byte array
|
|
156
|
+
// Else must be bytes, use them
|
|
157
|
+
const secret = typeof data === 'string'
|
|
158
|
+
? Buffer.from(data, 'hex')
|
|
159
|
+
: data;
|
|
160
|
+
// Check the lenth
|
|
161
|
+
if (secret.length !== 32) {
|
|
162
|
+
throw new KeyPairError('Invalid arg: must be 32 byte secret key', 'FROM_SECRET_KEY_ERROR');
|
|
159
163
|
}
|
|
160
164
|
// If pk Uint8Array, construct Secp256k1SecretKey object else use the object
|
|
161
|
-
const
|
|
165
|
+
const secretKey = new Secp256k1SecretKey(secret);
|
|
166
|
+
const publicKey = secretKey.computePublicKey();
|
|
162
167
|
// Return a new Keys object
|
|
163
|
-
return new SchnorrKeyPair({
|
|
164
|
-
secretKey: data instanceof Uint8Array ? new Secp256k1SecretKey(data) : data,
|
|
165
|
-
publicKey: secret.computePublicKey()
|
|
166
|
-
});
|
|
168
|
+
return new SchnorrKeyPair({ secretKey, publicKey });
|
|
167
169
|
}
|
|
168
170
|
/**
|
|
169
171
|
* Static method creates a new Keys (Secp256k1SecretKey/CompressedSecp256k1PublicKey) from bigint entropy.
|
package/dist/cjs/pair.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pair.js","sourceRoot":"","sources":["../../src/pair.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"pair.js","sourceRoot":"","sources":["../../src/pair.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,YAAY,EAEb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,4BAA4B,EAAa,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAa,MAAM,aAAa,CAAC;AAqB5D;;;;GAIG;AACH,MAAM,OAAO,cAAc;IACzB,uDA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,OAAO,CAAC,SAAS,CAAC;QAClC,IAAG,EAAE,IAAI,OAAO,EAAE,CAAC;YACjB,8DAA8D;YAC9D,OAAO,EAAE,CAAC,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC;QAChC,CAAC;QAED,MAAM,IAAI,YAAY,CAAC,oCAAoC,EAAE,sBAAsB,CAAC,CAAC;IACvF,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,QAAQ;QACpB,mCAAmC;QACnC,MAAM,EAAE,GAAG,kBAAkB,CAAC,MAAM,EAAE,CAAC;QAEvC,4CAA4C;QAC5C,MAAM,SAAS,GAAG,IAAI,kBAAkB,CAAC,EAAE,CAAC,CAAC;QAE7C,6CAA6C;QAC7C,MAAM,SAAS,GAAG,SAAS,CAAC,gBAAgB,EAAE,CAAC;QAE/C,2BAA2B;QAC3B,OAAO,IAAI,cAAc,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC;IACtD,CAAC;CACF"}
|
package/dist/cjs/public.js
CHANGED
|
@@ -21,23 +21,24 @@ export class CompressedSecp256k1PublicKey {
|
|
|
21
21
|
};
|
|
22
22
|
/**
|
|
23
23
|
* Creates a CompressedSecp256k1PublicKey instance.
|
|
24
|
-
* @param {Hex}
|
|
24
|
+
* @param {Hex} initialBytes The public key byte array.
|
|
25
25
|
* @throws {PublicKeyError} if the byte length is not 32 (x-only) or 33 (compressed)
|
|
26
26
|
*/
|
|
27
|
-
constructor(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
constructor(initialBytes) {
|
|
28
|
+
// Convert hex string to Uint8Array if necessary
|
|
29
|
+
const keyBytes = initialBytes instanceof Uint8Array
|
|
30
|
+
? initialBytes
|
|
31
|
+
: Uint8Array.from(Buffer.from(initialBytes, 'hex'));
|
|
31
32
|
// If the byte length is not 33, throw an error
|
|
32
|
-
if (!
|
|
33
|
-
throw new PublicKeyError('Invalid argument: byte length must be 33 (compressed)', 'CONSTRUCTOR_ERROR', {
|
|
33
|
+
if (!keyBytes || keyBytes.length !== 33) {
|
|
34
|
+
throw new PublicKeyError('Invalid argument: byte length must be 33 (compressed)', 'CONSTRUCTOR_ERROR', { keyBytes });
|
|
34
35
|
}
|
|
35
36
|
// Validate the point is on curve and in compressed form
|
|
36
|
-
if (!tinysecp.isPoint(
|
|
37
|
-
throw new PublicKeyError('Invalid argument: not a valid secp256k1 compressed point', 'CONSTRUCTOR_ERROR', {
|
|
37
|
+
if (!tinysecp.isPoint(keyBytes)) {
|
|
38
|
+
throw new PublicKeyError('Invalid argument: not a valid secp256k1 compressed point', 'CONSTRUCTOR_ERROR', { keyBytes });
|
|
38
39
|
}
|
|
39
40
|
// Set the bytes
|
|
40
|
-
this.#bytes =
|
|
41
|
+
this.#bytes = keyBytes;
|
|
41
42
|
// Set multibase
|
|
42
43
|
this.#multibase.encoded = this.encode();
|
|
43
44
|
this.#multibase.key = [...this.#multibase.prefix, ...this.compressed];
|
|
@@ -134,26 +135,6 @@ export class CompressedSecp256k1PublicKey {
|
|
|
134
135
|
bip340() {
|
|
135
136
|
return this.xOnly;
|
|
136
137
|
}
|
|
137
|
-
/**
|
|
138
|
-
* Returns the point of the public key.
|
|
139
|
-
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
140
|
-
* @returns {Point} The point of the public key.
|
|
141
|
-
* @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
|
|
142
|
-
*/
|
|
143
|
-
static point(pk) {
|
|
144
|
-
// If the public key is a hex string, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
145
|
-
if (typeof pk === 'string' && /^[0-9a-fA-F]+$/.test(pk)) {
|
|
146
|
-
const publicKey = new CompressedSecp256k1PublicKey(Buffer.fromHex(pk));
|
|
147
|
-
return publicKey.point;
|
|
148
|
-
}
|
|
149
|
-
// If the public key is a byte array or ArrayBuffer, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
150
|
-
if (pk instanceof Uint8Array || ArrayBuffer.isView(pk)) {
|
|
151
|
-
const publicKey = new CompressedSecp256k1PublicKey(pk);
|
|
152
|
-
return publicKey.point;
|
|
153
|
-
}
|
|
154
|
-
// If the public key is neither a hex string nor a byte array, throw an error
|
|
155
|
-
throw new PublicKeyError('Invalid publicKey: must be a hex string or byte array', 'POINT_ERROR', { publicKey: pk });
|
|
156
|
-
}
|
|
157
138
|
/**
|
|
158
139
|
* Decodes the multibase string to the 35-byte corresponding public key (2 byte prefix + 32 byte public key).
|
|
159
140
|
* @returns {KeyBytes} The decoded public key: prefix and public key bytes
|
|
@@ -182,17 +163,17 @@ export class CompressedSecp256k1PublicKey {
|
|
|
182
163
|
*/
|
|
183
164
|
encode() {
|
|
184
165
|
// Convert public key bytes to an array
|
|
185
|
-
const pk = this.compressed
|
|
166
|
+
const pk = Array.from(this.compressed);
|
|
186
167
|
// Ensure the public key is 33-byte secp256k1 compressed public key
|
|
187
168
|
if (pk.length !== 33) {
|
|
188
169
|
throw new PublicKeyError('Invalid argument: must be 33-byte (compressed) public key', 'ENCODE_MULTIBASE_ERROR');
|
|
189
170
|
}
|
|
190
171
|
// Convert prefix to an array
|
|
191
|
-
const publicKeyMultibase =
|
|
172
|
+
const publicKeyMultibase = Array.from(BIP340_PUBLIC_KEY_MULTIBASE_PREFIX);
|
|
192
173
|
// Push the public key bytes at the end of the prefix
|
|
193
174
|
publicKeyMultibase.push(...pk);
|
|
194
175
|
// Encode the bytes in base58btc format and return
|
|
195
|
-
return base58btc.encode(
|
|
176
|
+
return base58btc.encode(Uint8Array.from(publicKeyMultibase));
|
|
196
177
|
}
|
|
197
178
|
/**
|
|
198
179
|
* Verify a signature using schnorr or ecdsa.
|
|
@@ -230,40 +211,12 @@ export class CompressedSecp256k1PublicKey {
|
|
|
230
211
|
hex: this.hex,
|
|
231
212
|
multibase: this.multibase,
|
|
232
213
|
point: {
|
|
233
|
-
x: this.x
|
|
234
|
-
y: this.y
|
|
214
|
+
x: Array.from(this.x),
|
|
215
|
+
y: Array.from(this.y),
|
|
235
216
|
parity: this.parity,
|
|
236
217
|
},
|
|
237
218
|
};
|
|
238
219
|
}
|
|
239
|
-
/**
|
|
240
|
-
* Creates a CompressedSecp256k1PublicKey object from a JSON representation.
|
|
241
|
-
* @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
|
|
242
|
-
* @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
|
|
243
|
-
*/
|
|
244
|
-
static fromJSON(json) {
|
|
245
|
-
json.point.x.unshift(json.point.parity);
|
|
246
|
-
return new CompressedSecp256k1PublicKey(json.point.x.toUint8Array());
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Computes the deterministic public key for a given secret key.
|
|
250
|
-
* @param {Secp256k1SecretKey | KeyBytes} sk The Secp256k1SecretKey object or the secret key bytes
|
|
251
|
-
* @returns {CompressedSecp256k1PublicKey} A new CompressedSecp256k1PublicKey object
|
|
252
|
-
*/
|
|
253
|
-
static fromSecretKey(sk) {
|
|
254
|
-
// If the secret key is a Secp256k1SecretKey object, get the raw bytes else use the bytes
|
|
255
|
-
const bytes = sk instanceof Secp256k1SecretKey ? sk.bytes : sk;
|
|
256
|
-
// Throw error if the secret key is not 32 bytes
|
|
257
|
-
if (bytes.length !== 32) {
|
|
258
|
-
throw new PublicKeyError('Invalid arg: must be 32 byte secret key', 'FROM_SECRET_KEY_ERROR');
|
|
259
|
-
}
|
|
260
|
-
// Compute the public key from the secret key
|
|
261
|
-
const secret = sk instanceof Secp256k1SecretKey
|
|
262
|
-
? sk
|
|
263
|
-
: new Secp256k1SecretKey(sk);
|
|
264
|
-
// Return a new CompressedSecp256k1PublicKey object
|
|
265
|
-
return secret.computePublicKey();
|
|
266
|
-
}
|
|
267
220
|
/**
|
|
268
221
|
* Computes modular exponentiation: (base^exp) % mod.
|
|
269
222
|
* Used for computing modular square roots.
|
|
@@ -314,10 +267,72 @@ export class CompressedSecp256k1PublicKey {
|
|
|
314
267
|
// Compute y (do not enforce parity)
|
|
315
268
|
const y = this.sqrtMod(ySquared, CURVE.p);
|
|
316
269
|
// Convert x and y to Uint8Array
|
|
317
|
-
const yBytes = Buffer.
|
|
270
|
+
const yBytes = Buffer.from(y.toString(16).padStart(64, '0'), 'hex');
|
|
318
271
|
// Return 65-byte uncompressed public key: `0x04 || x || y`
|
|
319
272
|
return new Uint8Array(Buffer.concat([Buffer.from([0x04]), Buffer.from(this.x), yBytes]));
|
|
320
273
|
}
|
|
321
274
|
;
|
|
275
|
+
/**
|
|
276
|
+
* Static method to validate a public key.
|
|
277
|
+
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
278
|
+
* @returns {boolean} True if the public key is valid, false otherwise.
|
|
279
|
+
*/
|
|
280
|
+
static isValid(pk) {
|
|
281
|
+
try {
|
|
282
|
+
new CompressedSecp256k1PublicKey(pk);
|
|
283
|
+
return true;
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
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
|
+
/**
|
|
310
|
+
* Creates a CompressedSecp256k1PublicKey object from a JSON representation.
|
|
311
|
+
* @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
|
|
312
|
+
* @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
|
|
313
|
+
*/
|
|
314
|
+
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();
|
|
336
|
+
}
|
|
322
337
|
}
|
|
323
338
|
//# sourceMappingURL=public.js.map
|
package/dist/cjs/public.js.map
CHANGED
|
@@ -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;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAyGjD;;;;;;GAMG;AACH,MAAM,OAAO,4BAA4B;IACvC,4CAA4C;IACnC,MAAM,CAAW;IAE1B,kEAAkE;IACzD,UAAU,GAAoB;QACrC,MAAM,EAAI,kCAAkC;QAC5C,GAAG,EAAO,EAAE;QACZ,OAAO,EAAG,EAAE;KACb,CAAC;IAEF;;;;OAIG;IACH,YAAY,
|
|
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;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAyGjD;;;;;;GAMG;AACH,MAAM,OAAO,4BAA4B;IACvC,4CAA4C;IACnC,MAAM,CAAW;IAE1B,kEAAkE;IACzD,UAAU,GAAoB;QACrC,MAAM,EAAI,kCAAkC;QAC5C,GAAG,EAAO,EAAE;QACZ,OAAO,EAAG,EAAE;KACb,CAAC;IAEF;;;;OAIG;IACH,YAAY,YAAiB;QAC3B,gDAAgD;QAChD,MAAM,QAAQ,GAAG,YAAY,YAAY,UAAU;Y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M,CAAC,CAAC;QACxC,OAAO,IAAI,4BAA4B,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACzE,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,aAAa,CAAC,EAAiC;QACpD,yFAAyF;QACzF,MAAM,KAAK,GAAG,EAAE,YAAY,kBAAkB,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;QAE/D,gDAAgD;QAChD,IAAG,KAAK,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;YACvB,MAAM,IAAI,cAAc,CAAC,yCAAyC,EAAE,uBAAuB,CAAC,CAAC;QAC/F,CAAC;QAED,6CAA6C;QAC7C,MAAM,MAAM,GAAG,EAAE,YAAY,kBAAkB;YAC7C,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,IAAI,kBAAkB,CAAC,EAAE,CAAC,CAAC;QAE/B,mDAAmD;QACnD,OAAO,MAAM,CAAC,gBAAgB,EAAE,CAAC;IACnC,CAAC;CACF"}
|
package/dist/cjs/secret.js
CHANGED
|
@@ -91,16 +91,16 @@ export class Secp256k1SecretKey {
|
|
|
91
91
|
*/
|
|
92
92
|
encode() {
|
|
93
93
|
// Convert Uint8Array bytes to an Array
|
|
94
|
-
const secretKeyBytes = this.bytes
|
|
94
|
+
const secretKeyBytes = Array.from(this.bytes);
|
|
95
95
|
if (secretKeyBytes.length !== 32) {
|
|
96
96
|
throw new SecretKeyError('Invalid secret key: must be a valid 32-byte secret key', 'ENCODE_MULTIBASE_ERROR');
|
|
97
97
|
}
|
|
98
98
|
// Convert prefix to an array
|
|
99
|
-
const mbaseBytes =
|
|
99
|
+
const mbaseBytes = Array.from(BIP340_SECRET_KEY_MULTIBASE_PREFIX);
|
|
100
100
|
// Push the secret key bytes at the end of the prefix
|
|
101
101
|
mbaseBytes.push(...secretKeyBytes);
|
|
102
102
|
// Encode the bytes in base58btc format and return
|
|
103
|
-
return base58btc.encode(
|
|
103
|
+
return base58btc.encode(Uint8Array.from(mbaseBytes));
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
106
|
* Checks if this secret key is equal to another.
|
|
@@ -134,7 +134,7 @@ export class Secp256k1SecretKey {
|
|
|
134
134
|
*/
|
|
135
135
|
json() {
|
|
136
136
|
return {
|
|
137
|
-
bytes: this.bytes
|
|
137
|
+
bytes: Array.from(this.bytes),
|
|
138
138
|
seed: this.seed.toString(),
|
|
139
139
|
hex: this.hex,
|
|
140
140
|
};
|
package/dist/cjs/secret.js.map
CHANGED
|
@@ -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,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAsD3D;;;;;;;GAOG;AACH,MAAM,OAAO,kBAAkB;IAC7B,sEAAsE;IAC7D,MAAM,CAAY;IAE3B,gEAAgE;IACvD,KAAK,CAAU;IAExB,4DAA4D;IACnD,UAAU,CAAS;IAE5B;;;;OAIG;IACH,YAAY,OAAgB;QAC1B,4DAA4D;QAC5D,MAAM,OAAO,GAAG,OAAO,YAAY,UAAU,CAAC;QAC9C,MAAM,QAAQ,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC;QAC7C,IAAG,CAAC,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;YACzB,MAA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AAI,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;QAClG,yCAAyC;QACzC,OAAO,IAAI,kBAAkB,CAAC,eAAe,CAAC,CAAC;IACjD,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM;QAClB,+BAA+B;QAC/B,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;QAErC,4EAA4E;QAC5E,OAAO,eAAe,CAAC,SAAS,CAAC,CAAC;IACpC,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"}
|
package/dist/esm/pair.js
CHANGED
|
@@ -8,13 +8,13 @@ import { Secp256k1SecretKey } from './secret.js';
|
|
|
8
8
|
*/
|
|
9
9
|
export class SchnorrKeyPair {
|
|
10
10
|
/** @type {Secp256k1SecretKey} The secret key object */
|
|
11
|
-
|
|
11
|
+
#secretKey;
|
|
12
12
|
/** @type {CompressedSecp256k1PublicKey} The public key object */ ;
|
|
13
|
-
|
|
13
|
+
#publicKey;
|
|
14
14
|
/** @type {string} The public key in multibase format */
|
|
15
|
-
|
|
15
|
+
#publicKeyMultibase;
|
|
16
16
|
/** @type {string} The secret key in multibase format */
|
|
17
|
-
|
|
17
|
+
#secretKeyMultibase;
|
|
18
18
|
/**
|
|
19
19
|
* Creates an instance of Keys. Must provide a at least a secret key.
|
|
20
20
|
* Can optionally provide both a secret and public key, but must be a valid pair.
|
|
@@ -31,23 +31,23 @@ export class SchnorrKeyPair {
|
|
|
31
31
|
}
|
|
32
32
|
// Set the secretKey
|
|
33
33
|
if (params.secretKey instanceof Uint8Array) {
|
|
34
|
-
this
|
|
34
|
+
this.#secretKey = new Secp256k1SecretKey(params.secretKey);
|
|
35
35
|
}
|
|
36
36
|
else if (params.secretKey instanceof Secp256k1SecretKey) {
|
|
37
|
-
this
|
|
37
|
+
this.#secretKey = params.secretKey;
|
|
38
38
|
}
|
|
39
39
|
// Set the publicKey
|
|
40
40
|
if (params.publicKey instanceof CompressedSecp256k1PublicKey) {
|
|
41
|
-
this
|
|
41
|
+
this.#publicKey = params.publicKey;
|
|
42
42
|
}
|
|
43
43
|
else if (params.publicKey instanceof Uint8Array) {
|
|
44
|
-
this
|
|
44
|
+
this.#publicKey = new CompressedSecp256k1PublicKey(params.publicKey);
|
|
45
45
|
}
|
|
46
46
|
else {
|
|
47
|
-
this
|
|
47
|
+
this.#publicKey = this.#secretKey.computePublicKey();
|
|
48
48
|
}
|
|
49
|
-
this
|
|
50
|
-
this
|
|
49
|
+
this.#publicKeyMultibase = this.#publicKey.multibase.encoded;
|
|
50
|
+
this.#secretKeyMultibase = this.#secretKey ? this.#secretKey.multibase : '';
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
53
|
* Get the Secp256k1SecretKey.
|
|
@@ -56,15 +56,15 @@ export class SchnorrKeyPair {
|
|
|
56
56
|
*/
|
|
57
57
|
get secretKey() {
|
|
58
58
|
// If the secret key is not available, throw an error
|
|
59
|
-
if (!this
|
|
59
|
+
if (!this.#secretKey) {
|
|
60
60
|
throw new KeyPairError('Secret key not available', 'SECRET_KEY_ERROR');
|
|
61
61
|
}
|
|
62
62
|
// If the secret key is not valid, throw an error
|
|
63
|
-
if (!this.
|
|
63
|
+
if (!this.#secretKey.isValid()) {
|
|
64
64
|
throw new KeyPairError('Secret key is not valid', 'SECRET_KEY_ERROR');
|
|
65
65
|
}
|
|
66
66
|
// Return a copy of the secret key
|
|
67
|
-
const secret = this
|
|
67
|
+
const secret = this.#secretKey;
|
|
68
68
|
return secret;
|
|
69
69
|
}
|
|
70
70
|
/**
|
|
@@ -82,16 +82,16 @@ export class SchnorrKeyPair {
|
|
|
82
82
|
if (!publicKey.equals(cPk))
|
|
83
83
|
throw new KeyPairError('Public key is not a valid pair with the secret key', 'PUBLIC_KEY_ERROR');
|
|
84
84
|
}
|
|
85
|
-
this
|
|
86
|
-
this
|
|
87
|
-
this
|
|
85
|
+
this.#publicKey = publicKey;
|
|
86
|
+
this.#publicKeyMultibase = publicKey.multibase.encoded;
|
|
87
|
+
this.#secretKeyMultibase = this.#secretKey ? this.#secretKey.multibase : '';
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
90
|
* Get the CompressedSecp256k1PublicKey.
|
|
91
91
|
* @returns {CompressedSecp256k1PublicKey} The CompressedSecp256k1PublicKey object
|
|
92
92
|
*/
|
|
93
93
|
get publicKey() {
|
|
94
|
-
const publicKey = this
|
|
94
|
+
const publicKey = this.#publicKey;
|
|
95
95
|
return publicKey;
|
|
96
96
|
}
|
|
97
97
|
/**
|
|
@@ -111,7 +111,7 @@ export class SchnorrKeyPair {
|
|
|
111
111
|
get hex() {
|
|
112
112
|
return {
|
|
113
113
|
public: this.publicKey.hex,
|
|
114
|
-
secret: this
|
|
114
|
+
secret: this.#secretKey ? this.secretKey.hex : undefined
|
|
115
115
|
};
|
|
116
116
|
}
|
|
117
117
|
/**
|
|
@@ -120,8 +120,8 @@ export class SchnorrKeyPair {
|
|
|
120
120
|
*/
|
|
121
121
|
get multibase() {
|
|
122
122
|
return {
|
|
123
|
-
publicKeyMultibase: this
|
|
124
|
-
secretKeyMultibase: this
|
|
123
|
+
publicKeyMultibase: this.#publicKeyMultibase,
|
|
124
|
+
secretKeyMultibase: this.#secretKeyMultibase,
|
|
125
125
|
};
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
@@ -150,20 +150,22 @@ export class SchnorrKeyPair {
|
|
|
150
150
|
* @param {Secp256k1SecretKey | KeyBytes} data The secret key bytes
|
|
151
151
|
* @returns {SchnorrKeyPair} A new SchnorrKeyPair object
|
|
152
152
|
*/
|
|
153
|
-
static
|
|
154
|
-
// If the
|
|
155
|
-
|
|
156
|
-
//
|
|
157
|
-
|
|
158
|
-
|
|
153
|
+
static fromSecret(data) {
|
|
154
|
+
// If the data is Secp256k1SecretKey object, get the raw bytes
|
|
155
|
+
// Else if data is string, convert to byte array
|
|
156
|
+
// Else must be bytes, use them
|
|
157
|
+
const secret = typeof data === 'string'
|
|
158
|
+
? Buffer.from(data, 'hex')
|
|
159
|
+
: data;
|
|
160
|
+
// Check the lenth
|
|
161
|
+
if (secret.length !== 32) {
|
|
162
|
+
throw new KeyPairError('Invalid arg: must be 32 byte secret key', 'FROM_SECRET_KEY_ERROR');
|
|
159
163
|
}
|
|
160
164
|
// If pk Uint8Array, construct Secp256k1SecretKey object else use the object
|
|
161
|
-
const
|
|
165
|
+
const secretKey = new Secp256k1SecretKey(secret);
|
|
166
|
+
const publicKey = secretKey.computePublicKey();
|
|
162
167
|
// Return a new Keys object
|
|
163
|
-
return new SchnorrKeyPair({
|
|
164
|
-
secretKey: data instanceof Uint8Array ? new Secp256k1SecretKey(data) : data,
|
|
165
|
-
publicKey: secret.computePublicKey()
|
|
166
|
-
});
|
|
168
|
+
return new SchnorrKeyPair({ secretKey, publicKey });
|
|
167
169
|
}
|
|
168
170
|
/**
|
|
169
171
|
* Static method creates a new Keys (Secp256k1SecretKey/CompressedSecp256k1PublicKey) from bigint entropy.
|