-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support new didcomm v2 service type (#1902)
Signed-off-by: Timo Glastra <[email protected]>
- Loading branch information
1 parent
eef0660
commit d548fa4
Showing
29 changed files
with
571 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@credo-ts/indy-vdr': patch | ||
'@credo-ts/cheqd': patch | ||
'@credo-ts/core': patch | ||
--- | ||
|
||
feat: support new 'DIDCommMessaging' didcomm v2 service type (in addition to older 'DIDComm' service type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/core/src/modules/dids/__tests__/__fixtures__/didExample123DidcommV2Service.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"@context": ["https://w3id.org/did/v1"], | ||
"id": "did:example:123", | ||
"service": [ | ||
{ | ||
"id": "did:example:123#service-1", | ||
"type": "DIDCommMessaging", | ||
"serviceEndpoint": { | ||
"uri": "did:sov:Q4zqM7aXqm7gDQkUVLng9h", | ||
"routingKeys": ["Q4zqM7aXqm7gDQkUVLng9h"] | ||
} | ||
}, | ||
{ | ||
"id": "did:example:123#service-2", | ||
"type": "DIDComm", | ||
"serviceEndpoint": "https://agent.com/did-comm", | ||
"routingKeys": ["DADEajsDSaksLng9h"] | ||
}, | ||
{ | ||
"id": "did:example:123#service-3", | ||
"type": "DIDCommMessaging", | ||
"serviceEndpoint": [ | ||
{ | ||
"uri": "did:sov:Q4zqM7aXqm7gDQkUVLng9h", | ||
"routingKeys": ["Q4zqM7aXqm7gDQkUVLng9h"] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 41 additions & 4 deletions
45
packages/core/src/modules/dids/domain/service/DidDocumentService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,63 @@ | ||
import { IsString } from 'class-validator' | ||
import type { ValidationOptions } from 'class-validator' | ||
|
||
import { buildMessage, isString, IsString, ValidateBy } from 'class-validator' | ||
|
||
import { CredoError } from '../../../../error' | ||
import { isJsonObject, SingleOrArray } from '../../../../utils' | ||
import { getProtocolScheme } from '../../../../utils/uri' | ||
|
||
type ServiceEndpointType = SingleOrArray<string | Record<string, unknown>> | ||
|
||
export class DidDocumentService { | ||
public constructor(options: { id: string; serviceEndpoint: string; type: string }) { | ||
public constructor(options: { id: string; serviceEndpoint: ServiceEndpointType; type: string }) { | ||
if (options) { | ||
this.id = options.id | ||
this.serviceEndpoint = options.serviceEndpoint | ||
this.type = options.type | ||
} | ||
} | ||
|
||
/** | ||
* @deprecated will be removed in 0.6, as it's not possible from the base did document service class to determine | ||
* the protocol scheme. It needs to be implemented on a specific did document service class. | ||
*/ | ||
public get protocolScheme(): string { | ||
if (typeof this.serviceEndpoint !== 'string') { | ||
throw new CredoError('Unable to extract protocol scheme from serviceEndpoint as it is not a string.') | ||
} | ||
|
||
return getProtocolScheme(this.serviceEndpoint) | ||
} | ||
|
||
@IsString() | ||
public id!: string | ||
|
||
@IsString() | ||
public serviceEndpoint!: string | ||
@IsStringOrJsonObjectSingleOrArray() | ||
public serviceEndpoint!: SingleOrArray<string | Record<string, unknown>> | ||
|
||
@IsString() | ||
public type!: string | ||
} | ||
|
||
/** | ||
* Checks if a given value is a string, a json object, or an array of strings and json objects | ||
*/ | ||
function IsStringOrJsonObjectSingleOrArray(validationOptions?: Omit<ValidationOptions, 'each'>): PropertyDecorator { | ||
return ValidateBy( | ||
{ | ||
name: 'isStringOrJsonObjectSingleOrArray', | ||
validator: { | ||
validate: (value): boolean => | ||
isString(value) || | ||
isJsonObject(value) || | ||
(Array.isArray(value) && value.every((v) => isString(v) || isJsonObject(v))), | ||
defaultMessage: buildMessage( | ||
(eachPrefix) => | ||
eachPrefix + '$property must be a string, JSON object, or an array consisting of strings and JSON objects', | ||
validationOptions | ||
), | ||
}, | ||
}, | ||
validationOptions | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/core/src/modules/dids/domain/service/NewDidCommV2Service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Type } from 'class-transformer' | ||
import { IsOptional, IsString, ValidateNested } from 'class-validator' | ||
|
||
import { CredoError } from '../../../../error' | ||
import { SingleOrArray, IsInstanceOrArrayOfInstances, IsUri } from '../../../../utils' | ||
|
||
import { DidDocumentService } from './DidDocumentService' | ||
|
||
export interface NewDidCommV2ServiceEndpointOptions { | ||
uri: string | ||
routingKeys?: string[] | ||
accept?: string[] | ||
} | ||
|
||
export class NewDidCommV2ServiceEndpoint { | ||
public constructor(options: NewDidCommV2ServiceEndpointOptions) { | ||
if (options) { | ||
this.uri = options.uri | ||
this.routingKeys = options.routingKeys | ||
this.accept = options.accept | ||
} | ||
} | ||
|
||
@IsString() | ||
@IsUri() | ||
public uri!: string | ||
|
||
@IsString({ each: true }) | ||
@IsOptional() | ||
public routingKeys?: string[] | ||
|
||
@IsString({ each: true }) | ||
@IsOptional() | ||
public accept?: string[]; | ||
|
||
[key: string]: unknown | undefined | ||
} | ||
|
||
export interface DidCommV2ServiceOptions { | ||
id: string | ||
serviceEndpoint: SingleOrArray<NewDidCommV2ServiceEndpoint> | ||
} | ||
|
||
/** | ||
* Will be renamed to `DidCommV2Service` in 0.6 (and replace the current `DidCommV2Service`) | ||
*/ | ||
export class NewDidCommV2Service extends DidDocumentService { | ||
public constructor(options: DidCommV2ServiceOptions) { | ||
super({ ...options, type: NewDidCommV2Service.type }) | ||
|
||
if (options) { | ||
this.serviceEndpoint = options.serviceEndpoint | ||
} | ||
} | ||
|
||
public static type = 'DIDCommMessaging' | ||
|
||
@IsInstanceOrArrayOfInstances({ classType: [NewDidCommV2ServiceEndpoint] }) | ||
@ValidateNested() | ||
@Type(() => NewDidCommV2ServiceEndpoint) | ||
public serviceEndpoint!: SingleOrArray<NewDidCommV2ServiceEndpoint> | ||
|
||
public get firstServiceEndpointUri(): string { | ||
if (Array.isArray(this.serviceEndpoint)) { | ||
if (this.serviceEndpoint.length === 0) { | ||
throw new CredoError('No entries in serviceEndpoint array') | ||
} | ||
|
||
return this.serviceEndpoint[0].uri | ||
} | ||
|
||
return this.serviceEndpoint.uri | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.