diff --git a/package.json b/package.json index f8f792a2..19f5db11 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "@ethersproject/contracts": "^5.6.2", "@ethersproject/providers": "^5.6.8", "@ethersproject/transactions": "^5.6.2", - "did-resolver": "^3.2.2", + "did-resolver": "^4.0.0", "ethr-did-registry": "^1.0.0" } } diff --git a/src/__tests__/resolver.test.ts b/src/__tests__/resolver.test.ts index 287fa121..1d8c0218 100644 --- a/src/__tests__/resolver.test.ts +++ b/src/__tests__/resolver.test.ts @@ -568,6 +568,165 @@ describe('ethrResolver', () => { }) }) }) + + describe('add expanded service endpoints', () => { + it('resolves document', async () => { + expect.assertions(2) + + await new EthrDidController(identity, registryContract).setAttribute( + stringToBytes32('did/svc/HubService'), + JSON.stringify({uri: 'https://hubs.uport.me', transportType: 'http'}), + 86405, + { from: controller } + ) + const { didDocument } = await didResolver.resolve(did) + expect(didDocument).toEqual({ + '@context': expect.anything(), + id: did, + verificationMethod: [ + { + id: `${did}#controller`, + type: 'EcdsaSecp256k1RecoveryMethod2020', + controller: did, + blockchainAccountId: `eip155:1337:${controller}`, + }, + { + id: `${did}#delegate-4`, + type: 'EcdsaSecp256k1RecoveryMethod2020', + controller: did, + blockchainAccountId: `eip155:1337:${delegate2}`, + }, + { + id: `${did}#delegate-5`, + type: 'EcdsaSecp256k1VerificationKey2019', + controller: did, + publicKeyHex: '02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71', + }, + { + id: `${did}#delegate-6`, + type: 'Ed25519VerificationKey2018', + controller: did, + publicKeyBase64: Buffer.from( + '02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71', + 'hex' + ).toString('base64'), + }, + { + id: `${did}#delegate-7`, + type: 'RSAVerificationKey2018', + controller: did, + publicKeyPem: '-----BEGIN PUBLIC KEY...END PUBLIC KEY-----\r\n', + }, + ], + authentication: [`${did}#controller`, `${did}#delegate-4`], + assertionMethod: [ + `${did}#controller`, + `${did}#delegate-4`, + `${did}#delegate-5`, + `${did}#delegate-6`, + `${did}#delegate-7`, + ], + service: [ + { + id: `${did}#service-1`, + type: 'HubService', + serviceEndpoint: 'https://hubs.uport.me', + }, + { + id: `${did}#service-2`, + type: 'HubService', + serviceEndpoint: { uri: 'https://hubs.uport.me', transportType: 'http' }, + }, + ], + }) + + await new EthrDidController(identity, registryContract).setAttribute( + stringToBytes32('did/svc/HubService'), + JSON.stringify([{uri: 'https://hubs.uport.me', transportType: 'http'}, {uri: 'libp2p.star/123', transportType: 'libp2p'}]), + 86405, + { from: controller } + ) + const { didDocument: updatedDidDocument } = await didResolver.resolve(did) + expect(updatedDidDocument).toEqual({ + '@context': expect.anything(), + id: did, + verificationMethod: [ + { + id: `${did}#controller`, + type: 'EcdsaSecp256k1RecoveryMethod2020', + controller: did, + blockchainAccountId: `eip155:1337:${controller}`, + }, + { + id: `${did}#delegate-4`, + type: 'EcdsaSecp256k1RecoveryMethod2020', + controller: did, + blockchainAccountId: `eip155:1337:${delegate2}`, + }, + { + id: `${did}#delegate-5`, + type: 'EcdsaSecp256k1VerificationKey2019', + controller: did, + publicKeyHex: '02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71', + }, + { + id: `${did}#delegate-6`, + type: 'Ed25519VerificationKey2018', + controller: did, + publicKeyBase64: Buffer.from( + '02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71', + 'hex' + ).toString('base64'), + }, + { + id: `${did}#delegate-7`, + type: 'RSAVerificationKey2018', + controller: did, + publicKeyPem: '-----BEGIN PUBLIC KEY...END PUBLIC KEY-----\r\n', + }, + ], + authentication: [`${did}#controller`, `${did}#delegate-4`], + assertionMethod: [ + `${did}#controller`, + `${did}#delegate-4`, + `${did}#delegate-5`, + `${did}#delegate-6`, + `${did}#delegate-7`, + ], + service: [ + { + id: `${did}#service-1`, + type: 'HubService', + serviceEndpoint: 'https://hubs.uport.me', + }, + { + id: `${did}#service-2`, + type: 'HubService', + serviceEndpoint: { uri: 'https://hubs.uport.me', transportType: 'http' }, + }, + { + id: `${did}#service-3`, + type: 'HubService', + serviceEndpoint: [{uri: 'https://hubs.uport.me', transportType: 'http'}, {uri: 'libp2p.star/123', transportType: 'libp2p'}], + }, + ], + }) + + // undo side effects of this test + await new EthrDidController(identity, registryContract).revokeAttribute( + stringToBytes32('did/svc/HubService'), + JSON.stringify([{uri: 'https://hubs.uport.me', transportType: 'http'}, {uri: 'libp2p.star/123', transportType: 'libp2p'}]), + { from: controller } + ) + + // undo side effects of this test + await new EthrDidController(identity, registryContract).revokeAttribute( + stringToBytes32('did/svc/HubService'), + JSON.stringify({uri: 'https://hubs.uport.me', transportType: 'http'}), + { from: controller } + ) + }) + }) }) describe('revoke publicKey', () => { diff --git a/src/resolver.ts b/src/resolver.ts index fd50e97c..d7e6e3cc 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -10,7 +10,7 @@ import { DIDResolver, ParsedDID, Resolvable, - ServiceEndpoint, + Service, VerificationMethod, } from 'did-resolver' import { @@ -134,10 +134,11 @@ export class EthrDidResolver { let deactivated = false let delegateCount = 0 let serviceCount = 0 + let endpoint = '' const auth: Record = {} const keyAgreementRefs: Record = {} const pks: Record = {} - const services: Record = {} + const services: Record = {} for (const event of history) { if (blockHeight !== -1 && event.blockNumber > blockHeight) { if (nextVersionId > event.blockNumber) { @@ -217,10 +218,15 @@ export class EthrDidResolver { } case 'svc': serviceCount++ + try { + endpoint = JSON.parse(Buffer.from(currentEvent.value.slice(2), 'hex').toString()) + } catch { + endpoint = Buffer.from(currentEvent.value.slice(2), 'hex').toString() + } services[eventIndex] = { id: `${did}#service-${serviceCount}`, type: algorithm, - serviceEndpoint: Buffer.from(currentEvent.value.slice(2), 'hex').toString(), + serviceEndpoint: endpoint, } break } diff --git a/yarn.lock b/yarn.lock index db41cda1..1ced3e76 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4305,10 +4305,10 @@ dezalgo@^1.0.0: asap "^2.0.0" wrappy "1" -did-resolver@^3.2.2: - version "3.2.2" - resolved "https://registry.yarnpkg.com/did-resolver/-/did-resolver-3.2.2.tgz#6f4e252a810f785d1b28a10265fad6dffee25158" - integrity sha512-Eeo2F524VM5N3W4GwglZrnul2y6TLTwMQP3In62JdG34NZoqihYyOZLk+5wUW8sSgvIYIcJM8Dlt3xsdKZZ3tg== +did-resolver@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/did-resolver/-/did-resolver-4.0.0.tgz#fc8f657b4cd7f44c2921051fb046599fbe7d4b31" + integrity sha512-/roxrDr9EnAmLs+s9T+8+gcpilMo+IkeytcsGO7dcxvTmVJ+0Rt60HtV8o0UXHhGBo0Q+paMH/0ffXz1rqGFYg== diff-sequences@^28.1.1: version "28.1.1"