-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcredential-flow.ts
73 lines (70 loc) · 3.15 KB
/
credential-flow.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
import { CredentialPayload, IDIDManager, IIdentifier, TAgent, VerifiableCredential } from "@veramo/core"
import { ICredentialIssuer } from "@veramo/credential-w3c"
import { MY_CUSTOM_CONTEXT_URI } from "./setup"
/**
* Create a managed DID using the `defaultProvider` configured in ./setup.ts (did:key)
* @param agent
*/
export async function createDID(agent: TAgent<IDIDManager>): Promise<IIdentifier> {
const identifier = await agent.didManagerCreate()
return identifier
}
export async function createEthrDID(agent: TAgent<IDIDManager>): Promise<IIdentifier> {
const identifier = await agent.didManagerCreate({provider:"did:ethr:goerli"})
return identifier
}
/**
* Issue a JSON-LD Verifiable Credential using the DID managed by the agent
*
* The agent was initialized with a `CredentialIssuer` and `CredentialIssuerLD` plugins (See ./setup.ts) which provide
* the `createVerifiableCredential` functionality. They internally rely on the `DIDResolver`, `KeyManager`, and
* `DIDManager` plugins that are used to map the issuer of the `CredentialPayload` to a `VerificationMethod` in the
* issuer `DID Document` and to a signing key managed by the agent.
*
* @param issuer
* @param agent
*/
export async function createLDCredential(issuer: IIdentifier, agent: TAgent<ICredentialIssuer>): Promise<VerifiableCredential> {
const credential: CredentialPayload = {
'@context': [MY_CUSTOM_CONTEXT_URI],
issuer: issuer.did,
credentialSubject: {
"nothing": "else matters" // the `nothing` property is defined in the custom context (See ./setup.ts)
}
}
const verifiableCredential = await agent.createVerifiableCredential({
credential,
proofFormat: 'lds' // use LD Signatures as proof
})
return verifiableCredential
}
export async function createLDCredentialWithEthrIssuer(issuer: IIdentifier, agent: TAgent<ICredentialIssuer>): Promise<VerifiableCredential> {
const credential: CredentialPayload = {
'@context': [MY_CUSTOM_CONTEXT_URI],
issuer: issuer.did,
credentialSubject: {
"nothing": "else matters" // the `nothing` property is defined in the custom context (See ./setup.ts)
}
}
const verifiableCredential = await agent.createVerifiableCredential({
credential,
proofFormat: 'lds' // use LD Signatures as proof
})
return verifiableCredential
}
/**
* Verify a credential using the agent.
*
* The agent was initialized with the `CredentialIssuer` and `CredentialIssuerLD` plugins (See ./setup.ts) which
* perform the actual verification. These plugins use the `DIDResolver` plugin to automatically resolve the `DID
* Document` of the credential issuer during verification to obtain the verification method data specified by the
* `proof` property of the credential.
*
* Note: For the credential issued with a did:ethr, the easiest method is to add VeramoEcdsaSecp256k1RecoverySignature2020 in your agent setup. Else you won't be able to actually verify the credential.
* @param credential
* @param agent
*/
export async function verifyLDCredential(credential: VerifiableCredential, agent: TAgent<ICredentialIssuer>): Promise<boolean> {
const verified = await agent.verifyCredential({ credential })
return verified
}