Skip to content

Commit

Permalink
feat(utils): move JWK converter to utils package (#1298)
Browse files Browse the repository at this point in the history
  • Loading branch information
cre8 authored Dec 8, 2023
1 parent 462735d commit fc94764
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 42 deletions.
40 changes: 13 additions & 27 deletions packages/did-provider-jwk/src/jwk-did-provider.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -44,7 +43,7 @@ export class JwkDIDProvider extends AbstractIdentifierProvider {
{
publicKeyHex: key.publicKeyHex,
} as VerificationMethod,
options?.keyUse
options?.keyUse,
)
const jwkBase64url = encodeJoseBlob(jwk as {})

Expand All @@ -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<boolean> {
async deleteIdentifier(identifier: IIdentifier, context: IContext): Promise<boolean> {
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<any> {
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<any> {
throw Error('not_supported: JwkDIDProvider addService not possible')
}

async removeKey(
args: { identifier: IIdentifier; kid: string; options?: any },
context: IContext
context: IContext,
): Promise<any> {
throw Error('not_supported: JwkDIDProvider removeKey not possible')

}

async removeService(
args: { identifier: IIdentifier; id: string; options?: any },
context: IContext
context: IContext,
): Promise<any> {
throw Error('not_supported: JwkDIDProvider removeService not possible')
}

private async importOrGenerateKey(
args: JwkDidImportOrGenerateKeyArgs,
context: IContext
): Promise<IKey> {
private async importOrGenerateKey(args: JwkDidImportOrGenerateKeyArgs, context: IContext): Promise<IKey> {
if (args.options.privateKeyHex) {
return context.agent.keyManagerImport({
kms: args.kms || this.defaultKms,
Expand Down
3 changes: 1 addition & 2 deletions packages/did-provider-jwk/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DIDResolutionResult> {
return new Promise((resolve, reject) => {
Expand Down
13 changes: 2 additions & 11 deletions packages/did-provider-jwk/src/types/jwk-provider-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { JwkDidSupportedKeyTypes, KeyUse } from '@veramo/utils'

export type JwkCreateIdentifierOptions = {
keyType?: JwkDidSupportedKeyTypes
privateKeyHex?: string
Expand All @@ -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'
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions packages/utils/src/types/utility-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,14 @@ export type OrPromise<T> = T | Promise<T>
* @beta This API may change without a BREAKING CHANGE notice.
*/
export type RecordLike<T> = Map<string, T> | Record<string, T>

export enum SupportedKeyTypes {
Secp256r1 = 'Secp256r1',
Secp256k1 = 'Secp256k1',
Ed25519 = 'Ed25519',
X25519 = 'X25519',
}

export type KeyUse = 'sig' | 'enc'

export type JwkDidSupportedKeyTypes = keyof typeof SupportedKeyTypes

0 comments on commit fc94764

Please sign in to comment.