Skip to content

Commit

Permalink
docs: fix demo for oob (#779)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored May 24, 2022
1 parent 0831b9b commit 5b42e6c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
60 changes: 47 additions & 13 deletions demo/src/Alice.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/*eslint import/no-cycle: [2, { maxDepth: 1 }]*/
import type { CredentialExchangeRecord, ProofRecord } from '@aries-framework/core'
import type {
ConnectionRecord,
ConnectionStateChangedEvent,
CredentialExchangeRecord,
ProofRecord,
} from '@aries-framework/core'

import { ConnectionEventTypes } from '@aries-framework/core'

import { BaseAgent } from './BaseAgent'
import { greenText, Output, redText } from './OutputClass'

export class Alice extends BaseAgent {
public connectionRecordFaberId?: string
public outOfBandId?: string
public connected: boolean

public constructor(port: number, name: string) {
Expand All @@ -20,33 +26,61 @@ export class Alice extends BaseAgent {
}

private async getConnectionRecord() {
if (!this.connectionRecordFaberId) {
if (!this.outOfBandId) {
throw Error(redText(Output.MissingConnectionRecord))
}

const [connection] = await this.agent.connections.findAllByOutOfBandId(this.outOfBandId)

if (!connection) {
throw Error(redText(Output.MissingConnectionRecord))
}
return await this.agent.connections.getById(this.connectionRecordFaberId)

return connection
}

private async printConnectionInvite() {
const outOfBand = await this.agent.oob.createInvitation()
// FIXME: this won't work as oob doesn't create a connection immediately
const [connectionRecord] = await this.agent.connections.findAllByOutOfBandId(outOfBand.id)
if (!connectionRecord) {
throw new Error(redText(Output.NoConnectionRecordFromOutOfBand))
}
this.connectionRecordFaberId = connectionRecord.id
this.outOfBandId = outOfBand.id

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

private async waitForConnection() {
const connectionRecord = await this.getConnectionRecord()
if (!this.outOfBandId) {
throw new Error(redText(Output.MissingConnectionRecord))
}

console.log('Waiting for Faber to finish connection...')

const getConnectionRecord = (outOfBandId: string) =>
new Promise<ConnectionRecord>((resolve, reject) => {
// Timeout of 20 seconds
const timeoutId = setTimeout(() => reject(new Error(redText(Output.MissingConnectionRecord))), 20000)

// Start listener
this.agent.events.on<ConnectionStateChangedEvent>(ConnectionEventTypes.ConnectionStateChanged, (e) => {
if (e.payload.connectionRecord.outOfBandId !== outOfBandId) return

clearTimeout(timeoutId)
resolve(e.payload.connectionRecord)
})

// Also retrieve the connection record by invitation if the event has already fired
void this.agent.connections.findAllByOutOfBandId(outOfBandId).then(([connectionRecord]) => {
if (connectionRecord) {
clearTimeout(timeoutId)
resolve(connectionRecord)
}
})
})

const connectionRecord = await getConnectionRecord(this.outOfBandId)

try {
await this.agent.connections.returnWhenIsConnected(connectionRecord.id)
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion demo/src/AliceInquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class AliceInquirer extends BaseInquirer {
}

private async getPromptChoice() {
if (this.alice.connectionRecordFaberId) return inquirer.prompt([this.inquireOptions(this.promptOptionsString)])
if (this.alice.outOfBandId) return inquirer.prompt([this.inquireOptions(this.promptOptionsString)])

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

0 comments on commit 5b42e6c

Please sign in to comment.