Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(connection): add flow to wait for connection: complete #4

Open
wants to merge 2 commits into
base: feat/demo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions demo/src/annelein.ts
Original file line number Diff line number Diff line change
@@ -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"}
Expand All @@ -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"
Expand All @@ -45,14 +37,15 @@ 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)
}
} 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")
}
Expand Down Expand Up @@ -105,14 +98,14 @@ const createAgentAnnelein = async (bc_coverin: string): Promise<Agent> => {
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
}

Expand Down
2 changes: 1 addition & 1 deletion demo/src/annelein_inquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
39 changes: 18 additions & 21 deletions demo/src/klm.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
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,
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"}
Expand All @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Agent> => {
Expand Down Expand Up @@ -123,14 +120,14 @@ export const createAgentKLM = async (bc_coverin: string): Promise<Agent> => {
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
}

Expand All @@ -140,7 +137,7 @@ export const createAgentKLM = async (bc_coverin: string): Promise<Agent> => {
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)
}
Expand Down
78 changes: 34 additions & 44 deletions demo/src/proof_request.ts
Original file line number Diff line number Diff line change
@@ -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)
// }
await annelein.proofs.proposeProof(connectionRecord.id, presentationPreview)
}
2 changes: 1 addition & 1 deletion demo/src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CredDef> => {
export const register_credential_schema = async (klm: Agent) : Promise<CredDef> => {
const schema = await klm.ledger.registerSchema({
name: 'koninklijke luchtvaart maatschappij' + uuid(),
version: '1.0.6',
Expand Down