|
| 1 | +import type { SubjectMessage } from '../../../tests/transport/SubjectInboundTransport' |
| 2 | +import type { ProofStateChangedEvent } from '../src/modules/proofs' |
| 3 | + |
| 4 | +import { Subject, ReplaySubject } from 'rxjs' |
| 5 | + |
| 6 | +import { SubjectInboundTransport } from '../../../tests/transport/SubjectInboundTransport' |
| 7 | +import { SubjectOutboundTransport } from '../../../tests/transport/SubjectOutboundTransport' |
| 8 | +import { Agent } from '../src/agent/Agent' |
| 9 | +import { Attachment, AttachmentData } from '../src/decorators/attachment/Attachment' |
| 10 | +import { CredentialPreview } from '../src/modules/credentials' |
1 | 11 | import {
|
2 | 12 | PredicateType,
|
3 | 13 | ProofState,
|
4 | 14 | ProofAttributeInfo,
|
5 | 15 | AttributeFilter,
|
6 | 16 | ProofPredicateInfo,
|
7 | 17 | AutoAcceptProof,
|
| 18 | + ProofEventTypes, |
8 | 19 | } from '../src/modules/proofs'
|
| 20 | +import { LinkedAttachment } from '../src/utils/LinkedAttachment' |
| 21 | +import { uuid } from '../src/utils/uuid' |
9 | 22 |
|
10 |
| -import { setupProofsTest, waitForProofRecordSubject } from './helpers' |
| 23 | +import { |
| 24 | + getBaseConfig, |
| 25 | + issueCredential, |
| 26 | + makeConnection, |
| 27 | + prepareForIssuance, |
| 28 | + setupProofsTest, |
| 29 | + waitForProofRecordSubject, |
| 30 | +} from './helpers' |
11 | 31 | import testLogger from './logger'
|
12 | 32 |
|
13 | 33 | describe('Present Proof', () => {
|
| 34 | + let agents: Agent[] |
| 35 | + |
| 36 | + afterEach(async () => { |
| 37 | + for (const agent of agents) { |
| 38 | + await agent.shutdown() |
| 39 | + await agent.wallet.delete() |
| 40 | + } |
| 41 | + }) |
| 42 | + |
14 | 43 | test('Faber starts with connection-less proof requests to Alice', async () => {
|
15 | 44 | const { aliceAgent, faberAgent, aliceReplay, credDefId, faberReplay } = await setupProofsTest(
|
16 | 45 | 'Faber connection-less Proofs',
|
17 | 46 | 'Alice connection-less Proofs',
|
18 | 47 | AutoAcceptProof.Never
|
19 | 48 | )
|
| 49 | + agents = [aliceAgent, faberAgent] |
20 | 50 | testLogger.test('Faber sends presentation request to Alice')
|
21 | 51 |
|
22 | 52 | const attributes = {
|
@@ -93,6 +123,8 @@ describe('Present Proof', () => {
|
93 | 123 | AutoAcceptProof.Always
|
94 | 124 | )
|
95 | 125 |
|
| 126 | + agents = [aliceAgent, faberAgent] |
| 127 | + |
96 | 128 | const attributes = {
|
97 | 129 | name: new ProofAttributeInfo({
|
98 | 130 | name: 'name',
|
@@ -141,4 +173,159 @@ describe('Present Proof', () => {
|
141 | 173 | state: ProofState.Done,
|
142 | 174 | })
|
143 | 175 | })
|
| 176 | + |
| 177 | + test('Faber starts with connection-less proof requests to Alice with auto-accept enabled and both agents having a mediator', async () => { |
| 178 | + testLogger.test('Faber sends presentation request to Alice') |
| 179 | + |
| 180 | + const credentialPreview = CredentialPreview.fromRecord({ |
| 181 | + name: 'John', |
| 182 | + age: '99', |
| 183 | + }) |
| 184 | + |
| 185 | + const unique = uuid().substring(0, 4) |
| 186 | + |
| 187 | + const mediatorConfig = getBaseConfig(`Connectionless proofs with mediator Mediator-${unique}`, { |
| 188 | + autoAcceptMediationRequests: true, |
| 189 | + endpoints: ['rxjs:mediator'], |
| 190 | + }) |
| 191 | + |
| 192 | + const faberMessages = new Subject<SubjectMessage>() |
| 193 | + const aliceMessages = new Subject<SubjectMessage>() |
| 194 | + const mediatorMessages = new Subject<SubjectMessage>() |
| 195 | + |
| 196 | + const subjectMap = { |
| 197 | + 'rxjs:mediator': mediatorMessages, |
| 198 | + } |
| 199 | + |
| 200 | + // Initialize mediator |
| 201 | + const mediatorAgent = new Agent(mediatorConfig.config, mediatorConfig.agentDependencies) |
| 202 | + mediatorAgent.registerOutboundTransport(new SubjectOutboundTransport(mediatorMessages, subjectMap)) |
| 203 | + mediatorAgent.registerInboundTransport(new SubjectInboundTransport(mediatorMessages)) |
| 204 | + await mediatorAgent.initialize() |
| 205 | + |
| 206 | + const faberMediationInvitation = await mediatorAgent.connections.createConnection() |
| 207 | + const aliceMediationInvitation = await mediatorAgent.connections.createConnection() |
| 208 | + |
| 209 | + const faberConfig = getBaseConfig(`Connectionless proofs with mediator Faber-${unique}`, { |
| 210 | + autoAcceptProofs: AutoAcceptProof.Always, |
| 211 | + mediatorConnectionsInvite: faberMediationInvitation.invitation.toUrl({ domain: 'https://example.com' }), |
| 212 | + }) |
| 213 | + |
| 214 | + const aliceConfig = getBaseConfig(`Connectionless proofs with mediator Alice-${unique}`, { |
| 215 | + autoAcceptProofs: AutoAcceptProof.Always, |
| 216 | + // logger: new TestLogger(LogLevel.test), |
| 217 | + mediatorConnectionsInvite: aliceMediationInvitation.invitation.toUrl({ domain: 'https://example.com' }), |
| 218 | + }) |
| 219 | + |
| 220 | + const faberAgent = new Agent(faberConfig.config, faberConfig.agentDependencies) |
| 221 | + faberAgent.registerOutboundTransport(new SubjectOutboundTransport(faberMessages, subjectMap)) |
| 222 | + faberAgent.registerInboundTransport(new SubjectInboundTransport(faberMessages)) |
| 223 | + await faberAgent.initialize() |
| 224 | + |
| 225 | + const aliceAgent = new Agent(aliceConfig.config, aliceConfig.agentDependencies) |
| 226 | + aliceAgent.registerOutboundTransport(new SubjectOutboundTransport(aliceMessages, subjectMap)) |
| 227 | + aliceAgent.registerInboundTransport(new SubjectInboundTransport(aliceMessages)) |
| 228 | + await aliceAgent.initialize() |
| 229 | + |
| 230 | + agents = [aliceAgent, faberAgent, mediatorAgent] |
| 231 | + |
| 232 | + const { definition } = await prepareForIssuance(faberAgent, ['name', 'age', 'image_0', 'image_1']) |
| 233 | + |
| 234 | + const [faberConnection, aliceConnection] = await makeConnection(faberAgent, aliceAgent) |
| 235 | + expect(faberConnection.isReady).toBe(true) |
| 236 | + expect(aliceConnection.isReady).toBe(true) |
| 237 | + |
| 238 | + await issueCredential({ |
| 239 | + issuerAgent: faberAgent, |
| 240 | + issuerConnectionId: faberConnection.id, |
| 241 | + holderAgent: aliceAgent, |
| 242 | + credentialTemplate: { |
| 243 | + credentialDefinitionId: definition.id, |
| 244 | + comment: 'some comment about credential', |
| 245 | + preview: credentialPreview, |
| 246 | + linkedAttachments: [ |
| 247 | + new LinkedAttachment({ |
| 248 | + name: 'image_0', |
| 249 | + attachment: new Attachment({ |
| 250 | + filename: 'picture-of-a-cat.png', |
| 251 | + data: new AttachmentData({ base64: 'cGljdHVyZSBvZiBhIGNhdA==' }), |
| 252 | + }), |
| 253 | + }), |
| 254 | + new LinkedAttachment({ |
| 255 | + name: 'image_1', |
| 256 | + attachment: new Attachment({ |
| 257 | + filename: 'picture-of-a-dog.png', |
| 258 | + data: new AttachmentData({ base64: 'UGljdHVyZSBvZiBhIGRvZw==' }), |
| 259 | + }), |
| 260 | + }), |
| 261 | + ], |
| 262 | + }, |
| 263 | + }) |
| 264 | + const faberReplay = new ReplaySubject<ProofStateChangedEvent>() |
| 265 | + const aliceReplay = new ReplaySubject<ProofStateChangedEvent>() |
| 266 | + |
| 267 | + faberAgent.events.observable<ProofStateChangedEvent>(ProofEventTypes.ProofStateChanged).subscribe(faberReplay) |
| 268 | + aliceAgent.events.observable<ProofStateChangedEvent>(ProofEventTypes.ProofStateChanged).subscribe(aliceReplay) |
| 269 | + |
| 270 | + const attributes = { |
| 271 | + name: new ProofAttributeInfo({ |
| 272 | + name: 'name', |
| 273 | + restrictions: [ |
| 274 | + new AttributeFilter({ |
| 275 | + credentialDefinitionId: definition.id, |
| 276 | + }), |
| 277 | + ], |
| 278 | + }), |
| 279 | + } |
| 280 | + |
| 281 | + const predicates = { |
| 282 | + age: new ProofPredicateInfo({ |
| 283 | + name: 'age', |
| 284 | + predicateType: PredicateType.GreaterThanOrEqualTo, |
| 285 | + predicateValue: 50, |
| 286 | + restrictions: [ |
| 287 | + new AttributeFilter({ |
| 288 | + credentialDefinitionId: definition.id, |
| 289 | + }), |
| 290 | + ], |
| 291 | + }), |
| 292 | + } |
| 293 | + |
| 294 | + // eslint-disable-next-line prefer-const |
| 295 | + let { proofRecord: faberProofRecord, requestMessage } = await faberAgent.proofs.createOutOfBandRequest( |
| 296 | + { |
| 297 | + name: 'test-proof-request', |
| 298 | + requestedAttributes: attributes, |
| 299 | + requestedPredicates: predicates, |
| 300 | + }, |
| 301 | + { |
| 302 | + autoAcceptProof: AutoAcceptProof.ContentApproved, |
| 303 | + } |
| 304 | + ) |
| 305 | + |
| 306 | + const mediationRecord = await faberAgent.mediationRecipient.findDefaultMediator() |
| 307 | + if (!mediationRecord) { |
| 308 | + throw new Error('Faber agent has no default mediator') |
| 309 | + } |
| 310 | + |
| 311 | + expect(requestMessage).toMatchObject({ |
| 312 | + service: { |
| 313 | + recipientKeys: [expect.any(String)], |
| 314 | + routingKeys: mediationRecord.routingKeys, |
| 315 | + serviceEndpoint: mediationRecord.endpoint, |
| 316 | + }, |
| 317 | + }) |
| 318 | + |
| 319 | + await aliceAgent.receiveMessage(requestMessage.toJSON()) |
| 320 | + |
| 321 | + await waitForProofRecordSubject(aliceReplay, { |
| 322 | + threadId: faberProofRecord.threadId, |
| 323 | + state: ProofState.Done, |
| 324 | + }) |
| 325 | + |
| 326 | + await waitForProofRecordSubject(faberReplay, { |
| 327 | + threadId: faberProofRecord.threadId, |
| 328 | + state: ProofState.Done, |
| 329 | + }) |
| 330 | + }) |
144 | 331 | })
|
0 commit comments