@sphereon/ssi-types 0.8.0 → 0.8.1-next.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/mapper/credential-mapper.d.ts +19 -0
- package/dist/mapper/credential-mapper.d.ts.map +1 -0
- package/dist/mapper/credential-mapper.js +208 -0
- package/dist/mapper/credential-mapper.js.map +1 -0
- package/dist/mapper/index.d.ts +2 -0
- package/dist/mapper/index.d.ts.map +1 -0
- package/dist/mapper/index.js +18 -0
- package/dist/mapper/index.js.map +1 -0
- package/dist/types/did.d.ts +1 -0
- package/dist/types/did.d.ts.map +1 -1
- package/dist/types/did.js +49 -1
- package/dist/types/did.js.map +1 -1
- package/dist/types/pex.d.ts +1 -12
- package/dist/types/pex.d.ts.map +1 -1
- package/dist/types/vc.d.ts +54 -25
- package/dist/types/vc.d.ts.map +1 -1
- package/dist/types/vc.js +7 -7
- package/dist/types/vc.js.map +1 -1
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +18 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/object.d.ts +7 -0
- package/dist/utils/object.d.ts.map +1 -0
- package/dist/utils/object.js +21 -0
- package/dist/utils/object.js.map +1 -0
- package/package.json +16 -3
- package/src/index.ts +2 -0
- package/src/mapper/credential-mapper.ts +245 -0
- package/src/mapper/index.ts +1 -0
- package/src/types/did.ts +48 -0
- package/src/types/pex.ts +1 -13
- package/src/types/vc.ts +61 -28
- package/src/utils/index.ts +1 -0
- package/src/utils/object.ts +19 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./object"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/utils/object.ts"],"names":[],"mappings":"AAAA,qBAAa,WAAW;WACR,OAAO,CAAC,KAAK,EAAE,OAAO;WAItB,QAAQ,CAAC,KAAK,EAAE,OAAO;WAIvB,aAAa,CAAC,GAAG,EAAE,MAAM;WAMzB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO;CAGhD"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ObjectUtils = void 0;
|
|
4
|
+
class ObjectUtils {
|
|
5
|
+
static asArray(value) {
|
|
6
|
+
return Array.isArray(value) ? value : [value];
|
|
7
|
+
}
|
|
8
|
+
static isObject(value) {
|
|
9
|
+
return Object.prototype.toString.call(value) === '[object Object]';
|
|
10
|
+
}
|
|
11
|
+
static isUrlAbsolute(url) {
|
|
12
|
+
// regex to check for absolute IRI (starting scheme and ':') or blank node IRI
|
|
13
|
+
const isAbsoluteRegex = /^([A-Za-z][A-Za-z0-9+-.]*|_):[^\s]*$/;
|
|
14
|
+
ObjectUtils.isString(url) && isAbsoluteRegex.test(url);
|
|
15
|
+
}
|
|
16
|
+
static isString(value) {
|
|
17
|
+
return typeof value === 'string' || Object.prototype.toString.call(value) === '[object String]';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.ObjectUtils = ObjectUtils;
|
|
21
|
+
//# sourceMappingURL=object.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../../src/utils/object.ts"],"names":[],"mappings":";;;AAAA,MAAa,WAAW;IACf,MAAM,CAAC,OAAO,CAAC,KAAc;QAClC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;IAC/C,CAAC;IAEM,MAAM,CAAC,QAAQ,CAAC,KAAc;QACnC,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,iBAAiB,CAAA;IACpE,CAAC;IAEM,MAAM,CAAC,aAAa,CAAC,GAAW;QACrC,8EAA8E;QAC9E,MAAM,eAAe,GAAG,sCAAsC,CAAA;QAC9D,WAAW,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,eAAe,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IACxD,CAAC;IAEM,MAAM,CAAC,QAAQ,CAAC,KAAc;QACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,iBAAiB,CAAA;IACjG,CAAC;CACF;AAlBD,kCAkBC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sphereon/ssi-types",
|
|
3
3
|
"description": "SSI Common Types",
|
|
4
|
-
"version": "0.8.
|
|
4
|
+
"version": "0.8.1-next.12+84fb570",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc --build"
|
|
9
9
|
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"jwt-decode": "^3.1.2"
|
|
12
|
+
},
|
|
10
13
|
"devDependencies": {
|
|
14
|
+
"@types/jest": "^27.0.2",
|
|
15
|
+
"jest": "^27.3.1",
|
|
16
|
+
"prettier": "^2.4.1",
|
|
17
|
+
"ts-jest": "^27.0.7",
|
|
11
18
|
"typescript": "4.6.4"
|
|
12
19
|
},
|
|
13
20
|
"files": [
|
|
@@ -23,6 +30,12 @@
|
|
|
23
30
|
"repository": "git@github.com:Sphereon-OpenSource/ssi-sdk.git",
|
|
24
31
|
"author": "Sphereon <dev@sphereon.com>",
|
|
25
32
|
"license": "Apache-2.0",
|
|
26
|
-
"keywords": [
|
|
27
|
-
|
|
33
|
+
"keywords": [
|
|
34
|
+
"SSI",
|
|
35
|
+
"Common Credential Types",
|
|
36
|
+
"Credential Mapping",
|
|
37
|
+
"Verifiable Credentials",
|
|
38
|
+
"DIDs"
|
|
39
|
+
],
|
|
40
|
+
"gitHead": "84fb570f29ecd7a4fbd6477fd22284f47e814ab8"
|
|
28
41
|
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ClaimFormat,
|
|
3
|
+
ICredential,
|
|
4
|
+
IPresentation,
|
|
5
|
+
IVerifiableCredential,
|
|
6
|
+
IVerifiablePresentation,
|
|
7
|
+
JwtDecodedVerifiableCredential,
|
|
8
|
+
JwtDecodedVerifiablePresentation,
|
|
9
|
+
OriginalType,
|
|
10
|
+
OriginalVerifiableCredential,
|
|
11
|
+
OriginalVerifiablePresentation,
|
|
12
|
+
WrappedVerifiableCredential,
|
|
13
|
+
WrappedVerifiablePresentation,
|
|
14
|
+
} from '../types'
|
|
15
|
+
import jwt_decode from 'jwt-decode'
|
|
16
|
+
import { ObjectUtils } from '../utils'
|
|
17
|
+
|
|
18
|
+
export class CredentialMapper {
|
|
19
|
+
static decodeVerifiablePresentation(presentation: OriginalVerifiablePresentation): JwtDecodedVerifiablePresentation | IVerifiablePresentation {
|
|
20
|
+
if (CredentialMapper.isJwtEncoded(presentation)) {
|
|
21
|
+
return jwt_decode(presentation as string) as JwtDecodedVerifiablePresentation
|
|
22
|
+
} else if (CredentialMapper.isJwtDecodedPresentation(presentation)) {
|
|
23
|
+
return presentation as JwtDecodedVerifiablePresentation
|
|
24
|
+
} else if (CredentialMapper.isJsonLdString(presentation)) {
|
|
25
|
+
return JSON.parse(presentation as string) as IVerifiablePresentation
|
|
26
|
+
} else {
|
|
27
|
+
return presentation as IVerifiablePresentation
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static decodeVerifiableCredential(credential: OriginalVerifiableCredential): JwtDecodedVerifiableCredential | IVerifiableCredential {
|
|
32
|
+
if (CredentialMapper.isJwtEncoded(credential)) {
|
|
33
|
+
return jwt_decode(credential as string) as JwtDecodedVerifiableCredential
|
|
34
|
+
} else if (CredentialMapper.isJwtDecodedCredential(credential)) {
|
|
35
|
+
return credential as JwtDecodedVerifiableCredential
|
|
36
|
+
} else if (CredentialMapper.isJsonLdString(credential)) {
|
|
37
|
+
return JSON.parse(credential as string) as IVerifiableCredential
|
|
38
|
+
} else {
|
|
39
|
+
return credential as IVerifiableCredential
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static toWrappedVerifiablePresentation(presentation: OriginalVerifiablePresentation): WrappedVerifiablePresentation {
|
|
44
|
+
const original = presentation
|
|
45
|
+
const isJwtEncoded: boolean = CredentialMapper.isJwtEncoded(original)
|
|
46
|
+
const isJwtDecoded: boolean = CredentialMapper.isJwtDecodedPresentation(original)
|
|
47
|
+
|
|
48
|
+
const type = isJwtEncoded ? OriginalType.JWT_ENCODED : isJwtDecoded ? OriginalType.JWT_DECODED : OriginalType.JSONLD
|
|
49
|
+
const format: ClaimFormat = isJwtDecoded || isJwtEncoded ? 'jwt_vp' : 'ldp_vp'
|
|
50
|
+
const decoded = CredentialMapper.decodeVerifiablePresentation(original)
|
|
51
|
+
const vp: IPresentation =
|
|
52
|
+
isJwtEncoded || isJwtDecoded
|
|
53
|
+
? CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded as JwtDecodedVerifiablePresentation, false)
|
|
54
|
+
: (decoded as IPresentation)
|
|
55
|
+
const vcs: WrappedVerifiableCredential[] = CredentialMapper.toWrappedVerifiableCredentials(vp.verifiableCredential)
|
|
56
|
+
|
|
57
|
+
// todo: We probably want to add proofs as well
|
|
58
|
+
return {
|
|
59
|
+
type,
|
|
60
|
+
format,
|
|
61
|
+
original,
|
|
62
|
+
decoded,
|
|
63
|
+
presentation: {
|
|
64
|
+
...vp,
|
|
65
|
+
verifiableCredential: vcs, // We overwrite the credentials with wrapped versions, making it an InternalVerifiablePresentation. Note: we keep the singular key name of the vc data model
|
|
66
|
+
},
|
|
67
|
+
vcs,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static toWrappedVerifiableCredentials(verifiableCredentials: OriginalVerifiableCredential[]): WrappedVerifiableCredential[] {
|
|
72
|
+
return verifiableCredentials.map(CredentialMapper.toWrappedVerifiableCredential)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static toWrappedVerifiableCredential(verifiableCredential: OriginalVerifiableCredential): WrappedVerifiableCredential {
|
|
76
|
+
const original = verifiableCredential
|
|
77
|
+
|
|
78
|
+
const decoded = CredentialMapper.decodeVerifiableCredential(verifiableCredential)
|
|
79
|
+
|
|
80
|
+
const isJwtEncoded = CredentialMapper.isJwtEncoded(original)
|
|
81
|
+
const isJwtDecoded = CredentialMapper.isJwtDecodedCredential(original)
|
|
82
|
+
const type = isJwtEncoded ? OriginalType.JWT_ENCODED : isJwtDecoded ? OriginalType.JWT_DECODED : OriginalType.JSONLD
|
|
83
|
+
|
|
84
|
+
const credential =
|
|
85
|
+
isJwtDecoded || isJwtDecoded
|
|
86
|
+
? CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded as JwtDecodedVerifiableCredential)
|
|
87
|
+
: (decoded as ICredential)
|
|
88
|
+
const format = isJwtDecoded || isJwtDecoded ? 'jwt_vc' : 'ldp_vc'
|
|
89
|
+
//todo: We probably want to add proofs as well
|
|
90
|
+
return {
|
|
91
|
+
original,
|
|
92
|
+
decoded,
|
|
93
|
+
format,
|
|
94
|
+
type,
|
|
95
|
+
credential,
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public static isJwtEncoded(original: OriginalVerifiableCredential | OriginalVerifiablePresentation) {
|
|
100
|
+
return ObjectUtils.isString(original) && (original as string).startsWith('ey')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private static isJsonLdString(original: OriginalVerifiableCredential | OriginalVerifiablePresentation) {
|
|
104
|
+
return ObjectUtils.isString(original) && (original as string).includes('@context')
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public static isJwtDecodedCredential(original: OriginalVerifiableCredential): boolean {
|
|
108
|
+
return (<JwtDecodedVerifiableCredential>original)['vc'] !== undefined && (<JwtDecodedVerifiableCredential>original)['iss'] !== undefined
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public static isJwtDecodedPresentation(original: OriginalVerifiablePresentation): boolean {
|
|
112
|
+
return (<JwtDecodedVerifiablePresentation>original)['vp'] !== undefined && (<JwtDecodedVerifiablePresentation>original)['iss'] !== undefined
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static jwtEncodedPresentationToUniformPresentation(jwt: string, makeCredentialsUniform: boolean = true): IPresentation {
|
|
116
|
+
return CredentialMapper.jwtDecodedPresentationToUniformPresentation(jwt_decode(jwt), makeCredentialsUniform)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static jwtDecodedPresentationToUniformPresentation(
|
|
120
|
+
decoded: JwtDecodedVerifiablePresentation,
|
|
121
|
+
makeCredentialsUniform: boolean = true
|
|
122
|
+
): IPresentation {
|
|
123
|
+
const presentation: IPresentation = {
|
|
124
|
+
...(decoded.vp as IPresentation),
|
|
125
|
+
}
|
|
126
|
+
if (makeCredentialsUniform) {
|
|
127
|
+
presentation.verifiableCredential = decoded.vp.verifiableCredential.map(CredentialMapper.toUniformCredential) as IVerifiableCredential[] // We cast it because we IPresentation needs a VC. The internal Credential doesn't have the required Proof anymore (that is intended)
|
|
128
|
+
}
|
|
129
|
+
// Since this is a presentation, we delete any proof just to be sure (should not occur on JWT, but better safe than sorry)
|
|
130
|
+
delete presentation.proof
|
|
131
|
+
|
|
132
|
+
if (decoded.iss) {
|
|
133
|
+
const holder = presentation.holder
|
|
134
|
+
if (holder) {
|
|
135
|
+
if (holder !== decoded.iss) {
|
|
136
|
+
throw new Error(`Inconsistent holders between JWT claim (${decoded.iss}) and VC value (${holder})`)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
presentation.holder = decoded.iss
|
|
140
|
+
}
|
|
141
|
+
if (decoded.jti) {
|
|
142
|
+
const id = presentation.id
|
|
143
|
+
if (id && id !== decoded.jti) {
|
|
144
|
+
throw new Error(`Inconsistent VP ids between JWT claim (${decoded.jti}) and VP value (${id})`)
|
|
145
|
+
}
|
|
146
|
+
presentation.id = decoded.jti
|
|
147
|
+
}
|
|
148
|
+
return presentation
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static toUniformCredential(verifiableCredential: OriginalVerifiableCredential): ICredential {
|
|
152
|
+
const original = verifiableCredential
|
|
153
|
+
const decoded = CredentialMapper.decodeVerifiableCredential(verifiableCredential)
|
|
154
|
+
|
|
155
|
+
const isJwtEncoded: boolean = CredentialMapper.isJwtEncoded(original)
|
|
156
|
+
const isJwtDecoded: boolean = CredentialMapper.isJwtDecodedCredential(original)
|
|
157
|
+
|
|
158
|
+
if (isJwtDecoded || isJwtEncoded) {
|
|
159
|
+
return CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded as JwtDecodedVerifiableCredential)
|
|
160
|
+
} else {
|
|
161
|
+
return decoded as ICredential
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static toUniformPresentation(presentation: OriginalVerifiablePresentation): IPresentation {
|
|
166
|
+
const original = presentation
|
|
167
|
+
const decoded = CredentialMapper.decodeVerifiablePresentation(original)
|
|
168
|
+
const isJwtEncoded: boolean = CredentialMapper.isJwtEncoded(original)
|
|
169
|
+
const isJwtDecoded: boolean = CredentialMapper.isJwtDecodedPresentation(original)
|
|
170
|
+
const uniformPresentation =
|
|
171
|
+
isJwtEncoded || isJwtDecoded
|
|
172
|
+
? CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded as JwtDecodedVerifiablePresentation, false)
|
|
173
|
+
: (decoded as IPresentation)
|
|
174
|
+
uniformPresentation.verifiableCredential = uniformPresentation.verifiableCredential?.map(
|
|
175
|
+
CredentialMapper.toUniformCredential
|
|
176
|
+
) as IVerifiableCredential[] // We cast it because we IPresentation needs a VC. The internal Credential doesn't have the required Proof anymore (that is intended)
|
|
177
|
+
return uniformPresentation
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
static jwtEncodedCredentialToUniformCredential(jwt: string): ICredential {
|
|
181
|
+
return CredentialMapper.jwtDecodedCredentialToUniformCredential(jwt_decode(jwt))
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
static jwtDecodedCredentialToUniformCredential(decoded: JwtDecodedVerifiableCredential): ICredential {
|
|
185
|
+
const credential: ICredential = {
|
|
186
|
+
...(decoded.vc as ICredential),
|
|
187
|
+
}
|
|
188
|
+
// Since this is a credential, we delete any proof just to be sure (should not occur on JWT, but better safe than sorry)
|
|
189
|
+
delete credential.proof
|
|
190
|
+
|
|
191
|
+
if (decoded.exp) {
|
|
192
|
+
const expDate = credential.credentialSubject?.expirationDate
|
|
193
|
+
const jwtExp = parseInt(decoded.exp.toString())
|
|
194
|
+
// fix seconds to millisecs for the date
|
|
195
|
+
const expDateAsStr = jwtExp < 9999999999 ? new Date(jwtExp * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtExp).toISOString()
|
|
196
|
+
if (expDate && expDate !== expDateAsStr) {
|
|
197
|
+
throw new Error(`Inconsistent expiration dates between JWT claim (${expDateAsStr}) and VC value (${expDate})`)
|
|
198
|
+
}
|
|
199
|
+
credential.credentialSubject.expirationDate = expDateAsStr
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (decoded.nbf) {
|
|
203
|
+
const issuanceDate = credential.issuanceDate
|
|
204
|
+
const jwtNbf = parseInt(decoded.nbf.toString())
|
|
205
|
+
// fix seconds to millisecs for the date
|
|
206
|
+
const nbfDateAsStr = jwtNbf < 9999999999 ? new Date(jwtNbf * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtNbf).toISOString()
|
|
207
|
+
if (issuanceDate && issuanceDate !== nbfDateAsStr) {
|
|
208
|
+
throw new Error(`Inconsistent issuance dates between JWT claim (${nbfDateAsStr}) and VC value (${issuanceDate})`)
|
|
209
|
+
}
|
|
210
|
+
credential.issuanceDate = nbfDateAsStr
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (decoded.iss) {
|
|
214
|
+
const issuer = credential.issuer
|
|
215
|
+
if (issuer) {
|
|
216
|
+
if (typeof issuer === 'string') {
|
|
217
|
+
if (issuer !== decoded.iss) {
|
|
218
|
+
throw new Error(`Inconsistent issuers between JWT claim (${decoded.iss}) and VC value (${issuer})`)
|
|
219
|
+
}
|
|
220
|
+
} else {
|
|
221
|
+
if (issuer.id !== decoded.iss) {
|
|
222
|
+
throw new Error(`Inconsistent issuers between JWT claim (${decoded.iss}) and VC value (${issuer.id})`)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
credential.issuer = decoded.iss
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (decoded.sub) {
|
|
230
|
+
const csId = credential.credentialSubject?.id
|
|
231
|
+
if (csId && csId !== decoded.sub) {
|
|
232
|
+
throw new Error(`Inconsistent credential subject ids between JWT claim (${decoded.sub}) and VC value (${csId})`)
|
|
233
|
+
}
|
|
234
|
+
credential.credentialSubject.id = decoded.sub
|
|
235
|
+
}
|
|
236
|
+
if (decoded.jti) {
|
|
237
|
+
const id = credential.id
|
|
238
|
+
if (id && id !== decoded.jti) {
|
|
239
|
+
throw new Error(`Inconsistent credential ids between JWT claim (${decoded.jti}) and VC value (${id})`)
|
|
240
|
+
}
|
|
241
|
+
credential.id = decoded.jti
|
|
242
|
+
}
|
|
243
|
+
return credential
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './credential-mapper'
|
package/src/types/did.ts
CHANGED
|
@@ -33,3 +33,51 @@ export interface IParsedDID {
|
|
|
33
33
|
[index: string]: string
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
+
|
|
37
|
+
export const parseDid = (did: string): IParsedDID => {
|
|
38
|
+
const parsedDid = parse(did)
|
|
39
|
+
if (parsedDid === null) {
|
|
40
|
+
throw new Error('invalid did')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return parsedDid
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const parse = (didUrl: string): IParsedDID | null => {
|
|
47
|
+
const PCT_ENCODED = '(?:%[0-9a-fA-F]{2})'
|
|
48
|
+
const ID_CHAR = `(?:[a-zA-Z0-9._-]|${PCT_ENCODED})`
|
|
49
|
+
const METHOD = '([a-z0-9]+)'
|
|
50
|
+
const METHOD_ID = `((?:${ID_CHAR}*:)*(${ID_CHAR}+))`
|
|
51
|
+
const PARAM_CHAR = '[a-zA-Z0-9_.:%-]'
|
|
52
|
+
const PARAM = `;${PARAM_CHAR}+=${PARAM_CHAR}*`
|
|
53
|
+
const PARAMS = `((${PARAM})*)`
|
|
54
|
+
const PATH = `(/[^#?]*)?`
|
|
55
|
+
const QUERY = `([?][^#]*)?`
|
|
56
|
+
const FRAGMENT = `(#.*)?`
|
|
57
|
+
const DID_MATCHER = new RegExp(`^did:${METHOD}:${METHOD_ID}${PARAMS}${PATH}${QUERY}${FRAGMENT}$`)
|
|
58
|
+
|
|
59
|
+
if (didUrl === '' || !didUrl) return null
|
|
60
|
+
const sections = didUrl.match(DID_MATCHER)
|
|
61
|
+
if (sections) {
|
|
62
|
+
const parts: IParsedDID = {
|
|
63
|
+
did: `did:${sections[1]}:${sections[2]}`,
|
|
64
|
+
method: sections[1],
|
|
65
|
+
id: sections[2],
|
|
66
|
+
didUrl,
|
|
67
|
+
}
|
|
68
|
+
if (sections[4]) {
|
|
69
|
+
const params = sections[4].slice(1).split(';')
|
|
70
|
+
parts.params = {}
|
|
71
|
+
for (const p of params) {
|
|
72
|
+
const kv = p.split('=')
|
|
73
|
+
parts.params[kv[0]] = kv[1]
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (sections[6]) parts.path = sections[6]
|
|
77
|
+
if (sections[7]) parts.query = sections[7].slice(1)
|
|
78
|
+
if (sections[8]) parts.fragment = sections[8].slice(1)
|
|
79
|
+
return parts
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return null
|
|
83
|
+
}
|
package/src/types/pex.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* It
|
|
2
|
+
* It expresses how the inputs are presented as proofs to a Verifier.
|
|
3
3
|
*/
|
|
4
4
|
export interface PresentationSubmission {
|
|
5
5
|
/**
|
|
@@ -16,18 +16,6 @@ export interface PresentationSubmission {
|
|
|
16
16
|
descriptor_map: Array<Descriptor>
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
/**
|
|
20
|
-
* Presentation Echange API
|
|
21
|
-
* Presentation Exchange API
|
|
22
|
-
*
|
|
23
|
-
* The version of the OpenAPI document: 1.1.0
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
27
|
-
* https://openapi-generator.tech
|
|
28
|
-
* Do not edit the class manually.
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
19
|
/**
|
|
32
20
|
* descriptor map laying out the structure of the presentation submission.
|
|
33
21
|
*/
|
package/src/types/vc.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { PresentationSubmission } from './pex'
|
|
2
2
|
import { IProofPurpose, IProofType } from './did'
|
|
3
3
|
|
|
4
|
+
export type AdditionalClaims = Record<string, any>
|
|
5
|
+
|
|
4
6
|
export interface ICredential {
|
|
5
7
|
// If exp is present, the UNIX timestamp MUST be converted to an [XMLSCHEMA11-2] date-time, and MUST be used to set the value of the expirationDate property of credentialSubject of the new JSON object.
|
|
6
8
|
expirationDate?: string
|
|
@@ -9,9 +11,9 @@ export interface ICredential {
|
|
|
9
11
|
// If nbf is present, the UNIX timestamp MUST be converted to an [XMLSCHEMA11-2] date-time, and MUST be used to set the value of the issuanceDate property of the new JSON object.
|
|
10
12
|
issuanceDate: string
|
|
11
13
|
// If sub is present, the value MUST be used to set the value of the id property of credentialSubject of the new credential JSON object.
|
|
12
|
-
credentialSubject: ICredentialSubject
|
|
14
|
+
credentialSubject: ICredentialSubject & AdditionalClaims
|
|
13
15
|
// If jti is present, the value MUST be used to set the value of the id property of the new JSON object.
|
|
14
|
-
id
|
|
16
|
+
id?: string
|
|
15
17
|
'@context': ICredentialContextType[] | ICredentialContextType
|
|
16
18
|
credentialStatus?: ICredentialStatus
|
|
17
19
|
credentialSchema?: undefined | ICredentialSchemaType | ICredentialSchemaType[]
|
|
@@ -19,21 +21,18 @@ export interface ICredential {
|
|
|
19
21
|
name?: string
|
|
20
22
|
type: string[]
|
|
21
23
|
|
|
22
|
-
[x: string]:
|
|
24
|
+
[x: string]: any
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
export interface ICredentialSubject {
|
|
26
28
|
id?: string
|
|
27
|
-
|
|
28
|
-
[x: string]: unknown
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
export type ICredentialContextType = ICredentialContext | string
|
|
31
|
+
export type ICredentialContextType = (ICredentialContext & AdditionalClaims) | string
|
|
32
32
|
|
|
33
33
|
export interface ICredentialContext {
|
|
34
34
|
name?: string
|
|
35
35
|
did?: string
|
|
36
|
-
[x: string]: unknown
|
|
37
36
|
}
|
|
38
37
|
|
|
39
38
|
export type ICredentialSchemaType = ICredentialSchema | string
|
|
@@ -54,8 +53,6 @@ export interface IProof {
|
|
|
54
53
|
jws?: string // JWS based proof
|
|
55
54
|
nonce?: string // Similar to challenge. A nonce to protect against replay attacks, used in some ZKP proofs
|
|
56
55
|
requiredRevealStatements?: string[] // The parts of the proof that must be revealed in a derived proof
|
|
57
|
-
|
|
58
|
-
[x: string]: string | string[] | undefined
|
|
59
56
|
}
|
|
60
57
|
|
|
61
58
|
export interface ICredentialStatus {
|
|
@@ -66,7 +63,7 @@ export interface ICredentialStatus {
|
|
|
66
63
|
export interface IIssuer {
|
|
67
64
|
id: string
|
|
68
65
|
|
|
69
|
-
[x: string]:
|
|
66
|
+
[x: string]: any
|
|
70
67
|
}
|
|
71
68
|
|
|
72
69
|
export interface IHasProof {
|
|
@@ -76,11 +73,14 @@ export interface IHasProof {
|
|
|
76
73
|
export type IVerifiableCredential = ICredential & IHasProof
|
|
77
74
|
|
|
78
75
|
export interface IPresentation {
|
|
76
|
+
id?: string
|
|
79
77
|
'@context': ICredentialContextType | ICredentialContextType[]
|
|
80
78
|
type: string[]
|
|
81
|
-
verifiableCredential: IVerifiableCredential[]
|
|
79
|
+
verifiableCredential: IVerifiableCredential[] // we relax to ICredential for internal decoded stable representations without proofs
|
|
82
80
|
presentation_submission?: PresentationSubmission
|
|
83
81
|
holder?: string
|
|
82
|
+
|
|
83
|
+
[x: string]: any
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
export type IVerifiablePresentation = IPresentation & IHasProof
|
|
@@ -89,36 +89,59 @@ export interface WrappedVerifiableCredential {
|
|
|
89
89
|
/**
|
|
90
90
|
* Original VC that we've received
|
|
91
91
|
*/
|
|
92
|
-
original:
|
|
92
|
+
original: OriginalVerifiableCredential
|
|
93
93
|
/**
|
|
94
94
|
* In case of JWT credential it will be the decoded version. In other cases it will be the same as original one
|
|
95
95
|
*/
|
|
96
|
-
decoded:
|
|
96
|
+
decoded: JwtDecodedVerifiableCredential | IVerifiableCredential
|
|
97
97
|
/**
|
|
98
|
-
* Type of this credential. Supported types are json-ld and jwt
|
|
98
|
+
* Type of this credential. Supported types are json-ld and jwt (decoded/encoded)
|
|
99
99
|
*/
|
|
100
|
-
type:
|
|
100
|
+
type: OriginalType
|
|
101
101
|
/**
|
|
102
|
-
*
|
|
102
|
+
* The claim format, typically used during exchange transport protocols
|
|
103
103
|
*/
|
|
104
|
-
|
|
104
|
+
format: ClaimFormat
|
|
105
|
+
/**
|
|
106
|
+
* Internal stable representation of a Credential without Proofs, created based on https://www.w3.org/TR/vc-data-model/#jwt-decoding
|
|
107
|
+
*/
|
|
108
|
+
credential: ICredential
|
|
105
109
|
}
|
|
106
110
|
|
|
107
111
|
export interface WrappedVerifiablePresentation {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Original VP that we've received
|
|
114
|
+
*/
|
|
115
|
+
original: OriginalVerifiablePresentation
|
|
116
|
+
/**
|
|
117
|
+
* In case of JWT VP it will be the decoded version. In other cases it will be the same as original one
|
|
118
|
+
*/
|
|
119
|
+
decoded: JwtDecodedVerifiablePresentation | IVerifiablePresentation
|
|
120
|
+
/**
|
|
121
|
+
* Type of this Presentation. Supported types are json-ld and jwt (decoded/encoded)
|
|
122
|
+
*/
|
|
123
|
+
type: OriginalType
|
|
124
|
+
/**
|
|
125
|
+
* The claim format, typically used during exchange transport protocols
|
|
126
|
+
*/
|
|
127
|
+
format: ClaimFormat
|
|
128
|
+
/**
|
|
129
|
+
* Internal stable representation of a Presentation without proofs, created based on https://www.w3.org/TR/vc-data-model/#jwt-decoding
|
|
130
|
+
*/
|
|
131
|
+
presentation: UniformVerifiablePresentation
|
|
132
|
+
/**
|
|
133
|
+
* Wrapped Verifiable Credentials belonging to the Presentation
|
|
134
|
+
*/
|
|
112
135
|
vcs: WrappedVerifiableCredential[]
|
|
113
136
|
}
|
|
114
137
|
|
|
115
|
-
export enum
|
|
116
|
-
JSONLD,
|
|
117
|
-
JWT_ENCODED,
|
|
118
|
-
JWT_DECODED,
|
|
138
|
+
export enum OriginalType {
|
|
139
|
+
JSONLD = 'json-ld',
|
|
140
|
+
JWT_ENCODED = 'jwt-encoded',
|
|
141
|
+
JWT_DECODED = 'jwt-decoded',
|
|
119
142
|
}
|
|
120
143
|
|
|
121
|
-
export interface
|
|
144
|
+
export interface UniformVerifiablePresentation {
|
|
122
145
|
'@context': ICredentialContextType | ICredentialContextType[]
|
|
123
146
|
type: string[]
|
|
124
147
|
verifiableCredential: WrappedVerifiableCredential[]
|
|
@@ -126,20 +149,30 @@ export interface InternalPresentation {
|
|
|
126
149
|
holder?: string
|
|
127
150
|
}
|
|
128
151
|
|
|
129
|
-
export interface
|
|
152
|
+
export interface JwtDecodedVerifiableCredential {
|
|
130
153
|
vc: ICredential
|
|
131
154
|
exp: string
|
|
132
155
|
iss: string
|
|
133
156
|
nbf: string
|
|
134
157
|
sub: string
|
|
135
158
|
jti: string
|
|
159
|
+
|
|
160
|
+
[x: string]: any
|
|
136
161
|
}
|
|
137
162
|
|
|
138
|
-
export interface
|
|
163
|
+
export interface JwtDecodedVerifiablePresentation {
|
|
139
164
|
vp: IVerifiablePresentation
|
|
140
165
|
exp: string
|
|
141
166
|
iss: string
|
|
142
167
|
nbf: string
|
|
143
168
|
sub: string
|
|
144
169
|
jti: string
|
|
170
|
+
|
|
171
|
+
[x: string]: any
|
|
145
172
|
}
|
|
173
|
+
|
|
174
|
+
export type ClaimFormat = 'jwt' | 'jwt_vc' | 'jwt_vp' | 'ldp' | 'ldp_vc' | 'ldp_vp' | string
|
|
175
|
+
|
|
176
|
+
export type OriginalVerifiableCredential = IVerifiableCredential | JwtDecodedVerifiableCredential | string
|
|
177
|
+
export type OriginalVerifiablePresentation = IPresentation | JwtDecodedVerifiablePresentation | string
|
|
178
|
+
export type Original = OriginalVerifiablePresentation | OriginalVerifiableCredential
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './object'
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class ObjectUtils {
|
|
2
|
+
public static asArray(value: unknown) {
|
|
3
|
+
return Array.isArray(value) ? value : [value]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
public static isObject(value: unknown) {
|
|
7
|
+
return Object.prototype.toString.call(value) === '[object Object]'
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public static isUrlAbsolute(url: string) {
|
|
11
|
+
// regex to check for absolute IRI (starting scheme and ':') or blank node IRI
|
|
12
|
+
const isAbsoluteRegex = /^([A-Za-z][A-Za-z0-9+-.]*|_):[^\s]*$/
|
|
13
|
+
ObjectUtils.isString(url) && isAbsoluteRegex.test(url)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public static isString(value: unknown): boolean {
|
|
17
|
+
return typeof value === 'string' || Object.prototype.toString.call(value) === '[object String]'
|
|
18
|
+
}
|
|
19
|
+
}
|