@did-btcr2/kms 0.2.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/interface.ts CHANGED
@@ -1,86 +1,124 @@
1
1
  import { Bytes, HashBytes, KeyBytes, SignatureBytes } from '@did-btcr2/common';
2
2
  import { SchnorrKeyPair } from '@did-btcr2/keypair';
3
3
 
4
+ /** Opaque key identifier string. */
4
5
  export type KeyIdentifier = string;
5
6
 
7
+ /** Supported signature schemes. */
8
+ export type SigningScheme = 'schnorr' | 'ecdsa';
9
+
10
+ /** Options for sign and verify operations. */
11
+ export type SignOptions = {
12
+ /** Signature scheme. Defaults to 'schnorr'. */
13
+ scheme?: SigningScheme;
14
+ };
15
+
16
+ /** Stored key entry with optional secret key and metadata tags. */
17
+ export type KeyEntry = {
18
+ /** Secret key bytes. Undefined for public-key-only (watch-only) entries. */
19
+ secretKey?: KeyBytes;
20
+ /** Compressed secp256k1 public key bytes. Always present. */
21
+ publicKey: KeyBytes;
22
+ /** Arbitrary metadata tags (e.g. derivation path, account, DID). */
23
+ tags?: Record<string, string>;
24
+ };
25
+
26
+ /** Options for importing a key. */
27
+ export type ImportKeyOptions = {
28
+ /** Custom key identifier. Auto-generated URN if omitted. */
29
+ id?: KeyIdentifier;
30
+ /** Whether to set this key as the active key. Defaults to false. */
31
+ setActive?: boolean;
32
+ /** Metadata tags to associate with the key. */
33
+ tags?: Record<string, string>;
34
+ };
35
+
36
+ /** Options for generating a key. */
37
+ export type GenerateKeyOptions = {
38
+ /** Whether to set the generated key as the active key. Defaults to false. */
39
+ setActive?: boolean;
40
+ /** Metadata tags to associate with the key. */
41
+ tags?: Record<string, string>;
42
+ };
43
+
6
44
  /**
7
- * The interface for the Kms class.
45
+ * Interface for key management operations.
8
46
  * @interface KeyManager
9
- * @type {KeyManager}
10
47
  */
