import { AskarModule } from '@aries-framework/askar' import { Agent, InitConfig, ConnectionEventTypes, ConnectionStateChangedEvent, WsOutboundTransport, HttpOutboundTransport, DidExchangeState, OutOfBandRecord, ConnectionsModule, } from '@aries-framework/core' import { agentDependencies, HttpInboundTransport } from '@aries-framework/node' import { ariesAskar } from '@hyperledger/aries-askar-nodejs' const initializeAcmeAgent = async () => { // Simple agent configuration. This sets some basic fields like the wallet // configuration and the label. const config: InitConfig = { label: 'demo-agent-acme', walletConfig: { id: 'mainAcme', key: 'demoagentacme0000000000000000000', storage: { type: 'sqlite', uri: 'sqlite://:memory:' } //path: ':memory:' }, }, endpoints: ['http://localhost:3001'], } // A new instance of an agent is created here // Askar can also be replaced by the indy-sdk if required const agent = new Agent({ config, modules: { askar: new AskarModule({ ariesAskar }), connections: new ConnectionsModule({ autoAcceptConnections: true }), }, dependencies: agentDependencies, }) // Register a simple `WebSocket` outbound transport agent.registerOutboundTransport(new WsOutboundTransport()) // Register a simple `Http` outbound transport agent.registerOutboundTransport(new HttpOutboundTransport()) // Register a simple `Http` inbound transport agent.registerInboundTransport(new HttpInboundTransport({ port: 3001 })) // Initialize the agent await agent.initialize() return agent } const createNewInvitation = async (agent: Agent) => { const outOfBandRecord = await agent.oob.createInvitation() return { invitationUrl: outOfBandRecord.outOfBandInvitation.toUrl({ domain: 'https://example.org' }), outOfBandRecord, } } const setupConnectionListener = (agent: Agent, outOfBandRecord: OutOfBandRecord, cb: (...args: any) => void) => { agent.events.on(ConnectionEventTypes.ConnectionStateChanged, ({ payload }) => { if (payload.connectionRecord.outOfBandId !== outOfBandRecord.id) return if (payload.connectionRecord.state === DidExchangeState.Completed) { // the connection is now ready for usage in other protocols! console.log(`Connection for out-of-band id ${outOfBandRecord.id} completed`) // Custom business logic can be included here // In this example we can send a basic message to the connection, but // anything is possible //cb() // We exit the flow process.exit(0) } }) } const run = async () => { console.log('Initializing Acme agent...') const acmeAgent = await initializeAcmeAgent() console.log('Creating the invitation as Acme...') const { outOfBandRecord, invitationUrl } = await createNewInvitation(acmeAgent) console.log('Listening for connection changes...') setupConnectionListener(acmeAgent, outOfBandRecord, () => console.log('We now have an active connection to use in the following tutorials') ) } export default run; void run();