decentralized-identity / veramo

A JavaScript Framework for Verifiable Data
https://veramo.io
Apache License 2.0
414 stars 130 forks source link

Allow for User Defined `kid` on KeyManagerCreate #1353

Open radleylewis opened 4 months ago

radleylewis commented 4 months ago

Problem

Presently, the keyManagerCreate method automatically generates a kid. However, there are plausible use-cases whereby the user may wish to define this manually.

Solution The solution to this current limitation requires only basic changes to the code, which would not be breaking (noting that the provision of a manual kid would be optional and would be provided in the keyManagerCreateArgs (see below diff):

diff --git a/packages/key-manager/src/key-manager.ts b/packages/key-manager/src/key-manager.ts
index 21088ff3..1ba68ba6 100644
--- a/packages/key-manager/src/key-manager.ts
+++ b/packages/key-manager/src/key-manager.ts
@@ -22,7 +22,7 @@ import * as u8a from 'uint8arrays'
 import { createAnonDecrypter, createAnonEncrypter, createJWE, decryptJWE, type ECDH, type JWE } from 'did-jwt'
 import { convertEd25519PublicKeyToX25519 } from '@veramo/utils'
 import Debug from 'debug'
-import {getBytes, hexlify, toUtf8Bytes, toUtf8String, computeAddress, Transaction} from "ethers";
+import { getBytes, hexlify, toUtf8Bytes, toUtf8String, computeAddress, Transaction } from 'ethers'

 const debug = Debug('veramo:key-manager')

@@ -87,7 +87,8 @@ export class KeyManager implements IAgentPlugin {
   async keyManagerCreate(args: IKeyManagerCreateArgs): Promise<ManagedKeyInfo> {
     const kms = this.getKms(args.kms)
     const partialKey = await kms.createKey({ type: args.type, meta: args.meta })
-    const key: IKey = { ...partialKey, kms: args.kms }
+
+    const key: IKey = { ...partialKey, kms: args.kms, kid: args.kid ?? partialKey.kid }
     if (args.meta || key.meta) {
       key.meta = { ...args.meta, ...key.meta }
     }
diff --git a/packages/core-types/src/types/IKeyManager.ts b/packages/core-types/src/types/IKeyManager.ts
index 939c43c6..edb88f82 100644
--- a/packages/core-types/src/types/IKeyManager.ts
+++ b/packages/core-types/src/types/IKeyManager.ts
@@ -28,6 +28,11 @@ export type ManagedKeyInfo = Omit<IKey, 'privateKeyHex'>
  * @public
  */
 export interface IKeyManagerCreateArgs {
+  /**
+   * Kid
+   */
+  kid?: string
+
   /**
    * Key type
    *
pauldesmondparker commented 4 months ago

@radleylewis Where's the diff for the IKeyManagerCreateArgs definition? Probably need a change to packages/core-types/src/plugin.schema.json:512 as well. EDIT: Put it all in one place:

diff --git a/packages/core-types/src/plugin.schema.ts b/packages/core-types/src/plugin.schema.ts
index 738bf62a..0ebabe75 100644
--- a/packages/core-types/src/plugin.schema.ts
+++ b/packages/core-types/src/plugin.schema.ts
@@ -537,6 +537,10 @@ export const schema = {
               "type": "string",
               "description": "Key Management System"
             },
+            "kid": {
+              "type": "string",
+              "description": "Key ID"
+            },
             "meta": {
               "$ref": "#/components/schemas/KeyMetadata",
               "description": "Optional. Key meta data"
diff --git a/packages/core-types/src/types/IKeyManager.ts b/packages/core-types/src/types/IKeyManager.ts
index 939c43c6..f0e820c8 100644
--- a/packages/core-types/src/types/IKeyManager.ts
+++ b/packages/core-types/src/types/IKeyManager.ts
@@ -38,6 +38,11 @@ export interface IKeyManagerCreateArgs {
    */
   kms: string

+  /**
+   * Key ID
+   */
+  kid?: string
+
   /**
    * Optional. Key meta data
    */
diff --git a/packages/key-manager/src/key-manager.ts b/packages/key-manager/src/key-manager.ts
index 21088ff3..1ba68ba6 100644
--- a/packages/key-manager/src/key-manager.ts
+++ b/packages/key-manager/src/key-manager.ts
@@ -22,7 +22,7 @@ import * as u8a from 'uint8arrays'
 import { createAnonDecrypter, createAnonEncrypter, createJWE, decryptJWE, type ECDH, type JWE } from 'did-jwt'
 import { convertEd25519PublicKeyToX25519 } from '@veramo/utils'
 import Debug from 'debug'
-import {getBytes, hexlify, toUtf8Bytes, toUtf8String, computeAddress, Transaction} from "ethers";
+import { getBytes, hexlify, toUtf8Bytes, toUtf8String, computeAddress, Transaction } from 'ethers'

 const debug = Debug('veramo:key-manager')

@@ -87,7 +87,8 @@ export class KeyManager implements IAgentPlugin {
   async keyManagerCreate(args: IKeyManagerCreateArgs): Promise<ManagedKeyInfo> {
     const kms = this.getKms(args.kms)
     const partialKey = await kms.createKey({ type: args.type, meta: args.meta })
-    const key: IKey = { ...partialKey, kms: args.kms }
+
+    const key: IKey = { ...partialKey, kms: args.kms, kid: args.kid ?? partialKey.kid }
     if (args.meta || key.meta) {
       key.meta = { ...args.meta, ...key.meta }
     }    
stale[bot] commented 1 month ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.