@did-btcr2/keypair 0.7.0 → 0.7.2
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 +11 -1
- package/dist/cjs/pair.js.map +1 -1
- package/dist/cjs/public.js +42 -19
- package/dist/cjs/public.js.map +1 -1
- package/dist/cjs/secret.js +35 -14
- package/dist/cjs/secret.js.map +1 -1
- package/dist/esm/pair.js +11 -1
- package/dist/esm/pair.js.map +1 -1
- package/dist/esm/public.js +42 -19
- package/dist/esm/public.js.map +1 -1
- package/dist/esm/secret.js +35 -14
- package/dist/esm/secret.js.map +1 -1
- package/dist/types/pair.d.ts +7 -2
- package/dist/types/pair.d.ts.map +1 -1
- package/dist/types/public.d.ts +22 -10
- 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 +8 -1
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/pair.ts +13 -2
- package/src/public.ts +55 -24
- package/src/secret.ts +42 -17
- package/src/types.ts +8 -1
package/dist/esm/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
|
/**
|
|
@@ -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
|
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,
|
|
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;QAC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AAC,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/types/pair.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Hex, KeyBytes, SchnorrKeyPairObject } from '@did-btcr2/common';
|
|
2
2
|
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
3
3
|
import { Secp256k1SecretKey } from './secret.js';
|
|
4
|
-
import { MultibaseKeys, RawSchnorrKeyPair, SchnorrKeyPairParams } from './types.js';
|
|
4
|
+
import { HexSchnorrKeyPair, MultibaseKeys, RawSchnorrKeyPair, SchnorrKeyPairParams } from './types.js';
|
|
5
5
|
/**
|
|
6
6
|
* General KeyPair interface used by SchnorrKeyPair class.
|
|
7
7
|
* @interface KeyPair
|
|
@@ -64,10 +64,15 @@ export declare class SchnorrKeyPair implements KeyPair {
|
|
|
64
64
|
*/
|
|
65
65
|
get publicKey(): CompressedSecp256k1PublicKey;
|
|
66
66
|
/**
|
|
67
|
-
* Get the raw bytes of each key in the SchnorrKeyPair.
|
|
67
|
+
* Get the `raw` bytes of each key in the SchnorrKeyPair.
|
|
68
68
|
* @returns {RawSchnorrKeyPair} JSON object with the SchnorrKeyPair raw bytes.
|
|
69
69
|
*/
|
|
70
70
|
get raw(): RawSchnorrKeyPair;
|
|
71
|
+
/**
|
|
72
|
+
* Get the Keys in hex format.
|
|
73
|
+
* @returns {object} The Keys in hex format
|
|
74
|
+
*/
|
|
75
|
+
get hex(): HexSchnorrKeyPair;
|
|
71
76
|
/**
|
|
72
77
|
* Get the Keys in multibase format.
|
|
73
78
|
* @returns {MultibaseKeys} The Secp256k1SecretKey in multibase format
|
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,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;
|
|
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,iBAAiB,EAAE,aAAa,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEvG;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,QAAQ,CAAC,SAAS,EAAE,4BAA4B,CAAC;IAEjD;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE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ytes, MultibaseObject, PublicKeyObject } from '@did-btcr2/common';
|
|
2
2
|
import { Secp256k1SecretKey } from './secret.js';
|
|
3
|
+
import { CryptoOptions } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Point Interface representing an (x, y) coordinate on the secp256k1 curve.
|
|
6
|
+
* @interface Point
|
|
7
|
+
* @type {Point}
|
|
8
|
+
*/
|
|
3
9
|
export interface Point {
|
|
4
10
|
x: KeyBytes;
|
|
5
11
|
y: KeyBytes;
|
|
@@ -90,16 +96,13 @@ export interface PublicKey {
|
|
|
90
96
|
* @type {CompressedSecp256k1PublicKey}
|
|
91
97
|
*/
|
|
92
98
|
export declare class CompressedSecp256k1PublicKey implements PublicKey {
|
|
93
|
-
|
|
94
|
-
private readonly _bytes;
|
|
95
|
-
/** @type {MultibaseObject} The public key as a MultibaseObject */
|
|
96
|
-
private _multibase;
|
|
99
|
+
#private;
|
|
97
100
|
/**
|
|
98
101
|
* Creates a CompressedSecp256k1PublicKey instance.
|
|
99
|
-
* @param {
|
|
102
|
+
* @param {Hex} pk The public key byte array.
|
|
100
103
|
* @throws {PublicKeyError} if the byte length is not 32 (x-only) or 33 (compressed)
|
|
101
104
|
*/
|
|
102
|
-
constructor(
|
|
105
|
+
constructor(pk: Hex);
|
|
103
106
|
/**
|
|
104
107
|
* Get the compressed public key.
|
|
105
108
|
* @returns {KeyBytes} The 33-byte compressed public key (0x02 or 0x03, x).
|
|
@@ -142,9 +145,9 @@ export declare class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
142
145
|
get multibase(): MultibaseObject;
|
|
143
146
|
/**
|
|
144
147
|
* Returns the raw public key as a hex string.
|
|
145
|
-
* @returns {
|
|
148
|
+
* @returns {string} The public key as a hex string.
|
|
146
149
|
*/
|
|
147
|
-
get hex():
|
|
150
|
+
get hex(): string;
|
|
148
151
|
/**
|
|
149
152
|
* Return the public key point.
|
|
150
153
|
* @returns {Point} The public key point.
|
|
@@ -172,6 +175,15 @@ export declare class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
172
175
|
* @returns {string} The public key encoded in base-58-btc multibase format.
|
|
173
176
|
*/
|
|
174
177
|
encode(): string;
|
|
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: Bytes, data: Bytes, opts?: CryptoOptions): boolean;
|
|
175
187
|
/**
|
|
176
188
|
* Compares this public key to another public key.
|
|
177
189
|
* @param {CompressedSecp256k1PublicKey} other The other public key to compare
|
|
@@ -188,7 +200,7 @@ export declare class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
188
200
|
* @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
|
|
189
201
|
* @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
|
|
190
202
|
*/
|
|
191
|
-
static fromJSON(json:
|
|
203
|
+
static fromJSON(json: PublicKeyObject): CompressedSecp256k1PublicKey;
|
|
192
204
|
/**
|
|
193
205
|
* Computes the deterministic public key for a given secret key.
|
|
194
206
|
* @param {Secp256k1SecretKey | KeyBytes} sk The Secp256k1SecretKey object or the secret key bytes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,EAEL,GAAG,EACH,QAAQ,EACR,eAAe,EAEf,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAI3B,OAAO,EAAE,kBAAkB,EAAE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AGG;IACH,IAAI,CAAC,IAAI,QAAQ,CAGhB;IAED;;;OAGG;IACH,IAAI,CAAC,IAAI,QAAQ,CAGhB;IAED;;;OAGG;IACH,IAAI,SAAS,IAAI,eAAe,CAG/B;IAED;;;OAGG;IACH,IAAI,GAAG,IAAI,MAAM,CAGhB;IAED;;;OAGG;IACH,IAAI,KAAK,IAAI,KAAK,CAKjB;IAED;;;OAGG;IACI,MAAM,IAAI,QAAQ;IAIzB;;;;;OAKG;WACW,KAAK,CAAC,EAAE,EAAE,GAAG,GAAG,KAAK;IAoBnC;;;OAGG;IACI,MAAM,IAAI,QAAQ;IA8BzB;;;OAGG;IACI,MAAM,IAAI,MAAM;IAsBvB;;;;;;;OAOG;IACI,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,aAAa,GAAG,OAAO;IAY3E;;;;OAIG;IACI,MAAM,CAAC,KAAK,EAAE,4BAA4B,GAAG,OAAO;IAI3D;;;OAGG;IACI,IAAI,IAAI,eAAe;IAY9B;;;;OAIG;WACW,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,4BAA4B;IAK3E;;;;OAIG;WACW,aAAa,CAAC,EAAE,EAAE,kBAAkB,GAAG,QAAQ,GAAG,4BAA4B;IAkB5F;;;;;;;OAOG;IACI,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM;IAU7D;;;;;;OAMG;IACI,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM;IAI5C;;;;OAIG;IACI,KAAK,IAAI,UAAU;CAwB3B"}
|
package/dist/types/secret.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Bytes, Entropy, Hex, KeyBytes, SecretKeyObject } from '@did-btcr2/common';
|
|
1
|
+
import { Bytes, Entropy, Hex, KeyBytes, SecretKeyObject, SignatureBytes } from '@did-btcr2/common';
|
|
2
2
|
import { SchnorrKeyPair } from './pair.js';
|
|
3
3
|
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
4
|
+
import { CryptoOptions } from './types.js';
|
|
4
5
|
/**
|
|
5
6
|
* General SecretKey interface for the Secp256k1SecretKey class.
|
|
6
7
|
* @interface SecretKey
|
|
@@ -54,12 +55,7 @@ export interface SecretKey {
|
|
|
54
55
|
* @implements {SecretKey}
|
|
55
56
|
*/
|
|
56
57
|
export declare class Secp256k1SecretKey implements SecretKey {
|
|
57
|
-
|
|
58
|
-
private _bytes?;
|
|
59
|
-
/** @type {bigint} The entropy for the secret key as a bigint */
|
|
60
|
-
private _seed?;
|
|
61
|
-
/** @type {string} The secret key as a secretKeyMultibase */
|
|
62
|
-
private _multibase;
|
|
58
|
+
#private;
|
|
63
59
|
/**
|
|
64
60
|
* Instantiates an instance of Secp256k1SecretKey.
|
|
65
61
|
* @param {Entropy} entropy bytes (Uint8Array) or secret (bigint)
|
|
@@ -117,6 +113,15 @@ export declare class Secp256k1SecretKey implements SecretKey {
|
|
|
117
113
|
* @returns {boolean} True if the public key is valid, false otherwise
|
|
118
114
|
*/
|
|
119
115
|
hasValidPublicKey(): boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Produce a signature over arbitrary data using schnorr or ecdsa.
|
|
118
|
+
* @param {MessageBytes} data Data to be signed.
|
|
119
|
+
* @param {CryptoOptions} opts Options for signing.
|
|
120
|
+
* @param {('ecdsa' | 'schnorr')} opts.scheme The signature scheme to use. Default is 'schnorr'.
|
|
121
|
+
* @returns {SignatureBytes} Signature byte array.
|
|
122
|
+
* @throws {SecretKeyError} if no private key is provided.
|
|
123
|
+
*/
|
|
124
|
+
sign(data: Bytes, opts?: CryptoOptions): SignatureBytes;
|
|
120
125
|
/**
|
|
121
126
|
* Decodes the multibase string to the 34-byte secret key (2 byte prefix + 32 byte key).
|
|
122
127
|
* @param {string} multibase The multibase string to decode
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secret.d.ts","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,EAEL,OAAO,EACP,GAAG,EACH,QAAQ,EAER,eAAe,
|
|
1
|
+
{"version":3,"file":"secret.d.ts","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,EAEL,OAAO,EACP,GAAG,EACH,QAAQ,EAER,eAAe,EACf,cAAc,EACf,MAAM,mBAAmB,CAAC;AAK3B,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB;;;OAGG;IACH,KAAK,EAAE,QAAQ,CAAC;IAEhB;;;;OAIG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;OAGG;IACH,GAAG,EAAE,GAAG,CAAC;IAET;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC;IAE3C;;;OAGG;IACH,gBAAgB,IAAI,4BAA4B,CAAC;IAEjD;;;OAGG;IACH,OAAO,IAAI,OAAO,CAAC;IAEnB;;;OAGG;IACH,IAAI,IAAI,eAAe,CAAC;CACzB;AAED;;;;;;;GAOG;AACH,qBAAa,kBAAmB,YAAW,SAAS;;IAUlD;;;;OAIG;gBACS,OAAO,EAAE,OAAO;IAyC5B;;;OAGG;IACH,IAAI,KAAK,IAAI,UAAU,CAItB;IAED;;;OAGG;IACH,IAAI,IAAI,IAAI,MAAM,CAIjB;IAED;;;OAGG;IACH,IAAI,GAAG,IAAI,GAAG,CAGb;IAGD;;;OAGG;IACH,IAAI,SAAS,IAAI,MAAM,CAGtB;IAED;;;OAGG;IACI,MAAM,IAAI,MAAM;IAoBvB;;;;OAIG;IACI,MAAM,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO;IAKjD;;;OAGG;IACI,gBAAgB,IAAI,4BAA4B;IAuBvD;;;OAGG;IACI,IAAI,IAAI,eAAe;IAQ9B;;;OAGG;IACI,OAAO,IAAI,OAAO;IAIzB;;;OAGG;IACI,iBAAiB,IAAI,OAAO;IAanC;;;;;;;OAOG;IACI,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,aAAa,GAAG,cAAc;IAiB9D;;;;OAIG;WACW,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,KAAK;IA8B9C;;;;OAIG;WACW,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,kBAAkB;IAIjE;;;;;OAKG;WACW,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,cAAc;IAWxD;;;;OAIG;WACW,QAAQ,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM;IAI/C;;;;OAIG;WACW,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ;IAiB/C;;;;OAIG;WACW,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,kBAAkB;IAS9D;;;OAGG;WACW,MAAM,IAAI,QAAQ;IAQhC;;;OAGG;WACW,QAAQ,IAAI,kBAAkB;IAQ5C;;;;OAIG;WACW,YAAY,CAAC,KAAK,EAAE,QAAQ,GAAG,4BAA4B;CAI1E"}
|
package/dist/types/types.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import { KeyBytes } from '@did-btcr2/common';
|
|
1
|
+
import { Hex, KeyBytes } from '@did-btcr2/common';
|
|
2
2
|
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
3
3
|
import { Secp256k1SecretKey } from './secret.js';
|
|
4
|
+
export type CryptoOptions = {
|
|
5
|
+
scheme: 'ecdsa' | 'schnorr';
|
|
6
|
+
};
|
|
4
7
|
export type RawSchnorrKeyPair = {
|
|
5
8
|
public: KeyBytes;
|
|
6
9
|
secret?: KeyBytes;
|
|
7
10
|
};
|
|
11
|
+
export type HexSchnorrKeyPair = {
|
|
12
|
+
public: Hex;
|
|
13
|
+
secret?: Hex;
|
|
14
|
+
};
|
|
8
15
|
/** Params for the {@link SchnorrKeyPair} constructor */
|
|
9
16
|
export interface SchnorrKeyPairParams {
|
|
10
17
|
secretKey?: Secp256k1SecretKey | KeyBytes;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD,MAAM,MAAM,aAAa,GAAG;IAAE,MAAM,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,CAAA;AAE3D,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,EAAE,QAAQ,CAAC;IACjB,MAAM,CAAC,EAAE,QAAQ,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,EAAE,GAAG,CAAC;IACZ,MAAM,CAAC,EAAE,GAAG,CAAA;CACb,CAAA;AAED,wDAAwD;AACxD,MAAM,WAAW,oBAAoB;IACnC,SAAS,CAAC,EAAE,kBAAkB,GAAG,QAAQ,CAAC;IAC1C,SAAS,CAAC,EAAE,4BAA4B,GAAG,QAAQ,CAAC;CACrD;AAED,MAAM,WAAW,aAAa;IAC5B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,kBAAkB,EAAE,MAAM,CAAA;CAC3B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@did-btcr2/keypair",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "JavaScript/TypeScript implementation of secp256k1 public/private key pairs with BIP340 schnorr signatures. Used by various parts of the did-btcr2-js monorepo.",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"@noble/hashes": "^1.7.1",
|
|
63
63
|
"multiformats": "^13.3.2",
|
|
64
64
|
"tiny-secp256k1": "^2.2.3",
|
|
65
|
-
"@did-btcr2/common": "2.2.
|
|
65
|
+
"@did-btcr2/common": "2.2.2"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@eslint/js": "^9.20.0",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"@types/chai-as-promised": "^8.0.1",
|
|
71
71
|
"@types/eslint": "^9.6.1",
|
|
72
72
|
"@types/mocha": "^10.0.10",
|
|
73
|
-
"@types/node": "^22.
|
|
73
|
+
"@types/node": "^22.18.4",
|
|
74
74
|
"@typescript-eslint/eslint-plugin": "^8.24.1",
|
|
75
75
|
"@typescript-eslint/parser": "^8.24.1",
|
|
76
76
|
"c8": "^10.1.3",
|
package/src/pair.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
} from '@did-btcr2/common';
|
|
7
7
|
import { CompressedSecp256k1PublicKey } from './public.js';
|
|
8
8
|
import { Secp256k1SecretKey } from './secret.js';
|
|
9
|
-
import { MultibaseKeys, RawSchnorrKeyPair, SchnorrKeyPairParams } from './types.js';
|
|
9
|
+
import { HexSchnorrKeyPair, MultibaseKeys, RawSchnorrKeyPair, SchnorrKeyPairParams } from './types.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* General KeyPair interface used by SchnorrKeyPair class.
|
|
@@ -134,7 +134,7 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
134
134
|
}
|
|
135
135
|
|
|
136
136
|
/**
|
|
137
|
-
* Get the raw bytes of each key in the SchnorrKeyPair.
|
|
137
|
+
* Get the `raw` bytes of each key in the SchnorrKeyPair.
|
|
138
138
|
* @returns {RawSchnorrKeyPair} JSON object with the SchnorrKeyPair raw bytes.
|
|
139
139
|
*/
|
|
140
140
|
get raw(): RawSchnorrKeyPair {
|
|
@@ -144,6 +144,17 @@ export class SchnorrKeyPair implements KeyPair {
|
|
|
144
144
|
};
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Get the Keys in hex format.
|
|
149
|
+
* @returns {object} The Keys in hex format
|
|
150
|
+
*/
|
|
151
|
+
get hex(): HexSchnorrKeyPair {
|
|
152
|
+
return {
|
|
153
|
+
public : this.publicKey.hex,
|
|
154
|
+
secret : this._secretKey ? this.secretKey.hex : undefined
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
147
158
|
/**
|
|
148
159
|
* Get the Keys in multibase format.
|
|
149
160
|
* @returns {MultibaseKeys} The Secp256k1SecretKey in multibase format
|
package/src/public.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BIP340_PUBLIC_KEY_MULTIBASE_PREFIX,
|
|
3
3
|
BIP340_PUBLIC_KEY_MULTIBASE_PREFIX_HASH,
|
|
4
|
+
Bytes,
|
|
4
5
|
CURVE,
|
|
5
6
|
Hex,
|
|
6
7
|
KeyBytes,
|
|
7
|
-
Maybe,
|
|
8
8
|
MultibaseObject,
|
|
9
9
|
PublicKeyError,
|
|
10
10
|
PublicKeyObject
|
|
@@ -13,7 +13,13 @@ import { sha256 } from '@noble/hashes/sha2';
|
|
|
13
13
|
import { base58btc } from 'multiformats/bases/base58';
|
|
14
14
|
import * as tinysecp from 'tiny-secp256k1';
|
|
15
15
|
import { Secp256k1SecretKey } from './secret.js';
|
|
16
|
+
import { CryptoOptions } from './types.js';
|
|
16
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Point Interface representing an (x, y) coordinate on the secp256k1 curve.
|
|
20
|
+
* @interface Point
|
|
21
|
+
* @type {Point}
|
|
22
|
+
*/
|
|
17
23
|
export interface Point {
|
|
18
24
|
x: KeyBytes;
|
|
19
25
|
y: KeyBytes;
|
|
@@ -120,10 +126,10 @@ export interface PublicKey {
|
|
|
120
126
|
*/
|
|
121
127
|
export class CompressedSecp256k1PublicKey implements PublicKey {
|
|
122
128
|
/** @type {KeyBytes} The public key bytes */
|
|
123
|
-
|
|
129
|
+
readonly #bytes: KeyBytes;
|
|
124
130
|
|
|
125
131
|
/** @type {MultibaseObject} The public key as a MultibaseObject */
|
|
126
|
-
|
|
132
|
+
readonly #multibase: MultibaseObject = {
|
|
127
133
|
prefix : BIP340_PUBLIC_KEY_MULTIBASE_PREFIX,
|
|
128
134
|
key : [],
|
|
129
135
|
encoded : ''
|
|
@@ -131,31 +137,35 @@ export class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
131
137
|
|
|
132
138
|
/**
|
|
133
139
|
* Creates a CompressedSecp256k1PublicKey instance.
|
|
134
|
-
* @param {
|
|
140
|
+
* @param {Hex} pk The public key byte array.
|
|
135
141
|
* @throws {PublicKeyError} if the byte length is not 32 (x-only) or 33 (compressed)
|
|
136
142
|
*/
|
|
137
|
-
constructor(
|
|
143
|
+
constructor(pk: Hex) {
|
|
144
|
+
pk = pk instanceof Uint8Array
|
|
145
|
+
? pk
|
|
146
|
+
: Buffer.fromHex(pk);
|
|
147
|
+
|
|
138
148
|
// If the byte length is not 33, throw an error
|
|
139
|
-
if(
|
|
149
|
+
if(!pk || pk.length !== 33) {
|
|
140
150
|
throw new PublicKeyError(
|
|
141
151
|
'Invalid argument: byte length must be 33 (compressed)',
|
|
142
|
-
'CONSTRUCTOR_ERROR', {
|
|
152
|
+
'CONSTRUCTOR_ERROR', { pk }
|
|
143
153
|
);
|
|
144
154
|
}
|
|
145
155
|
|
|
146
156
|
// Validate the point is on curve and in compressed form
|
|
147
|
-
if (!tinysecp.isPoint(
|
|
157
|
+
if (!tinysecp.isPoint(pk)) {
|
|
148
158
|
throw new PublicKeyError(
|
|
149
|
-
'Invalid argument:
|
|
150
|
-
'CONSTRUCTOR_ERROR', {
|
|
159
|
+
'Invalid argument: not a valid secp256k1 compressed point',
|
|
160
|
+
'CONSTRUCTOR_ERROR', { pk }
|
|
151
161
|
);
|
|
152
162
|
}
|
|
153
163
|
// Set the bytes
|
|
154
|
-
this
|
|
164
|
+
this.#bytes = pk;
|
|
155
165
|
|
|
156
166
|
// Set multibase
|
|
157
|
-
this.
|
|
158
|
-
this.
|
|
167
|
+
this.#multibase.encoded = this.encode();
|
|
168
|
+
this.#multibase.key = [...this.#multibase.prefix, ...this.compressed];
|
|
159
169
|
}
|
|
160
170
|
|
|
161
171
|
/**
|
|
@@ -163,7 +173,7 @@ export class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
163
173
|
* @returns {KeyBytes} The 33-byte compressed public key (0x02 or 0x03, x).
|
|
164
174
|
*/
|
|
165
175
|
get compressed(): KeyBytes {
|
|
166
|
-
const bytes = new Uint8Array(this
|
|
176
|
+
const bytes = new Uint8Array(this.#bytes);
|
|
167
177
|
return bytes;
|
|
168
178
|
};
|
|
169
179
|
|
|
@@ -180,7 +190,8 @@ export class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
180
190
|
* X-only (32-byte) view of the public key per BIP-340.
|
|
181
191
|
*/
|
|
182
192
|
get xOnly(): KeyBytes {
|
|
183
|
-
|
|
193
|
+
const xOnly = this.compressed.slice(1);
|
|
194
|
+
return xOnly;
|
|
184
195
|
}
|
|
185
196
|
|
|
186
197
|
/**
|
|
@@ -189,7 +200,7 @@ export class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
189
200
|
* @throws {PublicKeyError} If the parity byte is not 0x02 or 0x03.
|
|
190
201
|
*/
|
|
191
202
|
get parity(): 0x02 | 0x03 {
|
|
192
|
-
const parity = this.
|
|
203
|
+
const parity = this.compressed[0];
|
|
193
204
|
if(![0x02, 0x03].includes(parity)) {
|
|
194
205
|
throw new PublicKeyError(
|
|
195
206
|
'Invalid state: parity byte must be 2 or 3',
|
|
@@ -204,7 +215,7 @@ export class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
204
215
|
* @returns {boolean} True if the public key has even Y.
|
|
205
216
|
*/
|
|
206
217
|
get isEven(): boolean {
|
|
207
|
-
return this.
|
|
218
|
+
return this.parity === 0x02;
|
|
208
219
|
}
|
|
209
220
|
|
|
210
221
|
/**
|
|
@@ -230,15 +241,15 @@ export class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
230
241
|
* @returns {MultibaseObject} An object containing the multibase bytes, address and prefix.
|
|
231
242
|
*/
|
|
232
243
|
get multibase(): MultibaseObject {
|
|
233
|
-
const multibase = this
|
|
244
|
+
const multibase = this.#multibase;
|
|
234
245
|
return multibase;
|
|
235
246
|
}
|
|
236
247
|
|
|
237
248
|
/**
|
|
238
249
|
* Returns the raw public key as a hex string.
|
|
239
|
-
* @returns {
|
|
250
|
+
* @returns {string} The public key as a hex string.
|
|
240
251
|
*/
|
|
241
|
-
get hex():
|
|
252
|
+
get hex(): string {
|
|
242
253
|
const hex = Buffer.from(this.compressed).toString('hex');
|
|
243
254
|
return hex;
|
|
244
255
|
}
|
|
@@ -268,7 +279,7 @@ export class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
268
279
|
* @returns {Point} The point of the public key.
|
|
269
280
|
* @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
|
|
270
281
|
*/
|
|
271
|
-
static point(pk: Hex): Point {
|
|
282
|
+
public static point(pk: Hex): Point {
|
|
272
283
|
// If the public key is a hex string, convert it to a CompressedSecp256k1PublicKey object and return the point
|
|
273
284
|
if(typeof pk === 'string' && /^[0-9a-fA-F]+$/.test(pk)) {
|
|
274
285
|
const publicKey = new CompressedSecp256k1PublicKey(Buffer.fromHex(pk));
|
|
@@ -348,6 +359,26 @@ export class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
348
359
|
return base58btc.encode(publicKeyMultibase.toUint8Array());
|
|
349
360
|
}
|
|
350
361
|
|
|
362
|
+
/**
|
|
363
|
+
* Verify a signature using schnorr or ecdsa.
|
|
364
|
+
* @param {SignatureBytes} signature Signature for verification.
|
|
365
|
+
* @param {string} data Data for verification.
|
|
366
|
+
* @param {CryptoOptions} opts Options for signing.
|
|
367
|
+
* @param {('ecdsa' | 'schnorr')} opts.scheme The signature scheme to use. Default is 'schnorr'.
|
|
368
|
+
* @returns {boolean} If the signature is valid against the public key.
|
|
369
|
+
*/
|
|
370
|
+
public verify(signature: Bytes, data: Bytes, opts?: CryptoOptions): boolean {
|
|
371
|
+
opts ??= { scheme: 'schnorr' };
|
|
372
|
+
// Verify the signature depending on the scheme and return the result
|
|
373
|
+
if(opts.scheme === 'ecdsa') {
|
|
374
|
+
return tinysecp.verify(data, this.compressed, signature); }
|
|
375
|
+
else if(opts.scheme === 'schnorr') {
|
|
376
|
+
return tinysecp.verifySchnorr(data, this.x, signature);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
throw new PublicKeyError(`Invalid scheme: ${opts.scheme}.`, 'VERIFY_SIGNATURE_ERROR', opts);
|
|
380
|
+
}
|
|
381
|
+
|
|
351
382
|
/**
|
|
352
383
|
* Compares this public key to another public key.
|
|
353
384
|
* @param {CompressedSecp256k1PublicKey} other The other public key to compare
|
|
@@ -378,9 +409,9 @@ export class CompressedSecp256k1PublicKey implements PublicKey {
|
|
|
378
409
|
* @param {PublicKeyObject} json The JSON object to initialize the CompressedSecp256k1PublicKey.
|
|
379
410
|
* @returns {CompressedSecp256k1PublicKey} The initialized CompressedSecp256k1PublicKey object.
|
|
380
411
|
*/
|
|
381
|
-
public static fromJSON(json:
|
|
382
|
-
json.x.unshift(json.parity);
|
|
383
|
-
return new CompressedSecp256k1PublicKey(json.x.toUint8Array());
|
|
412
|
+
public static fromJSON(json: PublicKeyObject): CompressedSecp256k1PublicKey {
|
|
413
|
+
json.point.x.unshift(json.point.parity);
|
|
414
|
+
return new CompressedSecp256k1PublicKey(json.point.x.toUint8Array());
|
|
384
415
|
}
|
|
385
416
|
|
|
386
417
|
/**
|