@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/dist/esm/public.js
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
import { BIP340_PUBLIC_KEY_MULTIBASE_PREFIX, BIP340_PUBLIC_KEY_MULTIBASE_PREFIX_HASH, CURVE, PublicKeyError } from '@did-btcr2/common';
|
|
2
2
|
import { sha256 } from '@noble/hashes/sha2';
|
|
3
3
|
import { base58btc } from 'multiformats/bases/base58';
|
|
4
|
-
import
|
|
4
|
+
import * as tinysecp from 'tiny-secp256k1';
|
|
5
|
+
import { Secp256k1SecretKey } from './secret.js';
|
|
5
6
|
/**
|
|
6
7
|
* Encapsulates a secp256k1 public key compliant to BIP-340 BIP schnorr signature scheme.
|
|
7
8
|
* Provides get methods for different formats (compressed, x-only, multibase).
|
|
8
9
|
* Provides helpers methods for comparison and serialization.
|
|
9
|
-
* @class
|
|
10
|
-
* @type {
|
|
10
|
+
* @class CompressedSecp256k1PublicKey
|
|
11
|
+
* @type {CompressedSecp256k1PublicKey}
|
|
11
12
|
*/
|
|
12
|
-
export class
|
|
13
|
+
export class CompressedSecp256k1PublicKey {
|
|
13
14
|
/** @type {KeyBytes} The public key bytes */
|
|
14
15
|
_bytes;
|
|
15
16
|
/** @type {MultibaseObject} The public key as a MultibaseObject */
|
|
16
17
|
_multibase = {
|
|
17
18
|
prefix: BIP340_PUBLIC_KEY_MULTIBASE_PREFIX,
|
|
18
19
|
key: [],
|
|
19
|
-
|
|
20
|
+
encoded: ''
|
|
20
21
|
};
|
|
21
22
|
/**
|
|
22
|
-
* Creates a
|
|
23
|
+
* Creates a CompressedSecp256k1PublicKey instance.
|
|
23
24
|
* @param {KeyBytes} bytes The public key byte array.
|
|
24
25
|
* @throws {PublicKeyError} if the byte length is not 32 (x-only) or 33 (compressed)
|
|
25
26
|
*/
|
|
@@ -28,10 +29,14 @@ export class PublicKey {
|
|
|
28
29
|
if (bytes.length !== 33) {
|
|
29
30
|
throw new PublicKeyError('Invalid argument: byte length must be 33 (compressed)', 'CONSTRUCTOR_ERROR', { bytes });
|
|
30
31
|
}
|
|
32
|
+
// Validate the point is on curve and in compressed form
|
|
33
|
+
if (!tinysecp.isPoint(bytes)) {
|
|
34
|
+
throw new PublicKeyError('Invalid argument: bytes are not a valid secp256k1 compressed point', 'CONSTRUCTOR_ERROR', { bytes });
|
|
35
|
+
}
|
|
31
36
|
// Set the bytes
|
|
32
37
|
this._bytes = bytes;
|
|
33
38
|
// Set multibase
|
|
34
|
-
this._multibase.
|
|
39
|
+
this._multibase.encoded = this.encode();
|
|
35
40
|
this._multibase.key = [...this._multibase.prefix, ...this.compressed];
|
|
36
41
|
}
|
|
37
42
|
/**
|
|
@@ -52,13 +57,30 @@ export class PublicKey {
|
|
|
52
57
|
return uncompressed;
|
|
53
58
|
}
|
|
54
59
|
/**
|
|
55
|
-
*
|
|
56
|
-
|
|
60
|
+
* X-only (32-byte) view of the public key per BIP-340.
|
|
61
|
+
*/
|
|
62
|
+
get xOnly() {
|
|
63
|
+
return this._bytes.slice(1);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Parity of the SEC compressed public key.
|
|
67
|
+
* @returns {0x02 | 0x03} The parity byte (0x02 if even, 0x03 if odd).
|
|
68
|
+
* @throws {PublicKeyError} If the parity byte is not 0x02 or 0x03.
|
|
57
69
|
*/
|
|
58
70
|
get parity() {
|
|
59
|
-
const parity = this.
|
|
71
|
+
const parity = this._bytes[0];
|
|
72
|
+
if (![0x02, 0x03].includes(parity)) {
|
|
73
|
+
throw new PublicKeyError('Invalid state: parity byte must be 2 or 3', 'PARITY_ERROR', { parity });
|
|
74
|
+
}
|
|
60
75
|
return parity;
|
|
61
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Whether the SEC compressed public key has even Y.
|
|
79
|
+
* @returns {boolean} True if the public key has even Y.
|
|
80
|
+
*/
|
|
81
|
+
get isEven() {
|
|
82
|
+
return this._bytes[0] === 0x02;
|
|
83
|
+
}
|
|
62
84
|
/**
|
|
63
85
|
* Get the x-coordinate of the public key.
|
|
64
86
|
* @returns {Uint8Array} The 32-byte x-coordinate of the public key.
|
|
@@ -101,6 +123,13 @@ export class PublicKey {
|
|
|
101
123
|
y: this.y
|
|
102
124
|
};
|
|
103
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Returns the BIP-340 (x-only) representation of this key.
|
|
128
|
+
* @returns {KeyBytes} The BIP-340 (x-only) representation of the public key.
|
|
129
|
+
*/
|
|
130
|
+
bip340() {
|
|
131
|
+
return this.xOnly;
|
|
132
|
+
}
|
|
104
133
|
/**
|
|
105
134
|
* Returns the point of the public key.
|
|
106
135
|
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
@@ -108,14 +137,14 @@ export class PublicKey {
|
|
|
108
137
|
* @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
|
|
109
138
|
*/
|
|
110
139
|
static point(pk) {
|
|
111
|
-
// If the public key is a hex string, convert it to a
|
|
140
|
+
// If the public key is a hex string, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
112
141
|
if (typeof pk === 'string' && /^[0-9a-fA-F]+$/.test(pk)) {
|
|
113
|
-
const publicKey = new
|
|
142
|
+
const publicKey = new CompressedSecp256k1PublicKey(Buffer.fromHex(pk));
|
|
114
143
|
return publicKey.point;
|
|
115
144
|
}
|
|
116
|
-
// If the public key is a byte array or ArrayBuffer, convert it to a
|
|
145
|
+
// If the public key is a byte array or ArrayBuffer, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
117
146
|
if (pk instanceof Uint8Array || ArrayBuffer.isView(pk)) {
|
|
118
|
-
const publicKey = new
|
|
147
|
+
const publicKey = new CompressedSecp256k1PublicKey(pk);
|
|
119
148
|
return publicKey.point;
|
|
120
149
|
}
|
|
121
150
|
// If the public key is neither a hex string nor a byte array, throw an error
|
|
@@ -127,7 +156,7 @@ export class PublicKey {
|
|
|
127
156
|
*/
|
|
128
157
|
decode() {
|
|
129
158
|
// Decode the public key multibase string
|
|
130
|
-
const decoded = base58btc.decode(this.multibase.
|
|
159
|
+
const decoded = base58btc.decode(this.multibase.encoded);
|
|
131
160
|
// If the public key bytes are not 35 bytes, throw an error
|
|
132
161
|
if (decoded.length !== 35) {
|
|
133
162
|
throw new PublicKeyError('Invalid argument: must be 35 byte publicKeyMultibase', 'DECODE_MULTIBASE_ERROR');
|
|
@@ -163,15 +192,15 @@ export class PublicKey {
|
|
|
163
192
|
}
|
|
164
193
|
/**
|
|
165
194
|
* Compares this public key to another public key.
|
|
166
|
-
* @param {
|
|
195
|
+
* @param {CompressedSecp256k1PublicKey} other The other public key to compare
|
|
167
196
|
* @returns {boolean} True if the public keys are equal, false otherwise.
|
|
168
197
|
*/
|
|
169
198
|
equals(other) {
|
|
170
199
|
return this.hex === other.hex;
|
|
171
200
|
}
|
|
172
201
|
/**
|
|
173
|
-
* JSON representation of a
|
|
174
|
-
* @returns {PublicKeyObject} The
|
|
202
|
+
* JSON representation of a CompressedSecp256k1PublicKey object.
|
|
203
|
+
* @returns {PublicKeyObject} The CompressedSecp256k1PublicKey as a JSON object.
|
|
175
204
|
*/
|
|
176
205
|
json() {
|
|
177
206
|
return {
|
|
@@ -185,30 +214,32 @@ export class PublicKey {
|
|
|
185
214
|
};
|
|
186
215
|
}
|
|
187
216
|
/**
|
|
188
|
-
* Creates a
|
|
189
|
-
* @param {PublicKeyObject} json The JSON object to initialize the
|
|
190
|
-
* @returns {
|
|
217
|
+
* Creates a CompressedSecp256k1PublicKey object from a JSON representation.
|
|
218
|
+
* @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
|
|
219
|
+
* @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
|
|
191
220
|
*/
|
|
192
221
|
static fromJSON(json) {
|
|
193
222
|
json.x.unshift(json.parity);
|
|
194
|
-
return new
|
|
223
|
+
return new CompressedSecp256k1PublicKey(json.x.toUint8Array());
|
|
195
224
|
}
|
|
196
225
|
/**
|
|
197
|
-
* Computes the deterministic public key for a given
|
|
198
|
-
* @param {
|
|
199
|
-
* @returns {
|
|
226
|
+
* Computes the deterministic public key for a given secret key.
|
|
227
|
+
* @param {Secp256k1SecretKey | KeyBytes} sk The Secp256k1SecretKey object or the secret key bytes
|
|
228
|
+
* @returns {CompressedSecp256k1PublicKey} A new CompressedSecp256k1PublicKey object
|
|
200
229
|
*/
|
|
201
230
|
static fromSecretKey(sk) {
|
|
202
|
-
// If the
|
|
203
|
-
const bytes = sk instanceof
|
|
204
|
-
// Throw error if the
|
|
231
|
+
// If the secret key is a Secp256k1SecretKey object, get the raw bytes else use the bytes
|
|
232
|
+
const bytes = sk instanceof Secp256k1SecretKey ? sk.bytes : sk;
|
|
233
|
+
// Throw error if the secret key is not 32 bytes
|
|
205
234
|
if (bytes.length !== 32) {
|
|
206
|
-
throw new PublicKeyError('Invalid arg: must be 32 byte
|
|
235
|
+
throw new PublicKeyError('Invalid arg: must be 32 byte secret key', 'FROM_SECRET_KEY_ERROR');
|
|
207
236
|
}
|
|
208
|
-
// Compute the public key from the
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
237
|
+
// Compute the public key from the secret key
|
|
238
|
+
const secret = sk instanceof Secp256k1SecretKey
|
|
239
|
+
? sk
|
|
240
|
+
: new Secp256k1SecretKey(sk);
|
|
241
|
+
// Return a new CompressedSecp256k1PublicKey object
|
|
242
|
+
return secret.computePublicKey();
|
|
212
243
|
}
|
|
213
244
|
/**
|
|
214
245
|
* Computes modular exponentiation: (base^exp) % mod.
|
|
@@ -265,19 +296,5 @@ export class PublicKey {
|
|
|
265
296
|
return new Uint8Array(Buffer.concat([Buffer.from([0x04]), Buffer.from(this.x), yBytes]));
|
|
266
297
|
}
|
|
267
298
|
;
|
|
268
|
-
/**
|
|
269
|
-
* Static version of liftX method.
|
|
270
|
-
* @param {KeyBytes} x The 32-byte x-coordinate to lift.
|
|
271
|
-
* @returns {Uint8Array} The 65-byte uncompressed public key (0x04, x, y).
|
|
272
|
-
*/
|
|
273
|
-
static xOnly(x) {
|
|
274
|
-
// Ensure x-coordinate is 32 bytes
|
|
275
|
-
if (x.length !== 32) {
|
|
276
|
-
throw new PublicKeyError('Invalid argument: x-coordinate length must be 32 bytes', 'LIFT_X_ERROR');
|
|
277
|
-
}
|
|
278
|
-
// Create a PublicKey instance and lift the x-coordinate
|
|
279
|
-
const publicKey = new PublicKey(x);
|
|
280
|
-
return publicKey.x;
|
|
281
|
-
}
|
|
282
299
|
}
|
|
283
300
|
//# sourceMappingURL=public.js.map
|
package/dist/esm/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,EACvC,KAAK,EAKL,cAAc,EAEf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"public.js","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kCAAkC,EAClC,uCAAuC,EACvC,KAAK,EAKL,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;AAmGjD;;;;;;GAMG;AACH,MAAM,OAAO,4BAA4B;IACvC,4CAA4C;IAC3B,MAAM,CAAW;IAElC,kEAAkE;IAC1D,UAAU,GAAoB;QACpC,MAAM,EAAI,kCAAkC;QAC5C,GAAG,EAAO,EAAE;QACZ,OAAO,EAAG,EAAE;KACb,CAAC;IAEF;;;;OAIG;IACH,YAAY,KAAe;QACzB,+CAA+C;QAC/C,IAAG,KAAK,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;YACvB,MAAM,IAAI,cAAc,CACtB,uDAAuD,EACvD,mBAAmB,EAAE,EAAE,KAAK,EAAE,CAC/B,CAAC;QACJ,CAAC;QAED,wDAAwD;QACxD,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YAC7B,MAAM,IAAI,cAAc,CACtB,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,KAAK,CAAC,CAAC,CAAC,CAAC;QAEvD,oCAAoC;QACpC,MAAM,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QAE1C,gCAAgC;QAChC,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;QAEhE,2DAA2D;QAC3D,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;IAC3F,CAAC;IAAA,CAAC;CACH"}
|
package/dist/esm/secret.js
CHANGED
|
@@ -4,15 +4,16 @@ import { getRandomValues } from 'crypto';
|
|
|
4
4
|
import { base58btc } from 'multiformats/bases/base58';
|
|
5
5
|
import * as tinysecp from 'tiny-secp256k1';
|
|
6
6
|
import { SchnorrKeyPair } from './pair.js';
|
|
7
|
+
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
7
8
|
/**
|
|
8
9
|
* Encapsulates a secp256k1 secret key
|
|
9
10
|
* Provides get methods for different formats (raw, secret, point).
|
|
10
11
|
* Provides helpers methods for comparison, serialization and publicKey generation.
|
|
11
|
-
* @class
|
|
12
|
-
* @type {
|
|
13
|
-
*
|
|
12
|
+
* @class Secp256k1SecretKey
|
|
13
|
+
* @type {Secp256k1SecretKey}
|
|
14
|
+
* @implements {SecretKey}
|
|
14
15
|
*/
|
|
15
|
-
export class
|
|
16
|
+
export class Secp256k1SecretKey {
|
|
16
17
|
/** @type {KeyBytes} The entropy for the secret key as a byte array */
|
|
17
18
|
_bytes;
|
|
18
19
|
/** @type {bigint} The entropy for the secret key as a bigint */
|
|
@@ -20,7 +21,7 @@ export class SecretKey {
|
|
|
20
21
|
/** @type {string} The secret key as a secretKeyMultibase */
|
|
21
22
|
_multibase;
|
|
22
23
|
/**
|
|
23
|
-
* Instantiates an instance of
|
|
24
|
+
* Instantiates an instance of Secp256k1SecretKey.
|
|
24
25
|
* @param {Entropy} entropy bytes (Uint8Array) or secret (bigint)
|
|
25
26
|
* @throws {SecretKeyError} If entropy is not provided, not a valid 32-byte secret key or not a valid bigint seed
|
|
26
27
|
*/
|
|
@@ -34,11 +35,11 @@ export class SecretKey {
|
|
|
34
35
|
// If bytes and bytes are not length 32
|
|
35
36
|
if (isBytes && entropy.length === 32) {
|
|
36
37
|
this._bytes = entropy;
|
|
37
|
-
this._seed =
|
|
38
|
+
this._seed = Secp256k1SecretKey.toSecret(entropy);
|
|
38
39
|
}
|
|
39
40
|
// If secret and secret is not a valid bigint, throw error
|
|
40
41
|
if (isSecret && !(entropy < 1n || entropy >= CURVE.n)) {
|
|
41
|
-
this._bytes =
|
|
42
|
+
this._bytes = Secp256k1SecretKey.toBytes(entropy);
|
|
42
43
|
this._seed = entropy;
|
|
43
44
|
}
|
|
44
45
|
if (!this._bytes || this._bytes.length !== 32) {
|
|
@@ -103,7 +104,7 @@ export class SecretKey {
|
|
|
103
104
|
}
|
|
104
105
|
/**
|
|
105
106
|
* Checks if this secret key is equal to another.
|
|
106
|
-
* @param {
|
|
107
|
+
* @param {Secp256k1SecretKey} other The other secret key
|
|
107
108
|
* @returns {boolean} True if the private keys are equal, false otherwise
|
|
108
109
|
*/
|
|
109
110
|
equals(other) {
|
|
@@ -112,7 +113,7 @@ export class SecretKey {
|
|
|
112
113
|
}
|
|
113
114
|
/**
|
|
114
115
|
* Computes the public key from the secret key bytes.
|
|
115
|
-
* @returns {
|
|
116
|
+
* @returns {CompressedSecp256k1PublicKey} The computed public key
|
|
116
117
|
*/
|
|
117
118
|
computePublicKey() {
|
|
118
119
|
// Derive the public key from the secret key
|
|
@@ -125,7 +126,7 @@ export class SecretKey {
|
|
|
125
126
|
if (publicKeyBytes.length !== 33) {
|
|
126
127
|
throw new SecretKeyError('Invalid compute: public key not compressed format', 'COMPUTE_PUBLIC_KEY_ERROR');
|
|
127
128
|
}
|
|
128
|
-
return publicKeyBytes;
|
|
129
|
+
return new CompressedSecp256k1PublicKey(publicKeyBytes);
|
|
129
130
|
}
|
|
130
131
|
/**
|
|
131
132
|
* Converts the secret key to a JSON object.
|
|
@@ -147,15 +148,16 @@ export class SecretKey {
|
|
|
147
148
|
}
|
|
148
149
|
/**
|
|
149
150
|
* Checks if the public key is a valid secp256k1 point.
|
|
150
|
-
* @param {PublicKey} pk The public key to validate
|
|
151
151
|
* @returns {boolean} True if the public key is valid, false otherwise
|
|
152
152
|
*/
|
|
153
|
-
|
|
153
|
+
hasValidPublicKey() {
|
|
154
|
+
// Compute the public key from the secret key and compress it
|
|
155
|
+
const pk = this.computePublicKey();
|
|
154
156
|
// If the public key is not valid, return false
|
|
155
157
|
if (!tinysecp.isPoint(pk.compressed)) {
|
|
156
158
|
return false;
|
|
157
159
|
}
|
|
158
|
-
//
|
|
160
|
+
// Return true if the computed public key equals the provided public key
|
|
159
161
|
return true;
|
|
160
162
|
}
|
|
161
163
|
/**
|
|
@@ -182,22 +184,22 @@ export class SecretKey {
|
|
|
182
184
|
return decoded;
|
|
183
185
|
}
|
|
184
186
|
/**
|
|
185
|
-
* Creates a
|
|
187
|
+
* Creates a Secp256k1SecretKey object from a JSON object.
|
|
186
188
|
* @param {SecretKeyObject} json The JSON object containing the secret key bytes
|
|
187
|
-
* @returns {
|
|
189
|
+
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
188
190
|
*/
|
|
189
191
|
static fromJSON(json) {
|
|
190
|
-
return new
|
|
192
|
+
return new Secp256k1SecretKey(new Uint8Array(json.bytes));
|
|
191
193
|
}
|
|
192
194
|
/**
|
|
193
|
-
* Converts a
|
|
194
|
-
* @param {KeyBytes} bytes
|
|
195
|
+
* Converts a Secp256k1SecretKey or KeyBytes to a SchnorrKeyPair.
|
|
196
|
+
* @param {KeyBytes} bytes The secret key bytes
|
|
195
197
|
* @returns {SchnorrKeyPair} The SchnorrKeyPair object containing the public and private keys
|
|
196
198
|
* @throws {SecretKeyError} If the secret key is not valid
|
|
197
199
|
*/
|
|
198
200
|
static toKeyPair(bytes) {
|
|
199
|
-
// Create a new
|
|
200
|
-
const secretKey = new
|
|
201
|
+
// Create a new Secp256k1SecretKey from the bytes
|
|
202
|
+
const secretKey = new Secp256k1SecretKey(bytes);
|
|
201
203
|
// Compute the public key from the secret key
|
|
202
204
|
const publicKey = secretKey.computePublicKey();
|
|
203
205
|
// Create a new Pair from the public key and secret key
|
|
@@ -226,17 +228,17 @@ export class SecretKey {
|
|
|
226
228
|
return new Uint8Array(bytes);
|
|
227
229
|
}
|
|
228
230
|
/**
|
|
229
|
-
* Creates a new
|
|
230
|
-
* @param {bigint}
|
|
231
|
-
* @returns {
|
|
231
|
+
* Creates a new Secp256k1SecretKey object from a bigint secret.
|
|
232
|
+
* @param {bigint} entropy The secret bigint
|
|
233
|
+
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
232
234
|
*/
|
|
233
|
-
static
|
|
235
|
+
static fromEntropy(entropy) {
|
|
234
236
|
// Convert the secret bigint to a hex string
|
|
235
|
-
const hexsecret =
|
|
237
|
+
const hexsecret = entropy.toString(16).padStart(64, '0');
|
|
236
238
|
// Convert the hex string to a Uint8Array
|
|
237
239
|
const privateKeyBytes = new Uint8Array(hexsecret.match(/.{2}/g).map(byte => parseInt(byte, 16)));
|
|
238
|
-
// Return a new
|
|
239
|
-
return new
|
|
240
|
+
// Return a new Secp256k1SecretKey object
|
|
241
|
+
return new Secp256k1SecretKey(privateKeyBytes);
|
|
240
242
|
}
|
|
241
243
|
/**
|
|
242
244
|
* Generates random secret key bytes.
|
|
@@ -249,23 +251,23 @@ export class SecretKey {
|
|
|
249
251
|
return getRandomValues(byteArray);
|
|
250
252
|
}
|
|
251
253
|
/**
|
|
252
|
-
* Creates a new
|
|
253
|
-
* @returns {
|
|
254
|
+
* Creates a new Secp256k1SecretKey from random secret key bytes.
|
|
255
|
+
* @returns {Secp256k1SecretKey} A new Secp256k1SecretKey object
|
|
254
256
|
*/
|
|
255
257
|
static generate() {
|
|
256
258
|
// Generate empty 32-byte array
|
|
257
259
|
const randomBytes = this.random();
|
|
258
260
|
// Use the getRandomValues function to fill the byteArray with random values
|
|
259
|
-
return new
|
|
261
|
+
return new Secp256k1SecretKey(randomBytes);
|
|
260
262
|
}
|
|
261
263
|
/**
|
|
262
264
|
* Generates a public key from the given secret key bytes.
|
|
263
265
|
* @param {KeyBytes} bytes The secret key bytes
|
|
264
|
-
* @returns {
|
|
266
|
+
* @returns {CompressedSecp256k1PublicKey} The computed public key bytes
|
|
265
267
|
*/
|
|
266
268
|
static getPublicKey(bytes) {
|
|
267
|
-
// Create a new
|
|
268
|
-
return new
|
|
269
|
+
// Create a new Secp256k1SecretKey from the bytes and compute the public key
|
|
270
|
+
return new Secp256k1SecretKey(bytes).computePublicKey();
|
|
269
271
|
}
|
|
270
272
|
}
|
|
271
273
|
//# sourceMappingURL=secret.js.map
|
package/dist/esm/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,EAEf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"secret.js","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":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AAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAE,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,IAAI,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;IAGD;;;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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
|
package/dist/types/pair.d.ts
CHANGED
|
@@ -1,47 +1,35 @@
|
|
|
1
1
|
import { Hex, KeyBytes, SchnorrKeyPairObject } from '@did-btcr2/common';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
3
|
+
import { Secp256k1SecretKey } from './secret.js';
|
|
4
|
+
import { MultibaseKeys, RawSchnorrKeyPair, SchnorrKeyPairParams } from './types.js';
|
|
4
5
|
/**
|
|
5
|
-
*
|
|
6
|
+
* General KeyPair interface used by SchnorrKeyPair class.
|
|
6
7
|
* @interface KeyPair
|
|
7
8
|
* @type {KeyPair}
|
|
8
9
|
*/
|
|
9
10
|
export interface KeyPair {
|
|
10
11
|
/**
|
|
11
|
-
* @type {
|
|
12
|
+
* @type {CompressedSecp256k1PublicKey} The public key associated with the SchnorrKeyPair (required).
|
|
12
13
|
*/
|
|
13
|
-
readonly publicKey:
|
|
14
|
+
readonly publicKey: CompressedSecp256k1PublicKey;
|
|
14
15
|
/**
|
|
15
|
-
* @type {
|
|
16
|
+
* @type {Secp256k1SecretKey} The secret key associated with the SchnorrKeyPair (optional).
|
|
16
17
|
* @throws {KeyPairError} If the secret key is not available.
|
|
17
18
|
*/
|
|
18
|
-
readonly secretKey?:
|
|
19
|
+
readonly secretKey?: Secp256k1SecretKey;
|
|
19
20
|
/**
|
|
20
21
|
* JSON representation of the SchnorrKeyPair object.
|
|
21
22
|
* @returns {SchnorrKeyPairObject} The SchnorrKeyPair as a JSON object.
|
|
22
23
|
*/
|
|
23
24
|
json(): SchnorrKeyPairObject;
|
|
24
25
|
}
|
|
25
|
-
type RawKeyPair = {
|
|
26
|
-
public: KeyBytes;
|
|
27
|
-
secret?: KeyBytes;
|
|
28
|
-
};
|
|
29
|
-
/** Params for the {@link SchnorrKeyPair} constructor */
|
|
30
|
-
interface KeyParams {
|
|
31
|
-
secretKey?: SecretKey | KeyBytes;
|
|
32
|
-
publicKey?: PublicKey | KeyBytes;
|
|
33
|
-
}
|
|
34
|
-
interface MultibaseKeys {
|
|
35
|
-
publicKeyMultibase: string;
|
|
36
|
-
secretKeyMultibase: string;
|
|
37
|
-
}
|
|
38
26
|
/**
|
|
39
|
-
* Encapsulates a
|
|
27
|
+
* Encapsulates a CompressedSecp256k1PublicKey and a Secp256k1SecretKey object as a single SchnorrKeyPair object.
|
|
40
28
|
* @class SchnorrKeyPair
|
|
41
29
|
* @type {SchnorrKeyPair}
|
|
42
30
|
*/
|
|
43
31
|
export declare class SchnorrKeyPair implements KeyPair {
|
|
44
|
-
/** @type {
|
|
32
|
+
/** @type {Secp256k1SecretKey} The secret key object */
|
|
45
33
|
private _secretKey?;
|
|
46
34
|
private _publicKey;
|
|
47
35
|
/** @type {string} The public key in multibase format */
|
|
@@ -50,35 +38,39 @@ export declare class SchnorrKeyPair implements KeyPair {
|
|
|
50
38
|
private _secretKeyMultibase;
|
|
51
39
|
/**
|
|
52
40
|
* Creates an instance of Keys. Must provide a at least a secret key.
|
|
53
|
-
* Can optionally provide both a
|
|
54
|
-
* @param {
|
|
41
|
+
* Can optionally provide both a secret and public key, but must be a valid pair.
|
|
42
|
+
* @param {SchnorrKeyPairParams} params The parameters to initialize the Keys object.
|
|
43
|
+
* @param {CompressedSecp256k1PublicKey | KeyBytes} params.publicKey The public key object or bytes
|
|
44
|
+
* @param {Secp256k1SecretKey | KeyBytes} [params.secret] The secret key object or bytes
|
|
45
|
+
* @throws {KeyPairError} If neither a public key or secret key is provided.
|
|
46
|
+
* @throws {KeyPairError} If the public key is not a valid pair with the secret key.
|
|
55
47
|
*/
|
|
56
|
-
constructor(
|
|
48
|
+
constructor(params?: SchnorrKeyPairParams);
|
|
57
49
|
/**
|
|
58
|
-
* Get the
|
|
59
|
-
* @returns {
|
|
50
|
+
* Get the Secp256k1SecretKey.
|
|
51
|
+
* @returns {Secp256k1SecretKey} The Secp256k1SecretKey object
|
|
60
52
|
* @throws {KeyPairError} If the secret key is not available
|
|
61
53
|
*/
|
|
62
|
-
get secretKey():
|
|
54
|
+
get secretKey(): Secp256k1SecretKey;
|
|
63
55
|
/**
|
|
64
|
-
* Set the
|
|
65
|
-
* @param {
|
|
56
|
+
* Set the CompressedSecp256k1PublicKey.
|
|
57
|
+
* @param {CompressedSecp256k1PublicKey} publicKey The CompressedSecp256k1PublicKey object
|
|
66
58
|
* @throws {KeyPairError} If the public key is not a valid pair with the secret key.
|
|
67
59
|
*/
|
|
68
|
-
set publicKey(publicKey:
|
|
60
|
+
set publicKey(publicKey: CompressedSecp256k1PublicKey);
|
|
69
61
|
/**
|
|
70
|
-
* Get the
|
|
71
|
-
* @returns {
|
|
62
|
+
* Get the CompressedSecp256k1PublicKey.
|
|
63
|
+
* @returns {CompressedSecp256k1PublicKey} The CompressedSecp256k1PublicKey object
|
|
72
64
|
*/
|
|
73
|
-
get publicKey():
|
|
65
|
+
get publicKey(): CompressedSecp256k1PublicKey;
|
|
74
66
|
/**
|
|
75
|
-
* Get the
|
|
76
|
-
* @returns {
|
|
67
|
+
* Get the raw bytes of each key in the SchnorrKeyPair.
|
|
68
|
+
* @returns {RawSchnorrKeyPair} JSON object with the SchnorrKeyPair raw bytes.
|
|
77
69
|
*/
|
|
78
|
-
get raw():
|
|
70
|
+
get raw(): RawSchnorrKeyPair;
|
|
79
71
|
/**
|
|
80
72
|
* Get the Keys in multibase format.
|
|
81
|
-
* @returns {MultibaseKeys} The
|
|
73
|
+
* @returns {MultibaseKeys} The Secp256k1SecretKey in multibase format
|
|
82
74
|
*/
|
|
83
75
|
get multibase(): MultibaseKeys;
|
|
84
76
|
/**
|
|
@@ -93,34 +85,33 @@ export declare class SchnorrKeyPair implements KeyPair {
|
|
|
93
85
|
*/
|
|
94
86
|
static fromJSON(keys: SchnorrKeyPairObject): SchnorrKeyPair;
|
|
95
87
|
/**
|
|
96
|
-
* Static method creates a new SchnorrKeyPair from a
|
|
97
|
-
* @param {
|
|
88
|
+
* Static method creates a new SchnorrKeyPair from a Secp256k1SecretKey object or secret key bytes.
|
|
89
|
+
* @param {Secp256k1SecretKey | KeyBytes} data The secret key bytes
|
|
98
90
|
* @returns {SchnorrKeyPair} A new SchnorrKeyPair object
|
|
99
91
|
*/
|
|
100
|
-
static fromPrivateKey(data:
|
|
92
|
+
static fromPrivateKey(data: Secp256k1SecretKey | KeyBytes): SchnorrKeyPair;
|
|
101
93
|
/**
|
|
102
|
-
* Static method creates a new Keys (
|
|
103
|
-
* @param {bigint}
|
|
104
|
-
* @returns {
|
|
94
|
+
* Static method creates a new Keys (Secp256k1SecretKey/CompressedSecp256k1PublicKey) from bigint entropy.
|
|
95
|
+
* @param {bigint} entropy The entropy in bigint form
|
|
96
|
+
* @returns {SchnorrKeyPair} A new SchnorrKeyPair object
|
|
105
97
|
*/
|
|
106
|
-
static
|
|
98
|
+
static fromEntropy(entropy: bigint): SchnorrKeyPair;
|
|
107
99
|
/**
|
|
108
100
|
* Converts key bytes to a hex string.
|
|
109
|
-
* @param {KeyBytes} keyBytes The key bytes (
|
|
101
|
+
* @param {KeyBytes} keyBytes The key bytes (secret or public).
|
|
110
102
|
* @returns {Hex} The key bytes as a hex string.
|
|
111
103
|
*/
|
|
112
104
|
static toHex(keyBytes: KeyBytes): Hex;
|
|
113
105
|
/**
|
|
114
106
|
* Compares two Keys objects for equality.
|
|
115
|
-
* @param {SchnorrKeyPair}
|
|
116
|
-
* @param {SchnorrKeyPair}
|
|
107
|
+
* @param {SchnorrKeyPair} kp The main keys.
|
|
108
|
+
* @param {SchnorrKeyPair} otherKp The other keys to compare.
|
|
117
109
|
* @returns {boolean} True if the public key and secret key are equal, false otherwise.
|
|
118
110
|
*/
|
|
119
|
-
static equals(
|
|
111
|
+
static equals(kp: SchnorrKeyPair, otherKp: SchnorrKeyPair): boolean;
|
|
120
112
|
/**
|
|
121
113
|
* Static method to generate a new random SchnorrKeyPair instance.
|
|
122
|
-
* @returns {SchnorrKeyPair} A new
|
|
114
|
+
* @returns {SchnorrKeyPair} A new Secp256k1SecretKey object.
|
|
123
115
|
*/
|
|
124
116
|
static generate(): SchnorrKeyPair;
|
|
125
117
|
}
|
|
126
|
-
export {};
|
package/dist/types/pair.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pair.d.ts","sourceRoot":"","sources":["../../src/pair.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,QAAQ,EAER,oBAAoB,EACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"pair.d.ts","sourceRoot":"","sources":["../../src/pair.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,QAAQ,EAER,oBAAoB,EACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEpF;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,QAAQ,CAAC,SAAS,EAAE,4BAA4B,CAAC;IAEjD;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,kBAAkB,CAAC;IAExC;;;OAGG;IACH,IAAI,IAAI,oBAAoB,CAAC;CAC9B;AAED;;;;GAIG;AACH,qBAAa,cAAe,YAAW,OAAO;IAC5C,uDAAuD;IACvD,OAAO,CAAC,UAAU,CAAC,CAAqB;IAGxC,OAAO,CAAC,UAAU,CAA+B;IAEjD,wDAAwD;IACxD,OAAO,CAAC,mBAAmB,CAAS;IAEpC,wDAAwD;IACxD,OAAO,CAAC,mBAAmB,CAAS;IAEpC;;;;;;;;OAQG;gBACS,MAAM,GAAE,oBAAyB;IA0B7C;;;;OAIG;IACH,IAAI,SAAS,IAAI,kBAAkB,CAYlC;IAED;;;;OAIG;IACH,IAAI,SAAS,CAAC,SAAS,EAAE,4BAA4B,EAapD;IAED;;;OAGG;IACH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