@did-btcr2/keypair 0.5.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.
@@ -0,0 +1,271 @@
1
+ import { BIP340_SECRET_KEY_MULTIBASE_PREFIX, BIP340_SECRET_KEY_MULTIBASE_PREFIX_HASH, CURVE, SecretKeyError } from '@did-btcr2/common';
2
+ import { sha256 } from '@noble/hashes/sha2';
3
+ import { getRandomValues } from 'crypto';
4
+ import { base58btc } from 'multiformats/bases/base58';
5
+ import * as tinysecp from 'tiny-secp256k1';
6
+ import { SchnorrKeyPair } from './pair.js';
7
+ /**
8
+ * Encapsulates a secp256k1 secret key
9
+ * Provides get methods for different formats (raw, secret, point).
10
+ * Provides helpers methods for comparison, serialization and publicKey generation.
11
+ * @class SecretKey
12
+ * @type {SecretKey}
13
+ *
14
+ */
15
+ export class SecretKey {
16
+ /** @type {KeyBytes} The entropy for the secret key as a byte array */
17
+ _bytes;
18
+ /** @type {bigint} The entropy for the secret key as a bigint */
19
+ _seed;
20
+ /** @type {string} The secret key as a secretKeyMultibase */
21
+ _multibase;
22
+ /**
23
+ * Instantiates an instance of SecretKey.
24
+ * @param {Entropy} entropy bytes (Uint8Array) or secret (bigint)
25
+ * @throws {SecretKeyError} If entropy is not provided, not a valid 32-byte secret key or not a valid bigint seed
26
+ */
27
+ constructor(entropy) {
28
+ // If entropy not valid bytes or bigint seed, throw an error
29
+ const isBytes = entropy instanceof Uint8Array;
30
+ const isSecret = typeof entropy === 'bigint';
31
+ if (!isBytes && !isSecret) {
32
+ throw new SecretKeyError('Invalid entropy: must be a valid byte array (32) or bigint', 'CONSTRUCTOR_ERROR');
33
+ }
34
+ // If bytes and bytes are not length 32
35
+ if (isBytes && entropy.length === 32) {
36
+ this._bytes = entropy;
37
+ this._seed = SecretKey.toSecret(entropy);
38
+ }
39
+ // If secret and secret is not a valid bigint, throw error
40
+ if (isSecret && !(entropy < 1n || entropy >= CURVE.n)) {
41
+ this._bytes = SecretKey.toBytes(entropy);
42
+ this._seed = entropy;
43
+ }
44
+ if (!this._bytes || this._bytes.length !== 32) {
45
+ throw new SecretKeyError('Invalid bytes: must be a valid 32-byte secret key', 'CONSTRUCTOR_ERROR');
46
+ }
47
+ if (!this._seed || (this._seed < 1n || this._seed >= CURVE.n)) {
48
+ throw new SecretKeyError('Invalid seed: must must be valid bigint', 'CONSTRUCTOR_ERROR');
49
+ }
50
+ // Set the secret key multibase
51
+ this._multibase = this.encode();
52
+ }
53
+ /**
54
+ * Get the secret key entropy as a byte array.
55
+ * @returns {KeyBytes} The secret key bytes as a Uint8Array
56
+ */
57
+ get bytes() {
58
+ // Return a copy of the secret key bytes
59
+ const bytes = new Uint8Array(this._bytes);
60
+ return bytes;
61
+ }
62
+ /**
63
+ * Get the secret key entropy as a bigint.
64
+ * @returns {bigint} The secret key as a bigint
65
+ */
66
+ get seed() {
67
+ // Memoize the secret and return
68
+ const seed = BigInt(this._seed);
69
+ return seed;
70
+ }
71
+ /**
72
+ * Returns the raw secret key as a hex string.
73
+ * @returns {Hex} The secret key as a hex string
74
+ */
75
+ get hex() {
76
+ // Convert the raw secret key bytes to a hex string
77
+ return Buffer.from(this.bytes).toString('hex');
78
+ }
79
+ /**
80
+ * Encode the secret key bytes as a secretKeyMultibase string.
81
+ * @returns {string} The secret key in base58btc multibase format
82
+ */
83
+ get multibase() {
84
+ const multibase = this._multibase;
85
+ return multibase;
86
+ }
87
+ /**
88
+ * Encodes the secret key bytes to BIP340 multibase format.
89
+ * @returns {string} The secret key in BIP340 multibase format.
90
+ */
91
+ encode() {
92
+ // Convert Uint8Array bytes to an Array
93
+ const secretKeyBytes = this.bytes.toArray();
94
+ if (secretKeyBytes.length !== 32) {
95
+ throw new SecretKeyError('Invalid secret key: must be a valid 32-byte secret key', 'ENCODE_MULTIBASE_ERROR');
96
+ }
97
+ // Convert prefix to an array
98
+ const mbaseBytes = BIP340_SECRET_KEY_MULTIBASE_PREFIX.toArray();
99
+ // Push the secret key bytes at the end of the prefix
100
+ mbaseBytes.push(...secretKeyBytes);
101
+ // Encode the bytes in base58btc format and return
102
+ return base58btc.encode(mbaseBytes.toUint8Array());
103
+ }
104
+ /**
105
+ * Checks if this secret key is equal to another.
106
+ * @param {SecretKey} other The other secret key
107
+ * @returns {boolean} True if the private keys are equal, false otherwise
108
+ */
109
+ equals(other) {
110
+ // Compare the hex strings of the private keys
111
+ return this.hex === other.hex;
112
+ }
113
+ /**
114
+ * Computes the public key from the secret key bytes.
115
+ * @returns {KeyBytes} The computed public key
116
+ */
117
+ computePublicKey() {
118
+ // Derive the public key from the secret key
119
+ const publicKeyBytes = tinysecp.pointFromScalar(this.bytes, true);
120
+ // If no public key, throw error
121
+ if (!publicKeyBytes) {
122
+ throw new SecretKeyError('Invalid compute: failed to derive public key', 'COMPUTE_PUBLIC_KEY_ERROR');
123
+ }
124
+ // If public key is not compressed, throw error
125
+ if (publicKeyBytes.length !== 33) {
126
+ throw new SecretKeyError('Invalid compute: public key not compressed format', 'COMPUTE_PUBLIC_KEY_ERROR');
127
+ }
128
+ return publicKeyBytes;
129
+ }
130
+ /**
131
+ * Converts the secret key to a JSON object.
132
+ * @returns {SecretKeyObject} The secret key as a JSON object
133
+ */
134
+ json() {
135
+ return {
136
+ bytes: this.bytes.toArray(),
137
+ seed: this.seed.toString(),
138
+ hex: this.hex,
139
+ };
140
+ }
141
+ /**
142
+ * Checks if the secret key is valid.
143
+ * @returns {boolean} True if the secret key is valid, false otherwise
144
+ */
145
+ isValid() {
146
+ return tinysecp.isPrivate(this.bytes);
147
+ }
148
+ /**
149
+ * Checks if the public key is a valid secp256k1 point.
150
+ * @param {PublicKey} pk The public key to validate
151
+ * @returns {boolean} True if the public key is valid, false otherwise
152
+ */
153
+ isValidPair(pk) {
154
+ // If the public key is not valid, return false
155
+ if (!tinysecp.isPoint(pk.compressed)) {
156
+ return false;
157
+ }
158
+ // Else return true
159
+ return true;
160
+ }
161
+ /**
162
+ * Decodes the multibase string to the 34-byte secret key (2 byte prefix + 32 byte key).
163
+ * @param {string} multibase The multibase string to decode
164
+ * @returns {Bytes} The decoded secret key.
165
+ */
166
+ static decode(multibase) {
167
+ // Decode the public key multibase string
168
+ const decoded = base58btc.decode(multibase);
169
+ // If the public key bytes are not 35 bytes, throw an error
170
+ if (decoded.length !== 34) {
171
+ throw new SecretKeyError('Invalid argument: must be 34 byte secretKeyMultibase', 'DECODE_MULTIBASE_ERROR');
172
+ }
173
+ // Grab the prefix bytes
174
+ const prefix = decoded.slice(0, 2);
175
+ // Compute the prefix hash
176
+ const prefixHash = Buffer.from(sha256(prefix)).toString('hex');
177
+ // If the prefix hash does not equal the BIP340 prefix hash, throw an error
178
+ if (prefixHash !== BIP340_SECRET_KEY_MULTIBASE_PREFIX_HASH) {
179
+ throw new SecretKeyError(`Invalid prefix: malformed multibase prefix ${prefix}`, 'DECODE_MULTIBASE_ERROR');
180
+ }
181
+ // Return the decoded key bytes
182
+ return decoded;
183
+ }
184
+ /**
185
+ * Creates a SecretKey object from a JSON object.
186
+ * @param {SecretKeyObject} json The JSON object containing the secret key bytes
187
+ * @returns {SecretKey} A new SecretKey object
188
+ */
189
+ static fromJSON(json) {
190
+ return new SecretKey(new Uint8Array(json.bytes));
191
+ }
192
+ /**
193
+ * Converts a SecretKey or KeyBytes to a Pair.
194
+ * @param {KeyBytes} bytes
195
+ * @returns {SchnorrKeyPair} The SchnorrKeyPair object containing the public and private keys
196
+ * @throws {SecretKeyError} If the secret key is not valid
197
+ */
198
+ static toKeyPair(bytes) {
199
+ // Create a new SecretKey from the bytes
200
+ const secretKey = new SecretKey(bytes);
201
+ // Compute the public key from the secret key
202
+ const publicKey = secretKey.computePublicKey();
203
+ // Create a new Pair from the public key and secret key
204
+ return new SchnorrKeyPair({ publicKey, secretKey });
205
+ }
206
+ /**
207
+ * Convert a bigint secret to secret key bytes.
208
+ * @param {KeyBytes} bytes The secret key bytes
209
+ * @returns {bigint} The secret key bytes as a bigint secret
210
+ */
211
+ static toSecret(bytes) {
212
+ return bytes.reduce((acc, byte) => (acc << 8n) | BigInt(byte), 0n);
213
+ }
214
+ /**
215
+ * Convert a secret key bytes to a bigint secret.
216
+ * @param {bigint} secret The secret key secret.
217
+ * @returns {KeyBytes} The secret key secret as secret key bytes.
218
+ */
219
+ static toBytes(secret) {
220
+ // Ensure it’s a valid 32-byte value in [1, n-1] and convert bigint to Uint8Array
221
+ const bytes = Uint8Array.from({ length: 32 }, (_, i) => Number(secret >> BigInt(8 * (31 - i)) & BigInt(0xff)));
222
+ // If bytes are not a valid secp256k1 secret key, throw error
223
+ if (!tinysecp.isPrivate(bytes)) {
224
+ throw new SecretKeyError('Invalid secret key: secret out of valid range', 'SET_PRIVATE_KEY_ERROR');
225
+ }
226
+ return new Uint8Array(bytes);
227
+ }
228
+ /**
229
+ * Creates a new SecretKey object from a bigint secret.
230
+ * @param {bigint} secret The secret bigint
231
+ * @returns {SecretKey} A new SecretKey object
232
+ */
233
+ static fromSecret(secret) {
234
+ // Convert the secret bigint to a hex string
235
+ const hexsecret = secret.toString(16).padStart(64, '0');
236
+ // Convert the hex string to a Uint8Array
237
+ const privateKeyBytes = new Uint8Array(hexsecret.match(/.{2}/g).map(byte => parseInt(byte, 16)));
238
+ // Return a new SecretKey object
239
+ return new SecretKey(privateKeyBytes);
240
+ }
241
+ /**
242
+ * Generates random secret key bytes.
243
+ * @returns {KeyBytes} Uint8Array of 32 random bytes.
244
+ */
245
+ static random() {
246
+ // Generate empty 32-byte array
247
+ const byteArray = new Uint8Array(32);
248
+ // Use the getRandomValues function to fill the byteArray with random values
249
+ return getRandomValues(byteArray);
250
+ }
251
+ /**
252
+ * Creates a new SecretKey from random secret key bytes.
253
+ * @returns {SecretKey} A new SecretKey object
254
+ */
255
+ static generate() {
256
+ // Generate empty 32-byte array
257
+ const randomBytes = this.random();
258
+ // Use the getRandomValues function to fill the byteArray with random values
259
+ return new SecretKey(randomBytes);
260
+ }
261
+ /**
262
+ * Generates a public key from the given secret key bytes.
263
+ * @param {KeyBytes} bytes The secret key bytes
264
+ * @returns {KeyBytes} The computed public key bytes
265
+ */
266
+ static getPublicKey(bytes) {
267
+ // Create a new SecretKey from the bytes and compute the public key
268
+ return new SecretKey(bytes).computePublicKey();
269
+ }
270
+ }
271
+ //# sourceMappingURL=secret.js.map
@@ -0,0 +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;AAuD3C;;;;;;;GAOG;AACH,MAAM,OAAO,SAAS;IACpB,sEAAsE;IAC9D,MAAM,CAAY;IAE1B,gEAAgE;IACxD,KAAK,CAAU;IAEvB,4DAA4D;IACpD,UAAU,CAAS;IAE3B;;;;OAIG;IACH,YAAY,OAAgB;QAC1B,4DAA4D;QAC5D,MAAM,OAAO,GAAG,OAAO,YAAY,UAAU,CAAC;QAC9C,MAAM,QAAQ,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC;QAC7C,IAAG,CAAC,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;YACzB,MAAM,IAAI,cAAc,CACtB,4DAA4D,EAC5D,mBAAmB,CACpB,CAAC;QACJ,CAAC;QAED,uCAAuC;QACvC,IAAI,OAAO,IAAI,OAAO,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;YACrC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;YACtB,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC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EAAE,CAAC,CAAC,QAAQ,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;QACxD,yCAAyC;QACzC,MAAM,eAAe,GAAG,IAAI,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAE,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;QAClG,gCAAgC;QAChC,OAAO,IAAI,SAAS,CAAC,eAAe,CAAC,CAAC;IACxC,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,SAAS,CAAC,WAAW,CAAC,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,YAAY,CAAC,KAAe;QACxC,mEAAmE;QACnE,OAAO,IAAI,SAAS,CAAC,KAAK,CAAC,CAAC,gBAAgB,EAAE,CAAC;IACjD,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ export * from './pair.js';
2
+ export * from './secret.js';
3
+ export * from './public.js';
@@ -0,0 +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"}
@@ -0,0 +1,126 @@
1
+ import { Hex, KeyBytes, SchnorrKeyPairObject } from '@did-btcr2/common';
2
+ import { PublicKey } from './public.js';
3
+ import { SecretKey } from './secret.js';
4
+ /**
5
+ * Interface for KeyPair class.
6
+ * @interface KeyPair
7
+ * @type {KeyPair}
8
+ */
9
+ export interface KeyPair {
10
+ /**
11
+ * @type {PublicKey} The PublicKey associated with the SchnorrKeyPair (required).
12
+ */
13
+ readonly publicKey: PublicKey;
14
+ /**
15
+ * @type {SecretKey} The SecretKey associated with the SchnorrKeyPair (optional).
16
+ * @throws {KeyPairError} If the secret key is not available.
17
+ */
18
+ readonly secretKey?: SecretKey;
19
+ /**
20
+ * JSON representation of the SchnorrKeyPair object.
21
+ * @returns {SchnorrKeyPairObject} The SchnorrKeyPair as a JSON object.
22
+ */
23
+ json(): SchnorrKeyPairObject;
24
+ }
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
+ /**
39
+ * Encapsulates a PublicKey and a SecretKey object as a single Keys object.
40
+ * @class SchnorrKeyPair
41
+ * @type {SchnorrKeyPair}
42
+ */
43
+ export declare class SchnorrKeyPair implements KeyPair {
44
+ /** @type {SecretKey} The secret key object */
45
+ private _secretKey?;
46
+ private _publicKey;
47
+ /** @type {string} The public key in multibase format */
48
+ private _publicKeyMultibase;
49
+ /** @type {string} The secret key in multibase format */
50
+ private _secretKeyMultibase;
51
+ /**
52
+ * Creates an instance of Keys. Must provide a at least a secret key.
53
+ * Can optionally provide both a private and public key, but must be a valid pair.
54
+ * @param {SecretKey} secretKey The secret key object
55
+ */
56
+ constructor({ secretKey, publicKey }?: KeyParams);
57
+ /**
58
+ * Get the SecretKey.
59
+ * @returns {SecretKey} The SecretKey object
60
+ * @throws {KeyPairError} If the secret key is not available
61
+ */
62
+ get secretKey(): SecretKey;
63
+ /**
64
+ * Set the PublicKey.
65
+ * @param {PublicKey} publicKey The PublicKey object
66
+ * @throws {KeyPairError} If the public key is not a valid pair with the secret key.
67
+ */
68
+ set publicKey(publicKey: PublicKey);
69
+ /**
70
+ * Get the PublicKey.
71
+ * @returns {PublicKey} The PublicKey object
72
+ */
73
+ get publicKey(): PublicKey;
74
+ /**
75
+ * Get the Keys as a raw key pair.
76
+ * @returns {RawKeyPair} The Keys as a raw key pair
77
+ */
78
+ get raw(): RawKeyPair;
79
+ /**
80
+ * Get the Keys in multibase format.
81
+ * @returns {MultibaseKeys} The SecretKey in multibase format
82
+ */
83
+ get multibase(): MultibaseKeys;
84
+ /**
85
+ * JSON representation of a Keys.
86
+ * @returns {SchnorrKeyPairObject} The Keys as a JSON object
87
+ */
88
+ json(): SchnorrKeyPairObject;
89
+ /**
90
+ * Static method creates a new Keys from a JSON object.
91
+ * @param {SchnorrKeyPairObject} keys The JSON object to initialize the Keys.
92
+ * @returns {SchnorrKeyPair} The initialized Keys object.
93
+ */
94
+ static fromJSON(keys: SchnorrKeyPairObject): SchnorrKeyPair;
95
+ /**
96
+ * Static method creates a new SchnorrKeyPair from a SecretKey object or secret key bytes.
97
+ * @param {SecretKey | KeyBytes} data The secret key bytes
98
+ * @returns {SchnorrKeyPair} A new SchnorrKeyPair object
99
+ */
100
+ static fromPrivateKey(data: SecretKey | KeyBytes): SchnorrKeyPair;
101
+ /**
102
+ * Static method creates a new Keys (SecretKey/PublicKey) bigint secret.
103
+ * @param {bigint} secret The secret key secret
104
+ * @returns {Keys} A new Keys object
105
+ */
106
+ static fromSecret(secret: bigint): SchnorrKeyPair;
107
+ /**
108
+ * Converts key bytes to a hex string.
109
+ * @param {KeyBytes} keyBytes The key bytes (private or public).
110
+ * @returns {Hex} The key bytes as a hex string.
111
+ */
112
+ static toHex(keyBytes: KeyBytes): Hex;
113
+ /**
114
+ * Compares two Keys objects for equality.
115
+ * @param {SchnorrKeyPair} keys The main keys.
116
+ * @param {SchnorrKeyPair} otherKeys The other keys to compare.
117
+ * @returns {boolean} True if the public key and secret key are equal, false otherwise.
118
+ */
119
+ static equals(keys: SchnorrKeyPair, otherKeys: SchnorrKeyPair): boolean;
120
+ /**
121
+ * Static method to generate a new random SchnorrKeyPair instance.
122
+ * @returns {SchnorrKeyPair} A new SecretKey object.
123
+ */
124
+ static generate(): SchnorrKeyPair;
125
+ }
126
+ export {};
@@ -0,0 +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,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;IAE9B;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;IAE/B;;;OAGG;IACH,IAAI,IAAI,oBAAoB,CAAC;CAC9B;AAED,KAAK,UAAU,GAAG;IAChB,MAAM,EAAE,QAAQ,CAAC;IACjB,MAAM,CAAC,EAAE,QAAQ,CAAA;CAClB,CAAA;AAED,wDAAwD;AACxD,UAAU,SAAS;IACjB,SAAS,CAAC,EAAE,SAAS,GAAG,QAAQ,CAAC;IACjC,SAAS,CAAC,EAAE,SAAS,GAAG,QAAQ,CAAC;CAClC;AAED,UAAU,aAAa;IACrB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,kBAAkB,EAAE,MAAM,CAAA;CAC3B;AACD;;;;GAIG;AACH,qBAAa,cAAe,YAAW,OAAO;IAC5C,8CAA8C;IAC9C,OAAO,CAAC,UAAU,CAAC,CAAY;IAG/B,OAAO,CAAC,UAAU,CAAY;IAE9B,wDAAwD;IACxD,OAAO,CAAC,mBAAmB,CAAS;IAEpC,wDAAwD;IACxD,OAAO,CAAC,mBAAmB,CAAS;IAEpC;;;;OAIG;gBACS,EAAE,SAAS,EAAE,SAAS,EAAE,GAAE,SAAc;IA0BpD;;;;OAIG;IACH,IAAI,SAAS,IAAI,SAAS,CAYzB;IAED;;;;OAIG;IACH,IAAI,SAAS,CAAC,SAAS,EAAE,SAAS,EAQjC;IAED;;;OAGG;IACH,IAAI,SAAS,IAAI,SAAS,CAGzB;IAED;;;OAGG;IACH,IAAI,GAAG,IAAI,UAAU,CAKpB;IAED;;;OAGG;IACH,IAAI,SAAS,IAAI,aAAa,CAK7B;IAED;;;OAGG;IACI,IAAI,IAAI,oBAAoB;IAOnC;;;;OAIG;WACW,QAAQ,CAAC,IAAI,EAAE,oBAAoB,GAAG,cAAc;IAMlE;;;;OAIG;WACW,cAAc,CAAC,IAAI,EAAE,SAAS,GAAG,QAAQ,GAAG,cAAc;IAoBxE;;;;OAIG;WACW,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,cAAc;IAMxD;;;;OAIG;WACW,KAAK,CAAC,QAAQ,EAAE,QAAQ,GAAG,GAAG;IAI5C;;;;;OAKG;WACW,MAAM,CAAC,IAAI,EAAE,cAAc,EAAE,SAAS,EAAE,cAAc,GAAG,OAAO;IAqB9E;;;OAGG;WACW,QAAQ,IAAI,cAAc;CAazC"}
@@ -0,0 +1,197 @@
1
+ import { Hex, KeyBytes, Maybe, MultibaseObject, PublicKeyObject } from '@did-btcr2/common';
2
+ import { SecretKey } from './secret.js';
3
+ export interface Point {
4
+ x: KeyBytes;
5
+ y: KeyBytes;
6
+ }
7
+ /**
8
+ * Interface for the PublicKey class.
9
+ * @interface IPublicKey
10
+ * @type {IPublicKey}
11
+ */
12
+ export interface IPublicKey {
13
+ /**
14
+ * Uncompressed public key getter.
15
+ * @readonly @type {KeyBytes} The 65 byte uncompressed public key [0x04, x-coord, y-coord].
16
+ */
17
+ uncompressed: KeyBytes;
18
+ /**
19
+ * Compressed public key getter.
20
+ * @readonly @type {KeyBytes} The 33 byte compressed public key [parity, x-coord].
21
+ */
22
+ compressed: KeyBytes;
23
+ /**
24
+ * PublicKey parity getter.
25
+ * @readonly @type {number} The 1 byte parity (0x02 if even, 0x03 if odd).
26
+ */
27
+ parity: number;
28
+ /**
29
+ * PublicKey x-coordinate getter.
30
+ * @readonly @type {KeyBytes} The 32 byte x-coordinate of the public key.
31
+ */
32
+ x: KeyBytes;
33
+ /**
34
+ * PublicKey y-coordinate getter.
35
+ * @readonly @type {KeyBytes} The 32 byte y-coordinate of the public key.
36
+ */
37
+ y: KeyBytes;
38
+ /**
39
+ * PublicKey multibase getter.
40
+ * @readonly @returns {MultibaseObject} The public key as MultibaseObject as a address string, key and prefix bytes.
41
+ */
42
+ multibase: MultibaseObject;
43
+ /**
44
+ * PublicKey hex string getter.
45
+ * @readonly @type {Hex} The public key as a hex string.
46
+ */
47
+ hex: Hex;
48
+ /**
49
+ * Decode the base58btc multibase string to the compressed public key prefixed with 0x02.
50
+ * @returns {KeyBytes} The public key as a 33-byte compressed public key with header.
51
+ */
52
+ decode(): KeyBytes;
53
+ /**
54
+ * Encode the PublicKey as an x-only base58btc multibase public key.
55
+ * @returns {string} The public key formatted a base58btc multibase string.
56
+ */
57
+ encode(): string;
58
+ /**
59
+ * PublicKey key equality check. Checks if `this` public key is equal to `other` public key.
60
+ * @param {PublicKey} other The public key to compare.
61
+ * @returns {boolean} True if the public keys are equal.
62
+ */
63
+ equals(other: PublicKey): boolean;
64
+ /**
65
+ * JSON representation of a PublicKey object.
66
+ * @returns {PublicKeyObject} The PublicKey as a JSON object.
67
+ */
68
+ json(): PublicKeyObject;
69
+ }
70
+ /**
71
+ * Encapsulates a secp256k1 public key compliant to BIP-340 BIP schnorr signature scheme.
72
+ * Provides get methods for different formats (compressed, x-only, multibase).
73
+ * Provides helpers methods for comparison and serialization.
74
+ * @class PublicKey
75
+ * @type {PublicKey}
76
+ */
77
+ export declare class PublicKey implements PublicKey {
78
+ /** @type {KeyBytes} The public key bytes */
79
+ private readonly _bytes;
80
+ /** @type {MultibaseObject} The public key as a MultibaseObject */
81
+ private _multibase;
82
+ /**
83
+ * Creates a PublicKey instance.
84
+ * @param {KeyBytes} bytes The public key byte array.
85
+ * @throws {PublicKeyError} if the byte length is not 32 (x-only) or 33 (compressed)
86
+ */
87
+ constructor(bytes: KeyBytes);
88
+ /**
89
+ * Get the compressed public key.
90
+ * @returns {KeyBytes} The 33-byte compressed public key (0x02 or 0x03, x).
91
+ */
92
+ get compressed(): KeyBytes;
93
+ /**
94
+ * Get the uncompressed public key.
95
+ * @returns {Uint8Array} The 65-byte uncompressed public key (0x04, x, y).
96
+ */
97
+ get uncompressed(): KeyBytes;
98
+ /**
99
+ * Get the parity byte of the public key.
100
+ * @returns {number} The parity byte of the public key.
101
+ */
102
+ get parity(): number;
103
+ /**
104
+ * Get the x-coordinate of the public key.
105
+ * @returns {Uint8Array} The 32-byte x-coordinate of the public key.
106
+ */
107
+ get x(): KeyBytes;
108
+ /**
109
+ * Get the y-coordinate of the public key.
110
+ * @returns {Uint8Array} The 32-byte y-coordinate of the public key.
111
+ */
112
+ get y(): KeyBytes;
113
+ /**
114
+ * Get the multibase public key.
115
+ * @returns {MultibaseObject} An object containing the multibase bytes, address and prefix.
116
+ */
117
+ get multibase(): MultibaseObject;
118
+ /**
119
+ * Returns the raw public key as a hex string.
120
+ * @returns {Hex} The public key as a hex string.
121
+ */
122
+ get hex(): Hex;
123
+ /**
124
+ * Return the public key point.
125
+ * @returns {Point} The public key point.
126
+ */
127
+ get point(): Point;
128
+ /**
129
+ * Returns the point of the public key.
130
+ * @param {Hex} pk The public key in hex (Uint8Array or string) format.
131
+ * @returns {Point} The point of the public key.
132
+ * @throws {PublicKeyError} If the public key is not a valid hex string or byte array.
133
+ */
134
+ static point(pk: Hex): Point;
135
+ /**
136
+ * Decodes the multibase string to the 35-byte corresponding public key (2 byte prefix + 32 byte public key).
137
+ * @returns {KeyBytes} The decoded public key: prefix and public key bytes
138
+ */
139
+ decode(): KeyBytes;
140
+ /**
141
+ * Encodes compressed secp256k1 public key from bytes to BIP340 multibase format.
142
+ * @returns {string} The public key encoded in base-58-btc multibase format.
143
+ */
144
+ encode(): string;
145
+ /**
146
+ * Compares this public key to another public key.
147
+ * @param {PublicKey} other The other public key to compare
148
+ * @returns {boolean} True if the public keys are equal, false otherwise.
149
+ */
150
+ equals(other: PublicKey): boolean;
151
+ /**
152
+ * JSON representation of a PublicKey object.
153
+ * @returns {PublicKeyObject} The PublicKey as a JSON object.
154
+ */
155
+ json(): PublicKeyObject;
156
+ /**
157
+ * Creates a PublicKey object from a JSON representation.
158
+ * @param {PublicKeyObject} json The JSON object to initialize the PublicKey.
159
+ * @returns {PublicKey} The initialized PublicKey object.
160
+ */
161
+ static fromJSON(json: Maybe<PublicKeyObject>): PublicKey;
162
+ /**
163
+ * Computes the deterministic public key for a given private key.
164
+ * @param {PrivateKey | KeyBytes} sk The PrivateKey object or the private key bytes
165
+ * @returns {PublicKey} A new PublicKey object
166
+ */
167
+ static fromSecretKey(sk: SecretKey | KeyBytes): PublicKey;
168
+ /**
169
+ * Computes modular exponentiation: (base^exp) % mod.
170
+ * Used for computing modular square roots.
171
+ * @param {bigint} base The base value
172
+ * @param {bigint} exp The exponent value
173
+ * @param {bigint} mod The modulus value
174
+ * @returns {bigint} The result of the modular exponentiation
175
+ */
176
+ modPow(base: bigint, exp: bigint, mod: bigint): bigint;
177
+ /**
178
+ * Computes `sqrt(a) mod p` using Tonelli-Shanks algorithm.
179
+ * This finds `y` such that `y^2 ≡ a mod p`.
180
+ * @param {bigint} a The value to find the square root of
181
+ * @param {bigint} p The prime modulus
182
+ * @returns {bigint} The square root of `a` mod `p`
183
+ */
184
+ sqrtMod(a: bigint, p: bigint): bigint;
185
+ /**
186
+ * Lifts a 32-byte x-only coordinate into a full secp256k1 point (x, y).
187
+ * @param xBytes 32-byte x-coordinate
188
+ * @returns {Uint8Array} 65-byte uncompressed public key (starts with `0x04`)
189
+ */
190
+ liftX(): Uint8Array;
191
+ /**
192
+ * Static version of liftX method.
193
+ * @param {KeyBytes} x The 32-byte x-coordinate to lift.
194
+ * @returns {Uint8Array} The 65-byte uncompressed public key (0x04, x, y).
195
+ */
196
+ static xOnly(x: KeyBytes): Uint8Array;
197
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../src/public.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,GAAG,EACH,QAAQ,EACR,KAAK,EACL,eAAe,EAEf,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,WAAW,KAAK;IACpB,CAAC,EAAE,QAAQ,CAAC;IACZ,CAAC,EAAE,QAAQ,CAAC;CACb;AAED;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB;;;OAGG;IACH,YAAY,EAAE,QAAQ,CAAC;IAEvB;;;OAGG;IACH,UAAU,EAAE,QAAQ,CAAC;IAErB;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;;OAGG;IACH,CAAC,EAAE,QAAQ,CAAC;IAEZ;;;OAGG;IACH,CAAC,EAAE,QAAQ,CAAC;IAEZ;;;OAGG;IACH,SAAS,EAAE,eAAe,CAAC;IAE3B;;;OAGG;IACH,GAAG,EAAE,GAAG,CAAC;IAET;;;OAGG;IACH,MAAM,IAAI,QAAQ,CAAC;IAEnB;;;OAGG;IACH,MAAM,IAAI,MAAM,CAAC;IAEjB;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC;IAElC;;;OAGG;IACH,IAAI,IAAI,eAAe,CAAC;CACzB;AAED;;;;;;GAMG;AACH,qBAAa,SAAU,YAAW,SAAS;IACzC,4CAA4C;IAC5C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAW;IAElC,kEAAkE;IAClE,OAAO,CAAC,UAAU,CAIhB;IAEF;;;;OAIG;gBACS,KAAK,EAAE,QAAQ;IAgB3B;;;OAGG;IACH,IAAI,UAAU,IAAI,QAAQ,CAGzB;IAED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