Skip to content

Commit

Permalink
feat: allow default registry from environment for ebsi v1 did driver
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Apr 18, 2023
1 parent 8869643 commit 217dfc0
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 61 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EBSI_DEFAULT_REGISTRY="https://api-pilot.ebsi.eu/did-registry/v4"
5 changes: 3 additions & 2 deletions packages/did-provider-lto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"build": "tsc --build",
"build:clean": "tsc --build --clean && tsc --build"
},
"dependencies": {
Expand All @@ -21,7 +21,8 @@
},
"devDependencies": {
"@types/uuid": "^8.3.3",
"did-resolver": "^4.1.0"
"did-resolver": "^4.1.0",
"typescript": "4.9.5"
},
"files": [
"dist/**/*",
Expand Down
1 change: 1 addition & 0 deletions packages/did-resolver-ebsi/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EBSI_DEFAULT_REGISTRY="https://api-pilot.ebsi.eu/did-registry/v4"
3 changes: 0 additions & 3 deletions packages/did-resolver-ebsi/api-extractor.json

This file was deleted.

10 changes: 1 addition & 9 deletions packages/did-resolver-ebsi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@
},
"dependencies": {
"cross-fetch": "^3.1.5",
"@stablelib/ed25519": "^1.0.3",
"did-resolver": "^4.1.0",
"multiformats": "^9.9.0",
"uint8arrays": "^3.1.1",
"lodash.isplainobject": "^4.0.6",
"web-encoding": "^1.1.5",
"varint": "^6.0.0",
"nist-weierstrauss": "^1.6.1",
"bigint-mod-arith": "^3.2.1",
"enhanced-resolve": "^5.12.0"
"dotenv": "^16.0.3"
},
"devDependencies": {
"@types/varint": "^6.0.1",
Expand Down
26 changes: 25 additions & 1 deletion packages/did-resolver-ebsi/src/__tests__/ebsi_resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@ import { DID_LD_JSON, getResolver } from '../index'
import * as fs from 'fs'

describe('@sphereon/ssi-sdk-ext.did-resolver-ebsi', () => {
it('should resolve a v1 did:ebsi', async () => {
it('should resolve a v1 did:ebsi against the default registry', async () => {
const resolver = new Resolver({ ...getResolver() })
const resolutionResult = await resolver.resolve('did:ebsi:z25gUx2D5Ujb6eZcmQEnertx#5jOg2ai976NEo_UKDCDHqDzO1vBx2RQJ_9ZuyZLqSCs', {
accept: DID_LD_JSON
})
expect(resolutionResult.didDocument).toEqual(JSON.parse(fs.readFileSync(`${__dirname}/fixtures/ebsiv1_did_doc.json`, { encoding: 'utf-8' })))
})
it('should resolve a v1 did:ebsi against a configured registry', async () => {
const resolver = new Resolver({ ...getResolver() })
const resolutionResult = await resolver.resolve('did:ebsi:z25gUx2D5Ujb6eZcmQEnertx#5jOg2ai976NEo_UKDCDHqDzO1vBx2RQJ_9ZuyZLqSCs', {
accept: DID_LD_JSON,
registry: 'https://api-pilot.ebsi.eu/did-registry/v4'
})
expect(resolutionResult.didDocument).toEqual(JSON.parse(fs.readFileSync(`${__dirname}/fixtures/ebsiv1_did_doc.json`, { encoding: 'utf-8' })))
})

it('should not resolve a v1 did:ebsi against a non-existing registry', async () => {
const resolver = new Resolver({ ...getResolver() })
await expect(resolver.resolve('did:ebsi:z25gUx2D5Ujb6eZcmQEnertx#5jOg2ai976NEo_UKDCDHqDzO1vBx2RQJ_9ZuyZLqSCs', {
accept: DID_LD_JSON,
registry: 'https://nope'
})).resolves.toEqual({
'didDocument': null,
'didDocumentMetadata': {},
'didResolutionMetadata': {
'contentType': 'application/did+ld+json',
'error': 'invalidDid',
'message': 'FetchError: request to https://nope/identifiers/did:ebsi:z25gUx2D5Ujb6eZcmQEnertx failed, reason: getaddrinfo ENOTFOUND nope'
}
})
})
})
7 changes: 5 additions & 2 deletions packages/did-resolver-ebsi/src/drivers/ebsi-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ const didURI = (did: string, options: DIDResolutionOptions) => {
if (registry.endsWith('/')) {
registry = registry.substring(0, registry.length - 1)
}
return `${registry}/identifiers/${did}`
if (!registry.includes('identifiers')) {
registry += '/identifiers'
}
return `${registry}/${did}`
}

const determineRegistry = (options: DIDResolutionOptions): string => {
if (options.registry && typeof options.registry === 'string') {
return options.registry
}
return 'https://api-pilot.ebsi.eu/did-registry/v4'
return process.env.EBSI_DEFAULT_REGISTRY ?? 'https://api-pilot.ebsi.eu/did-registry/v4'
}
export default { keyToDidDoc }
10 changes: 7 additions & 3 deletions packages/did-resolver-ebsi/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import * as ebsiV1 from './drivers/ebsi-v1'
import { DIDResolutionOptions, DIDResolutionResult, ParsedDID, Resolvable, ResolverRegistry } from 'did-resolver'

import { config } from 'dotenv'

config()
export const DID_LD_JSON = 'application/did+ld+json'
export const DID_JSON = 'application/did+json'
const methodToDriverMap: any = {
ebsi: ebsiV1,
ebsi: ebsiV1
}


export const getResolver = (): ResolverRegistry => {
return {
ebsi: async (did: string, parsed: ParsedDID, r: Resolvable, options: DIDResolutionOptions) => {
const response: DIDResolutionResult = {
didResolutionMetadata: {},
didDocument: null,
didDocumentMetadata: {},
didDocumentMetadata: {}
}

try {
Expand Down Expand Up @@ -46,7 +50,7 @@ export const getResolver = (): ResolverRegistry => {
response.didResolutionMetadata.message = e.toString()
}
return response
},
}
}
}

Expand Down
53 changes: 12 additions & 41 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 217dfc0

Please sign in to comment.