11
48
  export interface KeyManager {
12
- /**
13
- * The ID of the active key.
14
- * @readonly
15
- * @type {KeyIdentifier}
16
- */
17
- readonly activeKeyId?: KeyIdentifier
49
+ /** The ID of the active key. */
50
+ readonly activeKeyId?: KeyIdentifier;
18
51
 
19
52
  /**
20
- * Set the active key id.
21
- * @param id The key id to set as active.
53
+ * Set the active key.
54
+ * @param id The key identifier to set as active.
55
+ * @throws {KeyManagerError} If the key is not found.
22
56
  */
23
57
  setActiveKey(id: KeyIdentifier): void;
24
58
 
25
59
  /**
26
- * Import a key pair.
27
- * @param {SchnorrKeyPair} keyPair The secret key to import.
28
- * @param {{ id?: KeyIdentifier, setActive?: boolean }} options The options for importing the key pair.
29
- * @param {KeyIdentifier} [options.id] The ID of the key to import (optional).
30
- * @param {boolean} [options.setActive] Whether to set the key as active (optional, default: false).
31
- * @returns {KeyIdentifier} A promise that resolves to the key identifier of the imported key.
60
+ * Import a key pair. May be public-key-only for watch-only entries.
61
+ * @param keyPair The key pair to import.
62
+ * @param options Import options.
63
+ * @returns The key identifier of the imported key.
64
+ * @throws {KeyManagerError} If the key already exists.
32
65
  */
33
- importKey(keyPair: SchnorrKeyPair, options: { id?: KeyIdentifier; setActive?: boolean }): KeyIdentifier;
66
+ importKey(keyPair: SchnorrKeyPair, options?: ImportKeyOptions): KeyIdentifier;
34
67
 
35
68
  /**
36
- * Removes a key from the key store.
37
- * @param {KeyIdentifier} id The key identifier of the key to remove.
38
- * @param {{ force?: boolean }} options The options for removing the key.
39
- * @param {boolean} [options.force] Whether to force the removal of the key.
40
- * @returns {void} A promise that resolves when the key is removed.
69
+ * Remove a key from the store.
70
+ * @param id The key identifier to remove.
71
+ * @param options Removal options.
72
+ * @throws {KeyManagerError} If removing the active key without force, or key not found.
41
73
  */
42
- removeKey(id: KeyIdentifier, options: { force?: boolean }): void;
74
+ removeKey(id: KeyIdentifier, options?: { force?: boolean }): void;
43
75
 
44
76
  /**
45
- * Lists all key identifiers in the key store.
46
- * @returns {KeyIdentifier[]} An array of key identifiers.
77
+ * List all key identifiers.
78
+ * @returns Array of key identifiers.
47
79
  */
48
80
  listKeys(): KeyIdentifier[];
49
81
 
50
82
  /**
51
- * Gets the public key associated with the ID or active key.
52
- * @param {KeyIdentifier} [id] The ID of the key to get the public key for.
53
- * @returns {KeyBytes} A promise resolving to the public key bytes.
83
+ * Get the compressed public key bytes for a key.
84
+ * @param id Key identifier. Uses active key if omitted.
85
+ * @returns Compressed secp256k1 public key bytes.
86
+ * @throws {KeyManagerError} If key not found or no active key set.
54
87
  */
55
88
  getPublicKey(id?: KeyIdentifier): KeyBytes;
56
89
 
57
90
  /**
58
- * Signs the given data using the key associated with the key ID.
59
- * @param {Bytes} data The data to sign.
60
- * @param {KeyIdentifier} [id] The ID of the key to sign the data with.
61
- * @returns {SignatureBytes} A promise resolving to the signature of the data.
91
+ * Sign data using the specified key.
92
+ * @param data The data to sign.
93
+ * @param id Key identifier. Uses active key if omitted.
94
+ * @param options Signing options (scheme defaults to 'schnorr').
95
+ * @returns The signature bytes.
96
+ * @throws {KeyManagerError} If key not found, no active key, or key cannot sign.
62
97
  */
63
- sign(data: Bytes, id?: KeyIdentifier): SignatureBytes;
98
+ sign(data: Bytes, id?: KeyIdentifier, options?: SignOptions): SignatureBytes;
64
99
 
65
100
  /**
66
- * Verifies a signature using the key associated with the key ID.
67
- * @param {KeyIdentifier} id The ID of the key to verify the signature with.
68
- * @param {SignatureBytes} signature The signature to verify.
69
- * @param {Hex} data The data to verify the signature with.
70
- * @returns {boolean} A promise resolving to a boolean indicating the verification result.
101
+ * Verify a signature using the specified key.
102
+ * @param signature The signature to verify.
103
+ * @param data The data that was signed.
104
+ * @param id Key identifier. Uses active key if omitted.
105
+ * @param options Verification options (scheme defaults to 'schnorr').
106
+ * @returns True if the signature is valid.
107
+ * @throws {KeyManagerError} If key not found or no active key set.
71
108
  */
72
- verify(signature: SignatureBytes, data: Bytes, id?: KeyIdentifier): boolean;
109
+ verify(signature: SignatureBytes, data: Bytes, id?: KeyIdentifier, options?: SignOptions): boolean;
73
110
 
74
111
  /**
75
- * Computes the hash of the given data.
76
- * @param {Uint8Array} data The data to hash.
77
- * @returns {HashBytes} The hash of the data.
112
+ * Compute a SHA-256 hash of the given data.
113
+ * @param data The data to hash.
114
+ * @returns The hash bytes.
78
115
  */
79
116
  digest(data: Uint8Array): HashBytes;
80
117
 
81
118
  /**
82
- * Generates a new key pair and stores it in the key store.
83
- * @returns {KeyIdentifier} The identifier of the newly generated key.
119
+ * Generate a new key pair and store it.
120
+ * @param options Generation options.
121
+ * @returns The key identifier of the generated key.
84
122
  */
85
- generateKey(): KeyIdentifier;
86
- }
123
+ generateKey(options?: GenerateKeyOptions): KeyIdentifier;
124
+ }