1 import IdGenerator from '@/utils/IdGenerator'
2 import { EncryptedStream } from 'extension-streams'
3 import * as MsgTypes from './messages/types'
4 import * as EventNames from '@/messages/event'
5 import Bytomdapp from './dapp'
8 * This is the javascript which gets injected into
9 * the application and facilitates communication between
10 * bytom chrome extension and the web application.
14 // Injecting an encrypted stream into the web application.
15 const stream = new EncryptedStream(EventNames.INJECT, IdGenerator.text(64))
17 // Waiting for bytomExtension to push itself onto the application
18 stream.listenWith(msg => {
19 console.log('inject.stream.listen:', msg)
22 msg.hasOwnProperty('type') &&
23 msg.type === MsgTypes.PUSH_BYTOM
25 window.bytom = new Bytomdapp(stream, msg.payload)
30 msg.hasOwnProperty('type') &&
31 msg.type === MsgTypes.UPDATE_BYTOM
33 window.bytom[msg.payload.type] = msg.payload.value
37 // Syncing the streams between the extension and the web application
38 stream.sync(EventNames.BYTOM, stream.key)