-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add did:webvh as a DID resolver
- Loading branch information
1 parent
bea846b
commit 8bed54d
Showing
6 changed files
with
241 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/core/src/modules/dids/__tests__/webvh-did.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import type { AgentContext } from '../../../agent' | ||
import type { Wallet } from '../../../wallet' | ||
|
||
import { Subject } from 'rxjs' | ||
import { webcrypto } from 'node:crypto' | ||
|
||
import { InMemoryStorageService } from '../../../../../../tests/InMemoryStorageService' | ||
import { InMemoryWallet } from '../../../../../../tests/InMemoryWallet' | ||
import { getAgentConfig, getAgentContext } from '../../../../tests/helpers' | ||
import { EventEmitter } from '../../../agent/EventEmitter' | ||
import { InjectionSymbols } from '../../../constants' | ||
import { CacheModuleConfig } from '../../../modules/cache/CacheModuleConfig' | ||
import { InMemoryLruCache } from '../../cache/InMemoryLruCache' | ||
import { DidsModuleConfig } from '../DidsModuleConfig' | ||
import { DidDocument } from '../domain' | ||
import { DidDocumentRole } from '../domain/DidDocumentRole' | ||
import { WebVHDidResolver } from '../methods' | ||
import { DidRecord, DidRepository } from '../repository' | ||
import { DidResolverService } from '../services' | ||
|
||
// Set up crypto for noble-ed25519 | ||
;(globalThis as any).crypto = webcrypto | ||
|
||
describe('webvh dids', () => { | ||
const config = getAgentConfig('WebVH DIDs Lifecycle') | ||
|
||
let didRepository: DidRepository | ||
let didResolverService: DidResolverService | ||
let wallet: Wallet | ||
let agentContext: AgentContext | ||
let eventEmitter: EventEmitter | ||
|
||
beforeEach(async () => { | ||
wallet = new InMemoryWallet() | ||
const storageService = new InMemoryStorageService<DidRecord>() | ||
eventEmitter = new EventEmitter(config.agentDependencies, new Subject()) | ||
didRepository = new DidRepository(storageService, eventEmitter) | ||
|
||
const cacheModuleConfig = new CacheModuleConfig({ | ||
cache: new InMemoryLruCache({ limit: 1 }), | ||
}) | ||
|
||
agentContext = getAgentContext({ | ||
wallet, | ||
registerInstances: [ | ||
[DidRepository, didRepository], | ||
[InjectionSymbols.StorageService, storageService], | ||
[CacheModuleConfig, cacheModuleConfig], | ||
], | ||
}) | ||
await wallet.createAndOpen(config.walletConfig) | ||
|
||
didResolverService = new DidResolverService( | ||
config.logger, | ||
new DidsModuleConfig({ resolvers: [new WebVHDidResolver()] }), | ||
didRepository | ||
) | ||
}) | ||
|
||
afterEach(async () => { | ||
await wallet.delete() | ||
}) | ||
|
||
test('resolve a webvh did document', async () => { | ||
const did = | ||
'did:webvh:QmPEQVM1JPTyrvEgBcDXwjK4TeyLGSX1PxjgyeAisdWM1p:gist.githubusercontent.com:brianorwhatever:9c4633d18eb644f7a47f93a802691626:raw' | ||
|
||
const { didDocument: resolvedDidDocument } = await didResolverService.resolve(agentContext, did) | ||
|
||
expect(resolvedDidDocument).toBeInstanceOf(DidDocument) | ||
}) | ||
|
||
test('receive a webvh did and did document', async () => { | ||
const did = | ||
'did:webvh:QmPEQVM1JPTyrvEgBcDXwjK4TeyLGSX1PxjgyeAisdWM1p:gist.githubusercontent.com:brianorwhatever:9c4633d18eb644f7a47f93a802691626:raw' | ||
const didDocument = new DidDocument({ | ||
id: did, | ||
service: [], | ||
verificationMethod: [], | ||
}) | ||
|
||
const didDocumentRecord = new DidRecord({ | ||
did: did, | ||
role: DidDocumentRole.Received, | ||
didDocument: didDocument, | ||
}) | ||
|
||
await didRepository.save(agentContext, didDocumentRecord) | ||
|
||
const { didDocument: resolvedDidDocument } = await didResolverService.resolve(agentContext, did) | ||
expect(resolvedDidDocument).toBeInstanceOf(DidDocument) | ||
expect(resolvedDidDocument?.id).toBe(did) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ export * from './key' | |
export * from './peer' | ||
export * from './web' | ||
export * from './jwk' | ||
export * from './webvh' |
38 changes: 38 additions & 0 deletions
38
packages/core/src/modules/dids/methods/webvh/WebVHDidResolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { AgentContext } from '../../../../agent' | ||
import type { DidResolver } from '../../domain/DidResolver' | ||
import type { DidResolutionResult } from '../../types' | ||
|
||
import { resolveDID } from 'didwebvh-ts' | ||
|
||
import { JsonTransformer } from '../../../../utils/JsonTransformer' | ||
import { DidDocument } from '../../domain' | ||
|
||
export class WebVHDidResolver implements DidResolver { | ||
public readonly supportedMethods = ['webvh'] | ||
|
||
public readonly allowsCaching = true | ||
public readonly allowsLocalDidRecord = true | ||
|
||
// FIXME: Would be nice if we don't have to provide a did resolver instance | ||
// private _resolverInstance = new Resolver() | ||
// private resolver = didWeb.getResolver() | ||
|
||
public async resolve(agentContext: AgentContext, did: string): Promise<DidResolutionResult> { | ||
const result = await resolveDID(did) | ||
|
||
let didDocument = null | ||
|
||
if (result.doc) { | ||
didDocument = JsonTransformer.fromJSON(result.doc, DidDocument) | ||
} | ||
|
||
return { | ||
didDocument, | ||
didResolutionMetadata: { | ||
servedFromCache: false, | ||
servedFromDidRecord: false, | ||
}, | ||
didDocumentMetadata: result.meta, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './WebVHDidResolver' |
Oops, something went wrong.