Skip to content

Commit

Permalink
feat: add experimental support for ServiceEndpoint objects (decentral…
Browse files Browse the repository at this point in the history
…ized-identity#163)

* refactor: update did-resolver package add tests to cover `Service`/`ServiceEndpoint` types

* feat: try to decode the endpoint string as JSON first
  • Loading branch information
nickreynolds authored Aug 4, 2022
1 parent 11382cc commit 3919a25
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
159 changes: 159 additions & 0 deletions src/__tests__/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
12 changes: 9 additions & 3 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DIDResolver,
ParsedDID,
Resolvable,
ServiceEndpoint,
Service,
VerificationMethod,
} from 'did-resolver'
import {
Expand Down Expand Up @@ -134,10 +134,11 @@ export class EthrDidResolver {
let deactivated = false
let delegateCount = 0
let serviceCount = 0
let endpoint = ''
const auth: Record<string, string> = {}
const keyAgreementRefs: Record<string, string> = {}
const pks: Record<string, VerificationMethod> = {}
const services: Record<string, ServiceEndpoint> = {}
const services: Record<string, Service> = {}
for (const event of history) {
if (blockHeight !== -1 && event.blockNumber > blockHeight) {
if (nextVersionId > event.blockNumber) {
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3919a25

Please sign in to comment.