@did-btcr2/keypair 0.7.1 → 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 +109 -71
- package/dist/cjs/public.js.map +1 -1
- package/dist/cjs/secret.js +39 -18
- 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 +109 -71
- package/dist/esm/public.js.map +1 -1
- package/dist/esm/secret.js +39 -18
- 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 +46 -28
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/secret.d.ts +12 -7
- package/dist/types/secret.d.ts.map +1 -1
- package/dist/types/types.d.ts +3 -0
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/pair.ts +43 -46
- package/src/public.ts +139 -92
- package/src/secret.ts +46 -21
- package/src/types.ts +2 -0
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,EAA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,OAAO,CAAC,GAAG,CAAC;QAChC,CAAC;QAED,iDAAiD;QACjD,MAAM,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC;QACxB,MAAM,OAAO,GAAG,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
|
@@ -12,39 +12,43 @@ import { Secp256k1SecretKey } from './secret.js';
|
|
|
12
12
|
*/
|
|
13
13
|
export class CompressedSecp256k1PublicKey {
|
|
14
14
|
/** @type {KeyBytes} The public key bytes */
|
|
15
|
-
|
|
15
|
+
#bytes;
|
|
16
16
|
/** @type {MultibaseObject} The public key as a MultibaseObject */
|
|
17
|
-
|
|
17
|
+
#multibase = {
|
|
18
18
|
prefix: BIP340_PUBLIC_KEY_MULTIBASE_PREFIX,
|
|
19
19
|
key: [],
|
|
20
20
|
encoded: ''
|
|
21
21
|
};
|
|
22
22
|
/**
|
|
23
23
|
* Creates a CompressedSecp256k1PublicKey instance.
|
|
24
|
-
* @param {
|
|
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(
|
|
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'));
|
|
28
32
|
// If the byte length is not 33, throw an error
|
|
29
|
-
if (
|
|
30
|
-
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 });
|
|
31
35
|
}
|
|
32
36
|
// Validate the point is on curve and in compressed form
|
|
33
|
-
if (!tinysecp.isPoint(
|
|
34
|
-
throw new PublicKeyError('Invalid argument:
|
|
37
|
+
if (!tinysecp.isPoint(keyBytes)) {
|
|
38
|
+
throw new PublicKeyError('Invalid argument: not a valid secp256k1 compressed point', 'CONSTRUCTOR_ERROR', { keyBytes });
|
|
35
39
|
}
|
|
36
40
|
// Set the bytes
|
|
37
|
-
this
|
|
41
|
+
this.#bytes = keyBytes;
|
|
38
42
|
// Set multibase
|
|
39
|
-
this.
|
|
40
|
-
this.
|
|
43
|
+
this.#multibase.encoded = this.encode();
|
|
44
|
+
this.#multibase.key = [...this.#multibase.prefix, ...this.compressed];
|
|
41
45
|
}
|
|
42
46
|
/**
|
|
43
47
|
* Get the compressed public key.
|
|
44
48
|
* @returns {KeyBytes} The 33-byte compressed public key (0x02 or 0x03, x).
|
|
45
49
|
*/
|
|
46
50
|
get compressed() {
|
|
47
|
-
const bytes = new Uint8Array(this
|
|
51
|
+
const bytes = new Uint8Array(this.#bytes);
|
|
48
52
|
return bytes;
|
|
49
53
|
}
|
|
50
54
|
;
|
|
@@ -60,7 +64,8 @@ export class CompressedSecp256k1PublicKey {
|
|
|
60
64
|
* X-only (32-byte) view of the public key per BIP-340.
|
|
61
65
|
*/
|
|
62
66
|
get xOnly() {
|
|
63
|
-
|
|
67
|
+
const xOnly = this.compressed.slice(1);
|
|
68
|
+
return xOnly;
|
|
64
69
|
}
|
|
65
70
|
/**
|
|
66
71
|
* Parity of the SEC compressed public key.
|
|
@@ -68,7 +73,7 @@ export class CompressedSecp256k1PublicKey {
|
|
|
68
73
|
* @throws {PublicKeyError} If the parity byte is not 0x02 or 0x03.
|
|
69
74
|
*/
|
|
70
75
|
get parity() {
|
|
71
|
-
const parity = this.
|
|
76
|
+
const parity = this.compressed[0];
|
|
72
77
|
if (![0x02, 0x03].includes(parity)) {
|
|
73
78
|
throw new PublicKeyError('Invalid state: parity byte must be 2 or 3', 'PARITY_ERROR', { parity });
|
|
74
79
|
}
|
|
@@ -79,7 +84,7 @@ export class CompressedSecp256k1PublicKey {
|
|
|
79
84
|
* @returns {boolean} True if the public key has even Y.
|
|
80
85
|
*/
|
|
81
86
|
get isEven() {
|
|
82
|
-
return this.
|
|
87
|
+
return this.parity === 0x02;
|
|
83
88
|
}
|
|
84
89
|
/**
|
|
85
90
|
* Get the x-coordinate of the public key.
|
|
@@ -102,12 +107,12 @@ export class CompressedSecp256k1PublicKey {
|
|
|
102
107
|
* @returns {MultibaseObject} An object containing the multibase bytes, address and prefix.
|
|
103
108
|
*/
|
|
104
109
|
get multibase() {
|
|
105
|
-
const multibase = this
|
|
110
|
+
const multibase = this.#multibase;
|
|
106
111
|
return multibase;
|
|
107
112
|
}
|
|
108
113
|
/**
|
|
109
114
|
* Returns the raw public key as a hex string.
|
|
110
|
-
* @returns {
|
|
115
|
+
* @returns {string} The public key as a hex string.
|
|
111
116
|
*/
|
|
112
117
|
get hex() {
|
|
113
118
|
const hex = Buffer.from(this.compressed).toString('hex');
|
|
@@ -130,26 +135,6 @@ export class CompressedSecp256k1PublicKey {
|
|
|
130
135
|
bip340() {
|
|
131
136
|
return this.xOnly;
|
|
132
137
|
}
|
|
133
|
-
/**
|
|
134
|
-
* Returns the point of the public key.
|
|
135
|
-
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
136
|
-
* @returns {Point} The point of the public key.
|
|
137
|
-
* @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
|
|
138
|
-
*/
|
|
139
|
-
static point(pk) {
|
|
140
|
-
// If the public key is a hex string, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
141
|
-
if (typeof pk === 'string' && /^[0-9a-fA-F]+$/.test(pk)) {
|
|
142
|
-
const publicKey = new CompressedSecp256k1PublicKey(Buffer.fromHex(pk));
|
|
143
|
-
return publicKey.point;
|
|
144
|
-
}
|
|
145
|
-
// If the public key is a byte array or ArrayBuffer, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
146
|
-
if (pk instanceof Uint8Array || ArrayBuffer.isView(pk)) {
|
|
147
|
-
const publicKey = new CompressedSecp256k1PublicKey(pk);
|
|
148
|
-
return publicKey.point;
|
|
149
|
-
}
|
|
150
|
-
// If the public key is neither a hex string nor a byte array, throw an error
|
|
151
|
-
throw new PublicKeyError('Invalid publicKey: must be a hex string or byte array', 'POINT_ERROR', { publicKey: pk });
|
|
152
|
-
}
|
|
153
138
|
/**
|
|
154
139
|
* Decodes the multibase string to the 35-byte corresponding public key (2 byte prefix + 32 byte public key).
|
|
155
140
|
* @returns {KeyBytes} The decoded public key: prefix and public key bytes
|
|
@@ -178,17 +163,36 @@ export class CompressedSecp256k1PublicKey {
|
|
|
178
163
|
*/
|
|
179
164
|
encode() {
|
|
180
165
|
// Convert public key bytes to an array
|
|
181
|
-
const pk = this.compressed
|
|
166
|
+
const pk = Array.from(this.compressed);
|
|
182
167
|
// Ensure the public key is 33-byte secp256k1 compressed public key
|
|
183
168
|
if (pk.length !== 33) {
|
|
184
169
|
throw new PublicKeyError('Invalid argument: must be 33-byte (compressed) public key', 'ENCODE_MULTIBASE_ERROR');
|
|
185
170
|
}
|
|
186
171
|
// Convert prefix to an array
|
|
187
|
-
const publicKeyMultibase =
|
|
172
|
+
const publicKeyMultibase = Array.from(BIP340_PUBLIC_KEY_MULTIBASE_PREFIX);
|
|
188
173
|
// Push the public key bytes at the end of the prefix
|
|
189
174
|
publicKeyMultibase.push(...pk);
|
|
190
175
|
// Encode the bytes in base58btc format and return
|
|
191
|
-
return base58btc.encode(
|
|
176
|
+
return base58btc.encode(Uint8Array.from(publicKeyMultibase));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Verify a signature using schnorr or ecdsa.
|
|
180
|
+
* @param {SignatureBytes} signature Signature for verification.
|
|
181
|
+
* @param {string} data Data for verification.
|
|
182
|
+
* @param {CryptoOptions} opts Options for signing.
|
|
183
|
+
* @param {('ecdsa' | 'schnorr')} opts.scheme The signature scheme to use. Default is 'schnorr'.
|
|
184
|
+
* @returns {boolean} If the signature is valid against the public key.
|
|
185
|
+
*/
|
|
186
|
+
verify(signature, data, opts) {
|
|
187
|
+
opts ??= { scheme: 'schnorr' };
|
|
188
|
+
// Verify the signature depending on the scheme and return the result
|
|
189
|
+
if (opts.scheme === 'ecdsa') {
|
|
190
|
+
return tinysecp.verify(data, this.compressed, signature);
|
|
191
|
+
}
|
|
192
|
+
else if (opts.scheme === 'schnorr') {
|
|
193
|
+
return tinysecp.verifySchnorr(data, this.x, signature);
|
|
194
|
+
}
|
|
195
|
+
throw new PublicKeyError(`Invalid scheme: ${opts.scheme}.`, 'VERIFY_SIGNATURE_ERROR', opts);
|
|
192
196
|
}
|
|
193
197
|
/**
|
|
194
198
|
* Compares this public key to another public key.
|
|
@@ -207,40 +211,12 @@ export class CompressedSecp256k1PublicKey {
|
|
|
207
211
|
hex: this.hex,
|
|
208
212
|
multibase: this.multibase,
|
|
209
213
|
point: {
|
|
210
|
-
x: this.x
|
|
211
|
-
y: this.y
|
|
214
|
+
x: Array.from(this.x),
|
|
215
|
+
y: Array.from(this.y),
|
|
212
216
|
parity: this.parity,
|
|
213
217
|
},
|
|
214
218
|
};
|
|
215
219
|
}
|
|
216
|
-
/**
|
|
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.
|
|
220
|
-
*/
|
|
221
|
-
static fromJSON(json) {
|
|
222
|
-
json.point.x.unshift(json.point.parity);
|
|
223
|
-
return new CompressedSecp256k1PublicKey(json.point.x.toUint8Array());
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
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
|
|
229
|
-
*/
|
|
230
|
-
static fromSecretKey(sk) {
|
|
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
|
|
234
|
-
if (bytes.length !== 32) {
|
|
235
|
-
throw new PublicKeyError('Invalid arg: must be 32 byte secret key', 'FROM_SECRET_KEY_ERROR');
|
|
236
|
-
}
|
|
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();
|
|
243
|
-
}
|
|
244
220
|
/**
|
|
245
221
|
* Computes modular exponentiation: (base^exp) % mod.
|
|
246
222
|
* Used for computing modular square roots.
|
|
@@ -291,10 +267,72 @@ export class CompressedSecp256k1PublicKey {
|
|
|
291
267
|
// Compute y (do not enforce parity)
|
|
292
268
|
const y = this.sqrtMod(ySquared, CURVE.p);
|
|
293
269
|
// Convert x and y to Uint8Array
|
|
294
|
-
const yBytes = Buffer.
|
|
270
|
+
const yBytes = Buffer.from(y.toString(16).padStart(64, '0'), 'hex');
|
|
295
271
|
// Return 65-byte uncompressed public key: `0x04 || x || y`
|
|
296
272
|
return new Uint8Array(Buffer.concat([Buffer.from([0x04]), Buffer.from(this.x), yBytes]));
|
|
297
273
|
}
|
|
298
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
|
+
}
|
|
299
337
|
}
|
|
300
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,
|
|
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;YACjD,CAAC,CAAC,YAAY;YACd,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC,CAAC;QAEtD,+CAA+C;QAC/C,IAAG,CAAC,QAAQ,IAAI,QAAQ,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;YACvC,MAAM,IAAI,cAAc,CACtB,uDAAuD,EACvD,mBAAmB,EAAE,EAAE,QAAQ,EAAE,CAClC,CAAC;QACJ,CAAC;QAED,wDAAwD;QACxD,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;YAChC,MAAM,IAAI,cAAc,CACtB,0DAA0D,EAC1D,mBAAmB,EAAE,EAAE,QAAQ,EAAE,CAClC,CAAC;QACJ,CAAC;QACD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BAAkB;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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BIP340_SECRET_KEY_MULTIBASE_PREFIX, BIP340_SECRET_KEY_MULTIBASE_PREFIX_HASH, CURVE, SecretKeyError } from '@did-btcr2/common';
|
|
2
2
|
import { sha256 } from '@noble/hashes/sha2';
|
|
3
|
-
import { getRandomValues } from 'crypto';
|
|
3
|
+
import { getRandomValues, randomBytes } 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';
|
|
@@ -15,11 +15,11 @@ import { CompressedSecp256k1PublicKey } from './public.js';
|
|
|
15
15
|
*/
|
|
16
16
|
export class Secp256k1SecretKey {
|
|
17
17
|
/** @type {KeyBytes} The entropy for the secret key as a byte array */
|
|
18
|
-
|
|
18
|
+
#bytes;
|
|
19
19
|
/** @type {bigint} The entropy for the secret key as a bigint */
|
|
20
|
-
|
|
20
|
+
#seed;
|
|
21
21
|
/** @type {string} The secret key as a secretKeyMultibase */
|
|
22
|
-
|
|
22
|
+
#multibase;
|
|
23
23
|
/**
|
|
24
24
|
* Instantiates an instance of Secp256k1SecretKey.
|
|
25
25
|
* @param {Entropy} entropy bytes (Uint8Array) or secret (bigint)
|
|
@@ -34,22 +34,22 @@ export class Secp256k1SecretKey {
|
|
|
34
34
|
}
|
|
35
35
|
// If bytes and bytes are not length 32
|
|
36
36
|
if (isBytes && entropy.length === 32) {
|
|
37
|
-
this
|
|
38
|
-
this
|
|
37
|
+
this.#bytes = entropy;
|
|
38
|
+
this.#seed = Secp256k1SecretKey.toSecret(entropy);
|
|
39
39
|
}
|
|
40
40
|
// If secret and secret is not a valid bigint, throw error
|
|
41
41
|
if (isSecret && !(entropy < 1n || entropy >= CURVE.n)) {
|
|
42
|
-
this
|
|
43
|
-
this
|
|
42
|
+
this.#bytes = Secp256k1SecretKey.toBytes(entropy);
|
|
43
|
+
this.#seed = entropy;
|
|
44
44
|
}
|
|
45
|
-
if (!this
|
|
45
|
+
if (!this.#bytes || this.#bytes.length !== 32) {
|
|
46
46
|
throw new SecretKeyError('Invalid bytes: must be a valid 32-byte secret key', 'CONSTRUCTOR_ERROR');
|
|
47
47
|
}
|
|
48
|
-
if (!this
|
|
48
|
+
if (!this.#seed || (this.#seed < 1n || this.#seed >= CURVE.n)) {
|
|
49
49
|
throw new SecretKeyError('Invalid seed: must must be valid bigint', 'CONSTRUCTOR_ERROR');
|
|
50
50
|
}
|
|
51
51
|
// Set the secret key multibase
|
|
52
|
-
this
|
|
52
|
+
this.#multibase = this.encode();
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* Get the secret key entropy as a byte array.
|
|
@@ -57,7 +57,7 @@ export class Secp256k1SecretKey {
|
|
|
57
57
|
*/
|
|
58
58
|
get bytes() {
|
|
59
59
|
// Return a copy of the secret key bytes
|
|
60
|
-
const bytes = new Uint8Array(this
|
|
60
|
+
const bytes = new Uint8Array(this.#bytes);
|
|
61
61
|
return bytes;
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
@@ -66,7 +66,7 @@ export class Secp256k1SecretKey {
|
|
|
66
66
|
*/
|
|
67
67
|
get seed() {
|
|
68
68
|
// Memoize the secret and return
|
|
69
|
-
const seed = BigInt(this
|
|
69
|
+
const seed = BigInt(this.#seed);
|
|
70
70
|
return seed;
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
@@ -82,7 +82,7 @@ export class Secp256k1SecretKey {
|
|
|
82
82
|
* @returns {string} The secret key in base58btc multibase format
|
|
83
83
|
*/
|
|
84
84
|
get multibase() {
|
|
85
|
-
const multibase = this
|
|
85
|
+
const multibase = this.#multibase;
|
|
86
86
|
return multibase;
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
@@ -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
|
};
|
|
@@ -160,6 +160,27 @@ export class Secp256k1SecretKey {
|
|
|
160
160
|
// Return true if the computed public key equals the provided public key
|
|
161
161
|
return true;
|
|
162
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Produce a signature over arbitrary data using schnorr or ecdsa.
|
|
165
|
+
* @param {MessageBytes} data Data to be signed.
|
|
166
|
+
* @param {CryptoOptions} opts Options for signing.
|
|
167
|
+
* @param {('ecdsa' | 'schnorr')} opts.scheme The signature scheme to use. Default is 'schnorr'.
|
|
168
|
+
* @returns {SignatureBytes} Signature byte array.
|
|
169
|
+
* @throws {SecretKeyError} if no private key is provided.
|
|
170
|
+
*/
|
|
171
|
+
sign(data, opts) {
|
|
172
|
+
// Set default options if not provided
|
|
173
|
+
opts ??= { scheme: 'schnorr' };
|
|
174
|
+
// Sign ecdsa and return
|
|
175
|
+
if (opts.scheme === 'ecdsa') {
|
|
176
|
+
return tinysecp.sign(data, this.bytes);
|
|
177
|
+
}
|
|
178
|
+
// Sign schnorr and return
|
|
179
|
+
if (opts.scheme === 'schnorr') {
|
|
180
|
+
return tinysecp.signSchnorr(data, this.bytes, randomBytes(32));
|
|
181
|
+
}
|
|
182
|
+
throw new SecretKeyError(`Invalid scheme: ${opts.scheme}.`, 'SIGN_ERROR', opts);
|
|
183
|
+
}
|
|
163
184
|
/**
|
|
164
185
|
* Decodes the multibase string to the 34-byte secret key (2 byte prefix + 32 byte key).
|
|
165
186
|
* @param {string} multibase The multibase string to decode
|