/
CredentialEIP712.ts
380 lines (322 loc) · 11.6 KB
/
CredentialEIP712.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import {
CredentialPayload,
IAgentPlugin,
IIdentifier,
IKey,
PresentationPayload,
VerifiableCredential,
VerifiablePresentation,
} from '@veramo/core-types'
import {
extractIssuer,
getChainId,
getEthereumAddress,
intersect,
isDefined,
MANDATORY_CREDENTIAL_CONTEXT,
mapIdentifierKeysToDoc,
processEntryToArray,
removeDIDParameters,
resolveDidOrThrow,
} from '@veramo/utils'
import { schema } from '../plugin.schema.js'
import { recoverTypedSignature, SignTypedDataVersion } from '@metamask/eth-sig-util'
import {
ICreateVerifiableCredentialEIP712Args,
ICreateVerifiablePresentationEIP712Args,
ICredentialIssuerEIP712,
IRequiredContext,
IVerifyCredentialEIP712Args,
IVerifyPresentationEIP712Args,
} from '../types/ICredentialEIP712.js'
import { getEthTypesFromInputDoc } from 'eip-712-types-generation'
/**
* A Veramo plugin that implements the {@link ICredentialIssuerEIP712} methods.
*
* @beta This API may change without a BREAKING CHANGE notice.
*/
export class CredentialIssuerEIP712 implements IAgentPlugin {
readonly methods: ICredentialIssuerEIP712
readonly schema = schema.ICredentialIssuerEIP712
constructor() {
this.methods = {
createVerifiableCredentialEIP712: this.createVerifiableCredentialEIP712.bind(this),
createVerifiablePresentationEIP712: this.createVerifiablePresentationEIP712.bind(this),
verifyCredentialEIP712: this.verifyCredentialEIP712.bind(this),
verifyPresentationEIP712: this.verifyPresentationEIP712.bind(this),
matchKeyForEIP712: this.matchKeyForEIP712.bind(this),
}
}
/** {@inheritdoc ICredentialIssuerEIP712.createVerifiableCredentialEIP712} */
public async createVerifiableCredentialEIP712(
args: ICreateVerifiableCredentialEIP712Args,
context: IRequiredContext,
): Promise<VerifiableCredential> {
const credentialContext = processEntryToArray(
args?.credential?.['@context'],
MANDATORY_CREDENTIAL_CONTEXT,
)
const credentialType = processEntryToArray(args?.credential?.type, 'VerifiableCredential')
let issuanceDate = args?.credential?.issuanceDate || new Date().toISOString()
if (issuanceDate instanceof Date) {
issuanceDate = issuanceDate.toISOString()
}
const issuer = extractIssuer(args.credential, { removeParameters: true })
if (!issuer || typeof issuer === 'undefined') {
throw new Error('invalid_argument: credential.issuer must not be empty')
}
let keyRef = args.keyRef
const identifier = await context.agent.didManagerGet({ did: issuer })
if (!keyRef) {
const key = identifier.keys.find(
(k) => k.type === 'Secp256k1' && k.meta?.algorithms?.includes('eth_signTypedData'),
)
if (!key) throw Error('key_not_found: No suitable signing key is known for ' + identifier.did)
keyRef = key.kid
}
const extendedKeys = await mapIdentifierKeysToDoc(
identifier,
'verificationMethod',
context,
args.resolutionOptions,
)
const extendedKey = extendedKeys.find((key) => key.kid === keyRef)
if (!extendedKey)
throw Error('key_not_found: The signing key is not available in the issuer DID document')
let chainId
try {
chainId = getChainId(extendedKey.meta.verificationMethod)
} catch (e) {
chainId = 1
}
const credential: CredentialPayload = {
...args?.credential,
'@context': credentialContext,
type: credentialType,
issuanceDate,
proof: {
verificationMethod: extendedKey.meta.verificationMethod.id,
created: issuanceDate,
proofPurpose: 'assertionMethod',
type: 'EthereumEip712Signature2021',
},
}
const message = credential
const domain = {
chainId,
name: 'VerifiableCredential',
version: '1',
}
const primaryType = 'VerifiableCredential'
const allTypes = getEthTypesFromInputDoc(credential, primaryType)
const types = { ...allTypes }
const data = JSON.stringify({ domain, types, message, primaryType })
const signature = await context.agent.keyManagerSign({ keyRef, data, algorithm: 'eth_signTypedData' })
credential['proof']['proofValue'] = signature
credential['proof']['eip712'] = {
domain,
types: allTypes,
primaryType,
}
return credential as VerifiableCredential
}
/** {@inheritdoc ICredentialIssuerEIP712.verifyCredentialEIP712} */
private async verifyCredentialEIP712(
args: IVerifyCredentialEIP712Args,
context: IRequiredContext,
): Promise<boolean> {
const { credential } = args
if (!credential.proof || !credential.proof.proofValue)
throw new Error('invalid_argument: proof is undefined')
const { proof, ...signingInput } = credential
const { proofValue, eip712, eip712Domain, ...verifyInputProof } = proof
const verificationMessage = {
...signingInput,
proof: verifyInputProof,
}
const compat = {
...eip712Domain,
...eip712,
}
compat.types = compat.types || compat.messageSchema
if (!compat.primaryType || !compat.types || !compat.domain)
throw new Error('invalid_argument: proof is missing expected properties')
const objectToVerify = {
message: verificationMessage,
domain: compat.domain,
types: compat.types,
primaryType: compat.primaryType,
}
const recovered = recoverTypedSignature({
data: objectToVerify,
signature: proofValue,
version: SignTypedDataVersion.V4,
})
const issuer = extractIssuer(credential)
if (!issuer || typeof issuer === 'undefined') {
throw new Error('invalid_argument: credential.issuer must not be empty')
}
const didDocument = await resolveDidOrThrow(issuer, context, args.resolutionOptions)
if (didDocument.verificationMethod) {
for (const verificationMethod of didDocument.verificationMethod) {
if (getEthereumAddress(verificationMethod)?.toLowerCase() === recovered.toLowerCase()) {
return true
}
}
} else {
throw new Error('resolver_error: issuer DIDDocument does not contain any verificationMethods')
}
return false
}
/** {@inheritdoc ICredentialIssuerEIP712.createVerifiablePresentationEIP712} */
async createVerifiablePresentationEIP712(
args: ICreateVerifiablePresentationEIP712Args,
context: IRequiredContext,
): Promise<VerifiablePresentation> {
const presentationContext = processEntryToArray(
args?.presentation?.['@context'],
MANDATORY_CREDENTIAL_CONTEXT,
)
const presentationType = processEntryToArray(args?.presentation?.type, 'VerifiablePresentation')
let issuanceDate = args?.presentation?.issuanceDate || new Date().toISOString()
if (issuanceDate instanceof Date) {
issuanceDate = issuanceDate.toISOString()
}
const presentation: PresentationPayload = {
...args?.presentation,
'@context': presentationContext,
type: presentationType,
issuanceDate,
}
if (!isDefined(args.presentation.holder)) {
throw new Error('invalid_argument: presentation.holder must not be empty')
}
if (args.presentation.verifiableCredential) {
// EIP712 arrays must use a single data type, so we map all credentials to strings.
presentation.verifiableCredential = args.presentation.verifiableCredential.map((cred) => {
// map JWT credentials to their canonical form
if (typeof cred === 'string') {
return cred
} else if (cred.proof.jwt) {
return cred.proof.jwt
} else {
return JSON.stringify(cred)
}
})
}
const holder = removeDIDParameters(presentation.holder)
let identifier: IIdentifier
try {
identifier = await context.agent.didManagerGet({ did: holder })
} catch (e) {
throw new Error('invalid_argument: presentation.holder must be a DID managed by this agent')
}
let keyRef = args.keyRef
if (!keyRef) {
const key = identifier.keys.find(
(k) => k.type === 'Secp256k1' && k.meta?.algorithms?.includes('eth_signTypedData'),
)
if (!key) throw Error('key_not_found: No suitable signing key is known for ' + identifier.did)
keyRef = key.kid
}
const extendedKeys = await mapIdentifierKeysToDoc(
identifier,
'verificationMethod',
context,
args.resolutionOptions,
)
const extendedKey = extendedKeys.find((key) => key.kid === keyRef)
if (!extendedKey)
throw Error('key_not_found: The signing key is not available in the issuer DID document')
let chainId
try {
chainId = getChainId(extendedKey.meta.verificationMethod)
} catch (e) {
chainId = 1
}
presentation['proof'] = {
verificationMethod: extendedKey.meta.verificationMethod.id,
created: issuanceDate,
proofPurpose: 'assertionMethod',
type: 'EthereumEip712Signature2021',
}
const message = presentation
const domain = {
chainId,
name: 'VerifiablePresentation',
version: '1',
}
const primaryType = 'VerifiablePresentation'
const allTypes = getEthTypesFromInputDoc(presentation, primaryType)
const types = { ...allTypes }
const data = JSON.stringify({ domain, types, message })
const signature = await context.agent.keyManagerSign({ keyRef, data, algorithm: 'eth_signTypedData' })
presentation.proof.proofValue = signature
presentation.proof.eip712 = {
domain,
types: allTypes,
primaryType,
}
return presentation as VerifiablePresentation
}
/** {@inheritdoc ICredentialIssuerEIP712.verifyPresentationEIP712} */
private async verifyPresentationEIP712(
args: IVerifyPresentationEIP712Args,
context: IRequiredContext,
): Promise<boolean> {
const { presentation } = args
if (!presentation.proof || !presentation.proof.proofValue) throw new Error('Proof is undefined')
const { proof, ...signingInput } = presentation
const { proofValue, eip712, eip712Domain, ...verifyInputProof } = proof
const verificationMessage = {
...signingInput,
proof: verifyInputProof,
}
const compat = {
...eip712Domain,
...eip712,
}
compat.types = compat.types || compat.messageSchema
if (!compat.primaryType || !compat.types || !compat.domain)
throw new Error('invalid_argument: presentation proof is missing expected properties')
const objectToVerify = {
message: verificationMessage,
domain: compat.domain,
types: compat.types,
primaryType: compat.primaryType,
}
const recovered = recoverTypedSignature({
data: objectToVerify,
signature: proofValue,
version: SignTypedDataVersion.V4,
})
const issuer = extractIssuer(presentation)
if (!issuer || typeof issuer === 'undefined') {
throw new Error('invalid_argument: args.presentation.issuer must not be empty')
}
const didDocument = await resolveDidOrThrow(issuer, context, args.resolutionOptions)
if (didDocument.verificationMethod) {
for (const verificationMethod of didDocument.verificationMethod) {
if (getEthereumAddress(verificationMethod)?.toLowerCase() === recovered.toLowerCase()) {
return true
}
}
} else {
throw new Error('resolver_error: holder DIDDocument does not contain any verificationMethods')
}
return false
}
/**
* Checks if a key is suitable for signing EIP712 payloads.
* This relies on the metadata set by the key management system to determine if this key can sign EIP712 payloads.
*
* @param k - the key to check
*
* @internal
*/
async matchKeyForEIP712(k: IKey): Promise<boolean> {
return (
intersect(k.meta?.algorithms ?? [], ['eth_signTypedData', 'EthereumEip712Signature2021']).length > 0
)
}
}