UNPKG

did-relayer-quick

Version:

Veramo plugin that can enable creation and control of did:quick identifiers.

94 lines 3.9 kB
import { AbstractIdentifierProvider } from '@veramo/did-manager'; import Debug from 'debug'; const debug = Debug('veramo:did-provider-quick'); /** * {@link @veramo/did-manager#DIDManager} identifier provider for `did:quick` identifiers * @public */ export class QuickDIDProvider extends AbstractIdentifierProvider { defaultKms; relayerUrl; constructor(options) { super(); this.defaultKms = options.defaultKms; this.relayerUrl = options.relayerUrl; } async createIdentifier({ kms, options }, context) { const rootIdentifier = await context.agent.didManagerCreate({ provider: 'did:ethr:ganache', kms: this.defaultKms, }); // console.log("1 CREATED ROOT IDENTIFIER: ", rootIdentifier) console.log("1 GET ROOT ID DID: ", rootIdentifier.did); const gotRoot = await context.agent.didManagerGet({ did: rootIdentifier.did }); console.log("2 GOT ROOT IDENTIFIER: ", gotRoot); const identifier = { did: 'did:quick:' + rootIdentifier.did, controllerKeyId: rootIdentifier.keys[0].kid, keys: [...(rootIdentifier.keys || [])], services: [], }; const gotRoot2 = await context.agent.didManagerGet({ did: rootIdentifier.did }); console.log("3 GOT ROOT IDENTIFIER: ", gotRoot2); return identifier; // throw new Error('QuickDIDProvider createIdentifier not implemented.') } async updateIdentifier(args, context) { throw new Error('QuickDIDProvider updateIdentifier not supported yet.'); } async deleteIdentifier(identifier, context) { for (const { kid } of identifier.keys) { // FIXME: keys might be used by multiple DIDs or even independent await context.agent.keyManagerDelete({ kid }); } return true; } async addKey({ identifier, key, options }, context) { const rootDid = identifier.did.replace('did:quick:', ''); if (!rootDid.startsWith('did:ethr:')) { throw Error('root DID not of type did:ethr'); } console.log("2 GET ROOT ID DID: ", rootDid); const rootIdentifier = await context.agent.didManagerGet({ did: rootDid }); console.log("GOT ROOT IDENTIFIER: ", rootIdentifier); const proofFormats = await context.agent.listUsableProofFormats(rootIdentifier); console.log("proofFormats: ", proofFormats); const addKeyCred = await context.agent.createVerifiableCredential({ credential: { '@context': ['https://www.w3.org/2018/credentials/v1'], type: ['VerifiableCredential', 'DIDQuickUpdate', 'DIDQuickAddKey'], issuer: rootDid, issuanceDate: new Date().toISOString(), credentialSubject: key, }, proofFormat: proofFormats[0], }); console.log("addKeyCred: ", addKeyCred); console.log("relayerUrl: ", this.relayerUrl); const res = await fetch(`${this.relayerUrl}/add-did-quick-update`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'did-quick-update', media_type: 'credential+ld+json', data: addKeyCred }) }); if (res.ok) { return true; } throw new Error(`Failed to add key: ${res.statusText}`); } async addService({ identifier, service, options, }, context) { throw new Error('Method not implemented.'); } async removeKey(args, context) { throw new Error('Method not implemented.'); } async removeService(args, context) { throw new Error('Method not implemented.'); } } //# sourceMappingURL=quick-did-provider.js.map