Skip to content

Commit 4ead68e

Browse files
authored
fix(types): update Service interface to match latest DID spec (#136)
fixes #135 BREAKING CHANGE: the `ServiceEndpoint` type has been renamed to `Service` and a new type `ServiceEndpoint` was created to fit the [DID spec](https://www.w3.org/TR/did-core/#services)
1 parent 22cf5a9 commit 4ead68e

File tree

2 files changed

+116
-6
lines changed

2 files changed

+116
-6
lines changed

src/__tests__/resolver.test.ts

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { Resolver, parse } from '../resolver'
15+
import { Resolver, parse, DIDResolutionResult } from '../resolver'
1616

1717
describe('resolver', () => {
1818
describe('parse()', () => {
@@ -161,7 +161,7 @@ describe('resolver', () => {
161161
beforeAll(() => {
162162
mockmethod = jest.fn().mockReturnValue(mockReturn)
163163
resolver = new Resolver({
164-
example: async (did: string) => ({
164+
example: async (did: string): Promise<DIDResolutionResult> => ({
165165
didResolutionMetadata: { contentType: 'application/did+ld+json' },
166166
didDocument: {
167167
'@context': 'https://w3id.org/did/v1',
@@ -213,6 +213,114 @@ describe('resolver', () => {
213213
didDocumentMetadata: {},
214214
})
215215
})
216+
217+
it('resolves did document with service', async () => {
218+
const altResolver = new Resolver({
219+
example: async (did: string): Promise<DIDResolutionResult> => ({
220+
didResolutionMetadata: { contentType: 'application/did+ld+json' },
221+
didDocument: {
222+
'@context': 'https://w3id.org/did/v1',
223+
id: did,
224+
verificationMethod: [
225+
{
226+
id: 'owner',
227+
controller: '1234',
228+
type: 'xyz',
229+
},
230+
],
231+
service: [
232+
{
233+
id: `service-${did}`,
234+
type: 'Service',
235+
serviceEndpoint: 'https://example.com/',
236+
}
237+
],
238+
},
239+
didDocumentMetadata: {},
240+
}),
241+
mock: mockmethod,
242+
})
243+
244+
await expect(altResolver.resolve('did:example:123456789')).resolves.toEqual({
245+
didResolutionMetadata: { contentType: 'application/did+ld+json' },
246+
didDocument: {
247+
'@context': 'https://w3id.org/did/v1',
248+
id: 'did:example:123456789',
249+
verificationMethod: [
250+
{
251+
id: 'owner',
252+
controller: '1234',
253+
type: 'xyz',
254+
},
255+
],
256+
service: [
257+
{
258+
id: 'service-did:example:123456789',
259+
type: 'Service',
260+
serviceEndpoint: 'https://example.com/',
261+
}
262+
],
263+
},
264+
didDocumentMetadata: {},
265+
})
266+
})
267+
268+
it('resolves did document with expanded service', async () => {
269+
const altResolver = new Resolver({
270+
example: async (did: string): Promise<DIDResolutionResult> => ({
271+
didResolutionMetadata: { contentType: 'application/did+ld+json' },
272+
didDocument: {
273+
'@context': 'https://w3id.org/did/v1',
274+
id: did,
275+
verificationMethod: [
276+
{
277+
id: 'owner',
278+
controller: '1234',
279+
type: 'xyz',
280+
},
281+
],
282+
service: [
283+
{
284+
id: `service-${did}`,
285+
type: 'Service',
286+
serviceEndpoint: {
287+
uri: 'yep',
288+
accept: ['xyz']
289+
},
290+
}
291+
],
292+
},
293+
didDocumentMetadata: {},
294+
}),
295+
mock: mockmethod,
296+
})
297+
298+
await expect(altResolver.resolve('did:example:123456789')).resolves.toEqual({
299+
didResolutionMetadata: { contentType: 'application/did+ld+json' },
300+
didDocument: {
301+
'@context': 'https://w3id.org/did/v1',
302+
id: 'did:example:123456789',
303+
verificationMethod: [
304+
{
305+
id: 'owner',
306+
controller: '1234',
307+
type: 'xyz',
308+
},
309+
],
310+
service: [
311+
{
312+
id: 'service-did:example:123456789',
313+
type: 'Service',
314+
serviceEndpoint: {
315+
uri: 'yep',
316+
accept: ['xyz']
317+
},
318+
}
319+
],
320+
},
321+
didDocumentMetadata: {},
322+
})
323+
})
216324

217325
it('resolves did document with legacy resolver', async () => {
218326
const resolverWithLegacy = new Resolver(

src/resolver.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export type DIDDocument = {
5555
alsoKnownAs?: string[]
5656
controller?: string | string[]
5757
verificationMethod?: VerificationMethod[]
58-
service?: ServiceEndpoint[]
58+
service?: Service[]
5959
/**
6060
* @deprecated
6161
*/
@@ -64,13 +64,15 @@ export type DIDDocument = {
6464
[x in KeyCapabilitySection]?: (string | VerificationMethod)[]
6565
}
6666

67-
export interface ServiceEndpoint {
67+
export interface Service {
6868
id: string
6969
type: string
70-
serviceEndpoint: string
71-
description?: string
70+
serviceEndpoint: ServiceEndpoint | ServiceEndpoint[]
71+
[x: string]: any
7272
}
7373

74+
export type ServiceEndpoint = string | Record<string, any>
75+
7476
/**
7577
* Encapsulates a JSON web key type that includes only the public properties that
7678
* can be used in DID documents.

0 commit comments

Comments
 (0)