@did-btcr2/kms 0.1.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.
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Class representing a signer for cryptographic operations.
3
+ * Remains for backwards compatibility. Plan to migrate to Kms.
4
+ * @class Signer
5
+ * @type {Signer}
6
+ */
7
+ export class Signer {
8
+ /**
9
+ * The key pair used for signing.
10
+ * @type {SchnorrKeyPair}
11
+ */
12
+ keyPair;
13
+ /**
14
+ * The network associated with the signer.
15
+ * @type {keyof AvailableNetworks}
16
+ */
17
+ network;
18
+ /**
19
+ * Creates an instance of Signer.
20
+ * @param {{ keyPair: SchnorrKeyPair; network: keyof AvailableNetworks; }} params The parameters for the signer.
21
+ * @param {SchnorrKeyPair} params.keyPair The key pair used for signing.
22
+ * @param {keyof AvailableNetworks} params.network The network associated with the signer.
23
+ */
24
+ constructor(params) {
25
+ this.keyPair = params.keyPair;
26
+ this.network = params.network;
27
+ }
28
+ /**
29
+ * Gets the public key bytes.
30
+ * @returns {KeyBytes} The public key bytes.
31
+ */
32
+ get publicKey() {
33
+ return this.keyPair.publicKey.compressed;
34
+ }
35
+ /**
36
+ * Signs the given hash using ECDSA.
37
+ * @param {Bytes} hash The hash to sign.
38
+ * @returns {SignatureBytes} The signature of the hash.
39
+ */
40
+ signEcdsa(hash) {
41
+ return this.keyPair.secretKey.sign(hash, { scheme: 'ecdsa' });
42
+ }
43
+ ;
44
+ /**
45
+ * Signs the given hash using Schnorr signature.
46
+ * @param {Bytes} hash The hash to sign.
47
+ * @returns {SignatureBytes} The Schnorr signature of the hash.
48
+ */
49
+ sign(hash) {
50
+ return this.keyPair.secretKey.sign(hash);
51
+ }
52
+ }
53
+ //# sourceMappingURL=signer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signer.js","sourceRoot":"","sources":["../../src/signer.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,MAAM,OAAO,MAAM;IACjB;;;OAGG;IACH,OAAO,CAAiB;IAExB;;;OAGG;IACH,OAAO,CAA0B;IAEjC;;;;;OAKG;IACH,YAAY,MAAsE;QAChF,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC9B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IAChC,CAAC;IAED;;;OAGG;IACH,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC;IAC3C,CAAC;IAED;;;;OAIG;IACH,SAAS,CAAC,IAAW;QACnB,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;IAChE,CAAC;IAAA,CAAC;IAEF;;;;OAIG;IACH,IAAI,CAAC,IAAW;QACd,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3C,CAAC;CACF"}
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Re-implementation of a simple in-memory key-value store.
3
+ *
4
+ * The `MemoryStore` class is an implementation of
5
+ * `KeyValueStore` that holds data in memory.
6
+ *
7
+ * It provides a basic key-value store that works synchronously and keeps all
8
+ * data in memory. This can be used for testing, or for handling small amounts
9
+ * of data with simple key-value semantics.
10
+ *
11
+ * Example usage:
12
+ *
13
+ * ```ts
14
+ * const memoryStore = new MemoryStore<string, number>();
15
+ * await memoryStore.set("key1", 1);
16
+ * const value = await memoryStore.get("key1");
17
+ * console.log(value); // 1
18
+ * ```
19
+ *
20
+ * @public
21
+ */
22
+ export class MemoryStore {
23
+ /**
24
+ * A private field that contains the Map used as the key-value store.
25
+ */
26
+ store = new Map();
27
+ /**
28
+ * Clears all entries in the key-value store.
29
+ *
30
+ * @returns {void} returns once the operation is complete.
31
+ */
32
+ clear() {
33
+ this.store.clear();
34
+ }
35
+ /**
36
+ * This operation is no-op for `MemoryStore`.
37
+ */
38
+ close() {
39
+ /** no-op */
40
+ }
41
+ /**
42
+ * Deletes an entry from the key-value store by its key.
43
+ *
44
+ * @param {K} id - The key of the entry to delete.
45
+ * @returns {boolean} a boolean indicating whether the entry was successfully deleted.
46
+ */
47
+ delete(id) {
48
+ return this.store.delete(id);
49
+ }
50
+ /**
51
+ * Retrieves the value of an entry by its key.
52
+ *
53
+ * @param {K} id - The key of the entry to retrieve.
54
+ * @returns {V | undefined} the value of the entry, or `undefined` if the entry does not exist.
55
+ */
56
+ get(id) {
57
+ return this.store.get(id);
58
+ }
59
+ /**
60
+ * Checks for the presence of an entry by key.
61
+ *
62
+ * @param {K} id - The key to check for the existence of.
63
+ * @returns {boolean} a boolean indicating whether an element with the specified key exists or not.
64
+ */
65
+ has(id) {
66
+ return this.store.has(id);
67
+ }
68
+ /**
69
+ * Retrieves all values in the key-value store.
70
+
71
+ * @returns {Array<V>} an array of all values in the store.
72
+ */
73
+ list() {
74
+ return Array.from(this.store.values());
75
+ }
76
+ /**
77
+ * Retrieves all entries in the key-value store.
78
+ *
79
+ * @returns {Array<[K, V]>} an array of key-value pairs in the store.
80
+ */
81
+ entries() {
82
+ return Array.from(this.store.entries());
83
+ }
84
+ /**
85
+ * Sets the value of an entry in the key-value store.
86
+ *
87
+ * @param {K} id - The key of the entry to set.
88
+ * @param {V} key - The new value for the entry.
89
+ * @returns {void} once operation is complete.
90
+ */
91
+ set(id, key) {
92
+ this.store.set(id, key);
93
+ }
94
+ }
95
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/store.ts"],"names":[],"mappings":"AAmDA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,WAAW;IACtB;;OAEG;IACK,KAAK,GAAc,IAAI,GAAG,EAAE,CAAC;IAErC;;;;OAIG;IACH,KAAK;QACH,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,KAAK;QACH,YAAY;IACd,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,EAAK;QACV,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAC/B,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,EAAK;QACP,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC5B,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,EAAK;QACP,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI;QACF,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;IACzC,CAAC;IAED;;;;OAIG;IACH,OAAO;QACL,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;IAC1C,CAAC;IAED;;;;;;OAMG;IACH,GAAG,CAAC,EAAK,EAAE,GAAM;QACf,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;IAC1B,CAAC;CACF"}
@@ -0,0 +1,5 @@
1
+ export * from './interface.js';
2
+ export * from './kms.js';
3
+ export * from './signer.js';
4
+ export * from './store.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interface.js","sourceRoot":"","sources":["../../src/interface.ts"],"names":[],"mappings":""}
@@ -0,0 +1,270 @@
1
+ import { KeyManagerError } from '@did-btcr2/common';
2
+ import { SchnorrKeyPair } from '@did-btcr2/keypair';
3
+ import { sha256 } from '@noble/hashes/sha2.js';
4
+ import { MemoryStore } from './store.js';
5
+ /**
6
+ * Class for managing cryptographic keys for the BTCR2 DID method.
7
+ * @class Kms
8
+ * @type {Kms}
9
+ */
10
+ export class Kms {
11
+ /**
12
+ * Singleton instance of the Kms.
13
+ * @private
14
+ * @type {KeyManager}
15
+ */
16
+ static #instance;
17
+ /**
18
+ * The `store` is a private variable in `KeyManager`. It is a `KeyValueStore` instance used for
19
+ * storing and managing cryptographic keys. It allows the `KeyManager` class to save,
20
+ * retrieve, and handle keys efficiently within the local Key Management System (KMS) context.
21
+ * This variable can be configured to use different storage backends, like in-memory storage or
22
+ * persistent storage, providing flexibility in key management according to the application's
23
+ * requirements.
24
+ * @private
25
+ * @type {KeyValueStore<KeyIdentifier, KeyBytes>} The key store for managing cryptographic keys.
26
+ */
27
+ #store;
28
+ /**
29
+ * The `#activeKeyId` property is a string that points to the currently active key.
30
+ * It is used to identify the key that will be used for signing and verifying operations.
31
+ * This property is optional and can be set to a specific key ID when initializing the
32
+ * `KeyManager` instance. If not set, the key manager will use the default key id.
33
+ * @private
34
+ * @type {KeyIdentifier}
35
+ */
36
+ #activeKeyId;
37
+ /**
38
+ * Creates an instance of KeyManager.
39
+ * @param {KeyValueStore<KeyIdentifier, KeyBytes>} store An optional property to specify a custom
40
+ * `KeyValueStore` instance for key management. If not provided, {@link KeyManager} uses a default `MemoryStore`
41
+ * instance. This store is responsible for managing cryptographic keys, allowing them to be retrieved, stored, and
42
+ * managed during cryptographic operations.
43
+ */
44
+ constructor(store) {
45
+ // Set the default key store to a MemoryStore instance
46
+ this.#store = store ?? new MemoryStore();
47
+ }
48
+ /**
49
+ * Gets the ID of the active key.
50
+ * @returns {KeyIdentifier | undefined} The ID of the active key.
51
+ */
52
+ get activeKeyId() {
53
+ return this.#activeKeyId;
54
+ }
55
+ /**
56
+ * Gets the key pair associated with the given ID or the active key if no ID is provided.
57
+ * @param {KeyIdentifier} [id] The ID of the key to get.
58
+ * @returns {KeyBytes} A promise resolving to the key pair.
59
+ * @throws {KeyManagerError} If the key is not found or no active key is set.
60
+ */
61
+ #getKeyOrThrow(id) {
62
+ // Get the key id
63
+ const keyId = id ?? this.#activeKeyId;
64
+ // Throw an error if no active key is set
65
+ if (!keyId) {
66
+ throw new KeyManagerError('No active key set', 'ACTIVE_KEY_URI_NOT_SET');
67
+ }
68
+ // Get the secret key from the store, throw an error if not found
69
+ const _secretKey = this.#store.get(keyId);
70
+ if (!_secretKey) {
71
+ throw new KeyManagerError(`Key not found: ${keyId}`, 'KEY_NOT_FOUND');
72
+ }
73
+ // Create a key pair from the secret key
74
+ const kp = new SchnorrKeyPair({ secretKey: _secretKey });
75
+ // Return the secret key
76
+ return kp;
77
+ }
78
+ /**
79
+ * Checks if a key with the given ID exists in the key store.
80
+ * @param {KeyIdentifier} id The ID of the key to check.
81
+ * @returns {boolean} A promise resolving to a boolean indicating if the key exists.
82
+ */
83
+ #exists(id) {
84
+ const key = this.#store.get(id);
85
+ return !!key;
86
+ }
87
+ /**
88
+ * Removes a key from the key store.
89
+ * @param {KeyIdentifier} id The key identifier of the key to remove.
90
+ * @param {{ force?: boolean }} options The options for removing the key.
91
+ * @param {boolean} [options.force] Whether to force the removal of the key.
92
+ * @returns {void} A promise that resolves when the key is removed.
93
+ * @throws {KeyManagerError} If attempting to remove the active key without force.
94
+ */
95
+ removeKey(id, options = {}) {
96
+ // Check if trying to remove the active key without force
97
+ if (this.#activeKeyId === id && !options.force) {
98
+ throw new KeyManagerError('Cannot remove active key (use "force": true or switch active key)', 'ACTIVE_KEY_DELETE');
99
+ }
100
+ // Check if the key exists, if not throw an error
101
+ if (!this.#exists(id)) {
102
+ throw new KeyManagerError(`Key not found: ${id}`, 'KEY_NOT_FOUND');
103
+ }
104
+ // Remove the key from the store
105
+ this.#store.delete(id);
106
+ // Clear the active key if it was the one removed
107
+ if (this.#activeKeyId === id) {
108
+ this.#activeKeyId = undefined;
109
+ }
110
+ }
111
+ /**
112
+ * Lists all key identifiers in the key store.
113
+ * @returns {Promise<KeyIdentifier[]>} A promise that resolves to an array of key identifiers.
114
+ */
115
+ listKeys() {
116
+ return this.#store.entries().flatMap(([k, _]) => [k]);
117
+ }
118
+ /**
119
+ * Sets the active key to the key associated with the given ID.
120
+ * @param {KeyIdentifier} id The ID of the key to set as active.
121
+ * @returns {Promise<void>} A promise that resolves when the active key is set.
122
+ * @throws {KeyManagerError} If the key is not found.
123
+ */
124
+ setActiveKey(id) {
125
+ // Check if the key exists, if not throw an error
126
+ this.#getKeyOrThrow(id);
127
+ // Set the active key ID
128
+ this.#activeKeyId = id;
129
+ }
130
+ /**
131
+ * Gets the public key associated with the given ID or the active key if no ID is provided.
132
+ * @param {KeyIdentifier} [id] The ID of the key to get the public key for.
133
+ * @returns {Promise<KeyBytes>} A promise resolving to the public key bytes.
134
+ */
135
+ getPublicKey(id) {
136
+ // Get the key pair from the store
137
+ const { publicKey } = this.#getKeyOrThrow(id);
138
+ // Return the public key bytes
139
+ return publicKey.compressed;
140
+ }
141
+ /**
142
+ * Signs the given data using the key associated with the key ID.
143
+ * @param {Bytes} data The data to sign.
144
+ * @param {KeyIdentifier} [id] The ID of the key to sign the data with.
145
+ * @returns {Promise<SignatureBytes>} A promise resolving to the signature of the data.
146
+ */
147
+ sign(data, id) {
148
+ // Get the key from the store
149
+ const { secretKey } = this.#getKeyOrThrow(id);
150
+ // Check if the key can sign
151
+ if (!secretKey) {
152
+ throw new KeyManagerError(`Key ID ${id} is not a signer`, 'KEY_NOT_SIGNER');
153
+ }
154
+ // Sign the data using the key and return the signature
155
+ return secretKey.sign(data);
156
+ }
157
+ /**
158
+ * Verifies a signature using the key associated with the key ID.
159
+ * @param {KeyIdentifier} id The ID of the key to verify the signature with.
160
+ * @param {SignatureBytes} signature The signature to verify.
161
+ * @param {Hex} data The data to verify the signature with.
162
+ * @returns {Promise<boolean>} A promise resolving to a boolean indicating the verification result.
163
+ */
164
+ verify(signature, data, id) {
165
+ // Get the key from the store
166
+ const { publicKey } = this.#getKeyOrThrow(id);
167
+ // Verify the signature using the multikey
168
+ return publicKey.verify(signature, data);
169
+ }
170
+ /**
171
+ * Imports a key pair into the key store.
172
+ * @param {SchnorrKeyPair} keyPair The key pair to import.
173
+ * @param {{ id?: KeyIdentifier; setActive?: boolean }} options The options for importing the key pair.
174
+ * @param {KeyIdentifier} [options.id] The ID of the key to import (optional).
175
+ * @param {boolean} [options.setActive] Whether to set the key as active (optional, default: true).
176
+ * @returns {Promise<KeyIdentifier>} A promise resolving to the ID of the imported key.
177
+ */
178
+ importKey(keyPair, options = {}) {
179
+ // Ensure the key pair has a public key
180
+ if (!keyPair.publicKey) {
181
+ keyPair.publicKey = keyPair.secretKey.computePublicKey();
182
+ }
183
+ // Determine the key ID
184
+ const id = options.id ?? keyPair.publicKey.hex;
185
+ // Check if the key already exists
186
+ if (this.#exists(id)) {
187
+ throw new KeyManagerError(`Key already exists: ${id}`, 'KEY_FOUND');
188
+ }
189
+ // Store the key pair in the key store
190
+ this.#store.set(id, keyPair.secretKey.bytes);
191
+ // Determine whether to set the key as active, defaulting to true
192
+ const setActive = options.setActive ?? true;
193
+ // Set the active key if specified
194
+ if (setActive) {
195
+ this.#activeKeyId = id;
196
+ }
197
+ // Return the key ID
198
+ return id;
199
+ }
200
+ /**
201
+ * Computes the hash of the given data.
202
+ * @param {Uint8Array} data The data to hash.
203
+ * @returns {HashBytes} The hash of the data.
204
+ */
205
+ digest(data) {
206
+ return sha256(data);
207
+ }
208
+ /**
209
+ * Generates a new key pair and stores it in the key store.
210
+ * @returns {KeyIdentifier} The key identifier of the generated key.
211
+ */
212
+ generateKey() {
213
+ // Generate a new Schnorr key pair
214
+ const kp = SchnorrKeyPair.generate();
215
+ // Store the key pair in the key store
216
+ const id = kp.publicKey.hex;
217
+ this.#store.set(id, kp.secretKey.bytes);
218
+ // Set the active key to the newly generated key
219
+ this.#activeKeyId = id;
220
+ // Return the key ID
221
+ return id;
222
+ }
223
+ /**
224
+ * Initializes a singleton KeyManager instance.
225
+ * @param {SchnorrKeyPair} keyPair The secret key to import.
226
+ * @param {string} id The ID to set as the active key.
227
+ * @returns {void}
228
+ */
229
+ static initialize(keyPair, id) {
230
+ // Check if the KeyManager instance is already initialized
231
+ if (Kms.#instance) {
232
+ console.warn('WARNING: Kms global instance is already initialized.');
233
+ return Kms.#instance;
234
+ }
235
+ // Check if the keypair is provided
236
+ if (!keyPair) {
237
+ // Log a warning message if not provided
238
+ console.warn('WARNING: secretKey not provided, generating new SchnorrKeyPair ...');
239
+ }
240
+ // Generate a new keypair if not provided
241
+ keyPair ??= SchnorrKeyPair.generate();
242
+ // Initialize the singleton key manager with the keypair
243
+ Kms.#instance = new Kms();
244
+ // Import the keypair into the key store
245
+ Kms.#instance.importKey(keyPair, { setActive: true, id });
246
+ // Set the active key URI7
247
+ Kms.#instance.#activeKeyId = id;
248
+ // Log the active key ID
249
+ console.info(`Kms initialized with Active Key ID: ${Kms.#instance.#activeKeyId}`);
250
+ // Return the singleton instance
251
+ return Kms.#instance;
252
+ }
253
+ /**
254
+ * Retrieves a keypair from the key store using the provided key ID.
255
+ * @public
256
+ * @param {KeyIdentifier} id The ID of the keypair to retrieve.
257
+ * @returns {Promise<SchnorrKeyPair | undefined>} The retrieved keypair, or undefined if not found.
258
+ */
259
+ static getKey(id) {
260
+ // Ensure the Kms instance is initialized
261
+ if (!Kms.#instance) {
262
+ throw new KeyManagerError('Kms instance not initialized', 'KMS_NOT_INITIALIZED');
263
+ }
264
+ // Use the active key ID if not provided
265
+ id ??= Kms.#instance.activeKeyId;
266
+ // Instantiate a new Kms with the default key store
267
+ return Kms.#instance.#getKeyOrThrow(id);
268
+ }
269
+ }
270
+ //# sourceMappingURL=kms.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kms.js","sourceRoot":"","sources":["../../src/kms.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6C,eA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OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QAE1D,0BAA0B;QAC1B,GAAG,CAAC,SAAS,CAAC,YAAY,GAAG,EAAE,CAAC;QAEhC,wBAAwB;QACxB,OAAO,CAAC,IAAI,CAAC,uCAAuC,GAAG,CAAC,SAAS,CAAC,YAAY,EAAE,CAAC,CAAC;QAElF,gCAAgC;QAChC,OAAO,GAAG,CAAC,SAAS,CAAC;IACvB,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,EAAkB;QAC9B,yCAAyC;QACzC,IAAG,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC;YAClB,MAAM,IAAI,eAAe,CAAC,8BAA8B,EAAE,qBAAqB,CAAC,CAAC;QACnF,CAAC;QAED,wCAAwC;QACxC,EAAE,KAAK,GAAG,CAAC,SAAS,CAAC,WAAW,CAAC;QAEjC,mDAAmD;QACnD,OAAO,GAAG,CAAC,SAAS,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;IAC1C,CAAC;CACF"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Class representing a signer for cryptographic operations.
3
+ * Remains for backwards compatibility. Plan to migrate to Kms.
4
+ * @class Signer
5
+ * @type {Signer}
6
+ */
7
+ export class Signer {
8
+ /**
9
+ * The key pair used for signing.
10
+ * @type {SchnorrKeyPair}
11
+ */
12
+ keyPair;
13
+ /**
14
+ * The network associated with the signer.
15
+ * @type {keyof AvailableNetworks}
16
+ */
17
+ network;
18
+ /**
19
+ * Creates an instance of Signer.
20
+ * @param {{ keyPair: SchnorrKeyPair; network: keyof AvailableNetworks; }} params The parameters for the signer.
21
+ * @param {SchnorrKeyPair} params.keyPair The key pair used for signing.
22
+ * @param {keyof AvailableNetworks} params.network The network associated with the signer.
23
+ */
24
+ constructor(params) {
25
+ this.keyPair = params.keyPair;
26
+ this.network = params.network;
27
+ }
28
+ /**
29
+ * Gets the public key bytes.
30
+ * @returns {KeyBytes} The public key bytes.
31
+ */
32
+ get publicKey() {
33
+ return this.keyPair.publicKey.compressed;
34
+ }
35
+ /**
36
+ * Signs the given hash using ECDSA.
37
+ * @param {Bytes} hash The hash to sign.
38
+ * @returns {SignatureBytes} The signature of the hash.
39
+ */
40
+ signEcdsa(hash) {
41
+ return this.keyPair.secretKey.sign(hash, { scheme: 'ecdsa' });
42
+ }
43
+ ;
44
+ /**
45
+ * Signs the given hash using Schnorr signature.
46
+ * @param {Bytes} hash The hash to sign.
47
+ * @returns {SignatureBytes} The Schnorr signature of the hash.
48
+ */
49
+ sign(hash) {
50
+ return this.keyPair.secretKey.sign(hash);
51
+ }
52
+ }
53
+ //# sourceMappingURL=signer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signer.js","sourceRoot":"","sources":["../../src/signer.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,MAAM,OAAO,MAAM;IACjB;;;OAGG;IACH,OAAO,CAAiB;IAExB;;;OAGG;IACH,OAAO,CAA0B;IAEjC;;;;;OAKG;IACH,YAAY,MAAsE;QAChF,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC9B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IAChC,CAAC;IAED;;;OAGG;IACH,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC;IAC3C,CAAC;IAED;;;;OAIG;IACH,SAAS,CAAC,IAAW;QACnB,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;IAChE,CAAC;IAAA,CAAC;IAEF;;;;OAIG;IACH,IAAI,CAAC,IAAW;QACd,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3C,CAAC;CACF"}
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Re-implementation of a simple in-memory key-value store.
3
+ *
4
+ * The `MemoryStore` class is an implementation of
5
+ * `KeyValueStore` that holds data in memory.
6
+ *
7
+ * It provides a basic key-value store that works synchronously and keeps all
8
+ * data in memory. This can be used for testing, or for handling small amounts
9
+ * of data with simple key-value semantics.
10
+ *
11
+ * Example usage:
12
+ *
13
+ * ```ts
14
+ * const memoryStore = new MemoryStore<string, number>();
15
+ * await memoryStore.set("key1", 1);
16
+ * const value = await memoryStore.get("key1");
17
+ * console.log(value); // 1
18
+ * ```
19
+ *
20
+ * @public
21
+ */
22
+ export class MemoryStore {
23
+ /**
24
+ * A private field that contains the Map used as the key-value store.
25
+ */
26
+ store = new Map();
27
+ /**
28
+ * Clears all entries in the key-value store.
29
+ *
30
+ * @returns {void} returns once the operation is complete.
31
+ */
32
+ clear() {
33
+ this.store.clear();
34
+ }
35
+ /**
36
+ * This operation is no-op for `MemoryStore`.
37
+ */
38
+ close() {
39
+ /** no-op */
40
+ }
41
+ /**
42
+ * Deletes an entry from the key-value store by its key.
43
+ *
44
+ * @param {K} id - The key of the entry to delete.
45
+ * @returns {boolean} a boolean indicating whether the entry was successfully deleted.
46
+ */
47
+ delete(id) {
48
+ return this.store.delete(id);
49
+ }
50
+ /**
51
+ * Retrieves the value of an entry by its key.
52
+ *
53
+ * @param {K} id - The key of the entry to retrieve.
54
+ * @returns {V | undefined} the value of the entry, or `undefined` if the entry does not exist.
55
+ */
56
+ get(id) {
57
+ return this.store.get(id);
58
+ }
59
+ /**
60
+ * Checks for the presence of an entry by key.
61
+ *
62
+ * @param {K} id - The key to check for the existence of.
63
+ * @returns {boolean} a boolean indicating whether an element with the specified key exists or not.
64
+ */
65
+ has(id) {
66
+ return this.store.has(id);
67
+ }
68
+ /**
69
+ * Retrieves all values in the key-value store.
70
+
71
+ * @returns {Array<V>} an array of all values in the store.
72
+ */
73
+ list() {
74
+ return Array.from(this.store.values());
75
+ }
76
+ /**
77
+ * Retrieves all entries in the key-value store.
78
+ *
79
+ * @returns {Array<[K, V]>} an array of key-value pairs in the store.
80
+ */
81
+ entries() {
82
+ return Array.from(this.store.entries());
83
+ }
84
+ /**
85
+ * Sets the value of an entry in the key-value store.
86
+ *
87
+ * @param {K} id - The key of the entry to set.
88
+ * @param {V} key - The new value for the entry.
89
+ * @returns {void} once operation is complete.
90
+ */
91
+ set(id, key) {
92
+ this.store.set(id, key);
93
+ }
94
+ }
95
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/store.ts"],"names":[],"mappings":"AAmDA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,WAAW;IACtB;;OAEG;IACK,KAAK,GAAc,IAAI,GAAG,EAAE,CAAC;IAErC;;;;OAIG;IACH,KAAK;QACH,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,KAAK;QACH,YAAY;IACd,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,EAAK;QACV,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAC/B,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,EAAK;QACP,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC5B,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,EAAK;QACP,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI;QACF,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;IACzC,CAAC;IAED;;;;OAIG;IACH,OAAO;QACL,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;IAC1C,CAAC;IAED;;;;;;OAMG;IACH,GAAG,CAAC,EAAK,EAAE,GAAM;QACf,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;IAC1B,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ export * from './interface.js';
2
+ export * from './kms.js';
3
+ export * from './signer.js';
4
+ export * from './store.js';
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}