-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(did-discovery): implement a DID discovery plugin with simple pro…
…viders (#597) * feat(did-discovery): types and interfaces * feat: alias and profile discovery providers * docs: inline API docs * test: note about a DB breaking test * fix: license * fix: constructor takes an array of providers
- Loading branch information
1 parent
6c74eed
commit 6f01df3
Showing
22 changed files
with
788 additions
and
10 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
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
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
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,118 @@ | ||
import { IDIDDiscovery } from '../../packages/did-discovery' | ||
import { TAgent, IDIDManager, IKeyManager, IIdentifier } from '../../packages/core/src' | ||
import { IDataStoreORM } from '../../packages/data-store/src' | ||
import { ICredentialIssuer } from '../../packages/credential-w3c/src' | ||
import { getConnection } from 'typeorm' | ||
|
||
type ConfiguredAgent = TAgent<IDIDManager & IDIDDiscovery & IDataStoreORM & ICredentialIssuer> | ||
|
||
export default (testContext: { | ||
getAgent: () => ConfiguredAgent | ||
setup: () => Promise<boolean> | ||
tearDown: () => Promise<boolean> | ||
}) => { | ||
describe('DID discovery', () => { | ||
let agent: ConfiguredAgent | ||
|
||
beforeAll(async () => { | ||
await testContext.setup() | ||
agent = testContext.getAgent() | ||
return true | ||
}) | ||
afterAll(testContext.tearDown) | ||
|
||
|
||
it('should discover did by alias', async () => { | ||
const identifier = await agent.didManagerCreate({ | ||
alias: 'alice', | ||
}) | ||
|
||
const result = await agent.discoverDid({ query: 'alice'}) | ||
|
||
expect(result.results[0].matches[0]).toEqual({ | ||
did: identifier.did, | ||
metaData: { | ||
alias: 'alice' | ||
} | ||
}) | ||
}) | ||
|
||
|
||
it('should discover did by profile vc', async () => { | ||
const identifier = await agent.didManagerCreate({}) | ||
|
||
const verifiableCredential = await agent.createVerifiableCredential({ | ||
credential: { | ||
issuer: { id: identifier.did }, | ||
'@context': ['https://www.w3.org/2018/credentials/v1'], | ||
type: ['VerifiableCredential', 'Profile'], | ||
issuanceDate: new Date().toISOString(), | ||
credentialSubject: { | ||
id: identifier.did, | ||
name: 'bob', | ||
}, | ||
}, | ||
proofFormat: 'jwt', | ||
save: true | ||
}) | ||
|
||
const result = await agent.discoverDid({ query: 'bob'}) | ||
|
||
expect(result.results[0].matches[0]).toEqual({ | ||
did: identifier.did, | ||
metaData: { verifiableCredential } | ||
}) | ||
}) | ||
|
||
it('should discover did by alias and profile vc', async () => { | ||
const identifier = await agent.didManagerCreate({ | ||
alias: 'bob' | ||
}) | ||
|
||
const verifiableCredential = await agent.createVerifiableCredential({ | ||
credential: { | ||
issuer: { id: identifier.did }, | ||
'@context': ['https://www.w3.org/2018/credentials/v1'], | ||
type: ['VerifiableCredential', 'Profile'], | ||
issuanceDate: new Date().toISOString(), | ||
credentialSubject: { | ||
id: identifier.did, | ||
name: 'bobby', | ||
}, | ||
}, | ||
proofFormat: 'jwt', | ||
save: true | ||
}) | ||
|
||
const result = await agent.discoverDid({ query: 'bob'}) | ||
|
||
expect(result.results).toHaveLength(2) | ||
expect(result.results[0].matches).toHaveLength(1) | ||
expect(result.results[1].matches).toHaveLength(2) | ||
|
||
expect(result.results[0].matches[0]).toEqual({ | ||
did: identifier.did, | ||
metaData: { | ||
alias: 'bob' | ||
} | ||
}) | ||
|
||
expect(result.results[1].matches[1]).toEqual({ | ||
did: identifier.did, | ||
metaData: { verifiableCredential } | ||
}) | ||
}) | ||
|
||
// THIS HAS TO BE THE LAST TEST IN THIS FILE! | ||
it('should return errors', async () => { | ||
const connection = getConnection() | ||
await connection.query('PRAGMA foreign_keys = OFF;') | ||
await connection.query('DROP TABLE claim;') | ||
|
||
const result = await agent.discoverDid({ query: 'bob'}) | ||
expect(result.errors).toEqual({ profile: 'SQLITE_ERROR: no such table: claim' }) | ||
|
||
}) | ||
|
||
}) | ||
} |
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
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,37 @@ | ||
import { IAgentContext } from '@veramo/core' | ||
import { IDataStoreORM } from './data-store-orm' | ||
import { AbstractDidDiscoveryProvider, IDIDDiscoverMatch, IDIDDiscoveryProviderResult, IDIDDiscoveryDiscoverDidArgs } from '@veramo/did-discovery' | ||
|
||
export class ProfileDiscoveryProvider implements AbstractDidDiscoveryProvider { | ||
|
||
readonly name = 'profile' | ||
|
||
async discoverDid( | ||
args: IDIDDiscoveryDiscoverDidArgs, | ||
context: IAgentContext<IDataStoreORM>, | ||
): Promise<IDIDDiscoveryProviderResult> { | ||
const matches: IDIDDiscoverMatch[] = [] | ||
|
||
const credentials = await context.agent.dataStoreORMGetVerifiableCredentialsByClaims({ | ||
where: [ | ||
{ column: 'type', value: ['name'] }, | ||
{ column: 'value', value: [`${args.query}%`], op: 'Like' }, | ||
{ column: 'credentialType', value: ['VerifiableCredential,Profile'] } | ||
] | ||
}) | ||
|
||
credentials.forEach( vc => { | ||
matches.push({ | ||
did: vc.verifiableCredential.credentialSubject.id as string, | ||
metaData: { | ||
verifiableCredential: vc.verifiableCredential | ||
} | ||
}) | ||
}) | ||
|
||
return { | ||
provider: this.name, | ||
matches | ||
} | ||
} | ||
} |
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
Oops, something went wrong.