mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
WIP: Untrusted external connections eg: dapps
This commit is contained in:
parent
282775c52f
commit
79248ae5cd
@ -1,13 +1,181 @@
|
|||||||
// const SWcontroller = require('./sw-controller')
|
const ParentStream = require('iframe-stream').ParentStream
|
||||||
// const SwStream = require('sw-stream/lib/sw-stream.js')
|
const SWcontroller = require('./sw-controller')
|
||||||
// const startPopup = require('../app/scripts/popup-core')
|
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('outside:open')
|
|
||||||
// const background = new SWcontroller({
|
|
||||||
// fileName: 'sw-build.js',
|
|
||||||
// })
|
|
||||||
// background.on('ready', (readSw) => {
|
|
||||||
// startPopup(SwStream(background.controller))
|
|
||||||
// })
|
|
||||||
// background.startWorker()
|
|
||||||
console.log('hello from controller')
|
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)
|
window.addEventListener('load', web3Detect)
|
||||||
|
|
||||||
function web3Detect() {
|
function web3Detect() {
|
||||||
@ -18,6 +17,7 @@ function startApp(){
|
|||||||
web3.eth.getAccounts(function(err, addresses){
|
web3.eth.getAccounts(function(err, addresses){
|
||||||
if (err) throw err
|
if (err) throw err
|
||||||
console.log('set address', addresses[0])
|
console.log('set address', addresses[0])
|
||||||
|
debugger
|
||||||
primaryAccount = addresses[0]
|
primaryAccount = addresses[0]
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -53,4 +53,4 @@ function startApp(){
|
|||||||
function logToDom(message){
|
function logToDom(message){
|
||||||
document.body.appendChild(document.createTextNode(message))
|
document.body.appendChild(document.createTextNode(message))
|
||||||
console.log(message)
|
console.log(message)
|
||||||
}
|
}
|
||||||
|
@ -41,3 +41,5 @@ function hijackProvider(provider){
|
|||||||
_super(payload, cb)
|
_super(payload, cb)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,4 +22,3 @@ function getProvider(){
|
|||||||
return inpageProvider
|
return inpageProvider
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
27
library/lib/setup-untrusted-connection.js
Normal file
27
library/lib/setup-untrusted-connection.js
Normal file
@ -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,
|
container: document.body,
|
||||||
})
|
})
|
||||||
console.log('outside:open')
|
console.log('outside:open')
|
||||||
|
|
||||||
const background = new SWcontroller({
|
const background = new SWcontroller({
|
||||||
fileName: '/popup/sw-build.js',
|
fileName: '/popup/sw-build.js',
|
||||||
})
|
})
|
||||||
@ -27,5 +28,6 @@ background.on('ready', (readSw) => {
|
|||||||
// startPopup(inpageProvider)
|
// startPopup(inpageProvider)
|
||||||
startPopup(SwStream(background.controller))
|
startPopup(SwStream(background.controller))
|
||||||
})
|
})
|
||||||
|
background.on('message', (messageEvent) => {debugger})
|
||||||
background.startWorker()
|
background.startWorker()
|
||||||
console.log('hello from /library/popup.js')
|
console.log('hello from /library/popup.js')
|
||||||
|
@ -7,22 +7,10 @@ module.exports = class ClientSideServiceWorker extends EventEmitter{
|
|||||||
this.startDelay = opts.startDelay
|
this.startDelay = opts.startDelay
|
||||||
|
|
||||||
this.serviceWorkerApi = navigator.serviceWorker
|
this.serviceWorkerApi = navigator.serviceWorker
|
||||||
this.serviceWorkerApi.onmessage = (event) => this.emit('message', event)
|
this.serviceWorkerApi.onmessage = (messageEvent) => this.emit('message', messageEvent)
|
||||||
this.serviceWorkerApi.onerror = (event) => this.emit('error')
|
this.serviceWorkerApi.onerror = (err) => this.emit('error', err)
|
||||||
|
this.on('message', (messageEvent) => {debugger})
|
||||||
// temporary function
|
|
||||||
this.askForId = (message) => {
|
|
||||||
this.sendMessage('check-in')
|
|
||||||
.then((data) => console.log(`${message}----${data}`))
|
|
||||||
}
|
|
||||||
|
|
||||||
// if (!!this.serviceWorkerApi) this.askForId('before')
|
|
||||||
|
|
||||||
if (opts.initStart) this.startWorker()
|
if (opts.initStart) this.startWorker()
|
||||||
|
|
||||||
this.on('ready', (sw) => {
|
|
||||||
this.askForId('ready')
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get controller () {
|
get controller () {
|
||||||
@ -34,7 +22,6 @@ module.exports = class ClientSideServiceWorker extends EventEmitter{
|
|||||||
return this.registerWorker()
|
return this.registerWorker()
|
||||||
.then((sw) => {
|
.then((sw) => {
|
||||||
this.sw = sw
|
this.sw = sw
|
||||||
this.askForId('after register:')
|
|
||||||
this.sw.onerror = (err) => this.emit('error', err)
|
this.sw.onerror = (err) => this.emit('error', err)
|
||||||
this.sw = sw
|
this.sw = sw
|
||||||
this.emit('ready', this.sw)
|
this.emit('ready', this.sw)
|
||||||
@ -46,7 +33,6 @@ module.exports = class ClientSideServiceWorker extends EventEmitter{
|
|||||||
return this.serviceWorkerApi.register(this.fileName)
|
return this.serviceWorkerApi.register(this.fileName)
|
||||||
.then((registerdWorker) => {
|
.then((registerdWorker) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.askForId('after')
|
|
||||||
let timeOutId = setTimeout(() => {
|
let timeOutId = setTimeout(() => {
|
||||||
if (this.serviceWorkerApi.controller) return resolve(this.serviceWorkerApi.controller)
|
if (this.serviceWorkerApi.controller) return resolve(this.serviceWorkerApi.controller)
|
||||||
if (registerdWorker.active) return resolve(registerdWorker.active)
|
if (registerdWorker.active) return resolve(registerdWorker.active)
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
global.window = global
|
global.window = global
|
||||||
const SWGlobal = self
|
|
||||||
const urlUtil = require('url')
|
|
||||||
const endOfStream = require('end-of-stream')
|
|
||||||
const asyncQ = require('async-q')
|
const asyncQ = require('async-q')
|
||||||
const pipe = require('pump')
|
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 DbController = require('./controllers/index-db-controller')
|
||||||
|
|
||||||
const MetamaskController = require('../app/scripts/metamask-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 LocalStorageStore = require('obs-store/lib/localStorage')
|
||||||
const storeTransform = require('obs-store/lib/transform')
|
const storeTransform = require('obs-store/lib/transform')
|
||||||
const Migrator = require('../app/scripts/lib/migrator/')
|
const Migrator = require('../app/scripts/lib/migrator/')
|
||||||
@ -36,20 +33,6 @@ self.addEventListener('activate', function(event) {
|
|||||||
event.waitUntil(self.clients.claim())
|
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')
|
console.log('inside:open')
|
||||||
|
|
||||||
|
|
||||||
@ -117,8 +100,6 @@ function setupController (initState, client) {
|
|||||||
.catch((err) => {console.error(err)})
|
.catch((err) => {console.error(err)})
|
||||||
})
|
})
|
||||||
function versionifyData(state) {
|
function versionifyData(state) {
|
||||||
// let versionedData
|
|
||||||
// versionedData.data = state
|
|
||||||
return dbController.get()
|
return dbController.get()
|
||||||
.then((rawData) => {
|
.then((rawData) => {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
@ -143,6 +124,7 @@ function setupController (initState, client) {
|
|||||||
if (isMetaMaskInternalProcess) {
|
if (isMetaMaskInternalProcess) {
|
||||||
// communication with popup
|
// communication with popup
|
||||||
controller.setupTrustedCommunication(connectionStream, 'MetaMask')
|
controller.setupTrustedCommunication(connectionStream, 'MetaMask')
|
||||||
|
popupIsOpen = true
|
||||||
} else {
|
} else {
|
||||||
// communication with page
|
// communication with page
|
||||||
setupUntrustedCommunication(connectionStream, originDomain)
|
setupUntrustedCommunication(connectionStream, originDomain)
|
||||||
@ -175,6 +157,7 @@ function setupController (initState, client) {
|
|||||||
// // //
|
// // //
|
||||||
|
|
||||||
// // // popup trigger
|
// // // popup trigger
|
||||||
|
|
||||||
|
/*send a message to the client that has focus and tell it to open a window*/
|
||||||
function triggerUi () {
|
function triggerUi () {
|
||||||
if (!popupIsOpen) notification.show()
|
|
||||||
}
|
}
|
||||||
|
@ -110,7 +110,7 @@
|
|||||||
"valid-url": "^1.0.9",
|
"valid-url": "^1.0.9",
|
||||||
"vreme": "^3.0.2",
|
"vreme": "^3.0.2",
|
||||||
"web3": "0.18.2",
|
"web3": "0.18.2",
|
||||||
"web3-provider-engine": "^11.0.0",
|
"web3-provider-engine": "^11.0.1",
|
||||||
"web3-stream-provider": "^2.0.6",
|
"web3-stream-provider": "^2.0.6",
|
||||||
"xtend": "^4.0.1"
|
"xtend": "^4.0.1"
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user