Skip to content

Commit

Permalink
feat: Add factom-did module
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Nov 22, 2021
1 parent 236ca01 commit e6e3cfb
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/factom-did-provider/api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../include/api-extractor-base.json"
}
44 changes: 44 additions & 0 deletions packages/factom-did-provider/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@sphereon/veramo-factom-did-provider",
"description": "Veramo Factom DID provider",
"version": "0.0.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc"
},
"dependencies": {
"@sphereon/did-uni-client": "^0.3.3",
"@sphereon/ssi-sdk-core": "^0.0.1",
"@veramo/core": "^3.1.0",
"@veramo/did-manager": "^3.1.0",
"debug": "^4.1.1",
"did-jwt-vc": "^2.1.7",
"events": "^3.3.0",
"micro-base58": "^0.5.1",
"z-schema": "^5.0.2"
},
"devDependencies": {
"@types/debug": "4.1.7",
"did-resolver": "3.1.0",
"typescript": "4.4.3"
},
"files": [
"dist/**/*",
"src/**/*",
"plugin.schema.json",
"README.md",
"LICENSE"
],
"publishConfig": {
"access": "public"
},
"repository": "[email protected]:Sphereon-OpenSource/ssi-sdk.git",
"author": "Sphereon <[email protected]>",
"license": "Apache-2.0",
"keywords": [
"Factom",
"DID",
"Veramo"
]
}
121 changes: 121 additions & 0 deletions packages/factom-did-provider/src/factom-did-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { AbstractIdentifierProvider } from '@veramo/did-manager'
import { IAgentContext, IIdentifier, IKey, IKeyManager, IService, ManagedKeyInfo } from '@veramo/core'
import { DIDRegistrationRequestBuilder, UniRegistrar } from '@sphereon/did-uni-client'
import Debug from 'debug'
import { hexToMultibase, multibaseToHex } from '@sphereon/ssi-sdk-core'
import { ICreateIdentifierOpts, IDidKeyOpts, IManagementKeyOpts } from './types/factom-provider-types'
import { MultibaseFormat } from '@sphereon/ssi-sdk-core/dist/utils/encoding'

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

export type IRequiredContext = IAgentContext<IKeyManager>

export class FactomDIDProvider extends AbstractIdentifierProvider {
private readonly defaultKms: string
private readonly network: string | number
private readonly registrar: UniRegistrar

constructor(opts: { defaultKms: string; network: string | number; registrarUrl?: string }) {
super()
this.defaultKms = opts.defaultKms
this.network = opts.network
this.registrar = opts.registrarUrl ? new UniRegistrar().setBaseURL(opts.registrarUrl) : new UniRegistrar()
}

async createIdentifier(
{
kms,
options,
}: {
kms?: string
options?: ICreateIdentifierOpts
},
context: IRequiredContext
): Promise<Omit<IIdentifier, 'provider'>> {
const tags = options?.tags
const nonce = options?.nonce

let keys: ManagedKeyInfo[] = []
const didKeys = options?.didKeys.map(async (didKey) => {
const factomKey = await this.createKey(didKey, context, { kms })
const { key, ...payload } = factomKey
// keys.push(key);
return {
...payload,
priorityRequirement: didKey.priorityRequirement || 0,
}
})
const managementKeys = options?.managementKeys.map(async (mgmtKey) => {
const factomKey = await this.createKey(mgmtKey, context, { kms })
const { key, ...payload } = factomKey
keys.push(key)
return { ...payload, priority: mgmtKey.priority || 0 }
})

const factomOpts = {
didVersion: 'FACTOM_V1_JSON',
managementKeys,
didKeys,
tags,
nonce,
network: this.network,
}
// todo: add services

const request = new DIDRegistrationRequestBuilder().withOptions(factomOpts).build()
const registrationResult = await this.registrar.create('factom', request)

const identifier: Omit<IIdentifier, 'provider'> = {
did: registrationResult.didState.identifier as string,
controllerKeyId: keys.reverse()[0].kid,
keys,
services: [],
}
debug('Created', identifier.did)
return identifier
}

async deleteIdentifier(args: IIdentifier, context: IAgentContext<IKeyManager>): Promise<boolean> {
return Promise.resolve(false)
}

addKey(args: { identifier: IIdentifier; key: IKey; options?: any }, context: IAgentContext<IKeyManager>): Promise<any> {
return Promise.resolve(undefined)
}

removeKey(args: { identifier: IIdentifier; kid: string; options?: any }, context: IAgentContext<IKeyManager>): Promise<any> {
return Promise.resolve(undefined)
}

addService(args: { identifier: IIdentifier; service: IService; options?: any }, context: IAgentContext<IKeyManager>): Promise<any> {
return Promise.resolve(undefined)
}

removeService(args: { identifier: IIdentifier; id: string; options?: any }, context: IAgentContext<IKeyManager>): Promise<any> {
return Promise.resolve(undefined)
}

private async createKey(
mgmtKey: IManagementKeyOpts | IDidKeyOpts,
context: IAgentContext<IKeyManager>,
{ kms }: { kms?: string; alias?: string; options?: any }
) {
const key = mgmtKey.privateKeyMultibase
? await context.agent.keyManagerImport({
kms: kms || this.defaultKms,
type: 'Ed25519',
privateKeyHex: multibaseToHex(mgmtKey.privateKeyMultibase).value,
})
: await context.agent.keyManagerCreate({
kms: kms || this.defaultKms,
type: 'Ed25519',
})
const publicKeyMultibase = hexToMultibase(key.publicKeyHex, MultibaseFormat.BASE58).value
return {
key,
type: 'Ed25519VerificationKey2020',
keyIdentifier: key.kid,
publicKeyMultibase,
}
}
}
2 changes: 2 additions & 0 deletions packages/factom-did-provider/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types/factom-provider-types'
export { FactomDIDProvider } from './factom-did-provider'
17 changes: 17 additions & 0 deletions packages/factom-did-provider/src/types/factom-provider-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface IManagementKeyOpts {
priority?: number
privateKeyMultibase?: string
}

export interface IDidKeyOpts {
priorityRequirement: number
purpose: string[]
privateKeyMultibase?: string
}

export interface ICreateIdentifierOpts {
managementKeys: IManagementKeyOpts[]
didKeys: IDidKeyOpts[]
tags: string[]
nonce: string
}
9 changes: 9 additions & 0 deletions packages/factom-did-provider/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"declarationDir": "dist"
},
"references": [{ "path": "../ssi-sdk-core" }]
}

0 comments on commit e6e3cfb

Please sign in to comment.