Skip to content

Commit

Permalink
feat(cli): add did:pkh support to the default CLI config (#1133)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceanis committed Feb 24, 2023
1 parent 9c73d98 commit 19cccc1
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"ts-node": "10.9.1",
"typeorm": "^0.3.11",
"typescript": "4.9.4",
"uint8arrays": "4.0.3",
"uint8arrays": "3.1.1",
"uuid": "^9.0.0",
"web-did-resolver": "^2.0.21"
},
Expand Down
44 changes: 24 additions & 20 deletions packages/cli/default/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,24 @@ messageHandler:
didResolver:
$require: '@veramo/did-resolver#DIDResolverPlugin'
$args:
- resolver:
$require: did-resolver#Resolver
$args:
- ethr:
$ref: /ethr-did-resolver
web:
$ref: /web-did-resolver
key:
$ref: /did-key-resolver
3:
$ref: /3id-did-resolver
elem:
$ref: /universal-resolver
io:
$ref: /universal-resolver
ion:
$ref: /universal-resolver
sov:
$ref: /universal-resolver
- ethr:
$ref: /ethr-did-resolver
web:
$ref: /web-did-resolver
key:
$ref: /did-key-resolver
pkh:
$require: '@veramo/did-provider-pkh?t=function&p=/pkh#getDidPkhResolver'
3:
$ref: /3id-did-resolver
elem:
$ref: /universal-resolver
io:
$ref: /universal-resolver
ion:
$ref: /universal-resolver
sov:
$ref: /universal-resolver

ethr-did-resolver:
$require: ethr-did-resolver?t=function&p=/ethr#getResolver
Expand All @@ -190,7 +189,7 @@ web-did-resolver:

ceramic:
$require: '@ceramicnetwork/http-client#CeramicClient'
$args:
$args:
- 'https://gateway.ceramic.network'

3id-did-resolver:
Expand Down Expand Up @@ -259,6 +258,11 @@ didManager:
$require: '@veramo/did-provider-key#KeyDIDProvider'
$args:
- defaultKms: local
did:pkh:
$require: '@veramo/did-provider-pkh#PkhDIDProvider'
$args:
- defaultKms: local
chainId: 1

didDiscovery:
$require: '@veramo/did-discovery#DIDDiscovery'
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@veramo/did-manager": "^5.0.0",
"@veramo/did-provider-ethr": "^5.0.0",
"@veramo/did-provider-key": "^5.0.0",
"@veramo/did-provider-pkh": "^5.0.0",
"@veramo/did-provider-web": "^5.0.0",
"@veramo/did-resolver": "^5.0.0",
"@veramo/key-manager": "^5.0.0",
Expand Down
27 changes: 17 additions & 10 deletions packages/cli/src/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { jest } from '@jest/globals'
import { createObjects } from '../lib/objectCreator'
import { getConfig } from '../setup'

jest.useFakeTimers()

