From fc9476490d06243b9ad77592f3e1406c5b6563ce Mon Sep 17 00:00:00 2001 From: Mirko Mollik Date: Fri, 8 Dec 2023 12:38:50 +0100 Subject: [PATCH] feat(utils): move JWK converter to utils package (#1298) --- .../did-provider-jwk/src/jwk-did-provider.ts | 40 ++++++------------- packages/did-provider-jwk/src/resolver.ts | 3 +- .../src/types/jwk-provider-types.ts | 13 +----- packages/utils/src/index.ts | 1 + .../src/jwk-did-utils.ts} | 5 ++- packages/utils/src/types/utility-types.ts | 11 +++++ 6 files changed, 31 insertions(+), 42 deletions(-) rename packages/{did-provider-jwk/src/jwkDidUtils.ts => utils/src/jwk-did-utils.ts} (96%) diff --git a/packages/did-provider-jwk/src/jwk-did-provider.ts b/packages/did-provider-jwk/src/jwk-did-provider.ts index 550abfcfb..6a1941dbc 100644 --- a/packages/did-provider-jwk/src/jwk-did-provider.ts +++ b/packages/did-provider-jwk/src/jwk-did-provider.ts @@ -1,9 +1,8 @@ import { IIdentifier, IKey, IService, IAgentContext, IKeyManager } from '@veramo/core-types' import { AbstractIdentifierProvider } from '@veramo/did-manager' -import { encodeJoseBlob } from '@veramo/utils' +import { JwkDidSupportedKeyTypes, encodeJoseBlob, generateJwkFromVerificationMethod } from '@veramo/utils' import { VerificationMethod } from 'did-resolver' -import type { JwkCreateIdentifierOptions, JwkDidImportOrGenerateKeyArgs, JwkDidSupportedKeyTypes } from './types/jwk-provider-types.js' -import { generateJwkFromVerificationMethod } from './jwkDidUtils.js' +import type { JwkCreateIdentifierOptions, JwkDidImportOrGenerateKeyArgs } from './types/jwk-provider-types.js' import Debug from 'debug' const debug = Debug('veramo:did-jwk:identifier-provider') @@ -44,7 +43,7 @@ export class JwkDIDProvider extends AbstractIdentifierProvider { { publicKeyHex: key.publicKeyHex, } as VerificationMethod, - options?.keyUse + options?.keyUse, ) const jwkBase64url = encodeJoseBlob(jwk as {}) @@ -70,55 +69,42 @@ export class JwkDIDProvider extends AbstractIdentifierProvider { throw new Error('not_supported: JwkDIDProvider updateIdentifier not possible') } - async deleteIdentifier( - identifier: IIdentifier, - context: IContext - ): Promise { + async deleteIdentifier(identifier: IIdentifier, context: IContext): Promise { for (const { kid } of identifier.keys) { await context.agent.keyManagerDelete({ kid }) } return true } - async addKey({ - identifier, - key, - options, - }: { identifier: IIdentifier; key: IKey; options?: any }, - context: IContext + async addKey( + { identifier, key, options }: { identifier: IIdentifier; key: IKey; options?: any }, + context: IContext, ): Promise { throw Error('not_supported: JwkDIDProvider addKey not possible') } - async addService({ - identifier, - service, - options, - }: { identifier: IIdentifier; service: IService; options?: any }, - context: IContext + async addService( + { identifier, service, options }: { identifier: IIdentifier; service: IService; options?: any }, + context: IContext, ): Promise { throw Error('not_supported: JwkDIDProvider addService not possible') } async removeKey( args: { identifier: IIdentifier; kid: string; options?: any }, - context: IContext + context: IContext, ): Promise { throw Error('not_supported: JwkDIDProvider removeKey not possible') - } async removeService( args: { identifier: IIdentifier; id: string; options?: any }, - context: IContext + context: IContext, ): Promise { throw Error('not_supported: JwkDIDProvider removeService not possible') } - private async importOrGenerateKey( - args: JwkDidImportOrGenerateKeyArgs, - context: IContext - ): Promise { + private async importOrGenerateKey(args: JwkDidImportOrGenerateKeyArgs, context: IContext): Promise { if (args.options.privateKeyHex) { return context.agent.keyManagerImport({ kms: args.kms || this.defaultKms, diff --git a/packages/did-provider-jwk/src/resolver.ts b/packages/did-provider-jwk/src/resolver.ts index 336394995..378d72baa 100644 --- a/packages/did-provider-jwk/src/resolver.ts +++ b/packages/did-provider-jwk/src/resolver.ts @@ -7,8 +7,7 @@ import { Resolvable, JsonWebKey, } from 'did-resolver' -import { encodeBase64url, decodeBase64url } from '@veramo/utils' -import { isJWK } from './jwkDidUtils.js' +import { encodeBase64url, decodeBase64url, isJWK } from '@veramo/utils' function generateDidResolution(jwk: JsonWebKey, parsed: ParsedDID): Promise { return new Promise((resolve, reject) => { diff --git a/packages/did-provider-jwk/src/types/jwk-provider-types.ts b/packages/did-provider-jwk/src/types/jwk-provider-types.ts index be8899115..5846adba8 100644 --- a/packages/did-provider-jwk/src/types/jwk-provider-types.ts +++ b/packages/did-provider-jwk/src/types/jwk-provider-types.ts @@ -1,3 +1,5 @@ +import { JwkDidSupportedKeyTypes, KeyUse } from '@veramo/utils' + export type JwkCreateIdentifierOptions = { keyType?: JwkDidSupportedKeyTypes privateKeyHex?: string @@ -13,14 +15,3 @@ type ImportOrGenerateKeyOpts = { keyType: JwkDidSupportedKeyTypes privateKeyHex?: string } - -export enum SupportedKeyTypes { - Secp256r1 = 'Secp256r1', - Secp256k1 = 'Secp256k1', - Ed25519 = 'Ed25519', - X25519 = 'X25519', -} - -export type JwkDidSupportedKeyTypes = keyof typeof SupportedKeyTypes - -export type KeyUse = 'sig' | 'enc' diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index e1a4193ac..086c12996 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -9,3 +9,4 @@ export * from './credential-utils.js' export * from './did-utils.js' export * from './encodings.js' export * from './type-utils.js' +export * from './jwk-did-utils.js' diff --git a/packages/did-provider-jwk/src/jwkDidUtils.ts b/packages/utils/src/jwk-did-utils.ts similarity index 96% rename from packages/did-provider-jwk/src/jwkDidUtils.ts rename to packages/utils/src/jwk-did-utils.ts index e53bf0afa..f3b4b6b94 100644 --- a/packages/did-provider-jwk/src/jwkDidUtils.ts +++ b/packages/utils/src/jwk-did-utils.ts @@ -1,8 +1,9 @@ -import { JwkDidSupportedKeyTypes, KeyUse, SupportedKeyTypes } from './types/jwk-provider-types.js' +import { JwkDidSupportedKeyTypes, KeyUse, SupportedKeyTypes } from './types/utility-types.js' import type { VerificationMethod, JsonWebKey } from 'did-resolver' -import { hexToBytes, bytesToBase64url, extractPublicKeyHex } from '@veramo/utils' import { secp256k1 } from '@noble/curves/secp256k1' import { p256 } from '@noble/curves/p256' +import { bytesToBase64url, hexToBytes } from 'did-jwt' +import { extractPublicKeyHex } from './did-utils.js' export function getKeyUse(keyType: JwkDidSupportedKeyTypes, passedKeyUse?: KeyUse): KeyUse { if (passedKeyUse) { diff --git a/packages/utils/src/types/utility-types.ts b/packages/utils/src/types/utility-types.ts index 4919bedd3..0cdf7bc75 100644 --- a/packages/utils/src/types/utility-types.ts +++ b/packages/utils/src/types/utility-types.ts @@ -45,3 +45,14 @@ export type OrPromise = T | Promise * @beta This API may change without a BREAKING CHANGE notice. */ export type RecordLike = Map | Record + +export enum SupportedKeyTypes { + Secp256r1 = 'Secp256r1', + Secp256k1 = 'Secp256k1', + Ed25519 = 'Ed25519', + X25519 = 'X25519', +} + +export type KeyUse = 'sig' | 'enc' + +export type JwkDidSupportedKeyTypes = keyof typeof SupportedKeyTypes