Skip to content

Commit

Permalink
fix: add checks for unqualified schema and cred def id (openwallet-fo…
Browse files Browse the repository at this point in the history
…undation#1148)

Signed-off-by: Bryce McMath <[email protected]>
  • Loading branch information
bryce-mcmath authored May 27, 2024
1 parent 7a59478 commit 19a4257
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/oca/__tests__/__snapshots__/remote.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ exports[`RemoteOCABundleResolver should check fetch updated bundles 1`] = `
"path": "OCABundles/schema/bcgov-digital-trust/student-card/OCABundle.json",
"sha256": "e65625acad0f1baab8e05b6cd1a5619b22e1f20cb47768616680a0fb2e8dc21d",
},
"RGjWbW1eycP7FrMf4QJvX8:2:Person:1.0": {
"path": "OCABundles/schema/bcgov-digital-trust/person/OCABundle.json",
"sha256": "9187f651eab59811d8ec1c1257628c08f6daa1eb520bf379f022425f3a9701cf",
},
"RGjWbW1eycP7FrMf4QJvX8:3:CL:13:Person": {
"path": "OCABundles/schema/bcgov-digital-trust/person/OCABundle.json",
"sha256": "9187f651eab59811d8ec1c1257628c08f6daa1eb520bf379f022425f3a9701cf",
Expand Down Expand Up @@ -225,6 +229,10 @@ exports[`RemoteOCABundleResolver should update demand 1`] = `
"path": "OCABundles/schema/bcgov-digital-trust/student-card/OCABundle.json",
"sha256": "e65625acad0f1baab8e05b6cd1a5619b22e1f20cb47768616680a0fb2e8dc21d",
},
"RGjWbW1eycP7FrMf4QJvX8:2:Person:1.0": {
"path": "OCABundles/schema/bcgov-digital-trust/person/OCABundle.json",
"sha256": "9187f651eab59811d8ec1c1257628c08f6daa1eb520bf379f022425f3a9701cf",
},
"RGjWbW1eycP7FrMf4QJvX8:3:CL:13:Person": {
"path": "OCABundles/schema/bcgov-digital-trust/person/OCABundle.json",
"sha256": "9187f651eab59811d8ec1c1257628c08f6daa1eb520bf379f022425f3a9701cf",
Expand Down
4 changes: 4 additions & 0 deletions packages/oca/__tests__/fixtures/ocabundles.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"path": "OCABundles/schema/bcgov-digital-trust/person/OCABundle.json",
"sha256": "9187f651eab59811d8ec1c1257628c08f6daa1eb520bf379f022425f3a9701cf"
},
"RGjWbW1eycP7FrMf4QJvX8:2:Person:1.0": {
"path": "OCABundles/schema/bcgov-digital-trust/person/OCABundle.json",
"sha256": "9187f651eab59811d8ec1c1257628c08f6daa1eb520bf379f022425f3a9701cf"
},
"L6ASjmDDbDH7yPL1t2yFj9:2:Person:1.2": {
"path": "OCABundles/schema/bcgov-digital-trust/person-showcase/OCABundle.json",
"sha256": "84d27be5680762f5d2d59b9cc95a49874ce73dc67af6f854bc246ed7fa8e6dfb"
Expand Down
26 changes: 22 additions & 4 deletions packages/oca/__tests__/remote.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { RemoteOCABundleResolver } from '../src/legacy/resolver/remote-oca'
import fs from 'fs'
import { ocaCacheDataFileName, defaultBundleIndexFileName } from '../src/constants'
import axios from 'axios'
import MiniLogger from '../src/utils/logger'
import fs from 'fs'
import { readFile, writeFile, exists, mkdir, unlink } from 'react-native-fs'

import { ocaCacheDataFileName, defaultBundleIndexFileName } from '../src/constants'
import { RemoteOCABundleResolver } from '../src/legacy/resolver/remote-oca'

const bundleFileName = 'bundle.json'
const ocaPath = `${__dirname}/fixtures/${ocaCacheDataFileName}`
const ocaAsString = fs.readFileSync(ocaPath, 'utf8')
Expand All @@ -19,6 +19,10 @@ const keysToUpdated = [
'79e343ab4362d93974cdf78b00a916a5b3659178dfd170fcff90f1b5f10ceb72',
'29bf8f8729f235450980d0b273f9ca49b7ab73b7358467bf52b311bf40464bb0',
]
// Testing person credential as an example
const personSha = '9187f651eab59811d8ec1c1257628c08f6daa1eb520bf379f022425f3a9701cf'
const qualifiedCredDefId = 'did:indy:candy:RGjWbW1eycP7FrMf4QJvX8/anoncreds/v0/CLAIM_DEF/13/Person'
const qualifiedSchemaId = 'did:indy:candy:RGjWbW1eycP7FrMf4QJvX8/anoncreds/v0/SCHEMA/Person/1.0'

const getMock = jest.fn().mockImplementation((url) => {
const fileName = url.split('/').pop()
Expand Down Expand Up @@ -58,6 +62,8 @@ jest.mock('react-native-fs', () => ({
}
case defaultBundleIndexFileName:
return Promise.resolve(indexAsString)
case personSha:
return Promise.resolve(bundleAsString)
default:
return Promise.resolve('{}')
}
Expand Down Expand Up @@ -159,4 +165,16 @@ describe('RemoteOCABundleResolver', () => {
expect(resolver.indexFileEtag).toEqual(changedEtag)
expect(resolver['indexFile']).toMatchSnapshot()
})

it('should check unqualified cred def id as key if given qualified cred def id', async () => {
await resolver.checkForUpdates()
const bundle = await resolver.resolve({ identifiers: { credentialDefinitionId: qualifiedCredDefId } })
expect(bundle).toBeDefined()
})

it('should check unqualified schema id as key if given qualified schema id', async () => {
await resolver.checkForUpdates()
const bundle = await resolver.resolve({ identifiers: { schemaId: qualifiedSchemaId } })
expect(bundle).toBeDefined()
})
})
9 changes: 8 additions & 1 deletion packages/oca/src/legacy/resolver/remote-oca.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getUnQualifiedDidIndyDid } from '@credo-ts/anoncreds'
import axios from 'axios'
import { CachesDirectoryPath, readFile, writeFile, exists, mkdir, unlink } from 'react-native-fs'

Expand Down Expand Up @@ -504,9 +505,15 @@ export class RemoteOCABundleResolver extends DefaultOCABundleResolver {
*/
private matchBundleIndexEntry = (identifiers: Identifiers): string | undefined => {
const { schemaId, credentialDefinitionId, templateId } = identifiers
// also check unqualified schema and cred def id's if qualified versions exist
const unqualifiedSchemaId = schemaId?.startsWith('did:indy:') ? getUnQualifiedDidIndyDid(schemaId) : undefined
const unqualifiedCredDefId = credentialDefinitionId?.startsWith('did:indy:')
? getUnQualifiedDidIndyDid(credentialDefinitionId)
: undefined

// If more than one candidate identifier exists in the index file then
// order matters here.
const candidates = [schemaId, credentialDefinitionId, templateId].filter(
const candidates = [schemaId, unqualifiedSchemaId, credentialDefinitionId, unqualifiedCredDefId, templateId].filter(
(value) => value !== undefined && value !== null && value !== ''
)

Expand Down
2 changes: 1 addition & 1 deletion packages/oca/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// "paths": {} /* Specify a set of entries that re-map imports to additional lookup locations. */,
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
"types": ["jest"], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
Expand Down

0 comments on commit 19a4257

Please sign in to comment.