Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
init(cb) {
if (this.initialized) { return cb(); }
this.initialized = true;
this.opts.serialized = fs.readFileSync(this.opts.keystore).toString();
this.opts.ks = Lightwallet.keystore.deserialize(this.opts.serialized);
// this.opts.addresses = this.opts.ks.getAddresses().map(a => `0x${a}`);
this.opts.addresses = this.opts.ks.getAddresses().map(a => a); // removed prefix 0x
// pass opts
const { pollingInterval } = this.opts;
this.engine = new ProviderEngine({ pollingInterval });
this.engine.addProvider(new FilterProvider());
this.engine.addProvider(new NonceSubprovider());
this.engine.addProvider(new LighwalletSubprovider(this.opts));
this.engine.addProvider(new RpcSubprovider(this.opts));
// this.engine._fetchLatestBlock();
this.engine.start();
if (this.opts.prefund) {
console.log(`Ensuring all lightwallet accounts have ${this.opts.prefund / 1e18} Ether`);
return prefund(this.opts).then(cb);
}
return cb();
}
send() {
connect ({dispatch, commit, getters, state}) {
let providerEngine = null
if (window.web3) {
providerEngine = window.web3.currentProvider
} else {
const ROPSTEN_ENDPOINT = 'https://infura.io'
providerEngine = new ProviderEngine()
providerEngine.addProvider(new FilterSubprovider())
providerEngine.addProvider(new RpcSubprovider({rpcUrl: ROPSTEN_ENDPOINT}))
providerEngine.start()
}
zeroEx = new ZeroEx(providerEngine)
zeroEx._web3Wrapper.web3.version.getNetwork((error, response) => {
if (error) console.error(error)
if (parseInt(response) === 42 && window.location.hostname !== 'kovan.0xchange.me') {
window.location.href = 'https://kovan.0xchange.me'
} else if (parseInt(response) === 1 && window.location.hostname === 'kovan.0xchange.me') {
window.location.href = 'https://0xchange.me'
}
})
// commit('SET_ZERO_EX', zeroEx)
// 3117574 kovan
// 4145578 mainnet
export default function generateWeb3(store, network) {
// import store getters...
if (!network.provider) { return null; }
// TODO set poll speed based on network configs
const providerEngine = new ProviderEngine({ pollingInterval: 4 * 1000 });
providerEngine.addProvider(new FilterSubProvider());
providerEngine.addProvider(new ReduxSubProvider({ store, networkId: network.id }));
providerEngine.addProvider(new RpcProvider({ rpcUrl: network.provider }));
return new Web3(providerEngine);
}
const publicNodeUrlsIfExistsForNetworkId = configs.PUBLIC_NODE_URLS_BY_NETWORK_ID[networkIdIfExists];
const isPublicNodeAvailableForNetworkId = !_.isUndefined(publicNodeUrlsIfExistsForNetworkId);
if (shouldUserLedgerProvider && isNetworkIdAvailable) {
const isU2FSupported = await utils.isU2FSupportedAsync();
if (!isU2FSupported) {
throw new Error('Cannot update providerType to LEDGER without U2F support');
}
const provider = new Web3ProviderEngine();
const ledgerWalletConfigs = {
networkId: networkIdIfExists,
ledgerEthereumClientFactoryAsync: ledgerEthereumBrowserClientFactoryAsync,
};
const ledgerSubprovider = new LedgerSubprovider(ledgerWalletConfigs);
provider.addProvider(ledgerSubprovider);
provider.addProvider(new FilterSubprovider());
const rpcSubproviders = _.map(configs.PUBLIC_NODE_URLS_BY_NETWORK_ID[networkIdIfExists], publicNodeUrl => {
return new RPCSubprovider(publicNodeUrl);
});
provider.addProvider(new RedundantSubprovider(rpcSubproviders));
provider.start();
return [provider, ledgerSubprovider];
} else if (doesInjectedProviderExist && isPublicNodeAvailableForNetworkId) {
// We catch all requests involving a users account and send it to the injectedWeb3
// instance. All other requests go to the public hosted node.
const provider = new Web3ProviderEngine();
const providerName = this._getNameGivenProvider(injectedProviderIfExists);
// Wrap Metamask in a compatability wrapper MetamaskSubprovider (to handle inconsistencies)
const signerSubprovider =
providerName === constants.PROVIDER_NAME_METAMASK
? new MetamaskSubprovider(injectedProviderIfExists)
: new SignerSubprovider(injectedProviderIfExists);
const msgHashBuff = EthUtil.hashPersonalMessage(dataBuff);
const sig = EthUtil.ecsign(msgHashBuff, pkey);
const rpcSig = EthUtil.toRpcSig(sig.v, sig.r, sig.s);
cb(null, rpcSig);
},
signPersonalMessage(...args: any[]) {
this.signMessage(...args);
}
})
);
!shareNonce
? this.engine.addProvider(new NonceSubProvider())
: this.engine.addProvider(singletonNonceSubProvider);
this.engine.addProvider(new FiltersSubprovider());
if (typeof provider === "string") {
// shim Web3 to give it expected sendAsync method. Needed if web3-engine-provider upgraded!
// Web3.providers.HttpProvider.prototype.sendAsync =
// Web3.providers.HttpProvider.prototype.send;
this.engine.addProvider(
new ProviderSubprovider(
// @ts-ignore
new Web3.providers.HttpProvider(provider, { keepAlive: false })
)
);
} else {
this.engine.addProvider(new ProviderSubprovider(provider));
}
this.engine.start(); // Required by the provider engine.
}
export function configureEngine(engine, config) {
let supportsSubscribe = websocketRegex.test(config.rpcUrl)
normaliseConfig(config)
if (config.debug) engine.addProvider(new LoggingSubprovider())
if (config.useHacks) {
var syncCacheProvider = new SyncCacheSubprovider({cache: config.cache || window.localStorage, lastChanged: config.lastChanged})
engine.addProvider(syncCacheProvider)
syncCacheProvider.patchSend(engine)
}
engine.addProvider(new DefaultFixture({ web3_clientVersion: `SpaceSuit/${version}/javascript` }))
engine.addProvider(new DefaultBlockParameterSubprovider())
engine.addProvider(new NonceTrackerSubprovider())
engine.addProvider(new SanitizingSubprovider())
engine.addProvider(new CacheSubprovider())
if (supportsSubscribe) {
engine.addProvider(new FilterSubprovider({ pendingBlockTimeout: 5000 }))
} else {
let subscriptionSubprovider = new SubscriptionSubprovider({ pendingBlockTimeout: 5000 })
subscriptionSubprovider.on('data', (err, notification) => {
engine.emit('data', err, notification)
})
engine.addProvider(subscriptionSubprovider)
}
engine.addProvider(new InflightCacheSubprovider())
if (config.useHacks) engine.addProvider(new LowerCaseAddressesSubprovider())
if (config.useHacks) engine.addProvider(new GasPaddingSubprovider())
engine.addProvider(new SignToPersonalSignSubprovider({ stripPrefix: config.useHacks }))
engine.addProvider(new MinMaxGasPriceSubprovider({ minGasPrice: config.minGasPrice, maxGasPrice: config.maxGasPrice }))
let transportPromise = config.transport || TransportU2F.create()
let ledgerProvider = createLedgerSubprovider(
transportPromise, {
accountsOffset: config.accountsOffset,
this.addProvider(
new FixtureSubprovider({
eth_hashrate: '0x00',
eth_mining: false,
eth_syncing: true,
net_listening: true,
web3_clientVersion: `WalletConnect/v${pkg.version}/javascript`
})
)
this.addProvider(new CacheSubprovider())
this.addProvider(new SubscriptionsSubprovider())
this.addProvider(new FilterSubprovider())
this.addProvider(new NonceSubprovider())
this.addProvider(
new HookedWalletSubprovider({
getAccounts: async cb => {
try {
const wc = await this.getWalletConnector()
const accounts = wc.accounts
if (accounts && accounts.length) {
cb(null, accounts)
} else {
cb(new Error('Failed to get accounts'))
}
} catch (error) {
cb(error)