|
1 | 1 | /* eslint-disable no-console */
|
| 2 | +// @ts-ignore |
| 3 | +import { poll } from 'await-poll'; |
| 4 | +import logger from '../logger'; |
| 5 | +import path from 'path'; |
2 | 6 | import { Subject } from 'rxjs';
|
3 | 7 | import { ConnectionRecord } from '../storage/ConnectionRecord';
|
4 | 8 | import { Agent, InboundTransporter, OutboundTransporter } from '..';
|
5 | 9 | 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'); |
6 | 18 |
|
7 | 19 | // Custom matchers which can be used to extend Jest matchers via extend, e. g. `expect.extend({ toBeConnectedWith })`.
|
8 | 20 |
|
@@ -49,9 +61,90 @@ export class SubjectOutboundTransporter implements OutboundTransporter {
|
49 | 61 | }
|
50 | 62 |
|
51 | 63 | public async sendMessage(outboundPackage: OutboundPackage) {
|
52 |
| - console.log('Sending message...'); |
| 64 | + logger.logJson(`Sending outbound message to connection ${outboundPackage.connection.id}`, outboundPackage.payload); |
53 | 65 | const { payload } = outboundPackage;
|
54 |
| - console.log(payload); |
55 | 66 | this.subject.next(payload);
|
56 | 67 | }
|
57 | 68 | }
|
| 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