parent
cc935a1eba
commit
72a747165d
@ -1,59 +1,18 @@ |
||||
const web3 = require('web3') |
||||
const BlockAppsWeb3Provider = require('blockapps-web3') |
||||
const Transaction = require('ethereumjs-tx') |
||||
require('object.entries').shim() |
||||
const Web3 = require('web3') |
||||
const StreamProvider = require('./lib/stream-provider.js') |
||||
const LocalMessageDuplexStream = require('./lib/local-message-stream.js') |
||||
|
||||
// const rpcUrl = 'https://rpc.metamask.io'
|
||||
|
||||
// var provider = new MetamaskProvider(forwardPayload, rpcUrl)
|
||||
var provider = new BlockAppsWeb3Provider({ |
||||
host: 'http://hacknet.blockapps.net', |
||||
// host: 'http://api.blockapps.net',
|
||||
transaction_signer: {
|
||||
// Can be any object that implements the following methods:
|
||||
hasAddress: function(address, callback) { |
||||
console.log('metamask provider - asked for address ownership', address) |
||||
callback(null, true) |
||||
}, |
||||
signTransaction: function(txParams, callback) { |
||||
txParams.gasLimit = txParams.gas |
||||
var tx = new Transaction(txParams) |
||||
tx.sign(new Buffer('0d0ba14043088cd629a978b49c8691deca5926f0271432bc0064e4745bac0a9f', 'hex')) |
||||
callback(null, '0x'+tx.serialize().toString('hex')) |
||||
}, |
||||
}, |
||||
coinbase: '0x00000000000', |
||||
accounts: ['0x985095ef977ba75fb2bb79cd5c4b84c81392dff6'], |
||||
// host: function(){ debugger },
|
||||
}); |
||||
// setup plugin communication
|
||||
var pluginStream = new LocalMessageDuplexStream({ |
||||
name: 'inpage', |
||||
target: 'contentscript', |
||||
}) |
||||
var remoteProvider = new StreamProvider() |
||||
remoteProvider.pipe(pluginStream).pipe(remoteProvider) |
||||
|
||||
const documentOrigin = window.location.origin |
||||
const allowedMessageTarget = 'metamask' |
||||
const allowedMessageType = 'addUnsignedTx' |
||||
|
||||
web3.setProvider(provider) |
||||
// disable setProvider
|
||||
// create web3
|
||||
var web3 = new Web3(remoteProvider) |
||||
window.web3 = web3 |
||||
web3.setProvider = function(){} |
||||
|
||||
// injecting web3
|
||||
console.log('Metamask injected web3') |
||||
|
||||
|
||||
// log all the stuff!
|
||||
// provider.verbosity = 1
|
||||
|
||||
// web3.currentProvider.vm.onStep = function(data, cb){
|
||||
// console.log(data)
|
||||
// cb()
|
||||
// }
|
||||
|
||||
window.web3 = web3 |
||||
|
||||
|
||||
function forwardPayload(payload){ |
||||
window.postMessage({ |
||||
to: allowedMessageTarget, |
||||
type: allowedMessageType, |
||||
payload: payload, |
||||
}, documentOrigin) |
||||
} |
@ -0,0 +1,53 @@ |
||||
const Duplex = require('readable-stream').Duplex |
||||
const inherits = require('util').inherits |
||||
|
||||
module.exports = LocalMessageDuplexStream |
||||
|
||||
|
||||
inherits(LocalMessageDuplexStream, Duplex) |
||||
|
||||
function LocalMessageDuplexStream(opts){ |
||||
Duplex.call(this, { |
||||
objectMode: true, |
||||
}) |
||||
|
||||
// this._origin = opts.origin
|
||||
this._name = opts.name |
||||
this._target = opts.target |
||||
|
||||
// console.log('LocalMessageDuplexStream ('+this._name+') - initialized...')
|
||||
window.addEventListener('message', this._onMessage.bind(this), false) |
||||
} |
||||
|
||||
// private
|
||||
|
||||
LocalMessageDuplexStream.prototype._onMessage = function(event){ |
||||
var msg = event.data |
||||
// console.log('LocalMessageDuplexStream ('+this._name+') - heard message...')
|
||||
// validate message
|
||||
if (event.origin !== location.origin) return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (event.origin !== location.origin) ')
|
||||
if (typeof msg !== 'object') return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (typeof msg !== "object") ')
|
||||
if (msg.target !== this._name) return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (msg.target !== this._name) ', msg.target, this._name)
|
||||
if (!msg.data) return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (!msg.data) ')
|
||||
// console.log('LocalMessageDuplexStream ('+this._name+') - accepted', msg.data)
|
||||
// forward message
|
||||
this.push(msg.data) |
||||
} |
||||
|
||||
// stream plumbing
|
||||
|
||||
LocalMessageDuplexStream.prototype._read = noop |
||||
|
||||
LocalMessageDuplexStream.prototype._write = function(data, encoding, cb){ |
||||
// console.log('LocalMessageDuplexStream ('+this._name+') - sending message...')
|
||||
var message = { |
||||
target: this._target, |
||||
data: data, |
||||
} |
||||
window.postMessage(message, location.origin) |
||||
cb() |
||||
} |
||||
|
||||
// util
|
||||
|
||||
function noop(){} |
@ -0,0 +1,36 @@ |
||||
const Duplex = require('readable-stream').Duplex |
||||
const inherits = require('util').inherits |
||||
|
||||
module.exports = PortDuplexStream |
||||
|
||||
|
||||
inherits(PortDuplexStream, Duplex) |
||||
|
||||
function PortDuplexStream(port){ |
||||
Duplex.call(this, { |
||||
objectMode: true, |
||||
}) |
||||
this._port = port |
||||
port.onMessage.addListener(this._onMessage.bind(this)) |
||||
} |
||||
|
||||
// private
|
||||
|
||||
PortDuplexStream.prototype._onMessage = function(msg){ |
||||
// console.log('PortDuplexStream - saw message', msg)
|
||||
this.push(msg) |
||||
} |
||||
|
||||
// stream plumbing
|
||||
|
||||
PortDuplexStream.prototype._read = noop |
||||
|
||||
PortDuplexStream.prototype._write = function(msg, encoding, cb){ |
||||
// console.log('PortDuplexStream - sent message', msg)
|
||||
this._port.postMessage(msg) |
||||
cb() |
||||
} |
||||
|
||||
// util
|
||||
|
||||
function noop(){} |
@ -0,0 +1,50 @@ |
||||
const Duplex = require('readable-stream').Duplex |
||||
const inherits = require('util').inherits |
||||
|
||||
module.exports = StreamProvider |
||||
|
||||
|
||||
inherits(StreamProvider, Duplex) |
||||
|
||||
function StreamProvider(){ |
||||
Duplex.call(this, { |
||||
objectMode: true, |
||||
}) |
||||
|
||||
this._handlers = {} |
||||
} |
||||
|
||||
// public
|
||||
|
||||
StreamProvider.prototype.send = function(payload){ |
||||
throw new Error('StreamProvider - does not support synchronous RPC calls') |
||||
} |
||||
|
||||
StreamProvider.prototype.sendAsync = function(payload, callback){ |
||||
// console.log('StreamProvider - sending payload', payload)
|
||||
this._handlers[payload.id] = callback |
||||
this.push(payload) |
||||
} |
||||
|
||||
// private
|
||||
|
||||
StreamProvider.prototype._onResponse = function(payload){ |
||||
// console.log('StreamProvider - got response', payload)
|
||||
var callback = this._handlers[payload.id] |
||||
if (!callback) throw new Error('StreamProvider - Unknown response id') |
||||
delete this._handlers[payload.id] |
||||
callback(null, payload) |
||||
} |
||||
|
||||
// stream plumbing
|
||||
|
||||
StreamProvider.prototype._read = noop |
||||
|
||||
StreamProvider.prototype._write = function(msg, encoding, cb){ |
||||
this._onResponse(msg) |
||||
cb() |
||||
} |
||||
|
||||
// util
|
||||
|
||||
function noop(){} |
Loading…
Reference in new issue