WIP: Untrusted external connections eg: dapps

feature/default_network_editable
frankiebee 8 years ago
parent 282775c52f
commit 79248ae5cd
  1. 192
      library/controller.js
  2. 2
      library/example/index.js
  3. 2
      library/index.js
  4. 1
      library/lib/setup-provider.js
  5. 27
      library/lib/setup-untrusted-connection.js
  6. 2
      library/popup.js
  7. 20
      library/sw-controller.js
  8. 25
      library/sw-core.js
  9. 2
      package.json

@ -1,13 +1,181 @@
// const SWcontroller = require('./sw-controller')
// const SwStream = require('sw-stream/lib/sw-stream.js')
// const startPopup = require('../app/scripts/popup-core')
// console.log('outside:open')
// const background = new SWcontroller({
// fileName: 'sw-build.js',
// })
// background.on('ready', (readSw) => {
// startPopup(SwStream(background.controller))
// })
// background.startWorker()
const ParentStream = require('iframe-stream').ParentStream
const SWcontroller = require('./sw-controller')
const SwStream = require('sw-stream/lib/sw-stream.js')
const SetupUntrustedComunication = ('./lib/setup-untrusted-connection.js')
const background = new SWcontroller({
fileName: '/popup/sw-build.js',
})
background.on('ready', (readSw) => {
// var inpageProvider = new MetamaskInpageProvider(SwStream(background.controller))
let pageStream = new ParentStream()
let swStream = SwStream(background.controller)
pageStream.pipe(swStream).pipe(pageStream)
})
background.on('error', console.error)
background.startWorker()
console.log('hello from controller')
/*
const urlUtil = require('url')
const extend = require('xtend')
const Dnode = require('dnode')
const eos = require('end-of-stream')
const ParentStream = require('iframe-stream').ParentStream
const PortStream = require('../app/scripts/lib/port-stream.js')
const notification = require('../app/scripts/lib/notifications.js')
const messageManager = require('../app/scripts/lib/message-manager')
const setupMultiplex = require('../app/scripts/lib/stream-utils.js').setupMultiplex
const MetamaskController = require('../app/scripts/metamask-controller')
const extension = require('../app/scripts/lib/extension')
const STORAGE_KEY = 'metamask-config'
initializeZeroClient()
function initializeZeroClient() {
const controller = new MetamaskController({
// User confirmation callbacks:
showUnconfirmedMessage,
unlockAccountMessage,
showUnapprovedTx,
// Persistence Methods:
setData,
loadData,
})
const idStore = controller.idStore
function unlockAccountMessage () {
console.log('notif stub - unlockAccountMessage')
}
function showUnconfirmedMessage (msgParams, msgId) {
console.log('notif stub - showUnconfirmedMessage')
}
function showUnapprovedTx (txParams, txData, onTxDoneCb) {
console.log('notif stub - showUnapprovedTx')
}
//
// connect to other contexts
//
var connectionStream = new ParentStream()
connectRemote(connectionStream, getParentHref())
function connectRemote (connectionStream, originDomain) {
var isMetaMaskInternalProcess = (originDomain === '127.0.0.1:9001')
if (isMetaMaskInternalProcess) {
// communication with popup
setupTrustedCommunication(connectionStream, 'MetaMask')
} else {
// communication with page
setupUntrustedCommunication(connectionStream, originDomain)
}
}
function setupUntrustedCommunication (connectionStream, originDomain) {
// setup multiplexing
var mx = setupMultiplex(connectionStream)
// connect features
controller.setupProviderConnection(mx.createStream('provider'), originDomain)
controller.setupPublicConfig(mx.createStream('publicConfig'))
}
function setupTrustedCommunication (connectionStream, originDomain) {
// setup multiplexing
var mx = setupMultiplex(connectionStream)
// connect features
setupControllerConnection(mx.createStream('controller'))
controller.setupProviderConnection(mx.createStream('provider'), originDomain)
}
//
// remote features
//
function setupControllerConnection (stream) {
controller.stream = stream
var api = controller.getApi()
var dnode = Dnode(api)
stream.pipe(dnode).pipe(stream)
dnode.on('remote', (remote) => {
// push updates to popup
controller.ethStore.on('update', controller.sendUpdate.bind(controller))
controller.listeners.push(remote)
idStore.on('update', controller.sendUpdate.bind(controller))
// teardown on disconnect
eos(stream, () => {
controller.ethStore.removeListener('update', controller.sendUpdate.bind(controller))
})
})
}
function loadData () {
var oldData = getOldStyleData()
var newData
try {
newData = JSON.parse(window.localStorage[STORAGE_KEY])
} catch (e) {}
var data = extend({
meta: {
version: 0,
},
data: {
config: {
provider: {
type: 'testnet',
},
},
},
}, oldData || null, newData || null)
return data
}
function getOldStyleData () {
var config, wallet, seedWords
var result = {
meta: { version: 0 },
data: {},
}
try {
config = JSON.parse(window.localStorage['config'])
result.data.config = config
} catch (e) {}
try {
wallet = JSON.parse(window.localStorage['lightwallet'])
result.data.wallet = wallet
} catch (e) {}
try {
seedWords = window.localStorage['seedWords']
result.data.seedWords = seedWords
} catch (e) {}
return result
}
function setData (data) {
window.localStorage[STORAGE_KEY] = JSON.stringify(data)
}
function getParentHref(){
try {
var parentLocation = window.parent.location
return parentLocation.hostname + ':' + parentLocation.port
} catch (err) {
return 'unknown'
}
}
}
*/

