Skip to content

Commit

Permalink
docs: cleanup demo (#645)
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht authored Feb 21, 2022
1 parent 5912c0c commit 810d7bb
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 129 deletions.
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"alice": "ts-node src/AliceInquirer.ts",
"faber": "ts-node src/FaberInquirer.ts",
"refresh": "rm -rf ./node_modules ./yarn.lock && npm install"
"refresh": "rm -rf ./node_modules ./yarn.lock && yarn"
},
"devDependencies": {
"@aries-framework/core": "^0.1.0",
Expand Down
10 changes: 5 additions & 5 deletions demo/src/Alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Alice extends BaseAgent {

private async getConnectionRecord() {
if (!this.connectionRecordFaberId) {
throw Error(redText(Output.missingConnectionRecord))
throw Error(redText(Output.MissingConnectionRecord))
}
return await this.agent.connections.getById(this.connectionRecordFaberId)
}
Expand All @@ -30,7 +30,7 @@ export class Alice extends BaseAgent {
const invite = await this.agent.connections.createConnection()
this.connectionRecordFaberId = invite.connectionRecord.id

console.log(Output.connectionLink, invite.invitation.toUrl({ domain: `http://localhost:${this.port}` }), '\n')
console.log(Output.ConnectionLink, invite.invitation.toUrl({ domain: `http://localhost:${this.port}` }), '\n')
return invite.connectionRecord
}

Expand All @@ -44,7 +44,7 @@ export class Alice extends BaseAgent {
console.log(redText(`\nTimeout of 20 seconds reached.. Returning to home screen.\n`))
return
}
console.log(greenText(Output.connectionEstablished))
console.log(greenText(Output.ConnectionEstablished))
this.connected = true
}

Expand Down Expand Up @@ -73,9 +73,9 @@ export class Alice extends BaseAgent {
}

public async exit() {
console.log(Output.exit)
console.log(Output.Exit)
await this.agent.shutdown()
process.exit()
process.exit(0)
}

public async restart() {
Expand Down
40 changes: 18 additions & 22 deletions demo/src/AliceInquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const runAlice = async () => {
clear()
console.log(textSync('Alice', { horizontalLayout: 'full' }))
const alice = await AliceInquirer.build()
alice.processAnswer()
await alice.processAnswer()
}

enum PromptOptions {
Expand Down Expand Up @@ -42,18 +42,16 @@ export class AliceInquirer extends BaseInquirer {
}

private async getPromptChoice() {
if (this.alice.connectionRecordFaberId !== undefined) {
return inquirer.prompt([this.inquireOptions(this.promptOptionsString)])
}
if (this.alice.connectionRecordFaberId) return inquirer.prompt([this.inquireOptions(this.promptOptionsString)])

const reducedOption = [PromptOptions.CreateConnection, PromptOptions.Exit, PromptOptions.Restart]
return inquirer.prompt([this.inquireOptions(reducedOption)])
}

public async processAnswer() {
const choice = await this.getPromptChoice()
if (this.listener.on === true) {
return
}
if (this.listener.on) return

switch (choice.options) {
case PromptOptions.CreateConnection:
await this.connection()
Expand All @@ -68,11 +66,11 @@ export class AliceInquirer extends BaseInquirer {
await this.restart()
return
}
this.processAnswer()
await this.processAnswer()
}

public async acceptCredentialOffer(credentialRecord: CredentialRecord) {
const confirm = await inquirer.prompt([this.inquireConfirmation(Title.credentialOfferTitle)])
const confirm = await inquirer.prompt([this.inquireConfirmation(Title.CredentialOfferTitle)])
if (confirm.options === ConfirmOptions.No) {
await this.alice.agent.credentials.declineOffer(credentialRecord.id)
} else if (confirm.options === ConfirmOptions.Yes) {
Expand All @@ -81,7 +79,7 @@ export class AliceInquirer extends BaseInquirer {
}

public async acceptProofRequest(proofRecord: ProofRecord) {
const confirm = await inquirer.prompt([this.inquireConfirmation(Title.proofRequestTitle)])
const confirm = await inquirer.prompt([this.inquireConfirmation(Title.ProofRequestTitle)])
if (confirm.options === ConfirmOptions.No) {
await this.alice.agent.proofs.declineRequest(proofRecord.id)
} else if (confirm.options === ConfirmOptions.Yes) {
Expand All @@ -91,23 +89,21 @@ export class AliceInquirer extends BaseInquirer {

public async connection() {
await this.alice.setupConnection()
if (this.alice.connected === false) {
return
}
if (!this.alice.connected) return

this.listener.credentialOfferListener(this.alice, this)
this.listener.proofRequestListener(this.alice, this)
}

public async message() {
const message = await this.inquireMessage()
if (message === null) {
return
}
this.alice.sendMessage(message)
if (!message) return

await this.alice.sendMessage(message)
}

public async exit() {
const confirm = await inquirer.prompt([this.inquireConfirmation(Title.confirmTitle)])
const confirm = await inquirer.prompt([this.inquireConfirmation(Title.ConfirmTitle)])
if (confirm.options === ConfirmOptions.No) {
return
} else if (confirm.options === ConfirmOptions.Yes) {
Expand All @@ -116,15 +112,15 @@ export class AliceInquirer extends BaseInquirer {
}

public async restart() {
const confirm = await inquirer.prompt([this.inquireConfirmation(Title.confirmTitle)])
const confirm = await inquirer.prompt([this.inquireConfirmation(Title.ConfirmTitle)])
if (confirm.options === ConfirmOptions.No) {
this.processAnswer()
await this.processAnswer()
return
} else if (confirm.options === ConfirmOptions.Yes) {
await this.alice.restart()
runAlice()
await runAlice()
}
}
}

runAlice()
void runAlice()
12 changes: 6 additions & 6 deletions demo/src/BaseAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ export class BaseAgent {
this.port = port

const config: InitConfig = {
label: this.name,
label: name,
walletConfig: {
id: this.name,
key: this.name,
id: name,
key: name,
},
publicDidSeed: '6b8b882e2618fa5d45ee7229ca880083',
indyLedgers: [
{
genesisTransactions: bcovrin,
id: 'greenlights' + this.name,
id: 'greenlights' + name,
isProduction: false,
},
],
Expand All @@ -42,8 +42,8 @@ export class BaseAgent {

this.config = config

this.agent = new Agent(this.config, agentDependencies)
this.agent.registerInboundTransport(new HttpInboundTransport({ port: this.port }))
this.agent = new Agent(config, agentDependencies)
this.agent.registerInboundTransport(new HttpInboundTransport({ port }))
this.agent.registerOutboundTransport(new HttpOutboundTransport())
}

Expand Down
32 changes: 12 additions & 20 deletions demo/src/BaseInquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,26 @@ export class BaseInquirer {
}

public inquireOptions(promptOptions: string[]) {
const optionsInquirer = this.optionsInquirer
optionsInquirer.message = Title.optionsTitle
optionsInquirer.choices = promptOptions
return optionsInquirer
this.optionsInquirer.message = Title.OptionsTitle
this.optionsInquirer.choices = promptOptions
return this.optionsInquirer
}

public inquireInput(title: string) {
const inputInquirer = this.inputInquirer
inputInquirer.message = title
return inputInquirer
this.inputInquirer.message = title
return this.inputInquirer
}

public inquireConfirmation(title: string) {
const optionsInquirer = this.optionsInquirer
optionsInquirer.message = title
optionsInquirer.choices = [ConfirmOptions.Yes, ConfirmOptions.No]
return optionsInquirer
this.optionsInquirer.message = title
this.optionsInquirer.choices = [ConfirmOptions.Yes, ConfirmOptions.No]
return this.optionsInquirer
}

public async inquireMessage() {
const inputInquirer = this.inputInquirer
inputInquirer.message = Title.messageTitle
const message = await inquirer.prompt([inputInquirer])

if (message.input[0] == 'q') {
return null
} else {
return message.input
}
this.inputInquirer.message = Title.MessageTitle
const message = await inquirer.prompt([this.inputInquirer])

return message.input[0] === 'q' ? null : message.input
}
}
24 changes: 11 additions & 13 deletions demo/src/Faber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ConnectionRecord } from '@aries-framework/core'
import type { CredDef, Schema } from 'indy-sdk-react-native'
import type BottomBar from 'inquirer/lib/ui/bottom-bar'

import { CredentialPreview, ProofAttributeInfo, AttributeFilter, utils } from '@aries-framework/core'
import { AttributeFilter, CredentialPreview, ProofAttributeInfo, utils } from '@aries-framework/core'
import { ui } from 'inquirer'

import { BaseAgent } from './BaseAgent'
Expand All @@ -26,7 +26,7 @@ export class Faber extends BaseAgent {

private async getConnectionRecord() {
if (!this.connectionRecordAliceId) {
throw Error(redText(Output.missingConnectionRecord))
throw Error(redText(Output.MissingConnectionRecord))
}
return await this.agent.connections.getById(this.connectionRecordAliceId)
}
Expand All @@ -37,23 +37,21 @@ export class Faber extends BaseAgent {

private async waitForConnection(connectionRecord: ConnectionRecord) {
connectionRecord = await this.agent.connections.returnWhenIsConnected(connectionRecord.id)
console.log(greenText(Output.connectionEstablished))
console.log(greenText(Output.ConnectionEstablished))
return connectionRecord.id
}

public async acceptConnection(invitation_url: string) {
const connectionRecord = await this.receiveConnectionRequest(invitation_url)
if (connectionRecord === undefined) {
return
}

this.connectionRecordAliceId = await this.waitForConnection(connectionRecord)
}

private printSchema(name: string, version: string, attributes: string[]) {
console.log(`\n\nThe credential definition will look like this:\n`)
console.log(purpleText(`Name: ${Color.reset}${name}`))
console.log(purpleText(`Version: ${Color.reset}${version}`))
console.log(purpleText(`Attributes: ${Color.reset}${attributes[0]}, ${attributes[1]}, ${attributes[2]}\n`))
console.log(purpleText(`Name: ${Color.Reset}${name}`))
console.log(purpleText(`Version: ${Color.Reset}${version}`))
console.log(purpleText(`Attributes: ${Color.Reset}${attributes[0]}, ${attributes[1]}, ${attributes[2]}\n`))
}

private async registerSchema() {
Expand Down Expand Up @@ -101,7 +99,7 @@ export class Faber extends BaseAgent {
preview: credentialPreview,
})
this.ui.updateBottomBar(
`\nCredential offer sent!\n\nGo to the Alice agent to accept the credential offer\n\n${Color.reset}`
`\nCredential offer sent!\n\nGo to the Alice agent to accept the credential offer\n\n${Color.Reset}`
)
}

Expand Down Expand Up @@ -133,7 +131,7 @@ export class Faber extends BaseAgent {
requestedAttributes: proofAttribute,
})
this.ui.updateBottomBar(
`\nProof request sent!\n\nGo to the Alice agent to accept the proof request\n\n${Color.reset}`
`\nProof request sent!\n\nGo to the Alice agent to accept the proof request\n\n${Color.Reset}`
)
}

Expand All @@ -143,9 +141,9 @@ export class Faber extends BaseAgent {
}

public async exit() {
console.log(Output.exit)
console.log(Output.Exit)
await this.agent.shutdown()
process.exit()
process.exit(0)
}

public async restart() {
Expand Down
Loading

0 comments on commit 810d7bb

Please sign in to comment.