Skip to content

Commit

Permalink
fix: did sov service type resolving (#689)
Browse files Browse the repository at this point in the history
Signed-off-by: James Ebert <[email protected]>
  • Loading branch information
JamesKEbert authored Apr 15, 2022
1 parent e1528b9 commit dbcd8c4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
"type": "endpoint",
"serviceEndpoint": "https://ssi.com"
},
{
"accept": ["didcomm/aip2;env=rfc19"],
"id": "did:sov:R1xKJw17sUoXhejEpugMYJ#did-communication",
"priority": 0,
"recipientKeys": ["did:sov:R1xKJw17sUoXhejEpugMYJ#key-agreement-1"],
"routingKeys": [],
"serviceEndpoint": "https://ssi.com",
"type": "did-communication"
},
{
"id": "did:sov:R1xKJw17sUoXhejEpugMYJ#profile",
"serviceEndpoint": "https://profile.com",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DidDocumentBuilder } from '../../domain/DidDocumentBuilder'
import { DidCommService } from '../../domain/service/DidCommService'
import { DidCommV2Service } from '../../domain/service/DidCommV2Service'

export class IndyDidResolver implements DidResolver {
export class SovDidResolver implements DidResolver {
private indyLedgerService: IndyLedgerService

public constructor(indyLedgerService: IndyLedgerService) {
Expand Down Expand Up @@ -73,6 +73,27 @@ export class IndyDidResolver implements DidResolver {
}
}

// Process Indy Attrib Endpoint Types according to: https://sovrin-foundation.github.io/sovrin/spec/did-method-spec-template.html > Read (Resolve) > DID Service Endpoint
private processEndpointTypes(types?: string[]) {
const expectedTypes = ['endpoint', 'did-communication', 'DIDComm']
const defaultTypes = ['endpoint', 'did-communication']

// Return default types if types "is NOT present [or] empty"
if (!types || types?.length <= 0) {
return defaultTypes
}

// Return default types if types "contain any other values"
for (const type of types) {
if (!expectedTypes.includes(type)) {
return defaultTypes
}
}

// Return provided types
return types
}

private addServices(
builder: DidDocumentBuilder,
parsed: ParsedDid,
Expand All @@ -81,18 +102,22 @@ export class IndyDidResolver implements DidResolver {
) {
const { endpoint, routingKeys, types, ...otherEndpoints } = endpoints

// If 'endpoint' type add id to the services array
if (endpoint) {
builder.addService(
new DidDocumentService({
id: `${parsed.did}#endpoint`,
serviceEndpoint: endpoint,
type: 'endpoint',
})
)
const processedTypes = this.processEndpointTypes(types)

// If 'endpoint' included in types, add id to the services array
if (processedTypes.includes('endpoint')) {
builder.addService(
new DidDocumentService({
id: `${parsed.did}#endpoint`,
serviceEndpoint: endpoint,
type: 'endpoint',
})
)
}

// If 'did-communication' included in types, add DIDComm v1 entry
if (types?.includes('did-communication')) {
if (processedTypes.includes('did-communication')) {
builder.addService(
new DidCommService({
id: `${parsed.did}#did-communication`,
Expand All @@ -105,7 +130,7 @@ export class IndyDidResolver implements DidResolver {
)

// If 'DIDComm' included in types, add DIDComm v2 entry
if (types?.includes('DIDComm')) {
if (processedTypes.includes('DIDComm')) {
builder
.addService(
new DidCommV2Service({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import { IndyLedgerService } from '../../../../ledger/services/IndyLedgerService
import didSovR1xKJw17sUoXhejEpugMYJFixture from '../../../__tests__/__fixtures__/didSovR1xKJw17sUoXhejEpugMYJ.json'
import didSovWJz9mHyW9BZksioQnRsrAoFixture from '../../../__tests__/__fixtures__/didSovWJz9mHyW9BZksioQnRsrAo.json'
import { parseDid } from '../../../domain/parse'
import { IndyDidResolver } from '../IndyDidResolver'
import { SovDidResolver } from '../SovDidResolver'

jest.mock('../../../../ledger/services/IndyLedgerService')
const IndyLedgerServiceMock = IndyLedgerService as jest.Mock<IndyLedgerService>

describe('DidResolver', () => {
describe('IndyDidResolver', () => {
describe('SovDidResolver', () => {
let ledgerService: IndyLedgerService
let indyDidResolver: IndyDidResolver
let sovDidResolver: SovDidResolver

beforeEach(() => {
ledgerService = new IndyLedgerServiceMock()
indyDidResolver = new IndyDidResolver(ledgerService)
sovDidResolver = new SovDidResolver(ledgerService)
})

it('should correctly resolve a did:sov document', async () => {
Expand All @@ -40,7 +40,7 @@ describe('DidResolver', () => {
mockFunction(ledgerService.getPublicDid).mockResolvedValue(nymResponse)
mockFunction(ledgerService.getEndpointsForDid).mockResolvedValue(endpoints)

const result = await indyDidResolver.resolve(did, parseDid(did))
const result = await sovDidResolver.resolve(did, parseDid(did))

expect(JsonTransformer.toJSON(result)).toMatchObject({
didDocument: didSovR1xKJw17sUoXhejEpugMYJFixture,
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('DidResolver', () => {
mockFunction(ledgerService.getPublicDid).mockReturnValue(Promise.resolve(nymResponse))
mockFunction(ledgerService.getEndpointsForDid).mockReturnValue(Promise.resolve(endpoints))

const result = await indyDidResolver.resolve(did, parseDid(did))
const result = await sovDidResolver.resolve(did, parseDid(did))

expect(JsonTransformer.toJSON(result)).toMatchObject({
didDocument: didSovWJz9mHyW9BZksioQnRsrAoFixture,
Expand All @@ -85,7 +85,7 @@ describe('DidResolver', () => {

mockFunction(ledgerService.getPublicDid).mockRejectedValue(new Error('Error retrieving did'))

const result = await indyDidResolver.resolve(did, parseDid(did))
const result = await sovDidResolver.resolve(did, parseDid(did))

expect(result).toMatchObject({
didDocument: null,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/modules/dids/services/DidResolverService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { Lifecycle, scoped } from 'tsyringe'
import { AgentConfig } from '../../../agent/AgentConfig'
import { IndyLedgerService } from '../../ledger'
import { parseDid } from '../domain/parse'
import { IndyDidResolver } from '../methods/indy/IndyDidResolver'
import { KeyDidResolver } from '../methods/key/KeyDidResolver'
import { PeerDidResolver } from '../methods/peer/PeerDidResolver'
import { SovDidResolver } from '../methods/sov/SovDidResolver'
import { WebDidResolver } from '../methods/web/WebDidResolver'
import { DidRepository } from '../repository'

Expand All @@ -22,7 +22,7 @@ export class DidResolverService {
this.logger = agentConfig.logger

this.resolvers = [
new IndyDidResolver(indyLedgerService),
new SovDidResolver(indyLedgerService),
new WebDidResolver(),
new KeyDidResolver(),
new PeerDidResolver(didRepository),
Expand Down

0 comments on commit dbcd8c4

Please sign in to comment.