1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/lib/metamask-provider.js

56 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-01-15 03:26:54 +01:00
const ProviderEngine = require('web3-provider-engine')
const CacheSubprovider = require('web3-provider-engine/subproviders/cache.js')
2016-01-28 07:42:50 +01:00
const FixtureSubprovider = require('web3-provider-engine/subproviders/fixture.js')
2016-01-15 03:26:54 +01:00
const FilterSubprovider = require('web3-provider-engine/subproviders/filters.js')
const VmSubprovider = require('web3-provider-engine/subproviders/vm.js')
2016-01-15 11:03:42 +01:00
const HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js')
2016-01-15 03:26:54 +01:00
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js')
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
module.exports = metamaskProvider
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
function metamaskProvider(opts){
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
var engine = new ProviderEngine()
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
// cache layer
engine.addProvider(new CacheSubprovider())
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
// static results
2016-01-28 07:42:50 +01:00
engine.addProvider(new FixtureSubprovider({
2016-01-15 03:26:54 +01:00
web3_clientVersion: 'MetaMask-ProviderEngine/v0.0.0/javascript',
net_listening: true,
eth_hashrate: '0x0',
eth_mining: false,
eth_syncing: true,
2016-01-15 11:03:42 +01:00
}))
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
// filters
engine.addProvider(new FilterSubprovider())
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
// vm
engine.addProvider(new VmSubprovider())
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
// id mgmt
2016-01-15 11:03:42 +01:00
engine.addProvider(new HookedWalletSubprovider({
getAccounts: opts.getAccounts,
2016-01-19 02:05:46 +01:00
signTransaction: opts.signTransaction,
2016-01-15 11:03:42 +01:00
}))
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
// data source
engine.addProvider(new RpcSubprovider({
2016-01-15 11:03:42 +01:00
rpcUrl: opts.rpcUrl,
2016-01-15 03:26:54 +01:00
}))
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
// log new blocks
2016-01-17 01:22:54 +01:00
engine.on('block', function(block){
// console.log('================================')
console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
// console.log('================================')
})
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
// start polling for blocks
engine.start()
2015-08-01 08:05:04 +02:00
2016-01-15 03:26:54 +01:00
return engine
2015-08-01 08:05:04 +02:00
}