-
Notifications
You must be signed in to change notification settings - Fork 15
/
Holder.ts
112 lines (98 loc) · 3.24 KB
/
Holder.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
import { Connection, ConnectionInvitation, IssueCredentialRecord } from "@input-output-hk/prism-typescript-client";
import { Actor } from "./Actor";
import { HOLDER_AGENT_API_KEY, HOLDER_AGENT_URL } from "../common/Config";
export class Holder extends Actor {
/**
* The connection with the Issuer.
*/
connectionWithIssuer: Connection | undefined;
/**
* The connection with the Verifier.
*/
connectionWithVerifier: Connection | undefined;
/**
* The credential issued by the issuer.
*/
credential: IssueCredentialRecord | undefined;
/**
* The DID template used to create an unpublished DID for Holder.
* Authentication is the only purpose for the public key required for Holder.
*/
holderDidTemplate: string = `{
"documentTemplate": {
"publicKeys": [
{
"id": "authKey",
"purpose": "authentication"
}
],
"services": []
}
}`;
/**
* Creates a new instance of Holder.
*/
constructor() {
super(HOLDER_AGENT_URL, HOLDER_AGENT_API_KEY);
}
/**
* Creates an unpublished DID.
*/
createUnpublishedDid() {
this.did = this.didService.createUnpublishedDid(this.holderDidTemplate).longFormDid;
}
/**
* Accepts a connection invitation from an issuer.
* @param {ConnectionInvitation} invitation - The connection invitation.
*/
acceptIssuerConnection(invitation: ConnectionInvitation) {
this.connectionWithIssuer = this.connectionService.acceptConnectionInvitation(invitation);
}
/**
* Accepts a connection invitation from a verifier.
* @param {ConnectionInvitation} invitation - The connection invitation.
*/
acceptVerifierConnection(invitation: ConnectionInvitation) {
this.connectionWithVerifier = this.connectionService.acceptConnectionInvitation(invitation);
}
/**
* Waits for the connection with the issuer to be finalized.
*/
finalizeConnectionWithIssuer() {
this.connectionService.waitForConnectionState(
this.connectionWithIssuer!,
"ConnectionResponseReceived"
);
}
/**
* Waits for the connection with the verifier to be finalized.
*/
finalizeConnectionWithVerifier() {
this.connectionService.waitForConnectionState(
this.connectionWithVerifier!,
"ConnectionResponseReceived"
);
}
/**
* Waits for a credential offer and accepts it.
*/
waitAndAcceptCredentialOffer(thid: string) {
this.credential = this.credentialsService.waitForCredentialOffer(thid);
this.credentialsService.acceptCredentialOffer(this.credential, this.did!);
this.credentialsService.waitForCredentialState(this.credential, "RequestSent");
}
/**
* Waits for the credential to be received.
*/
receiveCredential() {
this.credentialsService.waitForCredentialState(this.credential!, "CredentialReceived");
}
/**
* Waits for a proof request, accepts it, and waits for the presentation to be sent.
*/
waitAndAcceptProofRequest(thid: string) {
const presentation = this.proofsService.waitForProof(thid);
this.proofsService.acceptProofRequest(presentation, this.credential!.recordId);
this.proofsService.waitForPresentationState(presentation.presentationId, "PresentationSent");
}
}