Skip to content

Commit 371fb08

Browse files
present proof protocol (openwallet-foundation#170)
Co-authored-by: Parth <[email protected]>
1 parent d9fe683 commit 371fb08

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3036
-108
lines changed

Diff for: jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ module.exports = {
1313
},
1414
collectCoverageFrom: ['src/lib/**/*.{js,jsx,tsx,ts}'],
1515
coveragePathIgnorePatterns: ['/node_modules/', '/__tests__/'],
16-
testTimeout: 45000,
16+
testTimeout: 60000,
1717
};

Diff for: src/lib/__tests__/credentials.test.ts

+8-45
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ import { Subject } from 'rxjs';
55
import path from 'path';
66
import indy from 'indy-sdk';
77
import { Agent } from '..';
8-
import { SubjectInboundTransporter, SubjectOutboundTransporter } from './helpers';
8+
import {
9+
ensurePublicDidIsOnLedger,
10+
makeConnection,
11+
registerDefinition,
12+
registerSchema,
13+
SubjectInboundTransporter,
14+
SubjectOutboundTransporter,
15+
} from './helpers';
916
import { CredentialRecord } from '../storage/CredentialRecord';
10-
import { SchemaTemplate, CredDefTemplate } from '../agent/LedgerService';
1117
import {
1218
CredentialPreview,
1319
CredentialPreviewAttribute,
@@ -199,46 +205,3 @@ describe('credentials', () => {
199205
});
200206
});
201207
});
202-
203-
async function registerSchema(agent: Agent, schemaTemplate: SchemaTemplate): Promise<[SchemaId, Schema]> {
204-
const [schemaId] = await agent.ledger.registerCredentialSchema(schemaTemplate);
205-
console.log('schemaId', schemaId);
206-
const ledgerSchema = await agent.ledger.getSchema(schemaId);
207-
console.log('ledgerSchemaId, ledgerSchema', schemaId, ledgerSchema);
208-
return [schemaId, ledgerSchema];
209-
}
210-
211-
async function registerDefinition(agent: Agent, definitionTemplate: CredDefTemplate): Promise<[CredDefId, CredDef]> {
212-
const [credDefId] = await agent.ledger.registerCredentialDefinition(definitionTemplate);
213-
const ledgerCredDef = await agent.ledger.getCredentialDefinition(credDefId);
214-
console.log('ledgerCredDefId, ledgerCredDef', credDefId, ledgerCredDef);
215-
return [credDefId, ledgerCredDef];
216-
}
217-
218-
async function ensurePublicDidIsOnLedger(agent: Agent, publicDid: Did) {
219-
try {
220-
console.log(`Ensure test DID ${publicDid} is written to ledger`);
221-
const agentPublicDid = await agent.ledger.getPublicDid(publicDid);
222-
console.log(`Ensure test DID ${publicDid} is written to ledger: Success`, agentPublicDid);
223-
} catch (error) {
224-
// Unfortunately, this won't prevent from the test suite running because of Jest runner runs all tests
225-
// regardless thorwn errors. We're more explicit about the problem with this error handling.
226-
throw new Error(`Test DID ${publicDid} is not written on ledger or ledger is not available.`);
227-
}
228-
}
229-
230-
async function makeConnection(agentA: Agent, agentB: Agent) {
231-
// eslint-disable-next-line prefer-const
232-
let { invitation, connectionRecord: agentAConnection } = await agentA.connections.createConnection();
233-
234-
let agentBConnection = await agentB.connections.receiveInvitation(invitation);
235-
236-
agentAConnection = await agentA.connections.returnWhenIsConnected(agentAConnection.id);
237-
238-
agentBConnection = await agentB.connections.returnWhenIsConnected(agentBConnection.id);
239-
240-
return {
241-
agentAConnection,
242-
agentBConnection,
243-
};
244-
}

Diff for: src/lib/__tests__/helpers.ts

+95-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
/* eslint-disable no-console */
2+
// @ts-ignore
3+
import { poll } from 'await-poll';
4+
import logger from '../logger';
5+
import path from 'path';
26
import { Subject } from 'rxjs';
37
import { ConnectionRecord } from '../storage/ConnectionRecord';
48
import { Agent, InboundTransporter, OutboundTransporter } from '..';
59
import { OutboundPackage, WireMessage } from '../types';
10+
import { SchemaTemplate, CredDefTemplate } from '../agent/LedgerService';
11+
import { CredentialOfferTemplate } from '../protocols/credentials/CredentialService';
12+
import { CredentialState } from '../protocols/credentials/CredentialState';
13+
import { CredentialRecord } from '../storage/CredentialRecord';
14+
15+
export const genesisPath = process.env.GENESIS_TXN_PATH
16+
? path.resolve(process.env.GENESIS_TXN_PATH)
17+
: path.join(__dirname, '../../../network/genesis/local-genesis.txn');
618

