From 2843584b72a32e5b712fba4ea4bafcda6ef8b52d Mon Sep 17 00:00:00 2001 From: annelein Date: Mon, 3 Jan 2022 15:17:25 +0100 Subject: [PATCH 1/2] feat(proof_flow): add proof proposal flow Signed-off-by: annelein --- demo/src/annelein.ts | 20 +++------ demo/src/annelein_inquirer.ts | 2 +- demo/src/klm.ts | 33 +++++++-------- demo/src/proof_request.ts | 78 +++++++++++++++-------------------- demo/src/register.ts | 2 +- 5 files changed, 56 insertions(+), 79 deletions(-) diff --git a/demo/src/annelein.ts b/demo/src/annelein.ts index 38a0c26a4f..d5d1f73de1 100644 --- a/demo/src/annelein.ts +++ b/demo/src/annelein.ts @@ -1,29 +1,21 @@ -import { CredentialEventTypes } from '@aries-framework/core'; -import { ConnectionInvitationMessage } from '@aries-framework/core'; import { ConnectionRecord } from '@aries-framework/core'; -import { CredentialState } from '@aries-framework/core'; -import { CredentialStateChangedEvent } from '@aries-framework/core'; import { Agent, -InitConfig, -ConsoleLogger, -LogLevel, +InitConfig, HttpOutboundTransport, BasicMessageEventTypes, BasicMessageStateChangedEvent, AutoAcceptCredential, AutoAcceptProof} from '@aries-framework/core' import { agentDependencies, HttpInboundTransport } from '@aries-framework/node' -//import { TestLogger } from '../../../packages/core/tests/logger' -//import { bc_coverin } from 'demo/src/index' import clear from 'clear'; import figlet from 'figlet'; import { annelein_inquirer } from './annelein_inquirer'; import { connection } from './connection'; -import { new_proof_preview } from './proof_request'; import { send_message } from './send_message'; import inquirer from 'inquirer' import { restart } from './restart'; +import { send_proof_proposal } from './proof_request'; import { accept_credential_offer } from './credential'; const bc_coverin = `{"reqSignature":{},"txn":{"data":{"data":{"alias":"Node1","blskey":"4N8aUNHSgjQVgkpm8nhNEfDf6txHznoYREg9kirmJrkivgL4oSEimFF6nsQ6M41QvhM2Z33nves5vfSn9n1UwNFJBYtWVnHYMATn76vLuL3zU88KyeAYcHfsih3He6UHcXDxcaecHVz6jhCYz1P2UZn2bDVruL5wXpehgBfBaLKm3Ba","blskey_pop":"RahHYiCvoNCtPTrVtP7nMC5eTYrsUA8WjXbdhNc8debh1agE9bGiJxWBXYNFbnJXoXhWFMvyqhqhRoq737YQemH5ik9oL7R4NTTCz2LEZhkgLJzB3QRQqJyBNyv7acbdHrAT8nQ9UkLbaVL9NBpnWXBTw4LEMePaSHEw66RzPNdAX1","client_ip":"138.197.138.255","client_port":9702,"node_ip":"138.197.138.255","node_port":9701,"services":["VALIDATOR"]},"dest":"Gw6pDLhcBcoQesN72qfotTgFa7cbuqZpkX3Xo6pLhPhv"},"metadata":{"from":"Th7MpTaRZVRYnPiabds81Y"},"type":"0"},"txnMetadata":{"seqNo":1,"txnId":"fea82e10e894419fe2bea7d96296a6d46f50f93f9eeda954ec461b2ed2950b62"},"ver":"1"} @@ -36,7 +28,7 @@ const ui = new inquirer.ui.BottomBar(); enum options { Connection = "setup connection", - Proof = "present proof", + Proof = "propose proof", Message = "send message", Exit = "exit", Restart = "restart" @@ -49,10 +41,8 @@ export const process_answer_annelein = async (annelein: Agent, answers: any) => accept_credential_offer(annelein, connectionRecord) } } else if (answers.options == options.Proof){ - ui.updateBottomBar('\x1b[36mRegistering a proof preview...\x1b[0m'); - const presentationPreview = await new_proof_preview() - if (connectionRecord !== undefined && presentationPreview !== undefined){ - await annelein.proofs.proposeProof(connectionRecord.id, presentationPreview) + if (connectionRecord !== undefined){ + await send_proof_proposal(annelein, connectionRecord) } else { console.log("\x1b[31m", 'Something went wrong.. Could it be that you have not set up a connection yet?', "\x1b[0m") } diff --git a/demo/src/annelein_inquirer.ts b/demo/src/annelein_inquirer.ts index bb39061a4c..4174baf3f3 100644 --- a/demo/src/annelein_inquirer.ts +++ b/demo/src/annelein_inquirer.ts @@ -14,7 +14,7 @@ export const annelein_inquirer = async (annelein: Agent) =>{ message: 'Options:', choices: ['Setup connection', - 'Send proof request', + 'Propose proof', 'Send Message', 'Exit', 'Restart'], diff --git a/demo/src/klm.ts b/demo/src/klm.ts index 8a6cd4a8b3..965d073659 100644 --- a/demo/src/klm.ts +++ b/demo/src/klm.ts @@ -1,12 +1,9 @@ import { ConnectionRecord } from '@aries-framework/core'; import { ProofStateChangedEvent } from '@aries-framework/core'; import { ProofState } from '@aries-framework/core'; -import { ProofEventTypes } from '@aries-framework/core'; import { Agent, -InitConfig, -ConsoleLogger, -LogLevel, +InitConfig, HttpOutboundTransport, BasicMessageEventTypes, BasicMessageStateChangedEvent, @@ -14,19 +11,17 @@ AutoAcceptCredential, AutoAcceptProof} from '@aries-framework/core' import { agentDependencies, HttpInboundTransport } from '@aries-framework/node' -// import { TestLogger } from '../../../packages/core/tests/logger' -// import { bc_coverin } from 'demo/src/index' import clear from 'clear'; import figlet from 'figlet'; import { accept_connection } from './connection'; import { issue_credential } from './credential'; import { klm_inquirer } from './klm_inquirer'; -import { register_schema } from './register'; +import { register_credential_schema } from './register'; import { send_message } from './send_message' import inquirer from 'inquirer' import { CredDef } from 'indy-sdk-react-native'; -import { exit } from 'process'; import { restart } from './restart'; +import { ProofEventTypes } from '@aries-framework/core'; const bc_coverin = `{"reqSignature":{},"txn":{"data":{"data":{"alias":"Node1","blskey":"4N8aUNHSgjQVgkpm8nhNEfDf6txHznoYREg9kirmJrkivgL4oSEimFF6nsQ6M41QvhM2Z33nves5vfSn9n1UwNFJBYtWVnHYMATn76vLuL3zU88KyeAYcHfsih3He6UHcXDxcaecHVz6jhCYz1P2UZn2bDVruL5wXpehgBfBaLKm3Ba","blskey_pop":"RahHYiCvoNCtPTrVtP7nMC5eTYrsUA8WjXbdhNc8debh1agE9bGiJxWBXYNFbnJXoXhWFMvyqhqhRoq737YQemH5ik9oL7R4NTTCz2LEZhkgLJzB3QRQqJyBNyv7acbdHrAT8nQ9UkLbaVL9NBpnWXBTw4LEMePaSHEw66RzPNdAX1","client_ip":"138.197.138.255","client_port":9702,"node_ip":"138.197.138.255","node_port":9701,"services":["VALIDATOR"]},"dest":"Gw6pDLhcBcoQesN72qfotTgFa7cbuqZpkX3Xo6pLhPhv"},"metadata":{"from":"Th7MpTaRZVRYnPiabds81Y"},"type":"0"},"txnMetadata":{"seqNo":1,"txnId":"fea82e10e894419fe2bea7d96296a6d46f50f93f9eeda954ec461b2ed2950b62"},"ver":"1"} {"reqSignature":{},"txn":{"data":{"data":{"alias":"Node2","blskey":"37rAPpXVoxzKhz7d9gkUe52XuXryuLXoM6P6LbWDB7LSbG62Lsb33sfG7zqS8TK1MXwuCHj1FKNzVpsnafmqLG1vXN88rt38mNFs9TENzm4QHdBzsvCuoBnPH7rpYYDo9DZNJePaDvRvqJKByCabubJz3XXKbEeshzpz4Ma5QYpJqjk","blskey_pop":"Qr658mWZ2YC8JXGXwMDQTzuZCWF7NK9EwxphGmcBvCh6ybUuLxbG65nsX4JvD4SPNtkJ2w9ug1yLTj6fgmuDg41TgECXjLCij3RMsV8CwewBVgVN67wsA45DFWvqvLtu4rjNnE9JbdFTc1Z4WCPA3Xan44K1HoHAq9EVeaRYs8zoF5","client_ip":"138.197.138.255","client_port":9704,"node_ip":"138.197.138.255","node_port":9703,"services":["VALIDATOR"]},"dest":"8ECVSk179mjsjKRLWiQtssMLgp6EPhWXtaYyStWPSGAb"},"metadata":{"from":"EbP4aYNeTHL6q385GuVpRV"},"type":"0"},"txnMetadata":{"seqNo":2,"txnId":"1ac8aece2a18ced660fef8694b61aac3af08ba875ce3026a160acbc3a3af35fc"},"ver":"1"} @@ -37,7 +32,6 @@ enum options { Connection = "setup connection", Credential = "offer credential", CredDef = "print credential definition", - Proof = "send proof request", Message = "send message", Exit = "exit", Restart = "restart" @@ -50,9 +44,10 @@ const ui = new inquirer.ui.BottomBar(); export const process_answer_klm = async (klm: Agent, answers: any) => { if (answers.options === options.Connection){ connectionRecord = await accept_connection(klm) + accept_proof_proposal(klm) } else if (answers.options == options.Credential){ ui.log.write('\x1b[36mRegistering a schema...\x1b[0m'); - credentialDefenition = await register_schema(klm) + credentialDefenition = await register_credential_schema(klm) if (connectionRecord !== undefined && credentialDefenition !== undefined){ await issue_credential(klm, credentialDefenition.id, connectionRecord) } else { @@ -85,14 +80,16 @@ export const process_answer_klm = async (klm: Agent, answers: any) => { process_answer_klm(klm, answer) } -const accept_proof_proprosal = (klm: Agent) => { - klm.events.on(ProofEventTypes.ProofStateChanged, (event: ProofStateChangedEvent) => { - if (event.payload.proofRecord.state !== ProofState.ProposalReceived) { - return +export const accept_proof_proposal = async (annelein: Agent) => { + annelein.events.on(ProofEventTypes.ProofStateChanged, + async ({ payload }: ProofStateChangedEvent) => { + if (payload.proofRecord.state !== ProofState.ProposalReceived) { + return + } + await annelein.proofs.acceptProposal(payload.proofRecord.id) + ui.log.write("\x1b[32m\nProof accepted!\n\x1b[0m"); } - ui.log.write("\x1b[32mProof proposal accepted!\x1b[0m"); - klm.proofs.acceptProposal(event.payload.proofRecord.id) - }) + ) } export const createAgentKLM = async (bc_coverin: string): Promise => { @@ -140,7 +137,7 @@ export const createAgentKLM = async (bc_coverin: string): Promise => { console.log(figlet.textSync('KLM', { horizontalLayout: 'full' })); const klm = await createAgentKLM(bc_coverin) console.log("\x1b[32m", 'Agent KLM created', "\x1b[0m") - accept_proof_proprosal(klm) + accept_proof_proposal(klm) const answer = await klm_inquirer(klm) process_answer_klm(klm, answer) } diff --git a/demo/src/proof_request.ts b/demo/src/proof_request.ts index 38de5f2efc..89ef9b4551 100644 --- a/demo/src/proof_request.ts +++ b/demo/src/proof_request.ts @@ -1,57 +1,47 @@ +import { ProofEventTypes } from '@aries-framework/core'; +import { ProofState } from '@aries-framework/core'; +import { ProofStateChangedEvent } from '@aries-framework/core'; import { Agent, ConnectionRecord, PresentationPreview, PresentationPreviewAttribute} from '@aries-framework/core' + import inquirer from 'inquirer' -export const new_proof_preview = async () => { - const answer = await inquirer - .prompt([ - { - type: 'input', - prefix: '', - name: 'credDef', - message: "Paste the credential definition here:", - }, - ]) - const presentationPreview = new PresentationPreview({ +const ui = new inquirer.ui.BottomBar(); +const credDef = '7KuDTpQh3GJ7Gp6kErpWvM:3:CL:115269:latest' + +export const send_proof_proposal = async (annelein: Agent, connectionRecord: ConnectionRecord) => { + annelein.events.on( + ProofEventTypes.ProofStateChanged, + async ({ payload }: ProofStateChangedEvent) => { + if (payload.proofRecord.state === ProofState.ProposalSent) { + ui.log.write("\x1b[32m\nproposal sent!\n\x1b[0m"); + return + } + } + ) + + const presentationPreview = new PresentationPreview({ attributes: [ new PresentationPreviewAttribute({ - name: 'actually happening', - credentialDefinitionId: answer.credDef, - value: 'yes', + name: 'name', + credentialDefinitionId: credDef, + value: 'annelein', + }), + new PresentationPreviewAttribute({ + name: 'date of birth', + credentialDefinitionId: credDef, + value: '09/09/1999', + }), + new PresentationPreviewAttribute({ + name: 'country of residence', + credentialDefinitionId: credDef, + value: 'the Netherlands', }) ], }) - return presentationPreview -} - -// export const proof_request = async (klm: Agent, annelein: Agent, connectionRecordAnnelein: ConnectionRecord, credDefId: string) => { - -// klm.events.on(ProofEventTypes.ProofStateChanged, (event: ProofStateChangedEvent) => { -// if (event.payload.proofRecord.state !== ProofState.ProposalReceived) { -// return -// } -// console.log("proof proprosal approved!") -// klm.proofs.acceptProposal(event.payload.proofRecord.id) -// }) - -// // annelein.events.on(ProofEventTypes.ProofStateChanged, async (event: ProofStateChangedEvent) => { -// // if (event.payload.proofRecord.state !== ProofState.RequestReceived) { -// // return -// // } - -// // klm.proofs.acceptProposal(event.payload.proofRecord.id) -// // }) - -// const proofRec = await annelein.proofs.proposeProof(connectionRecordAnnelein.id, presentationPreview) - -// // setInterval(async () => { -// // const reccie = await annelein.proofs.getById(proofRec.id) -// // console.log(reccie); - -// // }, 1000) -// // const requestedCredentials = await annelein.proofs.getRequestedCredentialsForProofRequest(proofRec.id) -// } \ No newline at end of file + await annelein.proofs.proposeProof(connectionRecord.id, presentationPreview) +} diff --git a/demo/src/register.ts b/demo/src/register.ts index 79098d166c..4021a17674 100644 --- a/demo/src/register.ts +++ b/demo/src/register.ts @@ -2,7 +2,7 @@ import { Agent } from '@aries-framework/core' import { uuid } from '@aries-framework/core/build/utils/uuid' import { CredDef } from 'indy-sdk-react-native' -export const register_schema = async (klm: Agent) : Promise => { +export const register_credential_schema = async (klm: Agent) : Promise => { const schema = await klm.ledger.registerSchema({ name: 'koninklijke luchtvaart maatschappij' + uuid(), version: '1.0.6', From 5c6b09cb29b606687c58b1d911af57c297539805 Mon Sep 17 00:00:00 2001 From: annelein Date: Wed, 5 Jan 2022 10:00:27 +0100 Subject: [PATCH 2/2] fix(connection): add flow to wait for connection: complete Signed-off-by: annelein --- demo/src/annelein.ts | 7 +++++-- demo/src/klm.ts | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/demo/src/annelein.ts b/demo/src/annelein.ts index d5d1f73de1..5848a78046 100644 --- a/demo/src/annelein.ts +++ b/demo/src/annelein.ts @@ -37,6 +37,9 @@ enum options { export const process_answer_annelein = async (annelein: Agent, answers: any) => { if (answers.options === options.Connection){ connectionRecord = await connection(annelein) + console.log("waiting for KLM to finish connection...") + connectionRecord = await annelein.connections.returnWhenIsConnected(connectionRecord.id) + console.log("\x1b[32m", "\nConnection established!\n", "\x1b[0m") if (connectionRecord !== undefined){ accept_credential_offer(annelein, connectionRecord) } @@ -95,14 +98,14 @@ const createAgentAnnelein = async (bc_coverin: string): Promise => { agent.registerInboundTransport(new HttpInboundTransport({port: port})) agent.registerOutboundTransport(new HttpOutboundTransport()) + await agent.initialize() + agent.events.on(BasicMessageEventTypes.BasicMessageStateChanged, (event: BasicMessageStateChangedEvent) => { if (event.payload.basicMessageRecord.role === 'receiver') { ui.log.write(`\x1b[35m\n${name} received a message: ${event.payload.message.content}\n\x1b[0m`); } }) - await agent.initialize() - return agent } diff --git a/demo/src/klm.ts b/demo/src/klm.ts index 965d073659..a8a75abddc 100644 --- a/demo/src/klm.ts +++ b/demo/src/klm.ts @@ -120,14 +120,14 @@ export const createAgentKLM = async (bc_coverin: string): Promise => { agent.registerInboundTransport(new HttpInboundTransport({port: port})) agent.registerOutboundTransport(new HttpOutboundTransport()) + await agent.initialize() + agent.events.on(BasicMessageEventTypes.BasicMessageStateChanged, (event: BasicMessageStateChangedEvent) => { if (event.payload.basicMessageRecord.role === 'receiver') { ui.log.write(`\x1b[35m\n${name} received a message: ${event.payload.message.content}\n\x1b[0m`); } }) - - await agent.initialize() - + return agent }