Skip to content

Commit

Permalink
handle newer serviceEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
brianorwhatever committed Jul 26, 2023
1 parent 49f380b commit 92d201c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
"dependencies": {
"buffer": "^6.0.3"
}
}
}
34 changes: 23 additions & 11 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const utf8 = {

export const base64url = {
encode: (unencoded: any): string => {
var encoded = base64.encode(unencoded);
const encoded = base64.encode(unencoded);
return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
},
decode: (encoded: any): Uint8Array => {
Expand All @@ -46,14 +46,30 @@ export const encodeService = (service: IDIDDocumentServiceDescriptor): string =>

export const decodeService = (did: string, service: string, index: number): IDIDDocumentServiceDescriptor => {
let val = JSON.parse(utf8.decode(base64url.decode(service)))
if (val.r) {
val['routingKeys'] = val.r;
delete val['r']
if (val.s) {
val['serviceEndpoint'] = val.s;
delete val['s']
}
if (val.a) {
val['accept'] = val.a;
delete val['a'];
if (typeof val.serviceEndpoint === 'object') {
if (val.serviceEndpoint.r) {
val.serviceEndpoint['routingKeys'] = val.r;
delete val.serviceEndpoint['r']
}
if (val.serviceEndpoint.a) {
val.serviceEndpoint['accept'] = val.a;
delete val.serviceEndpoint['a'];
}
} else {
if (val.r) {
val['routingKeys'] = val.r;
delete val['r']
}
if (val.a) {
val['accept'] = val.a;
delete val['a'];
}
}

if (val.t) {
if (val.t === 'dm') {
val.type = 'DIDCommMessaging'
Expand All @@ -64,10 +80,6 @@ export const decodeService = (did: string, service: string, index: number): IDID
}
delete val['t']
}
if (val.s) {
val['serviceEndpoint'] = val.s;
delete val['s']
}
return val;
}

Expand Down
23 changes: 23 additions & 0 deletions src/tests/lib/create.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@ describe('createNumAlgo2', () => {
expect(serv['r']).toStrictEqual(['did:example:123#456'])
expect(serv['a']).toStrictEqual(['didcomm/v2'])
})

it('should create valid peer:did with NumAlgo2 with service with serviceEndpoint object', async () => {
const service = {
'id': '#didcomm',
'type': 'DIDCommMessaging',
'serviceEndpoint': {
'uri': 'http://example.com',
'routingKeys': ['did:example:123#456'],
'accept': ['didcomm/v2']
}
}
const did = await createNumAlgo2([ed25519Key], undefined, service);
expect(did).toBeTruthy()
const segments = did.split('.');
const idx = segments.findIndex((s) => s.length > 1 && s[0] === 'S')
expect(segments[idx][0]).toBe('S')
const basedService = segments[idx].slice(1)
const serv = JSON.parse(utf8.decode(base64.decode(basedService)))
expect(serv['id']).toBe('#didcomm')
expect(serv['t']).toBe('dm')
expect(serv['s']).toStrictEqual({'uri':'http://example.com', 'r': ['did:example:123#456'], 'a': ['didcomm/v2']})
})

it('should require encryption key type X25519KeyAgreementKey2020', async () => {
try {
const did = await createNumAlgo2([ed25519Key], [ed25519Key]);
Expand Down
7 changes: 6 additions & 1 deletion src/tests/lib/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('resolve', () => {
const doc = await resolve('did:peer:0z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH')
expect(doc).toBeTruthy()
})
it('should create peer:did w/ numalgo1', async () => {
it('should resolve peer:did w/ numalgo1', async () => {
try {
const doc = await resolve('did:peer:1zQmZMygzYqNwU6Uhmewx5Xepf2VLp5S4HLSwwgf2aiKZuwa')
expect(true).toBeFalsy()
Expand All @@ -42,6 +42,11 @@ describe('resolve', () => {
expect(doc).toBeTruthy()
})

it('should resolve peer:did w/ serviceEndpoint object', async () => {
const doc = await resolve('did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.SeyJpZCI6ICIjZGlkY29tbSIsICJ0IjogImRtIiwgInMiOiB7InVyaSI6ICJodHRwOi8vZXhhbXBsZS5jb20iLCAiciI6IFsiZGlkOmV4YW1wbGU6MTIzIzQ1NiJdLCAiYSI6IFsiZGlkY29tbS92MiJdfX0')
expect(doc).toBeTruthy();
})

it('should fail if not peer:did', async () => {
try {
const doc = await resolve('did:example:123')
Expand Down

0 comments on commit 92d201c

Please sign in to comment.