forked from Indicio-tech/aries-framework-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredentials.test.ts
330 lines (292 loc) · 11.2 KB
/
credentials.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
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
/* eslint-disable no-console */
import indy from 'indy-sdk';
import type { CredDefId } from 'indy-sdk';
import { Subject } from 'rxjs';
import { Agent, ConnectionRecord } from '..';
import {
ensurePublicDidIsOnLedger,
makeConnection,
registerDefinition,
registerSchema,
SubjectInboundTransporter,
SubjectOutboundTransporter,
waitForCredentialRecord,
genesisPath,
} from './helpers';
import {
CredentialRecord,
CredentialState,
CredentialPreview,
CredentialPreviewAttribute,
} from '../modules/credentials';
import { InitConfig } from '../types';
import testLogger from './logger';
const faberConfig: InitConfig = {
label: 'Faber',
walletConfig: { id: 'credentials-test-faber' },
walletCredentials: { key: '00000000000000000000000000000Test01' },
publicDidSeed: process.env.TEST_AGENT_PUBLIC_DID_SEED,
autoAcceptConnections: true,
genesisPath,
poolName: 'credentials-test-faber-pool',
indy,
logger: testLogger,
};
const aliceConfig: InitConfig = {
label: 'Alice',
walletConfig: { id: 'credentials-test-alice' },
walletCredentials: { key: '00000000000000000000000000000Test01' },
autoAcceptConnections: true,
genesisPath,
poolName: 'credentials-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('credentials', () => {
let faberAgent: Agent;
let aliceAgent: Agent;
let credDefId: CredDefId;
let faberConnection: ConnectionRecord;
let aliceConnection: ConnectionRecord;
let faberCredentialRecord: CredentialRecord;
let aliceCredentialRecord: CredentialRecord;
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;
});
afterAll(async () => {
await faberAgent.closeAndDeleteWallet();
await aliceAgent.closeAndDeleteWallet();
});
test('Alice starts with credential proposal to Faber', async () => {
testLogger.test('Alice sends credential proposal to Faber');
let aliceCredentialRecord = await aliceAgent.credentials.proposeCredential(aliceConnection.id, {
credentialProposal: credentialPreview,
credentialDefinitionId: credDefId,
});
testLogger.test('Faber waits for credential proposal from Alice');
let faberCredentialRecord = await waitForCredentialRecord(faberAgent, {
threadId: aliceCredentialRecord.tags.threadId,
state: CredentialState.ProposalReceived,
});
testLogger.test('Faber sends credential offer to Alice');
faberCredentialRecord = await faberAgent.credentials.acceptProposal(faberCredentialRecord.id, {
comment: 'some comment about credential',
});
testLogger.test('Alice waits for credential offer from Faber');
aliceCredentialRecord = await waitForCredentialRecord(aliceAgent, {
threadId: faberCredentialRecord.tags.threadId,
state: CredentialState.OfferReceived,
});
// FIXME: expect below expects json, so we do a refetch because the current
// returns class instance
aliceCredentialRecord = await aliceAgent.credentials.getById(aliceCredentialRecord.id);
expect(aliceCredentialRecord).toMatchObject({
createdAt: expect.any(Number),
id: expect.any(String),
offerMessage: {
'@id': expect.any(String),
'@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0/offer-credential',
comment: 'some comment about credential',
credential_preview: {
'@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0/credential-preview',
attributes: [
{
name: 'name',
'mime-type': 'text/plain',
value: 'John',
},
{
name: 'age',
'mime-type': 'text/plain',
value: '99',
},
],
},
'offers~attach': expect.any(Array),
},
tags: { threadId: faberCredentialRecord.tags.threadId },
type: CredentialRecord.name,
state: CredentialState.OfferReceived,
});
testLogger.test('Alice sends credential request to Faber');
aliceCredentialRecord = await aliceAgent.credentials.acceptOffer(aliceCredentialRecord.id);
testLogger.test('Faber waits for credential request from Alice');
faberCredentialRecord = await waitForCredentialRecord(faberAgent, {
threadId: aliceCredentialRecord.tags.threadId,
state: CredentialState.RequestReceived,
});
testLogger.test('Faber sends credential to Alice');
faberCredentialRecord = await faberAgent.credentials.acceptRequest(faberCredentialRecord.id);
testLogger.test('Alice waits for credential from Faber');
aliceCredentialRecord = await waitForCredentialRecord(aliceAgent, {
threadId: faberCredentialRecord.tags.threadId,
state: CredentialState.CredentialReceived,
});
testLogger.test('Alice sends credential ack to Faber');
aliceCredentialRecord = await aliceAgent.credentials.acceptCredential(aliceCredentialRecord.id);
testLogger.test('Faber waits for credential ack from Alice');
faberCredentialRecord = await waitForCredentialRecord(faberAgent, {
threadId: faberCredentialRecord.tags.threadId,
state: CredentialState.Done,
});
expect(aliceCredentialRecord).toMatchObject({
type: CredentialRecord.name,
id: expect.any(String),
createdAt: expect.any(Number),
tags: {
threadId: expect.any(String),
},
offerMessage: expect.any(Object),
requestMessage: expect.any(Object),
requestMetadata: expect.any(Object),
credentialId: expect.any(String),
state: CredentialState.Done,
});
expect(faberCredentialRecord).toMatchObject({
type: CredentialRecord.name,
id: expect.any(String),
createdAt: expect.any(Number),
tags: {
threadId: expect.any(String),
},
offerMessage: expect.any(Object),
requestMessage: expect.any(Object),
requestMetadata: undefined,
credentialId: undefined,
state: CredentialState.Done,
});
});
test('Faber starts with credential offer to Alice', async () => {
testLogger.test('Faber sends credential offer to Alice');
faberCredentialRecord = await faberAgent.credentials.offerCredential(faberConnection.id, {
preview: credentialPreview,
credentialDefinitionId: credDefId,
comment: 'some comment about credential',
});
testLogger.test('Alice waits for credential offer from Faber');
aliceCredentialRecord = await waitForCredentialRecord(aliceAgent, {
threadId: faberCredentialRecord.tags.threadId,
state: CredentialState.OfferReceived,
});
// FIXME: expect below expects json, so we do a refetch because the current
// returns class instance
aliceCredentialRecord = await aliceAgent.credentials.getById(aliceCredentialRecord.id);
expect(aliceCredentialRecord).toMatchObject({
createdAt: expect.any(Number),
id: expect.any(String),
offerMessage: {
'@id': expect.any(String),
'@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0/offer-credential',
comment: 'some comment about credential',
credential_preview: {
'@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0/credential-preview',
attributes: [
{
name: 'name',
'mime-type': 'text/plain',
value: 'John',
},
{
name: 'age',
'mime-type': 'text/plain',
value: '99',
},
],
},
'offers~attach': expect.any(Array),
},
tags: { threadId: faberCredentialRecord.tags.threadId },
type: CredentialRecord.name,
state: CredentialState.OfferReceived,
});
testLogger.test('Alice sends credential request to Faber');
aliceCredentialRecord = await aliceAgent.credentials.acceptOffer(aliceCredentialRecord.id);
testLogger.test('Faber waits for credential request from Alice');
faberCredentialRecord = await waitForCredentialRecord(faberAgent, {
threadId: aliceCredentialRecord.tags.threadId,
state: CredentialState.RequestReceived,
});
testLogger.test('Faber sends credential to Alice');
faberCredentialRecord = await faberAgent.credentials.acceptRequest(faberCredentialRecord.id);
testLogger.test('Alice waits for credential from Faber');
aliceCredentialRecord = await waitForCredentialRecord(aliceAgent, {
threadId: faberCredentialRecord.tags.threadId,
state: CredentialState.CredentialReceived,
});
testLogger.test('Alice sends credential ack to Faber');
aliceCredentialRecord = await aliceAgent.credentials.acceptCredential(aliceCredentialRecord.id);
testLogger.test('Faber waits for credential ack from Alice');
faberCredentialRecord = await waitForCredentialRecord(faberAgent, {
threadId: faberCredentialRecord.tags.threadId,
state: CredentialState.Done,
});
expect(aliceCredentialRecord).toMatchObject({
type: CredentialRecord.name,
id: expect.any(String),
createdAt: expect.any(Number),
tags: {
threadId: expect.any(String),
},
offerMessage: expect.any(Object),
requestMessage: expect.any(Object),
requestMetadata: expect.any(Object),
credentialId: expect.any(String),
state: CredentialState.Done,
});
expect(faberCredentialRecord).toMatchObject({
type: CredentialRecord.name,
id: expect.any(String),
createdAt: expect.any(Number),
tags: {
threadId: expect.any(String),
},
offerMessage: expect.any(Object),
requestMessage: expect.any(Object),
requestMetadata: undefined,
credentialId: undefined,
state: CredentialState.Done,
});
});
});