veramo icon indicating copy to clipboard operation
veramo copied to clipboard

[proposal] Add temp memory key generation function

Open nklomp opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. For the DID ION keys in pr https://github.com/uport-project/veramo/pull/987 I needed to pre-create keys for rotation and recovery. I also needed to be able to assign specific key ids (kid). As I didn't want to duplicate all the logic to retrieve the public keys from different key types, I ended up basically using an in memory KMS for temporary keys (see below). It might be helpful to others to have this exposed as a function

Describe the solution you'd like expose a function that allows IKey generation completely in memory

Additional context Right now the DID Ion Provider is dependant on kms-local. If Veramo would provide this function that coupling could be removed.

The below line is the import one.

  const tmpKey = (await new KeyManagementSystem(new MemoryPrivateKeyStore()).importKey({
    type,
    privateKeyHex,
    kid,
  })) as IKey

The full reference how I used it in the ION provider

/**
 * Create a Veramo Key entirely in Memory. It is not stored
 *
 * Didn't want to recreate the logic to extract the pub key for the different key types
 * So let's create a temp in-mem kms to do it for us
 *
 * @param type
 * @param privateKeyHex
 * @param kid
 * @param kms
 * @param ionMeta
 */
export const tempMemoryKey = async (
  type: KeyType.Ed25519 | KeyType.Secp256k1 | KeyType,
  privateKeyHex: string,
  kid: string,
  kms: string,
  ionMeta: IonKeyMetadata
): Promise<IKey> => {
  const tmpKey = (await new KeyManagementSystem(new MemoryPrivateKeyStore()).importKey({
    type,
    privateKeyHex,
    kid,
  })) as IKey
  tmpKey.meta!.ion = JSON.parse(JSON.stringify(ionMeta))
  tmpKey.meta!.ion.commitment = computeCommitmentFromJwk(toIonPublicKeyJwk(tmpKey.publicKeyHex))
  tmpKey.kms = kms
  // tmpKey.privateKeyHex = privateKeyHex
  return tmpKey
}

nklomp avatar Oct 04 '22 13:10 nklomp

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.

stale[bot] avatar Jun 18 '23 07:06 stale[bot]