Skip to content

Commit

Permalink
refactor invitation step 2
Browse files Browse the repository at this point in the history
Signed-off-by: Clécio Varjão <[email protected]>
  • Loading branch information
cvarjao committed Aug 14, 2024
1 parent 5c33627 commit 55f184a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/Agent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger, OutOfBandRecord } from "@credo-ts/core";
import { CredentialDefinitionBuilder, OOB_CONNECTION_HANDSHAKE_PROTOCOL, ProofRequestBuilder, SchemaBuilder } from "./lib";
import { CredentialDefinitionBuilder, ProofRequestBuilder, SchemaBuilder } from "./lib";

export type InvitationRecordV2 = {invi_msg_id: string, invitation_url: string, invitation: any, presentation_exchange_id?: string}
export type InvitationRecordV1 = {connection_id?: string, invitation: any, invitation_url: string, presentation_exchange_id?: string}
Expand Down Expand Up @@ -31,10 +31,11 @@ export type HasId = {id: string}
export type HadConnectionId = {connection_id: string}
export type AcceptProofArgs = HasId | HadConnectionId
export type ReceiveInvitationResponse = { outOfBandRecord?: OutOfBandRecord; connectionRecord?: ConnectionRef, invitationRequestsThreadIds?: string[] }

export interface AriesAgent {
readonly logger: Logger
sendBasicMessage(connection_id: string, content: string): Promise<any>
createInvitationToConnect(): Promise<ResponseCreateInvitation>
createInvitationToConnect<T extends INVITATION_TYPE>(invitationType: T): Promise<CreateInvitationResponse<T>>
receiveInvitation(invitation: ResponseCreateInvitation): Promise<ReceiveInvitationResponse>;
startup(): Promise<void>;
shutdown(): Promise<void>;
Expand Down
4 changes: 3 additions & 1 deletion src/AgentCredo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
AcceptProofArgs,
AriesAgent,
CreateInvitationResponse,
CredentialOfferRef,
INVITATION_TYPE,
ReceiveInvitationResponse,
ResponseCreateInvitation,
} from "./Agent";
Expand Down Expand Up @@ -277,7 +279,7 @@ export class AgentCredo implements AriesAgent {
createSchema(_builder: SchemaBuilder): Promise<string | undefined> {
throw new Error("Method not implemented.");
}
createInvitationToConnect(): Promise<ResponseCreateInvitation> {
async createInvitationToConnect<T extends INVITATION_TYPE>(_invitationType: T): Promise<CreateInvitationResponse<T>> {
throw new Error("Method not implemented.");
}
async receiveInvitation(
Expand Down
4 changes: 2 additions & 2 deletions src/AgentManual.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AcceptProofArgs, AriesAgent, CredentialOfferRef, ReceiveInvitationResponse, ResponseCreateInvitation } from "./Agent";
import { AcceptProofArgs, AriesAgent, CreateInvitationResponse, CredentialOfferRef, INVITATION_TYPE, ReceiveInvitationResponse, ResponseCreateInvitation } from "./Agent";
import { Logger } from "@credo-ts/core";
import { CredentialDefinitionBuilder, ProofRequestBuilder, SchemaBuilder } from "./lib";
import QRCode from 'qrcode'
Expand Down Expand Up @@ -43,7 +43,7 @@ export class AgentManual implements AriesAgent {
createSchema(_builder: SchemaBuilder): Promise<string | undefined> {
throw new Error("Method not implemented.");
}
createInvitationToConnect(): Promise<ResponseCreateInvitation> {
async createInvitationToConnect<T extends INVITATION_TYPE>(_invitationType: T): Promise<CreateInvitationResponse<T>> {
throw new Error("Method not implemented.");
}
async receiveInvitation(ref: ResponseCreateInvitation): Promise<ReceiveInvitationResponse> {
Expand Down
14 changes: 12 additions & 2 deletions src/AgentTraction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _axios, { AxiosInstance } from "axios";
import { AcceptProofArgs, AriesAgent, ConnectionRef, CredentialOfferRef, INVITATION_TYPE, ReceiveInvitationResponse, ResponseCreateInvitation, ResponseCreateInvitationV1, ResponseCreateInvitationV2 } from "./Agent";
import { AcceptProofArgs, AriesAgent, ConnectionRef, CreateInvitationResponse, CredentialOfferRef, INVITATION_TYPE, ReceiveInvitationResponse, ResponseCreateInvitation, ResponseCreateInvitationV1, ResponseCreateInvitationV2 } from "./Agent";
import { CredentialDefinitionBuilder, extractResponseData, IssueCredentialPreviewV1, printResponse, ProofRequestBuilder, SchemaBuilder } from "./lib";
import { Logger } from "@credo-ts/core";

Expand Down Expand Up @@ -576,7 +576,17 @@ export class AgentTraction implements AriesAgent {
}
})
}
async createInvitationToConnect(): Promise<ResponseCreateInvitation> {
async createInvitationToConnect<T extends INVITATION_TYPE>(invitationType: T): Promise<CreateInvitationResponse<T>> {
switch(invitationType) {
case INVITATION_TYPE.CONN_1_0:
return this.__createInvitationToConnectConnV1() as Promise<CreateInvitationResponse<typeof invitationType>>
case INVITATION_TYPE.OOB_CONN_1_0:
return this.createOOBInvitationToConnect(invitationType) as Promise<CreateInvitationResponse<typeof invitationType>>
default:
throw new Error("Invalid invitation type");
}
}
async __createInvitationToConnectConnV1(): Promise<CreateInvitationResponse<INVITATION_TYPE.CONN_1_0>> {
const config = this.config
const http = this.axios
return http.post(`/connections/create-invitation`,{
Expand Down
4 changes: 2 additions & 2 deletions src/attestation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const stepTimeout = 999999999
const shortTimeout = (2 * 60) * 1000

import { setGlobalDispatcher, Agent } from 'undici';
import { AriesAgent, ResponseCreateInvitationV1 } from "./Agent";
import { AriesAgent, INVITATION_TYPE, ResponseCreateInvitationV1 } from "./Agent";
import { AgentManual } from "./AgentManual";
setGlobalDispatcher(new Agent({ connect: { timeout: 20_000 } }));

Expand Down Expand Up @@ -74,7 +74,7 @@ describe("AppAttestation", () => {
const verifier = agentA
const holder = agentB
logger.info(`Executing ${expect.getState().currentTestName}`)
const remoteInvitation = await verifier.createInvitationToConnect() as ResponseCreateInvitationV1
const remoteInvitation = await verifier.createInvitationToConnect(INVITATION_TYPE.CONN_1_0)
logger.info(`waiting for holder to accept connection`)
const agentBConnectionRef1 = await holder.receiveInvitation(remoteInvitation)
logger.info(`waiting for issuer to accept connection`)
Expand Down
8 changes: 4 additions & 4 deletions src/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const stepTimeout = 120_000
const shortTimeout = 40_000
import { dir as console_dir } from "console"
import { setGlobalDispatcher, Agent} from 'undici';
import { AriesAgent, INVITATION_TYPE, ResponseCreateInvitationV1 } from "./Agent";
import { AriesAgent, INVITATION_TYPE } from "./Agent";
import { AgentManual } from "./AgentManual";
import { cache_requests } from "./axios-traction-serializer";
setGlobalDispatcher(new Agent({connect: { timeout: 20_000 }}));
Expand Down Expand Up @@ -80,7 +80,7 @@ describe("Mandatory", () => {
const issuer = agentIssuer
const holder = agentB
logger.info(`Executing ${expect.getState().currentTestName}`)
const remoteInvitation = await issuer.createInvitationToConnect() as ResponseCreateInvitationV1
const remoteInvitation = await issuer.createInvitationToConnect(INVITATION_TYPE.CONN_1_0)
logger.info(`waiting for holder to accept connection`)
const agentBConnectionRef1 = await holder.receiveInvitation(remoteInvitation)
logger.info(`waiting for issuer to accept connection`)
Expand All @@ -98,7 +98,7 @@ describe("Mandatory", () => {
const issuer = agentIssuer
const holder = agentB
logger.info(`Executing ${expect.getState().currentTestName}`)
const remoteInvitation = await issuer.createOOBInvitationToConnect(INVITATION_TYPE.OOB_DIDX_1_1)
const remoteInvitation = await issuer.createInvitationToConnect(INVITATION_TYPE.OOB_DIDX_1_1)
logger.info(`waiting for holder to accept connection`)
const agentBConnectionRef1 = await holder.receiveInvitation(remoteInvitation)
logger.info(`waiting for issuer to accept connection`)
Expand Down Expand Up @@ -149,7 +149,7 @@ describe("Mandatory", () => {
const verifier = agentVerifier
const holder = agentB
logger.info(`Executing ${expect.getState().currentTestName}`)
const remoteInvitation = await verifier.createInvitationToConnect() as ResponseCreateInvitationV1
const remoteInvitation = await verifier.createInvitationToConnect(INVITATION_TYPE.CONN_1_0)
logger.info(`waiting for holder to accept connection`)
const agentBConnectionRef1 = await holder.receiveInvitation(remoteInvitation)
logger.info(`waiting for issuer to accept connection`)
Expand Down
7 changes: 4 additions & 3 deletions src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import axios, {AxiosInstance} from 'axios';
import { PersonCredential1 } from './mocks';
import { BaseLogger, LogLevel, ProofExchangeRecord } from '@credo-ts/core';
import { BaseLogger, InvitationType, LogLevel, ProofExchangeRecord } from '@credo-ts/core';
import { Logger } from 'pino';
import { AgentTraction } from './AgentTraction';
import { AriesAgent, ResponseCreateInvitation, ResponseCreateInvitationV1, ResponseCreateInvitationV2 } from './Agent';
import { AriesAgent, INVITATION_TYPE, ResponseCreateInvitation, ResponseCreateInvitationV1, ResponseCreateInvitationV2 } from './Agent';
import fs from 'node:fs';
import path from 'node:path';
import { log, dir} from "console"
Expand Down Expand Up @@ -804,7 +804,8 @@ export class Context {

export const issueCredential = async (issuer:AgentTraction, holder: AriesAgent, cred: PersonCredential1) => {
const { logger } = issuer
const remoteInvitation = await issuer.createInvitationToConnect() as ResponseCreateInvitationV1

const remoteInvitation = await issuer.createInvitationToConnect(INVITATION_TYPE.CONN_1_0)
logger.info(`waiting for holder to accept connection`)
const agentBConnectionRef1 = await holder.receiveInvitation(remoteInvitation)
logger.info(`waiting for issuer to accept connection`)
Expand Down

0 comments on commit 55f184a

Please sign in to comment.