Skip to content

Commit

Permalink
fix(did-provider-key): remove multicodec and multiformats in favor of…
Browse files Browse the repository at this point in the history
… local utils

partially closes #1245
  • Loading branch information
mirceanis committed Sep 27, 2023
1 parent b2e8e96 commit 52c3aef
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 35 deletions.
4 changes: 1 addition & 3 deletions packages/did-provider-key/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
"@veramo/did-manager": "workspace:^",
"@veramo/utils": "workspace:^",
"debug": "^4.3.3",
"did-resolver": "^4.1.0",
"multicodec": "^3.2.1",
"multiformats": "^12.0.1"
"did-resolver": "^4.1.0"
},
"devDependencies": {
"@types/debug": "4.1.8",
Expand Down
13 changes: 5 additions & 8 deletions packages/did-provider-key/src/key-did-provider.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { IAgentContext, IIdentifier, IKey, IKeyManager, IService, RequireOnly } from '@veramo/core-types'
import { AbstractIdentifierProvider } from '@veramo/did-manager'
import { hexToBytes } from '@veramo/utils'
import { bytesToMultibase, hexToBytes } from '@veramo/utils'
import { computePublicKey } from '@ethersproject/signing-key'
import { base58btc } from 'multiformats/bases/base58'
import Multicodec from 'multicodec'

import Debug from 'debug'

const debug = Debug('veramo:did-key:identifier-provider')

type IContext = IAgentContext<IKeyManager>
type CreateKeyDidOptions = {
keyType?: keyof typeof keyOptions
keyType?: keyof typeof keyCodecs
privateKeyHex?: string
}

const keyOptions = {
const keyCodecs = {
Ed25519: 'ed25519-pub',
X25519: 'x25519-pub',
Secp256k1: 'secp256k1-pub',
Expand All @@ -38,7 +36,7 @@ export class KeyDIDProvider extends AbstractIdentifierProvider {
{ kms, options }: { kms?: string; options?: CreateKeyDidOptions },
context: IContext,
): Promise<Omit<IIdentifier, 'provider'>> {
const keyType = (options?.keyType && keyOptions[options?.keyType] && options.keyType) || 'Ed25519'
const keyType = (options?.keyType && keyCodecs[options?.keyType] && options.keyType) || 'Ed25519'
const key = await this.importOrGenerateKey(
{
kms: kms || this.defaultKms,
Expand All @@ -51,8 +49,7 @@ export class KeyDIDProvider extends AbstractIdentifierProvider {
)

const publicKeyHex = key.type === 'Secp256k1' ? computePublicKey('0x' + key.publicKeyHex, true) : key.publicKeyHex
const multicodecEncoded = Multicodec.addPrefix(keyOptions[keyType], hexToBytes(publicKeyHex))
const methodSpecificId: string = base58btc.encode(multicodecEncoded)
const methodSpecificId: string = bytesToMultibase(hexToBytes(publicKeyHex), 'base58btc', keyCodecs[keyType])

const identifier: Omit<IIdentifier, 'provider'> = {
did: 'did:key:' + methodSpecificId,
Expand Down
27 changes: 3 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 52c3aef

Please sign in to comment.