@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.
- package/LICENSE +373 -0
- package/README.md +7 -0
- package/dist/cjs/index.js +5 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/interface.js +2 -0
- package/dist/cjs/interface.js.map +1 -0
- package/dist/cjs/kms.js +270 -0
- package/dist/cjs/kms.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/signer.js +53 -0
- package/dist/cjs/signer.js.map +1 -0
- package/dist/cjs/store.js +95 -0
- package/dist/cjs/store.js.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/interface.js +2 -0
- package/dist/esm/interface.js.map +1 -0
- package/dist/esm/kms.js +270 -0
- package/dist/esm/kms.js.map +1 -0
- package/dist/esm/signer.js +53 -0
- package/dist/esm/signer.js.map +1 -0
- package/dist/esm/store.js +95 -0
- package/dist/esm/store.js.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/interface.d.ts +79 -0
- package/dist/types/interface.d.ts.map +1 -0
- package/dist/types/kms.d.ts +106 -0
- package/dist/types/kms.d.ts.map +1 -0
- package/dist/types/signer.d.ts +48 -0
- package/dist/types/signer.d.ts.map +1 -0
- package/dist/types/store.d.ts +123 -0
- package/dist/types/store.d.ts.map +1 -0
- package/package.json +107 -0
- package/src/index.ts +4 -0
- package/src/interface.ts +84 -0
- package/src/kms.ts +324 -0
- package/src/signer.ts +60 -0
- package/src/store.ts +153 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Bytes, HashBytes, KeyBytes, KeyIdentifier, SignatureBytes } from '@did-btcr2/common';
|
|
2
|
+
import { SchnorrKeyPair } from '@did-btcr2/keypair';
|
|
3
|
+
/**
|
|
4
|
+
* The interface for the Kms class.
|
|
5
|
+
* @interface KeyManager
|
|
6
|
+
* @type {KeyManager}
|
|
7
|
+
*/
|
|
8
|
+
export interface KeyManager {
|
|
9
|
+
/**
|
|
10
|
+
* The ID of the active key.
|
|
11
|
+
* @readonly
|
|
12
|
+
* @type {KeyIdentifier}
|
|
13
|
+
*/
|
|
14
|
+
readonly activeKeyId?: KeyIdentifier;
|
|
15
|
+
/**
|
|
16
|
+
* Set the active key id.
|
|
17
|
+
* @param id The key id to set as active.
|
|
18
|
+
*/
|
|
19
|
+
setActiveKey(id: KeyIdentifier): void;
|
|
20
|
+
/**
|
|
21
|
+
* Import a key pair.
|
|
22
|
+
* @param {SchnorrKeyPair} keyPair The secret key to import.
|
|
23
|
+
* @param {{ id?: KeyIdentifier, setActive?: boolean }} options The options for importing the key pair.
|
|
24
|
+
* @param {KeyIdentifier} [options.id] The ID of the key to import (optional).
|
|
25
|
+
* @param {boolean} [options.setActive] Whether to set the key as active (optional, default: false).
|
|
26
|
+
* @returns {KeyIdentifier} A promise that resolves to the key identifier of the imported key.
|
|
27
|
+
*/
|
|
28
|
+
importKey(keyPair: SchnorrKeyPair, options: {
|
|
29
|
+
id?: KeyIdentifier;
|
|
30
|
+
setActive?: boolean;
|
|
31
|
+
}): KeyIdentifier;
|
|
32
|
+
/**
|
|
33
|
+
* Removes a key from the key store.
|
|
34
|
+
* @param {KeyIdentifier} id The key identifier of the key to remove.
|
|
35
|
+
* @param {{ force?: boolean }} options The options for removing the key.
|
|
36
|
+
* @param {boolean} [options.force] Whether to force the removal of the key.
|
|
37
|
+
* @returns {void} A promise that resolves when the key is removed.
|
|
38
|
+
*/
|
|
39
|
+
removeKey(id: KeyIdentifier, options: {
|
|
40
|
+
force?: boolean;
|
|
41
|
+
}): void;
|
|
42
|
+
/**
|
|
43
|
+
* Lists all key identifiers in the key store.
|
|
44
|
+
* @returns {KeyIdentifier[]} An array of key identifiers.
|
|
45
|
+
*/
|
|
46
|
+
listKeys(): KeyIdentifier[];
|
|
47
|
+
/**
|
|
48
|
+
* Gets the public key associated with the ID or active key.
|
|
49
|
+
* @param {KeyIdentifier} [id] The ID of the key to get the public key for.
|
|
50
|
+
* @returns {KeyBytes} A promise resolving to the public key bytes.
|
|
51
|
+
*/
|
|
52
|
+
getPublicKey(id?: KeyIdentifier): KeyBytes;
|
|
53
|
+
/**
|
|
54
|
+
* Signs the given data using the key associated with the key ID.
|
|
55
|
+
* @param {Bytes} data The data to sign.
|
|
56
|
+
* @param {KeyIdentifier} [id] The ID of the key to sign the data with.
|
|
57
|
+
* @returns {SignatureBytes} A promise resolving to the signature of the data.
|
|
58
|
+
*/
|
|
59
|
+
sign(data: Bytes, id?: KeyIdentifier): SignatureBytes;
|
|
60
|
+
/**
|
|
61
|
+
* Verifies a signature using the key associated with the key ID.
|
|
62
|
+
* @param {KeyIdentifier} id The ID of the key to verify the signature with.
|
|
63
|
+
* @param {SignatureBytes} signature The signature to verify.
|
|
64
|
+
* @param {Hex} data The data to verify the signature with.
|
|
65
|
+
* @returns {boolean} A promise resolving to a boolean indicating the verification result.
|
|
66
|
+
*/
|
|
67
|
+
verify(signature: SignatureBytes, data: Bytes, id?: KeyIdentifier): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Computes the hash of the given data.
|
|
70
|
+
* @param {Uint8Array} data The data to hash.
|
|
71
|
+
* @returns {HashBytes} The hash of the data.
|
|
72
|
+
*/
|
|
73
|
+
digest(data: Uint8Array): HashBytes;
|
|
74
|
+
/**
|
|
75
|
+
* Generates a new key pair and stores it in the key store.
|
|
76
|
+
* @returns {KeyIdentifier} The identifier of the newly generated key.
|
|
77
|
+
*/
|
|
78
|
+
generateKey(): KeyIdentifier;
|
|
79
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../src/interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAC9F,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB;;;;OAIG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,aAAa,CAAA;IAEpC;;;OAGG;IACH,YAAY,CAAC,EAAE,EAAE,aAAa,GAAG,IAAI,CAAC;IAEtC;;;;;;;OAOG;IACH,SAAS,CAAC,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE;QAAE,EAAE,CAAC,EAAE,aAAa,CAAC;QAAC,SAAS,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,aAAa,CAAC;IAExG;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,EAAE,aAAa,EAAE,OAAO,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,IAAI,CAAC;IAEjE;;;OAGG;IACH,QAAQ,IAAI,aAAa,EAAE,CAAC;IAE5B;;;;OAIG;IACH,YAAY,CAAC,EAAE,CAAC,EAAE,aAAa,GAAG,QAAQ,CAAC;IAE3C;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,EAAE,aAAa,GAAG,cAAc,CAAC;IAEtD;;;;;;OAMG;IACH,MAAM,CAAC,SAAS,EAAE,cAAc,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC;IAE5E;;;;OAIG;IACH,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,SAAS,CAAC;IAEpC;;;OAGG;IACH,WAAW,IAAI,aAAa,CAAC;CAC9B"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Bytes, HashBytes, KeyBytes, KeyIdentifier, SignatureBytes } from '@did-btcr2/common';
|
|
2
|
+
import { SchnorrKeyPair } from '@did-btcr2/keypair';
|
|
3
|
+
import { KeyManager } from './interface.js';
|
|
4
|
+
import { KeyValueStore } from './store.js';
|
|
5
|
+
/**
|
|
6
|
+
* Class for managing cryptographic keys for the BTCR2 DID method.
|
|
7
|
+
* @class Kms
|
|
8
|
+
* @type {Kms}
|
|
9
|
+
*/
|
|
10
|
+
export declare class Kms implements KeyManager {
|
|
11
|
+
#private;
|
|
12
|
+
/**
|
|
13
|
+
* Creates an instance of KeyManager.
|
|
14
|
+
* @param {KeyValueStore<KeyIdentifier, KeyBytes>} store An optional property to specify a custom
|
|
15
|
+
* `KeyValueStore` instance for key management. If not provided, {@link KeyManager} uses a default `MemoryStore`
|
|
16
|
+
* instance. This store is responsible for managing cryptographic keys, allowing them to be retrieved, stored, and
|
|
17
|
+
* managed during cryptographic operations.
|
|
18
|
+
*/
|
|
19
|
+
constructor(store?: KeyValueStore<KeyIdentifier, KeyBytes>);
|
|
20
|
+
/**
|
|
21
|
+
* Gets the ID of the active key.
|
|
22
|
+
* @returns {KeyIdentifier | undefined} The ID of the active key.
|
|
23
|
+
*/
|
|
24
|
+
get activeKeyId(): KeyIdentifier | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Removes a key from the key store.
|
|
27
|
+
* @param {KeyIdentifier} id The key identifier of the key to remove.
|
|
28
|
+
* @param {{ force?: boolean }} options The options for removing the key.
|
|
29
|
+
* @param {boolean} [options.force] Whether to force the removal of the key.
|
|
30
|
+
* @returns {void} A promise that resolves when the key is removed.
|
|
31
|
+
* @throws {KeyManagerError} If attempting to remove the active key without force.
|
|
32
|
+
*/
|
|
33
|
+
removeKey(id: KeyIdentifier, options?: {
|
|
34
|
+
force?: boolean;
|
|
35
|
+
}): void;
|
|
36
|
+
/**
|
|
37
|
+
* Lists all key identifiers in the key store.
|
|
38
|
+
* @returns {Promise<KeyIdentifier[]>} A promise that resolves to an array of key identifiers.
|
|
39
|
+
*/
|
|
40
|
+
listKeys(): KeyIdentifier[];
|
|
41
|
+
/**
|
|
42
|
+
* Sets the active key to the key associated with the given ID.
|
|
43
|
+
* @param {KeyIdentifier} id The ID of the key to set as active.
|
|
44
|
+
* @returns {Promise<void>} A promise that resolves when the active key is set.
|
|
45
|
+
* @throws {KeyManagerError} If the key is not found.
|
|
46
|
+
*/
|
|
47
|
+
setActiveKey(id: KeyIdentifier): void;
|
|
48
|
+
/**
|
|
49
|
+
* Gets the public key associated with the given ID or the active key if no ID is provided.
|
|
50
|
+
* @param {KeyIdentifier} [id] The ID of the key to get the public key for.
|
|
51
|
+
* @returns {Promise<KeyBytes>} A promise resolving to the public key bytes.
|
|
52
|
+
*/
|
|
53
|
+
getPublicKey(id?: KeyIdentifier): KeyBytes;
|
|
54
|
+
/**
|
|
55
|
+
* Signs the given data using the key associated with the key ID.
|
|
56
|
+
* @param {Bytes} data The data to sign.
|
|
57
|
+
* @param {KeyIdentifier} [id] The ID of the key to sign the data with.
|
|
58
|
+
* @returns {Promise<SignatureBytes>} A promise resolving to the signature of the data.
|
|
59
|
+
*/
|
|
60
|
+
sign(data: Bytes, id?: KeyIdentifier): SignatureBytes;
|
|
61
|
+
/**
|
|
62
|
+
* Verifies a signature using the key associated with the key ID.
|
|
63
|
+
* @param {KeyIdentifier} id The ID of the key to verify the signature with.
|
|
64
|
+
* @param {SignatureBytes} signature The signature to verify.
|
|
65
|
+
* @param {Hex} data The data to verify the signature with.
|
|
66
|
+
* @returns {Promise<boolean>} A promise resolving to a boolean indicating the verification result.
|
|
67
|
+
*/
|
|
68
|
+
verify(signature: SignatureBytes, data: Bytes, id?: KeyIdentifier): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Imports a key pair into the key store.
|
|
71
|
+
* @param {SchnorrKeyPair} keyPair The key pair to import.
|
|
72
|
+
* @param {{ id?: KeyIdentifier; setActive?: boolean }} options The options for importing the key pair.
|
|
73
|
+
* @param {KeyIdentifier} [options.id] The ID of the key to import (optional).
|
|
74
|
+
* @param {boolean} [options.setActive] Whether to set the key as active (optional, default: true).
|
|
75
|
+
* @returns {Promise<KeyIdentifier>} A promise resolving to the ID of the imported key.
|
|
76
|
+
*/
|
|
77
|
+
importKey(keyPair: SchnorrKeyPair, options?: {
|
|
78
|
+
id?: KeyIdentifier;
|
|
79
|
+
setActive?: boolean;
|
|
80
|
+
}): KeyIdentifier;
|
|
81
|
+
/**
|
|
82
|
+
* Computes the hash of the given data.
|
|
83
|
+
* @param {Uint8Array} data The data to hash.
|
|
84
|
+
* @returns {HashBytes} The hash of the data.
|
|
85
|
+
*/
|
|
86
|
+
digest(data: Uint8Array): HashBytes;
|
|
87
|
+
/**
|
|
88
|
+
* Generates a new key pair and stores it in the key store.
|
|
89
|
+
* @returns {KeyIdentifier} The key identifier of the generated key.
|
|
90
|
+
*/
|
|
91
|
+
generateKey(): KeyIdentifier;
|
|
92
|
+
/**
|
|
93
|
+
* Initializes a singleton KeyManager instance.
|
|
94
|
+
* @param {SchnorrKeyPair} keyPair The secret key to import.
|
|
95
|
+
* @param {string} id The ID to set as the active key.
|
|
96
|
+
* @returns {void}
|
|
97
|
+
*/
|
|
98
|
+
static initialize(keyPair: SchnorrKeyPair, id: string): Kms;
|
|
99
|
+
/**
|
|
100
|
+
* Retrieves a keypair from the key store using the provided key ID.
|
|
101
|
+
* @public
|
|
102
|
+
* @param {KeyIdentifier} id The ID of the keypair to retrieve.
|
|
103
|
+
* @returns {Promise<SchnorrKeyPair | undefined>} The retrieved keypair, or undefined if not found.
|
|
104
|
+
*/
|
|
105
|
+
static getKey(id?: KeyIdentifier): SchnorrKeyPair | undefined;
|
|
106
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kms.d.ts","sourceRoot":"","sources":["../../src/kms.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,aAAa,EAAmB,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAC/G,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAe,MAAM,YAAY,CAAC;AAExD;;;;GAIG;AACH,qBAAa,GAAI,YAAW,UAAU;;IA+BpC;;;;;;OAMG;gBACS,KAAK,CAAC,EAAE,aAAa,CAAC,aAAa,EAAE,QAAQ,CAAC;IAK1D;;;OAGG;IACH,IAAI,WAAW,IAAI,aAAa,GAAG,SAAS,CAE3C;IAuCD;;;;;;;OAOG;IACH,SAAS,CAAC,EAAE,EAAE,aAAa,EAAE,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,IAAI;IAoBrE;;;OAGG;IACH,QAAQ,IAAI,aAAa,EAAE;IAI3B;;;;;OAKG;IACH,YAAY,CAAC,EAAE,EAAE,aAAa,GAAG,IAAI;IAQrC;;;;OAIG;IACH,YAAY,CAAC,EAAE,CAAC,EAAE,aAAa,GAAG,QAAQ;IAQ1C;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,EAAE,aAAa,GAAG,cAAc;IAarD;;;;;;OAMG;IACH,MAAM,CAAC,SAAS,EAAE,cAAc,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,EAAE,aAAa,GAAG,OAAO;IAQ3E;;;;;;;OAOG;IACH,SAAS,CACP,OAAO,EAAE,cAAc,EACvB,OAAO,GAAE;QACP,EAAE,CAAC,EAAE,aAAa,CAAC;QACnB,SAAS,CAAC,EAAE,OAAO,CAAA;KACf,GACL,aAAa;IA6BhB;;;;OAIG;IACH,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,SAAS;IAInC;;;OAGG;IACH,WAAW,IAAI,aAAa;IAe5B;;;;;OAKG;IACH,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,cAAc,EAAE,EAAE,EAAE,MAAM,GAAG,GAAG;IAgC3D;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,aAAa,GAAG,cAAc,GAAG,SAAS;CAY9D"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AvailableNetworks } from '@did-btcr2/bitcoin';
|
|
2
|
+
import { KeyBytes, Bytes, SignatureBytes } from '@did-btcr2/common';
|
|
3
|
+
import { SchnorrKeyPair } from '@did-btcr2/keypair';
|
|
4
|
+
/**
|
|
5
|
+
* Class representing a signer for cryptographic operations.
|
|
6
|
+
* Remains for backwards compatibility. Plan to migrate to Kms.
|
|
7
|
+
* @class Signer
|
|
8
|
+
* @type {Signer}
|
|
9
|
+
*/
|
|
10
|
+
export declare class Signer {
|
|
11
|
+
/**
|
|
12
|
+
* The key pair used for signing.
|
|
13
|
+
* @type {SchnorrKeyPair}
|
|
14
|
+
*/
|
|
15
|
+
keyPair: SchnorrKeyPair;
|
|
16
|
+
/**
|
|
17
|
+
* The network associated with the signer.
|
|
18
|
+
* @type {keyof AvailableNetworks}
|
|
19
|
+
*/
|
|
20
|
+
network: keyof AvailableNetworks;
|
|
21
|
+
/**
|
|
22
|
+
* Creates an instance of Signer.
|
|
23
|
+
* @param {{ keyPair: SchnorrKeyPair; network: keyof AvailableNetworks; }} params The parameters for the signer.
|
|
24
|
+
* @param {SchnorrKeyPair} params.keyPair The key pair used for signing.
|
|
25
|
+
* @param {keyof AvailableNetworks} params.network The network associated with the signer.
|
|
26
|
+
*/
|
|
27
|
+
constructor(params: {
|
|
28
|
+
keyPair: SchnorrKeyPair;
|
|
29
|
+
network: keyof AvailableNetworks;
|
|
30
|
+
});
|
|
31
|
+
/**
|
|
32
|
+
* Gets the public key bytes.
|
|
33
|
+
* @returns {KeyBytes} The public key bytes.
|
|
34
|
+
*/
|
|
35
|
+
get publicKey(): KeyBytes;
|
|
36
|
+
/**
|
|
37
|
+
* Signs the given hash using ECDSA.
|
|
38
|
+
* @param {Bytes} hash The hash to sign.
|
|
39
|
+
* @returns {SignatureBytes} The signature of the hash.
|
|
40
|
+
*/
|
|
41
|
+
signEcdsa(hash: Bytes): SignatureBytes;
|
|
42
|
+
/**
|
|
43
|
+
* Signs the given hash using Schnorr signature.
|
|
44
|
+
* @param {Bytes} hash The hash to sign.
|
|
45
|
+
* @returns {SignatureBytes} The Schnorr signature of the hash.
|
|
46
|
+
*/
|
|
47
|
+
sign(hash: Bytes): SignatureBytes;
|
|
48
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAAG,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAErD;;;;;GAKG;AACH,qBAAa,MAAM;IACjB;;;OAGG;IACH,OAAO,EAAE,cAAc,CAAC;IAExB;;;OAGG;IACH,OAAO,EAAE,MAAM,iBAAiB,CAAC;IAEjC;;;;;OAKG;gBACS,MAAM,EAAE;QAAE,OAAO,EAAE,cAAc,CAAC;QAAC,OAAO,EAAE,MAAM,iBAAiB,CAAC;KAAE;IAKlF;;;OAGG;IACH,IAAI,SAAS,IAAI,QAAQ,CAExB;IAED;;;;OAIG;IACH,SAAS,CAAC,IAAI,EAAE,KAAK,GAAG,cAAc;IAItC;;;;OAIG;IACH,IAAI,CAAC,IAAI,EAAE,KAAK,GAAG,cAAc;CAGlC"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Re-implmentation of a interface for a generic key-value store.
|
|
3
|
+
*/
|
|
4
|
+
export interface KeyValueStore<K, V> {
|
|
5
|
+
/**
|
|
6
|
+
* Clears the store, removing all key-value pairs.
|
|
7
|
+
*
|
|
8
|
+
* @returns {void} when the store has been cleared.
|
|
9
|
+
*/
|
|
10
|
+
clear(): void;
|
|
11
|
+
/**
|
|
12
|
+
* Closes the store, freeing up any resources used. After calling this method, no other operations can be performed on the store.
|
|
13
|
+
*
|
|
14
|
+
* @returns {void} when the store has been closed.
|
|
15
|
+
*/
|
|
16
|
+
close(): void;
|
|
17
|
+
/**
|
|
18
|
+
* Deletes a key-value pair from the store.
|
|
19
|
+
*
|
|
20
|
+
* @param {K} key - The key of the value to delete.
|
|
21
|
+
* @returns {boolean | void} True if the element existed and has been removed, or false if the element does not exist.
|
|
22
|
+
*/
|
|
23
|
+
delete(key: K): boolean | void;
|
|
24
|
+
/**
|
|
25
|
+
* Fetches a value from the store given its key.
|
|
26
|
+
*
|
|
27
|
+
* @param {K} key - The key of the value to retrieve.
|
|
28
|
+
* @returns {V | undefined} The value associated with the key, or `undefined` if no value exists for that key.
|
|
29
|
+
*/
|
|
30
|
+
get(key: K): V | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Sets the value for a key in the store.
|
|
33
|
+
*
|
|
34
|
+
* @param {K} key - The key under which to store the value.
|
|
35
|
+
* @param {V} value - The value to be stored.
|
|
36
|
+
* @returns {void} once the value has been set.
|
|
37
|
+
*/
|
|
38
|
+
set(key: K, value: V): void;
|
|
39
|
+
/**
|
|
40
|
+
* Fetches the keys and values as a nested array.
|
|
41
|
+
*
|
|
42
|
+
* @returns {Array<[K, V]>} An array of key-value pair arrays in the store.
|
|
43
|
+
*/
|
|
44
|
+
entries(): Array<[K, V]>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Re-implementation of a simple in-memory key-value store.
|
|
48
|
+
*
|
|
49
|
+
* The `MemoryStore` class is an implementation of
|
|
50
|
+
* `KeyValueStore` that holds data in memory.
|
|
51
|
+
*
|
|
52
|
+
* It provides a basic key-value store that works synchronously and keeps all
|
|
53
|
+
* data in memory. This can be used for testing, or for handling small amounts
|
|
54
|
+
* of data with simple key-value semantics.
|
|
55
|
+
*
|
|
56
|
+
* Example usage:
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* const memoryStore = new MemoryStore<string, number>();
|
|
60
|
+
* await memoryStore.set("key1", 1);
|
|
61
|
+
* const value = await memoryStore.get("key1");
|
|
62
|
+
* console.log(value); // 1
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
67
|
+
export declare class MemoryStore<K, V> implements KeyValueStore<K, V> {
|
|
68
|
+
/**
|
|
69
|
+
* A private field that contains the Map used as the key-value store.
|
|
70
|
+
*/
|
|
71
|
+
private store;
|
|
72
|
+
/**
|
|
73
|
+
* Clears all entries in the key-value store.
|
|
74
|
+
*
|
|
75
|
+
* @returns {void} returns once the operation is complete.
|
|
76
|
+
*/
|
|
77
|
+
clear(): void;
|
|
78
|
+
/**
|
|
79
|
+
* This operation is no-op for `MemoryStore`.
|
|
80
|
+
*/
|
|
81
|
+
close(): void;
|
|
82
|
+
/**
|
|
83
|
+
* Deletes an entry from the key-value store by its key.
|
|
84
|
+
*
|
|
85
|
+
* @param {K} id - The key of the entry to delete.
|
|
86
|
+
* @returns {boolean} a boolean indicating whether the entry was successfully deleted.
|
|
87
|
+
*/
|
|
88
|
+
delete(id: K): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Retrieves the value of an entry by its key.
|
|
91
|
+
*
|
|
92
|
+
* @param {K} id - The key of the entry to retrieve.
|
|
93
|
+
* @returns {V | undefined} the value of the entry, or `undefined` if the entry does not exist.
|
|
94
|
+
*/
|
|
95
|
+
get(id: K): V | undefined;
|
|
96
|
+
/**
|
|
97
|
+
* Checks for the presence of an entry by key.
|
|
98
|
+
*
|
|
99
|
+
* @param {K} id - The key to check for the existence of.
|
|
100
|
+
* @returns {boolean} a boolean indicating whether an element with the specified key exists or not.
|
|
101
|
+
*/
|
|
102
|
+
has(id: K): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Retrieves all values in the key-value store.
|
|
105
|
+
|
|
106
|
+
* @returns {Array<V>} an array of all values in the store.
|
|
107
|
+
*/
|
|
108
|
+
list(): Array<V>;
|
|
109
|
+
/**
|
|
110
|
+
* Retrieves all entries in the key-value store.
|
|
111
|
+
*
|
|
112
|
+
* @returns {Array<[K, V]>} an array of key-value pairs in the store.
|
|
113
|
+
*/
|
|
114
|
+
entries(): Array<[K, V]>;
|
|
115
|
+
/**
|
|
116
|
+
* Sets the value of an entry in the key-value store.
|
|
117
|
+
*
|
|
118
|
+
* @param {K} id - The key of the entry to set.
|
|
119
|
+
* @param {V} key - The new value for the entry.
|
|
120
|
+
* @returns {void} once operation is complete.
|
|
121
|
+
*/
|
|
122
|
+
set(id: K, key: V): void;
|
|
123
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/store.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,EAAE,CAAC;IACjC;;;;OAIG;IACH,KAAK,IAAI,IAAI,CAAC;IAEd;;;;OAIG;IACH,KAAK,IAAI,IAAI,CAAC;IAEd;;;;;OAKG;IACH,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,GAAG,IAAI,CAAC;IAE/B;;;;;OAKG;IACH,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;IAE3B;;;;;;OAMG;IACH,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAE5B;;;;OAIG;IACH,OAAO,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,WAAW,CAAC,CAAC,EAAE,CAAC,CAAE,YAAW,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC;IAC3D;;OAEG;IACH,OAAO,CAAC,KAAK,CAAwB;IAErC;;;;OAIG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;;;;OAKG;IACH,MAAM,CAAC,EAAE,EAAE,CAAC,GAAG,OAAO;IAItB;;;;;OAKG;IACH,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS;IAIzB;;;;;OAKG;IACH,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,OAAO;IAInB;;;;OAIG;IACH,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC;IAIhB;;;;OAIG;IACH,OAAO,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAIxB;;;;;;OAMG;IACH,GAAG,CAAC,EAAE,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,IAAI;CAGzB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@did-btcr2/kms",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Key manager that can be used with the did:btcr2 DID method",
|
|
6
|
+
"main": "./dist/cjs/index.js",
|
|
7
|
+
"module": "./dist/esm/index.js",
|
|
8
|
+
"types": "./dist/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"import": "./dist/esm/index.js",
|
|
13
|
+
"require": "./dist/cjs/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src"
|
|
19
|
+
],
|
|
20
|
+
"license": "MPL-2.0",
|
|
21
|
+
"contributors": [
|
|
22
|
+
{
|
|
23
|
+
"name": "dcdpr",
|
|
24
|
+
"url": "https://github.com/dcdpr"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"name": "jintekc",
|
|
28
|
+
"url": "https://github.com/jintekc",
|
|
29
|
+
"email": "github@jintek.consulting"
|
|
30
|
+
}
|
|
31
|
+
],
|
|
32
|
+
"homepage": "https://github.com/dcdpr/did-btcr2-js/tree/main/packages/kms",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+ssh://git@github.com:dcdpr/did-btcr2-js.git",
|
|
36
|
+
"directory": "packages/kms"
|
|
37
|
+
},
|
|
38
|
+
"bugs": "https://github.com/dcdpr/did-btcr2-js/issues",
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22.0.0"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"did",
|
|
47
|
+
"dids",
|
|
48
|
+
"decentralized identity",
|
|
49
|
+
"decentralized identifiers",
|
|
50
|
+
"did method",
|
|
51
|
+
"did:btcr2",
|
|
52
|
+
"btcr2",
|
|
53
|
+
"kms",
|
|
54
|
+
"key management system",
|
|
55
|
+
"key manager"
|
|
56
|
+
],
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"@noble/hashes": "^2.0.1",
|
|
59
|
+
"@did-btcr2/common": "2.2.1",
|
|
60
|
+
"@did-btcr2/bitcoin": "0.3.1",
|
|
61
|
+
"@did-btcr2/keypair": "0.7.1",
|
|
62
|
+
"@did-btcr2/cryptosuite": "3.2.1"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@eslint/js": "^9.22.0",
|
|
66
|
+
"@types/chai": "^5.0.1",
|
|
67
|
+
"@types/chai-as-promised": "^8.0.1",
|
|
68
|
+
"@types/eslint": "^9.6.1",
|
|
69
|
+
"@types/mocha": "^10.0.9",
|
|
70
|
+
"@types/node": "^22.5.4",
|
|
71
|
+
"@typescript-eslint/eslint-plugin": "^8.5.0",
|
|
72
|
+
"@typescript-eslint/parser": "^8.5.0",
|
|
73
|
+
"c8": "^10.1.2",
|
|
74
|
+
"chai": "^5.1.2",
|
|
75
|
+
"chai-as-promised": "^8.0.0",
|
|
76
|
+
"esbuild": "^0.24.2",
|
|
77
|
+
"eslint": "^9.14.0",
|
|
78
|
+
"eslint-plugin-mocha": "^10.5.0",
|
|
79
|
+
"globals": "^15.11.0",
|
|
80
|
+
"mocha": "^10.8.2",
|
|
81
|
+
"mocha-junit-reporter": "^2.2.1",
|
|
82
|
+
"node-stdlib-browser": "^1.3.1",
|
|
83
|
+
"rimraf": "^6.0.1",
|
|
84
|
+
"typedoc-plugin-markdown": "^4.7.0",
|
|
85
|
+
"typescript": "^5.6.2",
|
|
86
|
+
"typescript-eslint": "^8.19.1"
|
|
87
|
+
},
|
|
88
|
+
"scripts": {
|
|
89
|
+
"clean:build": "rimraf dist",
|
|
90
|
+
"clean:tests": "rimraf coverage tests/compiled",
|
|
91
|
+
"clean:deps": "rimraf node_modules pnpm-lock.json",
|
|
92
|
+
"clean": "pnpm clean:build && pnpm clean:tests",
|
|
93
|
+
"wipe": "pnpm clean && pnpm clean:deps",
|
|
94
|
+
"reinstall": "pnpm install --force",
|
|
95
|
+
"build": "pnpm clean:build && pnpm build:esm && pnpm build:cjs",
|
|
96
|
+
"build:esm": "rimraf dist/esm dist/types && pnpm tsc -p tsconfig.json",
|
|
97
|
+
"build:cjs": "rimraf dist/cjs && tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
|
|
98
|
+
"build:tests": "pnpm clean:tests && pnpm tsc -p tests/tsconfig.json",
|
|
99
|
+
"build:all": "pnpm build && pnpm build:tests",
|
|
100
|
+
"lint": "eslint . --max-warnings 0",
|
|
101
|
+
"lint:fix": "eslint . --fix",
|
|
102
|
+
"test": "pnpm c8 mocha",
|
|
103
|
+
"build:test": "pnpm build && pnpm build:tests && pnpm c8 mocha",
|
|
104
|
+
"build:lint": "pnpm build && pnpm build:tests && pnpm lint:fix",
|
|
105
|
+
"prepublish": "pnpm build"
|
|
106
|
+
}
|
|
107
|
+
}
|
package/src/index.ts
ADDED
package/src/interface.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Bytes, HashBytes, KeyBytes, KeyIdentifier, SignatureBytes } from '@did-btcr2/common';
|
|
2
|
+
import { SchnorrKeyPair } from '@did-btcr2/keypair';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The interface for the Kms class.
|
|
6
|
+
* @interface KeyManager
|
|
7
|
+
* @type {KeyManager}
|
|
8
|
+
*/
|
|
9
|
+
export interface KeyManager {
|
|
10
|
+
/**
|
|
11
|
+
* The ID of the active key.
|
|
12
|
+
* @readonly
|
|
13
|
+
* @type {KeyIdentifier}
|
|
14
|
+
*/
|
|
15
|
+
readonly activeKeyId?: KeyIdentifier
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Set the active key id.
|
|
19
|
+
* @param id The key id to set as active.
|
|
20
|
+
*/
|
|
21
|
+
setActiveKey(id: KeyIdentifier): void;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Import a key pair.
|
|
25
|
+
* @param {SchnorrKeyPair} keyPair The secret key to import.
|
|
26
|
+
* @param {{ id?: KeyIdentifier, setActive?: boolean }} options The options for importing the key pair.
|
|
27
|
+
* @param {KeyIdentifier} [options.id] The ID of the key to import (optional).
|
|
28
|
+
* @param {boolean} [options.setActive] Whether to set the key as active (optional, default: false).
|
|
29
|
+
* @returns {KeyIdentifier} A promise that resolves to the key identifier of the imported key.
|
|
30
|
+
*/
|
|
31
|
+
importKey(keyPair: SchnorrKeyPair, options: { id?: KeyIdentifier; setActive?: boolean }): KeyIdentifier;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Removes a key from the key store.
|
|
35
|
+
* @param {KeyIdentifier} id The key identifier of the key to remove.
|
|
36
|
+
* @param {{ force?: boolean }} options The options for removing the key.
|
|
37
|
+
* @param {boolean} [options.force] Whether to force the removal of the key.
|
|
38
|
+
* @returns {void} A promise that resolves when the key is removed.
|
|
39
|
+
*/
|
|
40
|
+
removeKey(id: KeyIdentifier, options: { force?: boolean }): void;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Lists all key identifiers in the key store.
|
|
44
|
+
* @returns {KeyIdentifier[]} An array of key identifiers.
|
|
45
|
+
*/
|
|
46
|
+
listKeys(): KeyIdentifier[];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Gets the public key associated with the ID or active key.
|
|
50
|
+
* @param {KeyIdentifier} [id] The ID of the key to get the public key for.
|
|
51
|
+
* @returns {KeyBytes} A promise resolving to the public key bytes.
|
|
52
|
+
*/
|
|
53
|
+
getPublicKey(id?: KeyIdentifier): KeyBytes;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Signs the given data using the key associated with the key ID.
|
|
57
|
+
* @param {Bytes} data The data to sign.
|
|
58
|
+
* @param {KeyIdentifier} [id] The ID of the key to sign the data with.
|
|
59
|
+
* @returns {SignatureBytes} A promise resolving to the signature of the data.
|
|
60
|
+
*/
|
|
61
|
+
sign(data: Bytes, id?: KeyIdentifier): SignatureBytes;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Verifies a signature using the key associated with the key ID.
|
|
65
|
+
* @param {KeyIdentifier} id The ID of the key to verify the signature with.
|
|
66
|
+
* @param {SignatureBytes} signature The signature to verify.
|
|
67
|
+
* @param {Hex} data The data to verify the signature with.
|
|
68
|
+
* @returns {boolean} A promise resolving to a boolean indicating the verification result.
|
|
69
|
+
*/
|
|
70
|
+
verify(signature: SignatureBytes, data: Bytes, id?: KeyIdentifier): boolean;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Computes the hash of the given data.
|
|
74
|
+
* @param {Uint8Array} data The data to hash.
|
|
75
|
+
* @returns {HashBytes} The hash of the data.
|
|
76
|
+
*/
|
|
77
|
+
digest(data: Uint8Array): HashBytes;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Generates a new key pair and stores it in the key store.
|
|
81
|
+
* @returns {KeyIdentifier} The identifier of the newly generated key.
|
|
82
|
+
*/
|
|
83
|
+
generateKey(): KeyIdentifier;
|
|
84
|
+
}
|