Skip to content

Commit

Permalink
feat(wip): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
outSH committed Nov 2, 2023
1 parent f4d8c9f commit daf88ca
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 1,142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@hyperledger/anoncreds-nodejs": "^0.2.0-dev.4",
"@hyperledger/aries-askar-nodejs": "^0.2.0-dev.1",
"@hyperledger/indy-vdr-nodejs": "^0.2.0-dev.3",
"inquirer": "^8.2.6",
"loglevel": "^1.8.1"
},
"devDependencies": {
Expand All @@ -65,8 +66,7 @@
"@types/indy-sdk": "^1.16.26",
"@types/inquirer": "^8.2.6",
"clear": "^0.1.0",
"figlet": "^1.5.2",
"ts-node": "^10.4.0"
"figlet": "^1.5.2"
},
"engines": {
"node": ">=18",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./public-api";
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export async function createAliceAgent() {
const agent = await setupAgent(ALICE_AGENT_NAME, ALICE_AGENT_PORT);
setupCredentialListener(agent);
setupProofListener(agent);
// TOdo - connect listener with logs
return agent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,22 @@ export async function issueCredential(
credentialId: indyCredentialExchangeRecord.id,
};
}

export async function getAgentCredentials(agent: Agent) {
const validCredentials = await agent.credentials.findAllByQuery({
state: CredentialState.Done,
});
log.debug("Valid credentials count:", validCredentials.length);

return validCredentials.map((c) => {
return {
id: c.id,
schemaId: c.metadata.data["_anoncreds/credential"].schemaId,
credentialDefinitionId:
c.metadata.data["_anoncreds/credential"].credentialDefinitionId,
connectionId: c.connectionId,
credentials: c.credentials,
credentialAttributes: c.credentialAttributes,
};
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./lib/agent-setup";
export * from "./lib/connections";
export * from "./lib/credentials";
export * from "./lib/proofs";
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { Agent } from "@aries-framework/core";
import inquirer from "inquirer";
import * as log from "loglevel";
import {
createAliceAgent,
createNewConnectionInvitation,
getAgentCredentials,
waitForConnection,
} from "../public-api";

log.setDefaultLevel("DEBUG");

enum PromptOptions {
StartTrade = "Start the trade",
GetCredentials = "Get this agent credentials",
GetAssets = "Get assets",
Exit = "Exit",
}

async function connectAgentToBLP(clientAgent: Agent) {
log.debug("Connecting to the discounted asset trade sample app agent...");

// Create invitation
const { outOfBandRecord, invitationUrl } =
await createNewConnectionInvitation(clientAgent);
const isConnectedPromise = waitForConnection(clientAgent, outOfBandRecord.id);

// Send request to the BLP agent
// TODO - send with HTTP
log.debug("Invitation link:", invitationUrl);

// Wait for connection
await isConnectedPromise;
log.info("Connected to the discounted asset trade sample app agent!");
}

async function sendTradeRequest() {
// TODO
log.info("Trade request sent!");
}

async function printAgentCredentials(agent: Agent) {
const credentials = await getAgentCredentials(agent);
log.info(JSON.stringify(credentials, undefined, 2));
}

async function getAssetsFromSampleApp() {
// TODO
log.info("Assets: foo");
}

async function getPromptChoice() {
return inquirer.prompt({
type: "list",
prefix: "",
name: "menu",
message: "Action:",
choices: Object.values(PromptOptions),
});
}

async function menuLoop(agent: Agent) {
let isRunning = true;

while (isRunning) {
try {
const choice = await getPromptChoice();
switch (choice.menu) {
case PromptOptions.StartTrade:
await sendTradeRequest();
break;
case PromptOptions.GetCredentials:
await printAgentCredentials(agent);
break;
case PromptOptions.GetAssets:
await getAssetsFromSampleApp();
break;
case PromptOptions.Exit:
isRunning = false;
break;
}
} catch (error) {
if (error.isTtyError) {
log.error("Prompt couldn't be rendered in the current environment:");
isRunning = false;
} else {
log.error("Menu action error:", error);
}
}
}
}

async function run() {
const aliceAgent = await createAliceAgent();
log.debug("Alice (client) agent created");

try {
await connectAgentToBLP(aliceAgent);

log.debug("Running menu loop...");
await menuLoop(aliceAgent);
} catch (error) {
log.error("Client app error:", error);
} finally {
log.info("Exiting the client app...");
aliceAgent.shutdown();
}
}

run();
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import { connectAgents } from "../lib/connections";
import { issueCredential } from "../lib/credentials";
import { checkCredentialProof } from "../lib/proofs";

// https://github.com/SamVerschueren/listr ??
async function runTest() {
const aliceAgent = await createAliceAgent();
console.log("BOB FINISHED");
Expand All @@ -80,26 +81,26 @@ async function runTest() {
log.debug("Alice connection ID:", aliceAgentConRecord.id);
log.debug("Issuer connection ID:", issuerAgentConRecord.id);

console.log("Issue...");
const { credentialDefinitionId } = await issueCredential(
issuerAgent,
[
{ name: "first_name", value: "Alice" },
{ name: "last_name", value: "Garcia" },
{ name: "salary", value: "2400" },
{ name: "employee_status", value: "Permanent" },
{ name: "experience", value: "10" },
],
issuerAgentConRecord.id,
isserDid,
);
// console.log("Issue...");
// const { credentialDefinitionId } = await issueCredential(
// issuerAgent,
// [
// { name: "first_name", value: "Alice" },
// { name: "last_name", value: "Garcia" },
// { name: "salary", value: "2400" },
// { name: "employee_status", value: "Permanent" },
// { name: "experience", value: "10" },
// ],
// issuerAgentConRecord.id,
// isserDid,
// );

console.log("Check proof...");
await checkCredentialProof(
issuerAgent,
credentialDefinitionId,
issuerAgentConRecord.id,
);
// console.log("Check proof...");
// await checkCredentialProof(
// issuerAgent,
// credentialDefinitionId,
// issuerAgentConRecord.id,
// );

console.log("Close...");
await aliceAgent.shutdown();
Expand Down
27 changes: 23 additions & 4 deletions tools/docker/indy-testnet/agent/src/blp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,24 @@ const registerCredentialDefinition = async (
return credentialDefinitionResult;
};

const waitForConnectionReady = async (
agent: Agent,
outOfBandRecordId: string,
) => {
let connection: ConnectionRecord | undefined;
do {
await new Promise((resolve) => setTimeout(resolve, 500));

connection = (
await agent.connections.findAllByOutOfBandId(outOfBandRecordId)
).pop();

if (!connection) {
throw Error("No connection!");
}
} while (connection && !connection.isReady);
};

const run = async () => {
console.log("Initializing BLP agent...");
const blpAgent = await initializeBLPAgent();
Expand All @@ -386,11 +404,11 @@ const run = async () => {
let outOfBandRecordId = "8c334af4-ba40-496b-abbf-d61cd4b2ddf3";
try {
const invitationUrl =
"https://example.org?oob=eyJAdHlwZSI6Imh0dHBzOi8vZGlkY29tbS5vcmcvb3V0LW9mLWJhbmQvMS4xL2ludml0YXRpb24iLCJAaWQiOiJiZWYwYjA1Ny1iYjZlLTQzMDctYjY2ZS0yZDM0YWYzNWQ5MTAiLCJsYWJlbCI6ImRlbW8tYWdlbnQtYm9iIiwiYWNjZXB0IjpbImRpZGNvbW0vYWlwMSIsImRpZGNvbW0vYWlwMjtlbnY9cmZjMTkiXSwiaGFuZHNoYWtlX3Byb3RvY29scyI6WyJodHRwczovL2RpZGNvbW0ub3JnL2RpZGV4Y2hhbmdlLzEuMCIsImh0dHBzOi8vZGlkY29tbS5vcmcvY29ubmVjdGlvbnMvMS4wIl0sInNlcnZpY2VzIjpbeyJpZCI6IiNpbmxpbmUtMCIsInNlcnZpY2VFbmRwb2ludCI6Imh0dHA6Ly9sb2NhbGhvc3Q6MzAwMyIsInR5cGUiOiJkaWQtY29tbXVuaWNhdGlvbiIsInJlY2lwaWVudEtleXMiOlsiZGlkOmtleTp6Nk1rcTZ2WEdrN0JyQ1RuOUhUNnVqeUt0M2lHMjdWb3daTXBoMzdLWjR4Y1lVOTQiXSwicm91dGluZ0tleXMiOltdfV19";
"https://example.org?oob=eyJAdHlwZSI6Imh0dHBzOi8vZGlkY29tbS5vcmcvb3V0LW9mLWJhbmQvMS4xL2ludml0YXRpb24iLCJAaWQiOiJiNjUxYjhiMC1hOGZhLTQwNmItOTg2Yi1jMTRjMDAzZTNjNDEiLCJsYWJlbCI6ImFsaWNlQ2FjdGlBZ2VudCIsImFjY2VwdCI6WyJkaWRjb21tL2FpcDEiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzE5Il0sImhhbmRzaGFrZV9wcm90b2NvbHMiOlsiaHR0cHM6Ly9kaWRjb21tLm9yZy9kaWRleGNoYW5nZS8xLjAiLCJodHRwczovL2RpZGNvbW0ub3JnL2Nvbm5lY3Rpb25zLzEuMCJdLCJzZXJ2aWNlcyI6W3siaWQiOiIjaW5saW5lLTAiLCJzZXJ2aWNlRW5kcG9pbnQiOiJodHRwOi8vbG9jYWxob3N0OjMwMDMiLCJ0eXBlIjoiZGlkLWNvbW11bmljYXRpb24iLCJyZWNpcGllbnRLZXlzIjpbImRpZDprZXk6ejZNa3NQMmJaZHhjVGtlTVhFYThNRmhrWnVpdXpRdFllZEtlNldlaktuNTF4cmdrIl0sInJvdXRpbmdLZXlzIjpbXX1dfQ";
console.log("Accepting the invitation from alice in BLP...");

const oob = await receiveInvitation(blpAgent, invitationUrl);
outOfBandRecordId = oob.id;
await waitForConnectionReady(blpAgent, outOfBandRecordId);
} catch (error) {
console.log("Connection alrady on");
}
Expand Down Expand Up @@ -420,11 +438,12 @@ const run = async () => {
);

// Send proof
console.log("Request proof");
const credentialDefinitionId =
"did:indy:bcovrin:test:Th7MpTaRZVRYnPiabds81Y/anoncreds/v0/CLAIM_DEF/63/default"; // TODO - get??
"did:indy:bcovrin:test:Th7MpTaRZVRYnPiabds81Y/anoncreds/v0/CLAIM_DEF/95/default"; // TODO - get??
const proofAttribute = {
name: {
name: "name",
name: "employee_status",
restrictions: [
{
cred_def_id: credentialDefinitionId,
Expand Down
Loading

0 comments on commit daf88ca

Please sign in to comment.