describe('cli version', () => {
const writeMock = jest.fn()

Expand All @@ -18,21 +16,30 @@ describe('cli version', () => {
jest.clearAllMocks()
})

it('should load the dbConnection', async () => {
const { dbConnection } = await createObjects(await getConfig('./packages/cli/default/default.yml'), {
dbConnection: '/dbConnection',
})
expect(dbConnection).toBeDefined()
})

// this seems to fail because of an incompatibility between jest and the `multiformats@11` transitive dependency
it.skip('should load the agent', async () => {
const { agent } = await createObjects(await getConfig('./packages/cli/default/default.yml'), {
agent: '/agent',
})
expect(agent).toBeDefined()
})

// this seems to fail because of some timing issues or an incompatibility with the `chalk` transitive dependency
it.skip('should list version number', async () => {
expect(() => {
// veramo.parse(['--version'], { from: 'user' })
}).toThrow()
expect(writeMock).toHaveBeenCalledWith(expect.stringMatching(/^\d\.\d\.\d\n?$/))
})

it.skip('should load the dbConnection', async () => {
// this seems to fail because of some timing issues or an incompatibility with the `chalk` transitive dependency
// all other tests that need to load the dbConnection fail similarly
const res = await createObjects(getConfig('./packages/cli/default/default.yml'), {
my: '/dbConnection',
})
})

// this seems to fail because of some timing issues or an incompatibility with the `chalk` transitive dependency
it.skip('should check the default config', async () => {
expect(() => {
// veramo.parse(['config', 'check', '-f', './packages/cli/default/default.yml'], { from: 'user' })
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lib/agentCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createObjects } from './objectCreator.js'
* @see {@link https://veramo.io/docs/veramo_agent/configuration_internals | Configuration Internals} for details on
* the configuration options.
*
* @beta
* @beta - This API may change without a major version bump
*/
export async function createAgentFromConfig<T extends IPluginMethodMap>(config: object): Promise<TAgent<T>> {
// @ts-ignore
Expand Down
20 changes: 11 additions & 9 deletions packages/cli/src/lib/objectCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,32 @@ import { resolve } from 'path'
*
* The config can contain references (`$ref`) to other objects within using JSON pointers.
* Example:
* ```json5
* ```json
* {
* rpcUrl: "http://localhost:8545",
* endpoint: {
* url: {
* "rpcUrl": "http://localhost:8545",
* "endpoint": {
* "url": {
* "$ref": "/rpcUrl"
* }
* }
* }
* ```
*
* The config object can also contain references to NPM modules using the `$require` property.
* Example:
* ```json5
* ```json
* {
* agent: {
* "agent": {
* "$require": "@veramo/core#Agent",
* "$args": {
* plugins: [
* "plugins": [
* { "$require": "@veramo/did-comm#DIDComm" },
* ]
* }
* }
* }
* ```
*
* Environment variables can also be specified using the `$env` property.
*
* @see Please see {@link https://veramo.io/docs/veramo_agent/configuration_internals | Configuration Internals} for
Expand All @@ -44,9 +46,9 @@ import { resolve } from 'path'
* @param config - The configuration object
* @param pointers - A map of JSON pointers to objects within that config that you wish to create
*
* @beta
* @beta - This API may change without a major version bump
*/
export async function createObjects(config: object, pointers: Record<string, string>): Promise<any> {
export async function createObjects(config: object, pointers: Record<string, string>): Promise<Record<string, any>> {
const objects = {}

async function resolveRefs(input: any): Promise<any> {
Expand Down
28 changes: 22 additions & 6 deletions packages/cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,33 @@ import { createAgentFromConfig } from './lib/agentCreator.js'

import fs from 'fs'

export const getConfig = (fileName: string): any => {
if (!fs.existsSync(fileName)) {
console.log('Config file not found: ' + fileName)
/**
* Parses a yaml config file and returns a config object
* @param filePath
*/
export const getConfig = async (filePath: fs.PathLike): Promise<{ version?: number; [x: string]: any }> => {
let fileContent: string

// read file async
try {
fileContent = await fs.promises.readFile(filePath, 'utf8')
} catch (e) {
console.log('Config file not found: ' + filePath)
console.log('Use "veramo config create" to create one')
process.exit(1)
}

const config = yaml.parse(fs.readFileSync(fileName).toString(), { prettyErrors: true })
let config

try {
config = yaml.parse(fileContent, { prettyErrors: true })
} catch (e) {
console.error(`Unable to parse config file: ${e.message} ${e.linePos}`)
process.exit(1)
}

if (config?.version != 3) {
console.log('Unsupported configuration file version:', config.version)
console.error('Unsupported configuration file version:', config.version)
process.exit(1)
}
return config
Expand All @@ -47,7 +63,7 @@ export type ConfiguredAgent = TAgent<EnabledInterfaces>

export async function getAgent(fileName: string): Promise<ConfiguredAgent> {
try {
return await createAgentFromConfig<EnabledInterfaces>(getConfig(fileName))
return await createAgentFromConfig<EnabledInterfaces>(await getConfig(fileName))
} catch (e: any) {
console.log('Unable to create agent from ' + fileName + '.', e.message)
process.exit(1)
Expand Down
1 change: 1 addition & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
{ "path": "../did-manager" },
{ "path": "../did-provider-ethr" },
{ "path": "../did-provider-key" },
{ "path": "../did-provider-pkh" },
{ "path": "../did-provider-web" },
{ "path": "../did-resolver" },
{ "path": "../key-manager" },
Expand Down
8 changes: 6 additions & 2 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 19cccc1

Please sign in to comment.