Skip to content

Commit

Permalink
feat: Missing Attribute Proof request (#4)
Browse files Browse the repository at this point in the history
* formatting updated

Signed-off-by: al-rosenthal <[email protected]>

* feat: added new tests for proof flow

Signed-off-by: al-rosenthal <[email protected]>

* feat: added test for sending a credential

Signed-off-by: al-rosenthal <[email protected]>

* feat: added writeQRCode method

Signed-off-by: al-rosenthal <[email protected]>

---------

Signed-off-by: al-rosenthal <[email protected]>
  • Loading branch information
al-rosenthal authored Nov 29, 2024
1 parent 6151a60 commit 2b464a2
Show file tree
Hide file tree
Showing 7 changed files with 1,471 additions and 986 deletions.
Empty file added invitations
Empty file.
76 changes: 38 additions & 38 deletions src/AgentCredo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,25 +179,25 @@ export const createAgent = async (

agent.registerOutboundTransport(wsTransport);
agent.registerOutboundTransport(httpTransport);

await agent.initialize();

console.log("AFJ Agent initialized");

if (config.mediatorInvitationUrl) {
await agent.mediationRecipient.initiateMessagePickup(undefined, MediatorPickupStrategy.Implicit)
}

createLinkSecretIfRequired(agent);

const indyVdrPoolService =
agent.dependencyManager.resolve(IndyVdrPoolService);
await Promise.all(
indyVdrPoolService.pools.map((pool) =>
(pool as unknown as any).pool.refresh()
)
);

return agent;
};

Expand All @@ -219,19 +219,19 @@ export class AgentCredo implements AriesAgent {
(
| V1CredentialProtocol
| V2CredentialProtocol<
(
| LegacyIndyCredentialFormatService
| AnonCredsCredentialFormatService
)[]
>
(
| LegacyIndyCredentialFormatService
| AnonCredsCredentialFormatService
)[]
>
)[]
>;
proofs: ProofsModule<
(
| V1ProofProtocol
| V2ProofProtocol<
(LegacyIndyProofFormatService | AnonCredsProofFormatService)[]
>
(LegacyIndyProofFormatService | AnonCredsProofFormatService)[]
>
)[]
>;
}>;
Expand All @@ -257,28 +257,28 @@ export class AgentCredo implements AriesAgent {
this.basicMessageReceivedSubscription?.unsubscribe()
this.basicMessageReceivedSubscription = undefined
}

private filterByType = <R extends BaseRecordAny>(recordClass: RecordClass<R>) => {
return pipe(
map((event: BaseEvent) => (event.payload as Record<string, R>).record),
filter((record: R) => record.type === recordClass.type),
)
}

private recordsAddedByType = <R extends BaseRecordAny>(recordClass: RecordClass<R>) => {
if (!this.agent) {
throw new Error('Agent is required to check record type')
}

if (!recordClass) {
throw new Error("The recordClass can't be undefined")
}

return this.agent?.events.observable<RecordSavedEvent<R>>(RepositoryEventTypes.RecordSaved).pipe(this.filterByType(recordClass))
}

sendBasicMessage(connection_id: string, content: string): Promise<any> {
return this.agent.basicMessages.sendMessage(connection_id, content)
return this.agent.basicMessages.sendMessage(connection_id, content);
}

sendOOBConnectionlessProofRequest(
Expand All @@ -302,28 +302,27 @@ export class AgentCredo implements AriesAgent {
}

async acceptProof(proof: AcceptProofArgs): Promise<void> {

while (true) {
const proofs = await this.agent.proofs.getAll();
//console.log(`Proofs ${proofs.length}`)
for (let index = 0; index < proofs.length; index++) {
const p = proofs[index];
//console.dir(p.toJSON());
if ("id" in proof) {
//console.log(`[${index + 1}/${proofs.length}] - id:${p.id}, threadId:${p.threadId}, arg:${proof.id}`);
if (p.threadId === proof.id) {
await this.agent.proofs.acceptRequest({ proofRecordId: p.id });
return;
}
} else if ("connection_id" in proof) {
if (p.connectionId === proof.connection_id){
await this.agent.proofs.acceptRequest({ proofRecordId: p.id });
return;
}
while (true) {
const proofs = await this.agent.proofs.getAll();
//console.log(`Proofs ${proofs.length}`)
for (let index = 0; index < proofs.length; index++) {
const p = proofs[index];
//console.dir(p.toJSON());
if ("id" in proof) {
//console.log(`[${index + 1}/${proofs.length}] - id:${p.id}, threadId:${p.threadId}, arg:${proof.id}`);
if (p.threadId === proof.id) {
await this.agent.proofs.acceptRequest({ proofRecordId: p.id });
return;
}
} else if ("connection_id" in proof) {
if (p.connectionId === proof.connection_id) {
await this.agent.proofs.acceptRequest({ proofRecordId: p.id });
return;
}
}
waitFor(1000);
}
waitFor(1000);
}
}

async findCredentialOffer(connectionId: string): Promise<CredentialOfferRef> {
Expand All @@ -345,8 +344,9 @@ export class AgentCredo implements AriesAgent {
createSchema(_builder: SchemaBuilder): Promise<string | undefined> {
throw new Error("Method not implemented.");
}

async createInvitationToConnect<T extends INVITATION_TYPE>(_invitationType: T): Promise<CreateInvitationResponse<T>> {
async createInvitationToConnect<T extends INVITATION_TYPE>(
_invitationType: T
): Promise<CreateInvitationResponse<T>> {
throw new Error("Method not implemented.");
}

Expand Down Expand Up @@ -406,6 +406,6 @@ export class AgentCredo implements AriesAgent {
await this.agent?.mediationRecipient?.stopMessagePickup();
await this.agent?.shutdown();

this.basicMessageReceivedSubscription?.unsubscribe()
this.basicMessageReceivedSubscription?.unsubscribe()
}
}
122 changes: 72 additions & 50 deletions src/AgentManual.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,88 @@
import { AcceptProofArgs, AriesAgent, CreateInvitationResponse, CredentialOfferRef, INVITATION_TYPE, 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'
import fs from 'node:fs';
import path from 'node:path';
import { log } from "console"
import chalk from "chalk"
import {
CredentialDefinitionBuilder,
ProofRequestBuilder,
SchemaBuilder,
} from "./lib";
import QRCode from "qrcode";
import fs from "node:fs";
import path from "node:path";
import { log } from "console";
import chalk from "chalk";

export class AgentManual implements AriesAgent {
config: any;
public readonly logger: Logger;
public constructor(config:any,logger: Logger){
this.config = config
this.logger = logger
}
config: any;
public readonly logger: Logger;
public constructor(config: any, logger: Logger) {
this.config = config;
this.logger = logger;
}
sendBasicMessage(_connection_id: string, content: string): Promise<any> {
log(chalk.yellowBright(`> Send a message with '${content}' as content`))
return Promise.resolve()
log(chalk.yellowBright(`> Send a message with '${content}' as content`));
return Promise.resolve();
}
sendOOBConnectionlessProofRequest(_builder: ProofRequestBuilder): Promise<any | undefined> {
sendOOBConnectionlessProofRequest(
_builder: ProofRequestBuilder
): Promise<any | undefined> {
throw new Error("Method not implemented.");
}
acceptProof(_proof: AcceptProofArgs): Promise<void> {
//throw new Error("Method not implemented.");
return Promise.resolve()
return Promise.resolve();
}
waitForPresentation(_presentation_exchange_id: string): Promise<void> {
throw new Error("Method not implemented.");
}
sendConnectionlessProofRequest(_builder: ProofRequestBuilder): Promise<ResponseCreateInvitation> {
sendConnectionlessProofRequest(
_builder: ProofRequestBuilder
): Promise<ResponseCreateInvitation> {
throw new Error("Method not implemented.");
}
async acceptCredentialOffer(_offer: CredentialOfferRef): Promise<void> {
console.warn("Accept Credential");
}
async findCredentialOffer(connectionId: string): Promise<CredentialOfferRef> {
return { id: "undefined", connection_id: connectionId };
}
createSchemaCredDefinition(
_credDefBuilder: CredentialDefinitionBuilder
): Promise<string | undefined> {
throw new Error("Method not implemented.");
}
createSchema(_builder: SchemaBuilder): Promise<string | undefined> {
throw new Error("Method not implemented.");
}
async acceptCredentialOffer(_offer: CredentialOfferRef): Promise<void> {
console.warn('Accept Credential')
}
async findCredentialOffer(connectionId: string): Promise<CredentialOfferRef> {
return {id: 'undefined', connection_id: connectionId}
}
createSchemaCredDefinition(_credDefBuilder: CredentialDefinitionBuilder): Promise<string | undefined> {
throw new Error("Method not implemented.");
}
createSchema(_builder: SchemaBuilder): Promise<string | undefined> {
throw new Error("Method not implemented.");
}
async createInvitationToConnect<T extends INVITATION_TYPE>(_invitationType: T): Promise<CreateInvitationResponse<T>> {
throw new Error("Method not implemented.");
}
async receiveInvitation(ref: ResponseCreateInvitation, appName:string = 'BC Wallet App'): Promise<ReceiveInvitationResponse> {
const relativePath = './tmp/__qrcode.png'
const QRCodePath = path.resolve(process.cwd() as string, relativePath)
fs.mkdirSync(path.dirname(QRCodePath), { recursive: true })
//fs.writeFileSync(path.join(process.cwd(), 'invitation_url.txt'), ref.payload.invitation_url)
//fs.writeFileSync(path.join(process.cwd(), 'invitation.json'), JSON.stringify((ref.payload.invitation as any)))
await QRCode.toFile(
QRCodePath,
ref.payload.invitation_url,
{margin: 10}
async createInvitationToConnect<T extends INVITATION_TYPE>(
_invitationType: T
): Promise<CreateInvitationResponse<T>> {
throw new Error("Method not implemented.");
}
async receiveInvitation(
ref: ResponseCreateInvitation,
appName: string = "BC Wallet App"
): Promise<ReceiveInvitationResponse> {
const relativePath = "./tmp/__qrcode.png";
const QRCodePath = path.resolve(process.cwd() as string, relativePath);
fs.mkdirSync(path.dirname(QRCodePath), { recursive: true });
//fs.writeFileSync(path.join(process.cwd(), 'invitation_url.txt'), ref.payload.invitation_url)
//fs.writeFileSync(path.join(process.cwd(), 'invitation.json'), JSON.stringify((ref.payload.invitation as any)))
await QRCode.toFile(QRCodePath, ref.payload.invitation_url, { margin: 10 });
log(
chalk.yellowBright(
`> Scan QR Code image using "${appName}" from ${relativePath}`
)
log(chalk.yellowBright(`> Scan QR Code image using "${appName}" from ${relativePath}`))
return {}
}
public async startup(){
}
public async shutdown() {}
}
);
return {};
}
public async startup() {}
public async shutdown() {}
}
Loading

0 comments on commit 2b464a2

Please sign in to comment.