@ -1,4 +1,3 @@
window.addEventListener('load', web3Detect)
function web3Detect() {
@ -18,6 +17,7 @@ function startApp(){
web3.eth.getAccounts(function(err, addresses){
if (err) throw err
console.log('set address', addresses[0])
debugger
primaryAccount = addresses[0]
})

@ -41,3 +41,5 @@ function hijackProvider(provider){
_super(payload, cb)
}
}

@ -22,4 +22,3 @@ function getProvider(){
return inpageProvider
}

@ -0,0 +1,27 @@
/*
IFRAME
var pageStream = new LocalMessageDuplexStream({
name: 'contentscript',
target: 'inpage',
})
SERVICEWORKER
pageStream.on('error', console.error)
var pluginPort = extension.runtime.connect({name: 'contentscript'})
var pluginStream = new PortStream(pluginPort)
pluginStream.on('error', console.error)
IFRAME --> SW
// forward communication plugin->inpage
pageStream.pipe(pluginStream).pipe(pageStream)
*/
module.exports = SetupUntrustedComunicationWithSW
function SetupUntrustedComunicationWithSW (connectionStream, readySwStream) {
pageStream.on('error', console.error)
var pluginPort = extension.runtime.connect({name: 'contentscript'})
var pluginStream = new PortStream(pluginPort)
pluginStream.on('error', console.error)
// forward communication plugin->inpage
pageStream.pipe(pluginStream).pipe(pageStream)
}

@ -19,6 +19,7 @@ var iframeStream = setupIframe({
container: document.body,
})
console.log('outside:open')
const background = new SWcontroller({
fileName: '/popup/sw-build.js',
})
@ -27,5 +28,6 @@ background.on('ready', (readSw) => {
// startPopup(inpageProvider)
startPopup(SwStream(background.controller))
})
background.on('message', (messageEvent) => {debugger})
background.startWorker()
console.log('hello from /library/popup.js')

@ -7,22 +7,10 @@ module.exports = class ClientSideServiceWorker extends EventEmitter{
this.startDelay = opts.startDelay
this.serviceWorkerApi = navigator.serviceWorker
this.serviceWorkerApi.onmessage = (event) => this.emit('message', event)
this.serviceWorkerApi.onerror = (event) => this.emit('error')
// temporary function
this.askForId = (message) => {
this.sendMessage('check-in')
.then((data) => console.log(`${message}----${data}`))
}
// if (!!this.serviceWorkerApi) this.askForId('before')
this.serviceWorkerApi.onmessage = (messageEvent) => this.emit('message', messageEvent)
this.serviceWorkerApi.onerror = (err) => this.emit('error', err)
this.on('message', (messageEvent) => {debugger})
if (opts.initStart) this.startWorker()
this.on('ready', (sw) => {
this.askForId('ready')
})
}
get controller () {
@ -34,7 +22,6 @@ module.exports = class ClientSideServiceWorker extends EventEmitter{
return this.registerWorker()
.then((sw) => {
this.sw = sw
this.askForId('after register:')
this.sw.onerror = (err) => this.emit('error', err)
this.sw = sw
this.emit('ready', this.sw)
@ -46,7 +33,6 @@ module.exports = class ClientSideServiceWorker extends EventEmitter{
return this.serviceWorkerApi.register(this.fileName)
.then((registerdWorker) => {
return new Promise((resolve, reject) => {
this.askForId('after')
let timeOutId = setTimeout(() => {
if (this.serviceWorkerApi.controller) return resolve(this.serviceWorkerApi.controller)
if (registerdWorker.active) return resolve(registerdWorker.active)

@ -1,7 +1,4 @@
global.window = global
const SWGlobal = self
const urlUtil = require('url')
const endOfStream = require('end-of-stream')
const asyncQ = require('async-q')
const pipe = require('pump')
@ -14,7 +11,7 @@ const PortStream = require('../app/scripts/lib/port-stream.js')
const DbController = require('./controllers/index-db-controller')
const MetamaskController = require('../app/scripts/metamask-controller')
// const extension = require('../app/scripts/lib/extension')
const extension = {} //require('../app/scripts/lib/extension')
// const LocalStorageStore = require('obs-store/lib/localStorage')
const storeTransform = require('obs-store/lib/transform')
const Migrator = require('../app/scripts/lib/migrator/')
@ -36,20 +33,6 @@ self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim())
})
self.onsync = function (syncEvent) {
// What is done when a sync even is fired
console.log('inside:sync')
var focused
self.clients.matchAll()
.then(clients => {
clients.forEach(function(client) {
})
})
}
console.log('inside:open')
@ -117,8 +100,6 @@ function setupController (initState, client) {
.catch((err) => {console.error(err)})
})
function versionifyData(state) {
// let versionedData
// versionedData.data = state
return dbController.get()
.then((rawData) => {
return Promise.resolve({
@ -143,6 +124,7 @@ function setupController (initState, client) {
if (isMetaMaskInternalProcess) {
// communication with popup
controller.setupTrustedCommunication(connectionStream, 'MetaMask')
popupIsOpen = true
} else {
// communication with page
setupUntrustedCommunication(connectionStream, originDomain)
@ -175,6 +157,7 @@ function setupController (initState, client) {
// // //
// // // popup trigger
/*send a message to the client that has focus and tell it to open a window*/
function triggerUi () {
if (!popupIsOpen) notification.show()
}

@ -110,7 +110,7 @@
"valid-url": "^1.0.9",
"vreme": "^3.0.2",
"web3": "0.18.2",
"web3-provider-engine": "^11.0.0",
"web3-provider-engine": "^11.0.1",
"web3-stream-provider": "^2.0.6",
"xtend": "^4.0.1"
},

Loading…
Cancel
Save