719
// Custom matchers which can be used to extend Jest matchers via extend, e. g. `expect.extend({ toBeConnectedWith })`.
820

@@ -49,9 +61,90 @@ export class SubjectOutboundTransporter implements OutboundTransporter {
4961
}
5062

5163
public async sendMessage(outboundPackage: OutboundPackage) {
52-
console.log('Sending message...');
64+
logger.logJson(`Sending outbound message to connection ${outboundPackage.connection.id}`, outboundPackage.payload);
5365
const { payload } = outboundPackage;
54-
console.log(payload);
5566
this.subject.next(payload);
5667
}
5768
}
69+
70+
export async function makeConnection(agentA: Agent, agentB: Agent) {
71+
// eslint-disable-next-line prefer-const
72+
let { invitation, connectionRecord: agentAConnection } = await agentA.connections.createConnection();
73+
let agentBConnection = await agentB.connections.receiveInvitation(invitation);
74+
75+
agentAConnection = await agentA.connections.returnWhenIsConnected(agentAConnection.id);
76+
agentBConnection = await agentB.connections.returnWhenIsConnected(agentBConnection.id);
77+
78+
return {
79+
agentAConnection,
80+
agentBConnection,
81+
};
82+
}
83+
84+
export async function registerSchema(agent: Agent, schemaTemplate: SchemaTemplate): Promise<[SchemaId, Schema]> {
85+
const [schemaId] = await agent.ledger.registerCredentialSchema(schemaTemplate);
86+
const ledgerSchema = await agent.ledger.getSchema(schemaId);
87+
logger.logJson(`created schema with id ${schemaId}`, ledgerSchema);
88+
return [schemaId, ledgerSchema];
89+
}
90+
91+
export async function registerDefinition(
92+
agent: Agent,
93+
definitionTemplate: CredDefTemplate
94+
): Promise<[CredDefId, CredDef]> {
95+
const [credDefId] = await agent.ledger.registerCredentialDefinition(definitionTemplate);
96+
const ledgerCredDef = await agent.ledger.getCredentialDefinition(credDefId);
97+
logger.logJson(`created credential definition with id ${credDefId}`, ledgerCredDef);
98+
return [credDefId, ledgerCredDef];
99+
}
100+
101+
export async function ensurePublicDidIsOnLedger(agent: Agent, publicDid: Did) {
102+
try {
103+
logger.log(`Ensure test DID ${publicDid} is written to ledger`);
104+
await agent.ledger.getPublicDid(publicDid);
105+
} catch (error) {
106+
// Unfortunately, this won't prevent from the test suite running because of Jest runner runs all tests
107+
// regardless of thrown errors. We're more explicit about the problem with this error handling.
108+
throw new Error(`Test DID ${publicDid} is not written on ledger or ledger is not available.`);
109+
}
110+
}
111+
112+
export async function issueCredential({
113+
issuerAgent,
114+
issuerConnectionId,
115+
holderAgent,
116+
credentialTemplate,
117+
}: {
118+
issuerAgent: Agent;
119+
issuerConnectionId: string;
120+
holderAgent: Agent;
121+
credentialTemplate: CredentialOfferTemplate;
122+
}) {
123+
const issuerConnection = await issuerAgent.connections.getById(issuerConnectionId);
124+
125+
await issuerAgent.credentials.issueCredential(issuerConnection, credentialTemplate);
126+
// We assume that Alice has only one credential and it's a credential from Faber
127+
let [holderCredential] = await poll(
128+
() => holderAgent.credentials.getCredentials(),
129+
(credentials: CredentialRecord[]) => credentials.length < 1,
130+
100
131+
);
132+
// Accept credential offer from Faber
133+
await holderAgent.credentials.acceptCredential(holderCredential);
134+
135+
// We assume that Alice has only one credential and it's a credential from Faber
136+
const [issuerCredential] = await poll(
137+
() => issuerAgent.credentials.getCredentials(),
138+
(credentials: CredentialRecord[]) => credentials.length < 1 || credentials[0].state !== CredentialState.Done,
139+
100
140+
);
141+
142+
// We assume that Alice has only one credential and it's a credential from Faber
143+
[holderCredential] = await poll(
144+
() => holderAgent.credentials.getCredentials(),
145+
(credentials: CredentialRecord[]) => credentials.length < 1 || credentials[0].state !== CredentialState.Done,
146+
100
147+
);
148+
149+
return { issuerCredential, holderCredential };
150+
}

0 commit comments

Comments
 (0)