forked from openwallet-foundation/credo-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAliceInquirer.ts
More file actions
128 lines (108 loc) · 3.87 KB
/
Copy pathAliceInquirer.ts
File metadata and controls
128 lines (108 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { CredentialExchangeRecord, ProofExchangeRecord } from '@aries-framework/core'
import { clear } from 'console'
import { textSync } from 'figlet'
import { prompt } from 'inquirer'
import { Alice } from './Alice'
import { BaseInquirer, ConfirmOptions } from './BaseInquirer'
import { Listener } from './Listener'
import { Title } from './OutputClass'
export const runAlice = async () => {
clear()
console.log(textSync('Alice', { horizontalLayout: 'full' }))
const alice = await AliceInquirer.build()
await alice.processAnswer()
}
enum PromptOptions {
ReceiveConnectionUrl = 'Receive connection invitation',
SendMessage = 'Send message',
Exit = 'Exit',
Restart = 'Restart',
}
export class AliceInquirer extends BaseInquirer {
public alice: Alice
public promptOptionsString: string[]
public listener: Listener
public constructor(alice: Alice) {
super()
this.alice = alice
this.listener = new Listener()
this.promptOptionsString = Object.values(PromptOptions)
this.listener.messageListener(this.alice.agent, this.alice.name)
}
public static async build(): Promise<AliceInquirer> {
const alice = await Alice.build()
return new AliceInquirer(alice)
}
private async getPromptChoice() {
if (this.alice.connectionRecordFaberId) return prompt([this.inquireOptions(this.promptOptionsString)])
const reducedOption = [PromptOptions.ReceiveConnectionUrl, PromptOptions.Exit, PromptOptions.Restart]
return prompt([this.inquireOptions(reducedOption)])
}
public async processAnswer() {
const choice = await this.getPromptChoice()
if (this.listener.on) return
switch (choice.options) {
case PromptOptions.ReceiveConnectionUrl:
await this.connection()
break
case PromptOptions.SendMessage:
await this.message()
break
case PromptOptions.Exit:
await this.exit()
break
case PromptOptions.Restart:
await this.restart()
return
}
await this.processAnswer()
}
public async acceptCredentialOffer(credentialRecord: CredentialExchangeRecord) {
const confirm = await prompt([this.inquireConfirmation(Title.CredentialOfferTitle)])
if (confirm.options === ConfirmOptions.No) {
await this.alice.agent.credentials.declineOffer(credentialRecord.id)
} else if (confirm.options === ConfirmOptions.Yes) {
await this.alice.acceptCredentialOffer(credentialRecord)
}
}
public async acceptProofRequest(proofRecord: ProofExchangeRecord) {
const confirm = await prompt([this.inquireConfirmation(Title.ProofRequestTitle)])
if (confirm.options === ConfirmOptions.No) {
await this.alice.agent.proofs.declineRequest(proofRecord.id)
} else if (confirm.options === ConfirmOptions.Yes) {
await this.alice.acceptProofRequest(proofRecord)
}
}
public async connection() {
const title = Title.InvitationTitle
const getUrl = await prompt([this.inquireInput(title)])
await this.alice.acceptConnection(getUrl.input)
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) return
await this.alice.sendMessage(message)
}
public async exit() {
const confirm = await prompt([this.inquireConfirmation(Title.ConfirmTitle)])
if (confirm.options === ConfirmOptions.No) {
return
} else if (confirm.options === ConfirmOptions.Yes) {
await this.alice.exit()
}
}
public async restart() {
const confirm = await prompt([this.inquireConfirmation(Title.ConfirmTitle)])
if (confirm.options === ConfirmOptions.No) {
await this.processAnswer()
return
} else if (confirm.options === ConfirmOptions.Yes) {
await this.alice.restart()
await runAlice()
}
}
}
void runAlice()