Skip to content

Commit

Permalink
fix(connections): allow ; to convert legacy did (#882)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra committed Jun 20, 2022
1 parent ad35b08 commit 448a29d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('ConnectionService', () => {
const processedConnection = await connectionService.processRequest(messageContext, outOfBand)

expect(processedConnection.state).toBe(DidExchangeState.RequestReceived)
expect(processedConnection.theirDid).toBe('did:peer:1zQmfPPbuG8vajHvYjGUW8CN5k9rLuuMmYSGBYwJqJDDUS72')
expect(processedConnection.theirDid).toBe('did:peer:1zQmW2esSyEVGzrh3CFt1eQZUHEAb3Li1hyPudPhSoFevrFY')
expect(processedConnection.theirLabel).toBe('test-label')
expect(processedConnection.threadId).toBe(connectionRequest.id)
expect(processedConnection.imageUrl).toBe(connectionImageUrl)
Expand Down Expand Up @@ -267,7 +267,7 @@ describe('ConnectionService', () => {
const processedConnection = await connectionService.processRequest(messageContext, outOfBand)

expect(processedConnection.state).toBe(DidExchangeState.RequestReceived)
expect(processedConnection.theirDid).toBe('did:peer:1zQmfPPbuG8vajHvYjGUW8CN5k9rLuuMmYSGBYwJqJDDUS72')
expect(processedConnection.theirDid).toBe('did:peer:1zQmW2esSyEVGzrh3CFt1eQZUHEAb3Li1hyPudPhSoFevrFY')
expect(processedConnection.theirLabel).toBe('test-label')
expect(processedConnection.threadId).toBe(connectionRequest.id)

Expand Down
70 changes: 70 additions & 0 deletions packages/core/src/modules/connections/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,74 @@ describe('convertToNewDidDocument', () => {
}),
])
})

test('will use ; as an id mark instead of # if the # is missing in a service id', () => {
const oldDocument = new DidDoc({
id: 'did:sov:LjgpST2rjsoxYegQDRm7EL',
authentication: [],
publicKey: [],
service: [
new IndyAgentService({
id: 'did:sov:SKJVx2kn373FNgvff1SbJo;service-1',
serviceEndpoint: 'did:sov:SKJVx2kn373FNgvff1SbJo',
recipientKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
routingKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
priority: 5,
}),
],
})
const newDocument = convertToNewDidDocument(oldDocument)

expect(newDocument.service).toEqual([
new IndyAgentService({
id: '#service-1',
serviceEndpoint: 'did:sov:SKJVx2kn373FNgvff1SbJo',
recipientKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
routingKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
priority: 5,
}),
])
})

test('will only split on the first ; or # and leave the other ones in place as id values', () => {
const oldDocument = new DidDoc({
id: 'did:sov:LjgpST2rjsoxYegQDRm7EL',
authentication: [],
publicKey: [],
service: [
new IndyAgentService({
id: 'did:sov:SKJVx2kn373FNgvff1SbJo;service-1;something-extra',
serviceEndpoint: 'did:sov:SKJVx2kn373FNgvff1SbJo',
recipientKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
routingKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
priority: 5,
}),
new IndyAgentService({
id: 'did:sov:SKJVx2kn373FNgvff1SbJo#service-2#something-extra',
serviceEndpoint: 'did:sov:SKJVx2kn373FNgvff1SbJo',
recipientKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
routingKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
priority: 5,
}),
],
})
const newDocument = convertToNewDidDocument(oldDocument)

expect(newDocument.service).toEqual([
new IndyAgentService({
id: '#service-1;something-extra',
serviceEndpoint: 'did:sov:SKJVx2kn373FNgvff1SbJo',
recipientKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
routingKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
priority: 5,
}),
new IndyAgentService({
id: '#service-2#something-extra',
serviceEndpoint: 'did:sov:SKJVx2kn373FNgvff1SbJo',
recipientKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
routingKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
priority: 5,
}),
])
})
})
11 changes: 9 additions & 2 deletions packages/core/src/modules/connections/services/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,16 @@ export function convertToNewDidDocument(didDoc: DidDoc): DidDocument {
}

function normalizeId(fullId: string): `#${string}` {
const [, id] = fullId.split('#')
// Some old dids use `;` as the delimiter for the id. If we can't find a `#`
// and a `;` exists, we will parse everything after `;` as the id.
if (!fullId.includes('#') && fullId.includes(';')) {
const [, ...ids] = fullId.split(';')

return `#${id ?? fullId}`
return `#${ids.join(';')}`
}

const [, ...ids] = fullId.split('#')
return `#${ids.length ? ids.join('#') : fullId}`
}

function convertPublicKeyToVerificationMethod(publicKey: PublicKey) {
Expand Down

0 comments on commit 448a29d

Please sign in to comment.