forked from Indicio-tech/aries-framework-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproofs.test.ts
262 lines (229 loc) · 8.2 KB
/
proofs.test.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
/* eslint-disable no-console */
import indy from 'indy-sdk';
import type { CredDefId } from 'indy-sdk';
import { Subject } from 'rxjs';
import { Agent } from '..';
import {
ensurePublicDidIsOnLedger,
makeConnection,
registerDefinition,
registerSchema,
SubjectInboundTransporter,
SubjectOutboundTransporter,
genesisPath,
issueCredential,
waitForProofRecord,
} from './helpers';
import { CredentialPreview, CredentialPreviewAttribute } from '../modules/credentials';
import { InitConfig } from '../types';
import {
PredicateType,
PresentationPreview,
PresentationPreviewAttribute,
PresentationPreviewPredicate,
ProofState,
ProofAttributeInfo,
AttributeFilter,
ProofPredicateInfo,
} from '../modules/proofs';
import { ConnectionRecord } from '../modules/connections';
import testLogger from './logger';
const faberConfig: InitConfig = {
label: 'Faber',
walletConfig: { id: 'proofs-test-faber' },
walletCredentials: { key: '00000000000000000000000000000Test01' },
publicDidSeed: process.env.TEST_AGENT_PUBLIC_DID_SEED,
autoAcceptConnections: true,
genesisPath,
poolName: 'proofs-test-faber-pool',
indy,
logger: testLogger,
};
const aliceConfig: InitConfig = {
label: 'Alice',
walletConfig: { id: 'proofs-test-alice' },
walletCredentials: { key: '00000000000000000000000000000Test01' },
autoAcceptConnections: true,
genesisPath,
poolName: 'proofs-test-alice-pool',
indy,
logger: testLogger,
};
const credentialPreview = new CredentialPreview({
attributes: [
new CredentialPreviewAttribute({
name: 'name',
mimeType: 'text/plain',
value: 'John',
}),
new CredentialPreviewAttribute({
name: 'age',
mimeType: 'text/plain',
value: '99',
}),
],
});
describe('Present Proof', () => {
let faberAgent: Agent;
let aliceAgent: Agent;
let credDefId: CredDefId;
let faberConnection: ConnectionRecord;
let aliceConnection: ConnectionRecord;
let presentationPreview: PresentationPreview;
beforeAll(async () => {
const faberMessages = new Subject();
const aliceMessages = new Subject();
const faberAgentInbound = new SubjectInboundTransporter(faberMessages);
const faberAgentOutbound = new SubjectOutboundTransporter(aliceMessages);
const aliceAgentInbound = new SubjectInboundTransporter(aliceMessages);
const aliceAgentOutbound = new SubjectOutboundTransporter(faberMessages);
faberAgent = new Agent(faberConfig, faberAgentInbound, faberAgentOutbound);
aliceAgent = new Agent(aliceConfig, aliceAgentInbound, aliceAgentOutbound);
await faberAgent.init();
await aliceAgent.init();
const schemaTemplate = {
name: `test-schema-${Date.now()}`,
attributes: ['name', 'age'],
version: '1.0',
};
const [, ledgerSchema] = await registerSchema(faberAgent, schemaTemplate);
const definitionTemplate = {
schema: ledgerSchema,
tag: 'TAG',
signatureType: 'CL',
config: { supportRevocation: false },
};
const [ledgerCredDefId] = await registerDefinition(faberAgent, definitionTemplate);
credDefId = ledgerCredDefId;
const publicDid = faberAgent.publicDid?.did;
await ensurePublicDidIsOnLedger(faberAgent, publicDid!);
const { agentAConnection, agentBConnection } = await makeConnection(faberAgent, aliceAgent);
faberConnection = agentAConnection;
aliceConnection = agentBConnection;
presentationPreview = new PresentationPreview({
attributes: [
new PresentationPreviewAttribute({
name: 'name',
credentialDefinitionId: credDefId,
referent: '0',
value: 'John',
}),
],
predicates: [
new PresentationPreviewPredicate({
name: 'age',
credentialDefinitionId: credDefId,
predicate: PredicateType.GreaterThanOrEqualTo,
threshold: 50,
}),
],
});
await issueCredential({
issuerAgent: faberAgent,
issuerConnectionId: faberConnection.id,
holderAgent: aliceAgent,
credentialTemplate: {
credentialDefinitionId: credDefId,
comment: 'some comment about credential',
preview: credentialPreview,
},
});
});
afterAll(async () => {
await faberAgent.closeAndDeleteWallet();
await aliceAgent.closeAndDeleteWallet();
});
test('Alice starts with proof proposal to Faber', async () => {
testLogger.test('Alice sends presentation proposal to Faber');
let aliceProofRecord = await aliceAgent.proofs.proposeProof(aliceConnection.id, presentationPreview);
testLogger.test('Faber waits for presentation proposal from Alice');
let faberProofRecord = await waitForProofRecord(faberAgent, {
threadId: aliceProofRecord.tags.threadId,
state: ProofState.ProposalReceived,
});
testLogger.test('Faber accepts presentation proposal from Alice');
faberProofRecord = await faberAgent.proofs.acceptProposal(faberProofRecord.id);
testLogger.test('Alice waits for presentation request from Faber');
aliceProofRecord = await waitForProofRecord(aliceAgent, {
threadId: aliceProofRecord.tags.threadId,
state: ProofState.RequestReceived,
});
testLogger.test('Alice accepts presentation request from Faber');
const indyProofRequest = aliceProofRecord.requestMessage?.indyProofRequest;
const requestedCredentials = await aliceAgent.proofs.getRequestedCredentialsForProofRequest(
indyProofRequest!,
presentationPreview
);
await aliceAgent.proofs.acceptRequest(aliceProofRecord.id, requestedCredentials);
testLogger.test('Faber waits for presentation from Alice');
faberProofRecord = await waitForProofRecord(faberAgent, {
threadId: aliceProofRecord.tags.threadId,
state: ProofState.PresentationReceived,
});
// assert presentation is valid
expect(faberProofRecord.isVerified).toBe(true);
// Faber accepts presentation
await faberAgent.proofs.acceptPresentation(faberProofRecord.id);
// Alice waits till it receives presentation ack
aliceProofRecord = await waitForProofRecord(aliceAgent, {
threadId: aliceProofRecord.tags.threadId,
state: ProofState.Done,
});
});
test('Faber starts with proof requests to Alice', async () => {
testLogger.test('Faber sends presentation request to Alice');
const attributes = {
name: new ProofAttributeInfo({
name: 'name',
restrictions: [
new AttributeFilter({
credentialDefinitionId: credDefId,
}),
],
}),
};
const predicates = {
age: new ProofPredicateInfo({
name: 'age',
predicateType: PredicateType.GreaterThanOrEqualTo,
predicateValue: 50,
restrictions: [
new AttributeFilter({
credentialDefinitionId: credDefId,
}),
],
}),
};
let faberProofRecord = await faberAgent.proofs.requestProof(faberConnection.id, {
name: 'test-proof-request',
requestedAttributes: attributes,
requestedPredicates: predicates,
});
testLogger.test('Alice waits for presentation request from Faber');
let aliceProofRecord = await waitForProofRecord(aliceAgent, {
threadId: faberProofRecord.tags.threadId,
state: ProofState.RequestReceived,
});
testLogger.test('Alice accepts presentation request from Faber');
const indyProofRequest = aliceProofRecord.requestMessage?.indyProofRequest;
const requestedCredentials = await aliceAgent.proofs.getRequestedCredentialsForProofRequest(
indyProofRequest!,
presentationPreview
);
await aliceAgent.proofs.acceptRequest(aliceProofRecord.id, requestedCredentials);
testLogger.test('Faber waits for presentation from Alice');
faberProofRecord = await waitForProofRecord(faberAgent, {
threadId: aliceProofRecord.tags.threadId,
state: ProofState.PresentationReceived,
});
// assert presentation is valid
expect(faberProofRecord.isVerified).toBe(true);
// Faber accepts presentation
await faberAgent.proofs.acceptPresentation(faberProofRecord.id);
// Alice waits till it receives presentation ack
aliceProofRecord = await waitForProofRecord(aliceAgent, {
threadId: aliceProofRecord.tags.threadId,
state: ProofState.Done,
});
});
});