forked from openwallet-foundation/credo-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredentials.test.ts
More file actions
250 lines (219 loc) · 8.94 KB
/
credentials.test.ts
File metadata and controls
250 lines (219 loc) · 8.94 KB
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
/* eslint-disable no-console */
// @ts-ignore
import { poll } from 'await-poll';
import { Subject } from 'rxjs';
import path from 'path';
import indy from 'indy-sdk';
import { Agent } from '..';
import { SubjectInboundTransporter, SubjectOutboundTransporter } from './helpers';
import { CredentialRecord } from '../storage/CredentialRecord';
import { SchemaTemplate, CredDefTemplate } from '../agent/LedgerService';
import {
CredentialPreview,
CredentialPreviewAttribute,
} from '../protocols/credentials/messages/CredentialOfferMessage';
import { CredentialState } from '../protocols/credentials/CredentialState';
import { InitConfig } from '../types';
const genesisPath = process.env.GENESIS_TXN_PATH
? path.resolve(process.env.GENESIS_TXN_PATH)
: path.join(__dirname, '../../../network/genesis/local-genesis.txn');
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',
};
const aliceConfig: InitConfig = {
label: 'Alice',
walletConfig: { id: 'credentials-test-alice' },
walletCredentials: { key: '00000000000000000000000000000Test01' },
autoAcceptConnections: true,
genesisPath,
poolName: 'credentials-test-alice-pool',
};
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;
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, indy);
aliceAgent = new Agent(aliceConfig, aliceAgentInbound, aliceAgentOutbound, indy);
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: { support_revocation: false },
};
const [ledgerCredDefId] = await registerDefinition(faberAgent, definitionTemplate);
credDefId = ledgerCredDefId;
const publidDid = faberAgent.getPublicDid()?.did ?? 'Th7MpTaRZVRYnPiabds81Y';
await ensurePublicDidIsOnLedger(faberAgent, publidDid);
await makeConnection(faberAgent, aliceAgent);
});
afterAll(async () => {
await faberAgent.closeAndDeleteWallet();
await aliceAgent.closeAndDeleteWallet();
});
test(`when faber issues credential then alice gets credential offer`, async () => {
// We assume that Faber has only one connection and it's a connection with Alice
const [firstConnection] = await faberAgent.connections.getAll();
// Issue credential from Faber to Alice
await faberAgent.credentials.issueCredential(firstConnection, {
credentialDefinitionId: credDefId,
comment: 'some comment about credential',
preview: credentialPreview,
});
// We assume that Alice has only one credential and it's a credential from Faber
const [firstCredential] = await poll(
() => aliceAgent.credentials.getCredentials(),
(credentials: CredentialRecord[]) => credentials.length < 1,
100
);
expect(firstCredential).toMatchObject({
createdAt: expect.any(Number),
id: expect.any(String),
offer: {
'@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: firstCredential.offer['@id'] },
type: CredentialRecord.name,
state: CredentialState.OfferReceived,
});
});
test(`when alice accepts the credential offer then faber sends a credential to alice`, async () => {
// We assume that Alice has only one credential and it's a credential from Faber
let [aliceCredential] = await aliceAgent.credentials.getCredentials();
// We assume that Faber has only one credential and it's a credential issued to Alice
let [faberCredential] = await faberAgent.credentials.getCredentials();
// Accept credential offer from Faber
await aliceAgent.credentials.acceptCredential(aliceCredential);
aliceCredential = await poll(
() => aliceAgent.credentials.find(aliceCredential.id),
(credentialRecord: CredentialRecord) => !credentialRecord || credentialRecord.state !== CredentialState.Done,
100
);
console.log('aliceCredential', aliceCredential);
faberCredential = await poll(
async () => faberAgent.credentials.find(faberCredential.id),
(credentialRecord: CredentialRecord) => !credentialRecord || credentialRecord.state !== CredentialState.Done,
100
);
console.log('faberCredential', faberCredential);
expect(aliceCredential).toMatchObject({
type: CredentialRecord.name,
id: expect.any(String),
createdAt: expect.any(Number),
tags: {
threadId: expect.any(String),
},
offer: expect.any(Object),
request: undefined,
requestMetadata: expect.any(Object),
credentialId: expect.any(String),
state: CredentialState.Done,
});
expect(faberCredential).toMatchObject({
type: CredentialRecord.name,
id: expect.any(String),
createdAt: expect.any(Number),
tags: {
threadId: expect.any(String),
},
offer: expect.any(Object),
request: expect.any(Object),
requestMetadata: undefined,
credentialId: undefined,
state: CredentialState.Done,
});
});
});
async function registerSchema(agent: Agent, schemaTemplate: SchemaTemplate): Promise<[SchemaId, Schema]> {
const [schemaId] = await agent.ledger.registerCredentialSchema(schemaTemplate);
console.log('schemaId', schemaId);
const ledgerSchema = await agent.ledger.getSchema(schemaId);
console.log('ledgerSchemaId, ledgerSchema', schemaId, ledgerSchema);
return [schemaId, ledgerSchema];
}
async function registerDefinition(agent: Agent, definitionTemplate: CredDefTemplate): Promise<[CredDefId, CredDef]> {
const [credDefId] = await agent.ledger.registerCredentialDefinition(definitionTemplate);
const ledgerCredDef = await agent.ledger.getCredentialDefinition(credDefId);
console.log('ledgerCredDefId, ledgerCredDef', credDefId, ledgerCredDef);
return [credDefId, ledgerCredDef];
}
async function ensurePublicDidIsOnLedger(agent: Agent, publicDid: Did) {
try {
console.log(`Ensure test DID ${publicDid} is written to ledger`);
const agentPublicDid = await agent.ledger.getPublicDid(publicDid);
console.log(`Ensure test DID ${publicDid} is written to ledger: Success`, agentPublicDid);
} catch (error) {
// Unfortunately, this won't prevent from the test suite running because of Jest runner runs all tests
// regardless thorwn errors. We're more explicit about the problem with this error handling.
throw new Error(`Test DID ${publicDid} is not written on ledger or ledger is not available.`);
}
}
async function makeConnection(agentA: Agent, agentB: Agent) {
const aliceConnectionAtAliceBob = await agentA.connections.createConnection();
if (!aliceConnectionAtAliceBob.invitation) {
throw new Error('There is no invitation in newly created connection!');
}
const bobConnectionAtBobAlice = await agentB.connections.receiveInvitation(
aliceConnectionAtAliceBob.invitation.toJSON()
);
const aliceConnectionRecordAtAliceBob = await agentA.connections.returnWhenIsConnected(aliceConnectionAtAliceBob.id);
if (!aliceConnectionRecordAtAliceBob) {
throw new Error('Connection not found!');
}
const bobConnectionRecordAtBobAlice = await agentB.connections.returnWhenIsConnected(bobConnectionAtBobAlice.id);
if (!bobConnectionRecordAtBobAlice) {
throw new Error('Connection not found!');
}
}