@did-btcr2/keypair 0.10.0 → 0.11.1
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 +21 -139
- package/dist/cjs/public.js.map +1 -1
- package/dist/cjs/secret.js +65 -82
- 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 +21 -139
- package/dist/esm/public.js.map +1 -1
- package/dist/esm/secret.js +65 -82
- 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 +7 -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 +5 -5
- package/src/pair.ts +51 -56
- package/src/public.ts +25 -176
- package/src/secret.ts +72 -111
- package/src/types.ts +2 -2
package/dist/cjs/pair.js
CHANGED
|
@@ -7,22 +7,8 @@ import { Secp256k1SecretKey } from './secret.js';
|
|
|
7
7
|
* @type {SchnorrKeyPair}
|
|
8
8
|
*/
|
|
9
9
|
export class SchnorrKeyPair {
|
|
10
|
-
/**
|
|
11
|
-
* The secret key objec
|
|
12
|
-
*/
|
|
13
10
|
#secretKey;
|
|
14
|
-
/**
|
|
15
|
-
* The public key object
|
|
16
|
-
*/ ;
|
|
17
11
|
#publicKey;
|
|
18
|
-
/**
|
|
19
|
-
* The public key in multibase forma
|
|
20
|
-
*/
|
|
21
|
-
#publicKeyMultibase;
|
|
22
|
-
/**
|
|
23
|
-
* The secret key in multibase forma
|
|
24
|
-
*/
|
|
25
|
-
#secretKeyMultibase;
|
|
26
12
|
/**
|
|
27
13
|
* Creates an instance of Keys. Must provide a at least a secret key.
|
|
28
14
|
* Can optionally provide both a secret and public key, but must be a valid pair.
|
|
@@ -54,8 +40,13 @@ export class SchnorrKeyPair {
|
|
|
54
40
|
else {
|
|
55
41
|
this.#publicKey = this.#secretKey.computePublicKey();
|
|
56
42
|
}
|
|
57
|
-
|
|
58
|
-
|
|
43
|
+
// Validate that an explicitly provided public key matches the secret key
|
|
44
|
+
if (this.#secretKey && params.publicKey) {
|
|
45
|
+
const derived = this.#secretKey.computePublicKey();
|
|
46
|
+
if (!this.#publicKey.equals(derived)) {
|
|
47
|
+
throw new KeyPairError('Public key does not match secret key', 'CONSTRUCTOR_ERROR');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
59
50
|
}
|
|
60
51
|
/**
|
|
61
52
|
* Get the Secp256k1SecretKey.
|
|
@@ -81,26 +72,27 @@ export class SchnorrKeyPair {
|
|
|
81
72
|
* @throws {KeyPairError} If the public key is not a valid pair with the secret key.
|
|
82
73
|
*/
|
|
83
74
|
set publicKey(publicKey) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (!
|
|
87
|
-
throw new KeyPairError('
|
|
75
|
+
if (this.#secretKey) {
|
|
76
|
+
const derived = this.#secretKey.computePublicKey();
|
|
77
|
+
if (!publicKey.equals(derived)) {
|
|
78
|
+
throw new KeyPairError('Public key does not match secret key', 'PUBLIC_KEY_ERROR');
|
|
88
79
|
}
|
|
89
|
-
const cPk = this.secretKey.computePublicKey();
|
|
90
|
-
if (!publicKey.equals(cPk))
|
|
91
|
-
throw new KeyPairError('Public key is not a valid pair with the secret key', 'PUBLIC_KEY_ERROR');
|
|
92
80
|
}
|
|
93
81
|
this.#publicKey = publicKey;
|
|
94
|
-
this.#publicKeyMultibase = publicKey.multibase.encoded;
|
|
95
|
-
this.#secretKeyMultibase = this.#secretKey ? this.#secretKey.multibase : '';
|
|
96
82
|
}
|
|
97
83
|
/**
|
|
98
84
|
* Get the CompressedSecp256k1PublicKey.
|
|
99
85
|
* @returns {CompressedSecp256k1PublicKey} The CompressedSecp256k1PublicKey object
|
|
100
86
|
*/
|
|
101
87
|
get publicKey() {
|
|
102
|
-
|
|
103
|
-
|
|
88
|
+
return this.#publicKey;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Whether this key pair contains a secret key.
|
|
92
|
+
* @returns {boolean} True if the secret key is present.
|
|
93
|
+
*/
|
|
94
|
+
get hasSecretKey() {
|
|
95
|
+
return !!this.#secretKey;
|
|
104
96
|
}
|
|
105
97
|
/**
|
|
106
98
|
* Get the `raw` bytes of each key in the SchnorrKeyPair.
|
|
@@ -109,7 +101,7 @@ export class SchnorrKeyPair {
|
|
|
109
101
|
get raw() {
|
|
110
102
|
return {
|
|
111
103
|
public: this.publicKey.x,
|
|
112
|
-
secret: this
|
|
104
|
+
secret: this.#secretKey ? this.#secretKey.bytes : undefined
|
|
113
105
|
};
|
|
114
106
|
}
|
|
115
107
|
/**
|
|
@@ -119,7 +111,7 @@ export class SchnorrKeyPair {
|
|
|
119
111
|
get hex() {
|
|
120
112
|
return {
|
|
121
113
|
public: this.publicKey.hex,
|
|
122
|
-
secret: this.#secretKey ? this
|
|
114
|
+
secret: this.#secretKey ? this.#secretKey.hex : undefined
|
|
123
115
|
};
|
|
124
116
|
}
|
|
125
117
|
/**
|
|
@@ -128,20 +120,36 @@ export class SchnorrKeyPair {
|
|
|
128
120
|
*/
|
|
129
121
|
get multibase() {
|
|
130
122
|
return {
|
|
131
|
-
publicKeyMultibase: this.#
|
|
132
|
-
secretKeyMultibase: this.#
|
|
123
|
+
publicKeyMultibase: this.#publicKey.multibase.encoded,
|
|
124
|
+
secretKeyMultibase: this.#secretKey ? this.#secretKey.multibase : '',
|
|
133
125
|
};
|
|
134
126
|
}
|
|
135
127
|
/**
|
|
136
|
-
* JSON representation
|
|
137
|
-
*
|
|
128
|
+
* Safe JSON representation. Only includes the public key.
|
|
129
|
+
* Called implicitly by JSON.stringify(). Use exportJSON() for full serialization.
|
|
130
|
+
* @returns {{ publicKey: PublicKeyObject }} The JSON representation of the public key
|
|
138
131
|
*/
|
|
139
132
|
toJSON() {
|
|
133
|
+
return { publicKey: this.publicKey.toJSON() };
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Exports the full key pair as a JSON object. Contains sensitive material.
|
|
137
|
+
* @returns {SchnorrKeyPairObject} The key pair as a JSON object
|
|
138
|
+
* @throws {KeyPairError} If the secret key is not available
|
|
139
|
+
*/
|
|
140
|
+
exportJSON() {
|
|
141
|
+
if (!this.#secretKey) {
|
|
142
|
+
throw new KeyPairError('Cannot export: secret key required. Use publicKey.toJSON() for public-key-only pairs.', 'SERIALIZE_ERROR');
|
|
143
|
+
}
|
|
140
144
|
return {
|
|
141
|
-
secretKey: this
|
|
145
|
+
secretKey: this.#secretKey.exportJSON(),
|
|
142
146
|
publicKey: this.publicKey.toJSON()
|
|
143
147
|
};
|
|
144
148
|
}
|
|
149
|
+
/** @override Prevents secret material from appearing in Node.js inspect */
|
|
150
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
151
|
+
return `[SchnorrKeyPair ${this.publicKey.hex}]`;
|
|
152
|
+
}
|
|
145
153
|
/**
|
|
146
154
|
* Static method creates a new Keys from a JSON object.
|
|
147
155
|
* @param {SchnorrKeyPairObject} keys The JSON object to initialize the Keys.
|
|
@@ -200,21 +208,7 @@ export class SchnorrKeyPair {
|
|
|
200
208
|
* @returns {boolean} True if the public key and secret key are equal, false otherwise.
|
|
201
209
|
*/
|
|
202
210
|
static equals(kp, otherKp) {
|
|
203
|
-
|
|
204
|
-
const pk = kp.publicKey;
|
|
205
|
-
const otherPk = otherKp.publicKey;
|
|
206
|
-
// If publicKeys present, use to compare as hex strings.
|
|
207
|
-
if (pk && otherPk) {
|
|
208
|
-
return pk.hex === otherPk.hex;
|
|
209
|
-
}
|
|
210
|
-
// Deconstruct the secret keys from the key pairs
|
|
211
|
-
const sk = kp.secretKey;
|
|
212
|
-
const otherSk = otherKp.secretKey;
|
|
213
|
-
if (sk && otherSk) {
|
|
214
|
-
// Get the public key hex strings for both key pair publicKeys
|
|
215
|
-
return sk.hex === otherSk.hex;
|
|
216
|
-
}
|
|
217
|
-
throw new KeyPairError('Cannot compare invalid key pair(s)', 'KEYPAIR_EQUALS_ERROR');
|
|
211
|
+
return kp.publicKey.equals(otherKp.publicKey);
|
|
218
212
|
}
|
|
219
213
|
/**
|
|
220
214
|
* Static method to generate a new random SchnorrKeyPair instance.
|
package/dist/cjs/pair.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pair.js","sourceRoot":"","sources":["../../src/pair.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,YAAY,
|
|
1
|
+
{"version":3,"file":"pair.js","sourceRoot":"","sources":["../../src/pair.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,YAAY,EAGb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,4BAA4B,EAAa,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAa,MAAM,aAAa,CAAC;AAoB5D;;;;GAIG;AACH,MAAM,OAAO,cAAc;IACzB,UAAU,CAAsB;IAChC,UAAU,CAA+B;IAEzC;;;;;;;;OAQG;IACH,YAAY,SAA+B,EAAE;QAC3C,iDAAiD;QACjD,IAAI,CAAC,MAAM,CAAC,SAAS,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;YAC3C,MAAM,IAAI,YAAY,CAAC,qDAAqD,EAAE,mBAAmB,CAAC,CAAC;QACrG,CAAC;QAED,oBAAoB;QACpB,IAAG,MAAM,CAAC,SAAS,YAAY,UAAU,EAAE,CAAC;YAC1C,IAAI,CAAC,UAAU,GAAG,IAAI,kBAAkB,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAC7D,CAAC;aAAM,IAAI,MAAM,CAAC,SAAS,YAAY,kBAAkB,EAAE,CAAC;YAC1D,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,SAAS,CAAC;QACrC,CAA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B,OAAO,IAAI,cAAc,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC;IACtD,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,UAAU,CAAC,IAAY;QAC5B,MAAM,SAAS,GAAG,kBAAkB,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QACtD,MAAM,SAAS,GAAG,SAAS,CAAC,gBAAgB,EAAE,CAAC;QAC/C,OAAO,IAAI,cAAc,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC;IACtD,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,KAAK,CAAC,QAAkB;QAC7B,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,EAAkB,EAAE,OAAuB;QACvD,OAAO,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChD,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,QAAQ;QACb,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
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
|
|
1
|
+
import { PublicKeyError } from '@did-btcr2/common';
|
|
2
|
+
import { secp256k1, schnorr } from '@noble/curves/secp256k1.js';
|
|
3
|
+
import { equalBytes } from '@noble/curves/utils.js';
|
|
4
|
+
import { base58 } from '@scure/base';
|
|
5
|
+
export const BIP340_PUBLIC_KEY_MULTIBASE_PREFIX = new Uint8Array([0xe7, 0x01]);
|
|
6
6
|
/**
|
|
7
7
|
* Encapsulates a secp256k1 public key compliant to BIP-340 BIP schnorr signature scheme.
|
|
8
8
|
* Provides get methods for different formats (compressed, x-only, multibase).
|
|
@@ -38,11 +38,11 @@ export class CompressedSecp256k1PublicKey {
|
|
|
38
38
|
throw new PublicKeyError('Invalid argument: byte length must be 33 (compressed)', 'CONSTRUCTOR_ERROR', { keyBytes });
|
|
39
39
|
}
|
|
40
40
|
// Validate the point is on curve and in compressed form
|
|
41
|
-
if (!
|
|
41
|
+
if (!secp256k1.utils.isValidPublicKey(keyBytes)) {
|
|
42
42
|
throw new PublicKeyError('Invalid argument: not a valid secp256k1 compressed point', 'CONSTRUCTOR_ERROR', { keyBytes });
|
|
43
43
|
}
|
|
44
|
-
//
|
|
45
|
-
this.#bytes = keyBytes;
|
|
44
|
+
// Defensive copy — caller cannot mutate internal state
|
|
45
|
+
this.#bytes = new Uint8Array(keyBytes);
|
|
46
46
|
// Set multibase
|
|
47
47
|
this.#multibase.encoded = this.encode();
|
|
48
48
|
this.#multibase.key = [...this.#multibase.prefix, ...this.compressed];
|
|
@@ -61,8 +61,7 @@ export class CompressedSecp256k1PublicKey {
|
|
|
61
61
|
* @returns {Uint8Array} The 65-byte uncompressed public key (0x04, x, y).
|
|
62
62
|
*/
|
|
63
63
|
get uncompressed() {
|
|
64
|
-
|
|
65
|
-
return uncompressed;
|
|
64
|
+
return secp256k1.Point.fromBytes(this.compressed).toBytes(false);
|
|
66
65
|
}
|
|
67
66
|
/**
|
|
68
67
|
* X-only (32-byte) view of the public key per BIP-340.
|
|
@@ -111,8 +110,11 @@ export class CompressedSecp256k1PublicKey {
|
|
|
111
110
|
* @returns {MultibaseObject} An object containing the multibase bytes, address and prefix.
|
|
112
111
|
*/
|
|
113
112
|
get multibase() {
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
return {
|
|
114
|
+
prefix: new Uint8Array(this.#multibase.prefix),
|
|
115
|
+
key: [...this.#multibase.key],
|
|
116
|
+
encoded: this.#multibase.encoded
|
|
117
|
+
};
|
|
116
118
|
}
|
|
117
119
|
/**
|
|
118
120
|
* Returns the raw public key as a hex string.
|
|
@@ -144,40 +146,17 @@ export class CompressedSecp256k1PublicKey {
|
|
|
144
146
|
* @returns {KeyBytes} The decoded public key: prefix and public key bytes
|
|
145
147
|
*/
|
|
146
148
|
decode() {
|
|
147
|
-
|
|
148
|
-
const decoded = base58btc.decode(this.multibase.encoded);
|
|
149
|
-
// If the public key bytes are not 35 bytes, throw an error
|
|
150
|
-
if (decoded.length !== 35) {
|
|
151
|
-
throw new PublicKeyError('Invalid argument: must be 35 byte publicKeyMultibase', 'DECODE_MULTIBASE_ERROR');
|
|
152
|
-
}
|
|
153
|
-
// Grab the prefix bytes
|
|
154
|
-
const prefix = decoded.slice(0, 2);
|
|
155
|
-
// Compute the prefix hash
|
|
156
|
-
const prefixHash = Buffer.from(sha256(prefix)).toString('hex');
|
|
157
|
-
// If the prefix hash does not equal the BIP340 prefix hash, throw an error
|
|
158
|
-
if (prefixHash !== BIP340_PUBLIC_KEY_MULTIBASE_PREFIX_HASH) {
|
|
159
|
-
throw new PublicKeyError(`Invalid prefix: malformed multibase prefix ${prefix}`, 'DECODE_MULTIBASE_ERROR');
|
|
160
|
-
}
|
|
161
|
-
// Return the decoded public key bytes
|
|
162
|
-
return decoded;
|
|
149
|
+
return base58.decode(this.multibase.encoded.slice(1));
|
|
163
150
|
}
|
|
164
151
|
/**
|
|
165
152
|
* Encodes compressed secp256k1 public key from bytes to BIP340 multibase format.
|
|
166
153
|
* @returns {string} The public key encoded in base-58-btc multibase format.
|
|
167
154
|
*/
|
|
168
155
|
encode() {
|
|
169
|
-
// Convert public key bytes to an array
|
|
170
156
|
const pk = Array.from(this.compressed);
|
|
171
|
-
// Ensure the public key is 33-byte secp256k1 compressed public key
|
|
172
|
-
if (pk.length !== 33) {
|
|
173
|
-
throw new PublicKeyError('Invalid argument: must be 33-byte (compressed) public key', 'ENCODE_MULTIBASE_ERROR');
|
|
174
|
-
}
|
|
175
|
-
// Convert prefix to an array
|
|
176
157
|
const publicKeyMultibase = Array.from(BIP340_PUBLIC_KEY_MULTIBASE_PREFIX);
|
|
177
|
-
// Push the public key bytes at the end of the prefix
|
|
178
158
|
publicKeyMultibase.push(...pk);
|
|
179
|
-
|
|
180
|
-
return base58btc.encode(Uint8Array.from(publicKeyMultibase));
|
|
159
|
+
return 'z' + base58.encode(Uint8Array.from(publicKeyMultibase));
|
|
181
160
|
}
|
|
182
161
|
/**
|
|
183
162
|
* Verify a signature using schnorr or ecdsa.
|
|
@@ -190,24 +169,22 @@ export class CompressedSecp256k1PublicKey {
|
|
|
190
169
|
verify(signature, data, opts) {
|
|
191
170
|
// Default to schnorr scheme
|
|
192
171
|
opts ??= { scheme: 'schnorr' };
|
|
193
|
-
// If scheme is ecdsa, verify using ecdsa
|
|
194
172
|
if (opts.scheme === 'ecdsa') {
|
|
195
|
-
return
|
|
173
|
+
return secp256k1.verify(signature, data, this.compressed);
|
|
196
174
|
}
|
|
197
|
-
// If scheme is schnorr, verify using schnorr
|
|
198
175
|
else if (opts.scheme === 'schnorr') {
|
|
199
|
-
return
|
|
176
|
+
return schnorr.verify(signature, data, this.x);
|
|
200
177
|
}
|
|
201
178
|
// If scheme is neither ecdsa nor schnorr, throw an error
|
|
202
179
|
throw new PublicKeyError(`Invalid scheme: ${opts.scheme}.`, 'VERIFY_SIGNATURE_ERROR', opts);
|
|
203
180
|
}
|
|
204
181
|
/**
|
|
205
182
|
* Compares this public key to another public key.
|
|
206
|
-
* @param {
|
|
183
|
+
* @param {PublicKey} other The other public key to compare
|
|
207
184
|
* @returns {boolean} True if the public keys are equal, false otherwise.
|
|
208
185
|
*/
|
|
209
186
|
equals(other) {
|
|
210
|
-
return this.
|
|
187
|
+
return equalBytes(this.compressed, other.compressed);
|
|
211
188
|
}
|
|
212
189
|
/**
|
|
213
190
|
* JSON representation of a CompressedSecp256k1PublicKey object.
|
|
@@ -224,61 +201,6 @@ export class CompressedSecp256k1PublicKey {
|
|
|
224
201
|
},
|
|
225
202
|
};
|
|
226
203
|
}
|
|
227
|
-
/**
|
|
228
|
-
* Computes modular exponentiation: (base^exp) % mod.
|
|
229
|
-
* Used for computing modular square roots.
|
|
230
|
-
* @param {bigint} base The base value
|
|
231
|
-
* @param {bigint} exp The exponent value
|
|
232
|
-
* @param {bigint} mod The modulus value
|
|
233
|
-
* @returns {bigint} The result of the modular exponentiation
|
|
234
|
-
*/
|
|
235
|
-
modPow(base, exp, mod) {
|
|
236
|
-
let result = 1n;
|
|
237
|
-
while (exp > 0n) {
|
|
238
|
-
if (exp & 1n)
|
|
239
|
-
result = (result * base) % mod;
|
|
240
|
-
base = (base * base) % mod;
|
|
241
|
-
exp >>= 1n;
|
|
242
|
-
}
|
|
243
|
-
return result;
|
|
244
|
-
}
|
|
245
|
-
;
|
|
246
|
-
/**
|
|
247
|
-
* Computes `sqrt(a) mod p` using Tonelli-Shanks algorithm.
|
|
248
|
-
* This finds `y` such that `y^2 ≡ a mod p`.
|
|
249
|
-
* @param {bigint} a The value to find the square root of
|
|
250
|
-
* @param {bigint} p The prime modulus
|
|
251
|
-
* @returns {bigint} The square root of `a` mod `p`
|
|
252
|
-
*/
|
|
253
|
-
sqrtMod(a, p) {
|
|
254
|
-
return this.modPow(a, (p + 1n) >> 2n, p);
|
|
255
|
-
}
|
|
256
|
-
;
|
|
257
|
-
/**
|
|
258
|
-
* Lifts a 32-byte x-only coordinate into a full secp256k1 point (x, y).
|
|
259
|
-
* @param xBytes 32-byte x-coordinate
|
|
260
|
-
* @returns {Uint8Array} 65-byte uncompressed public key (starts with `0x04`)
|
|
261
|
-
*/
|
|
262
|
-
liftX() {
|
|
263
|
-
// Ensure x-coordinate is 32 bytes
|
|
264
|
-
if (this.x.length !== 32) {
|
|
265
|
-
throw new PublicKeyError('Invalid argument: x-coordinate length must be 32 bytes', 'LIFT_X_ERROR');
|
|
266
|
-
}
|
|
267
|
-
// Convert x from Uint8Array → BigInt
|
|
268
|
-
const x = BigInt('0x' + Buffer.from(this.x).toString('hex'));
|
|
269
|
-
if (x <= 0n || x >= CURVE.p) {
|
|
270
|
-
throw new PublicKeyError('Invalid conversion: x out of range as BigInt', 'LIFT_X_ERROR');
|
|
271
|
-
}
|
|
272
|
-
// Compute y² = x³ + 7 mod p
|
|
273
|
-
const ySquared = BigInt((x ** 3n + CURVE.b) % CURVE.p);
|
|
274
|
-
// Compute y (do not enforce parity)
|
|
275
|
-
const y = this.sqrtMod(ySquared, CURVE.p);
|
|
276
|
-
// Convert x and y to Uint8Array
|
|
277
|
-
const yBytes = Buffer.from(y.toString(16).padStart(64, '0'), 'hex');
|
|
278
|
-
// Return 65-byte uncompressed public key: `0x04 || x || y`
|
|
279
|
-
return new Uint8Array(Buffer.concat([Buffer.from([0x04]), Buffer.from(this.x), yBytes]));
|
|
280
|
-
}
|
|
281
|
-
;
|
|
282
204
|
/**
|
|
283
205
|
* Static method to validate a public key.
|
|
284
206
|
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
@@ -293,53 +215,13 @@ export class CompressedSecp256k1PublicKey {
|
|
|
293
215
|
return false;
|
|
294
216
|
}
|
|
295
217
|
}
|
|
296
|
-
/**
|
|
297
|
-
* Returns the point of the public key.
|
|
298
|
-
* @param {Hex} pk The public key in hex (Uint8Array or string) format.
|
|
299
|
-
* @returns {Point} The point of the public key.
|
|
300
|
-
* @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
|
|
301
|
-
*/
|
|
302
|
-
static point(pk) {
|
|
303
|
-
// If the public key is a hex string, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
304
|
-
if (typeof pk === 'string' && /^[0-9a-fA-F]+$/.test(pk)) {
|
|
305
|
-
const publicKey = new CompressedSecp256k1PublicKey(Buffer.from(pk, 'hex'));
|
|
306
|
-
return publicKey.point;
|
|
307
|
-
}
|
|
308
|
-
// If the public key is a byte array or ArrayBuffer, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
309
|
-
if (pk instanceof Uint8Array || ArrayBuffer.isView(pk)) {
|
|
310
|
-
const publicKey = new CompressedSecp256k1PublicKey(pk);
|
|
311
|
-
return publicKey.point;
|
|
312
|
-
}
|
|
313
|
-
// If the public key is neither a hex string nor a byte array, throw an error
|
|
314
|
-
throw new PublicKeyError('Invalid publicKey: must be a hex string or byte array', 'POINT_ERROR', { publicKey: pk });
|
|
315
|
-
}
|
|
316
218
|
/**
|
|
317
219
|
* Creates a CompressedSecp256k1PublicKey object from a JSON representation.
|
|
318
220
|
* @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
|
|
319
221
|
* @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
|
|
320
222
|
*/
|
|
321
223
|
static fromJSON(json) {
|
|
322
|
-
json.point.
|
|
323
|
-
return new CompressedSecp256k1PublicKey(Uint8Array.from(json.point.x));
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Computes the deterministic public key for a given secret key.
|
|
327
|
-
* @param {Secp256k1SecretKey | KeyBytes} sk The Secp256k1SecretKey object or the secret key bytes
|
|
328
|
-
* @returns {CompressedSecp256k1PublicKey} A new CompressedSecp256k1PublicKey object
|
|
329
|
-
*/
|
|
330
|
-
static fromSecretKey(sk) {
|
|
331
|
-
// If the secret key is a Secp256k1SecretKey object, get the raw bytes else use the bytes
|
|
332
|
-
const bytes = sk instanceof Secp256k1SecretKey ? sk.bytes : sk;
|
|
333
|
-
// Throw error if the secret key is not 32 bytes
|
|
334
|
-
if (bytes.length !== 32) {
|
|
335
|
-
throw new PublicKeyError('Invalid arg: must be 32 byte secret key', 'FROM_SECRET_KEY_ERROR');
|
|
336
|
-
}
|
|
337
|
-
// Compute the public key from the secret key
|
|
338
|
-
const secret = sk instanceof Secp256k1SecretKey
|
|
339
|
-
? sk
|
|
340
|
-
: new Secp256k1SecretKey(sk);
|
|
341
|
-
// Return a new CompressedSecp256k1PublicKey object
|
|
342
|
-
return secret.computePublicKey();
|
|
224
|
+
return new CompressedSecp256k1PublicKey(Uint8Array.from([json.point.parity, ...json.point.x]));
|
|
343
225
|
}
|
|
344
226
|
}
|
|
345
227
|
//# 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,
|
|
1
|
+
{"version":3,"file":"public.js","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,cAAc,EAEf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,MAAM,CAAC,MAAM,kCAAkC,GAAU,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAkGtF;;;;;;GAMG;AACH,MAAM,OAAO,4BAA4B;IACvC;;QAEI;IACK,MAAM,CAAW;IAE1B;;OAEG;IACM,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,IA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