Skip to content

Commit

Permalink
Merge 68a150d into 984b520
Browse files Browse the repository at this point in the history
  • Loading branch information
nickreynolds authored May 31, 2022
2 parents 984b520 + 68a150d commit 64f01cb
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 13 deletions.
4 changes: 2 additions & 2 deletions __tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import {
KeyStore,
migrations,
PrivateKeyStore,
ProfileDiscoveryProvider,
DataStoreDiscoveryProvider,
} from '../packages/data-store/src'
import { FakeDidProvider, FakeDidResolver } from './utils/fake-did'

Expand Down Expand Up @@ -214,7 +214,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
}),
new SelectiveDisclosure(),
new DIDDiscovery({
providers: [new AliasDiscoveryProvider(), new ProfileDiscoveryProvider()],
providers: [new AliasDiscoveryProvider(), new DataStoreDiscoveryProvider()],
}),
...(options?.plugins || []),
],
Expand Down
4 changes: 2 additions & 2 deletions __tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
KeyStore,
migrations,
PrivateKeyStore,
ProfileDiscoveryProvider,
DataStoreDiscoveryProvider,
} from '../packages/data-store/src'
import { Connection, createConnection } from 'typeorm'
import { AgentRestClient } from '../packages/remote-client/src'
Expand Down Expand Up @@ -197,7 +197,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
}),
new SelectiveDisclosure(),
new DIDDiscovery({
providers: [new AliasDiscoveryProvider(), new ProfileDiscoveryProvider()],
providers: [new AliasDiscoveryProvider(), new DataStoreDiscoveryProvider()],
}),
...(options?.plugins || []),
],
Expand Down
23 changes: 20 additions & 3 deletions __tests__/shared/didDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ export default (testContext: {
})

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[1].matches).toHaveLength(3)

expect(result.results[0].matches[0]).toEqual({
did: identifier.did,
Expand All @@ -98,14 +97,32 @@ export default (testContext: {
did: identifier.did,
metaData: { verifiableCredential },
})

expect(result.results[1].matches[2]).toEqual({
did: identifier.did,
metaData: {
alias: 'bob',
},
})

const byDIDFragmentResult = await agent.discoverDid({ query: identifier.did.substring(3, identifier.did.length - 3)})
expect(byDIDFragmentResult.results).toHaveLength(1)
expect(byDIDFragmentResult.results[0].matches).toHaveLength(2)

expect(byDIDFragmentResult.results[0].matches[1]).toEqual({
did: identifier.did,
metaData: {
alias: 'bob'
}
})
})

// THIS HAS TO BE THE LAST TEST IN THIS FILE!
it('should return errors', async () => {
const connection = getConnection('did-discovery-test')
await connection.close()
const result = await agent.discoverDid({ query: 'bob' })
expect(result?.errors?.profile).toMatch(
expect(result!.errors!['data-store-discovery']).toMatch(
/(Connection with sqlite database is not established)|(Cannot read property 'connect' of undefined)/,
)
})
Expand Down
57 changes: 52 additions & 5 deletions packages/data-store/src/did-discovery-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,78 @@ import {
IDIDDiscoveryDiscoverDidArgs,
} from '@veramo/did-discovery'

export class ProfileDiscoveryProvider implements AbstractDidDiscoveryProvider {
readonly name = 'profile'
export class DataStoreDiscoveryProvider implements AbstractDidDiscoveryProvider {
readonly name = 'data-store-discovery'

async discoverDid(
args: IDIDDiscoveryDiscoverDidArgs,
context: IAgentContext<IDataStoreORM>,
): Promise<IDIDDiscoveryProviderResult> {
const matches: IDIDDiscoverMatch[] = []

const credentials = await context.agent.dataStoreORMGetVerifiableCredentialsByClaims({
const credentialsByName = await context.agent.dataStoreORMGetVerifiableCredentialsByClaims({
where: [
{ column: 'type', value: ['name'] },
{ column: 'value', value: [`${args.query}%`], op: 'Like' },
{ column: 'value', value: [`%${args.query}%`], op: 'Like' },
{ column: 'credentialType', value: ['VerifiableCredential,Profile'] },
],
})

credentials.forEach((vc) => {
credentialsByName.forEach((vc) => {
matches.push({
did: vc.verifiableCredential.credentialSubject.id as string,
metaData: {
verifiableCredential: vc.verifiableCredential,
},
})
})

const credentialsByDID = await context.agent.dataStoreORMGetVerifiableCredentialsByClaims({
where: [
{ column: 'type', value: ['name'] },
{ column: 'subject', value: [`%${args.query}%`], op: 'Like' },
{ column: 'credentialType', value: ['VerifiableCredential,Profile'] },
],
})

credentialsByDID.forEach((vc) => {
matches.push({
did: vc.verifiableCredential.credentialSubject.id as string,
metaData: {
verifiableCredential: vc.verifiableCredential,
},
})
})

const identifiersByDID = await context.agent.dataStoreORMGetIdentifiers({
where: [
{ column: 'did', value: [`%${args.query}%`], op: 'Like'}
]
})

identifiersByDID.forEach((identifier) => {
matches.push({
did: identifier.did as string,
metaData: {
alias: identifier.alias
}
})
})

const identifiersByAlias = await context.agent.dataStoreORMGetIdentifiers({
where: [
{ column: 'alias', value: [`%${args.query}%`], op: 'Like'}
]
})

identifiersByAlias.forEach((identifier) => {
matches.push({
did: identifier.did as string,
metaData: {
alias: identifier.alias
}
})
})

return {
provider: this.name,
Expand Down
2 changes: 1 addition & 1 deletion packages/data-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export { KeyStore } from './identifier/key-store'
export { PrivateKeyStore } from './identifier/private-key-store'
export { DataStore } from './data-store'
export { DataStoreORM } from './data-store-orm'
export { ProfileDiscoveryProvider } from './did-discovery-provider'
export { DataStoreDiscoveryProvider } from './did-discovery-provider'
import { Key, KeyType } from './entities/key'
import { Identifier } from './entities/identifier'
import { Claim } from './entities/claim'
Expand Down

0 comments on commit 64f01cb

Please sign in to comment.