Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function metamaskProvider(opts){
var engine = new ProviderEngine()
// nonce tracker
engine.addProvider(new NonceTrackerSubprovider())
// cache layer
engine.addProvider(new CacheSubprovider())
// static results
engine.addProvider(new FixtureSubprovider({
web3_clientVersion: 'MetaMask-ProviderEngine/v0.0.0/javascript',
net_listening: true,
eth_hashrate: '0x0',
eth_mining: false,
eth_syncing: true,
}))
// filters
engine.addProvider(new FilterSubprovider())
signTransaction(txParams, cb) {
let pkey;
if (tmp_wallets[txParams.from]) {
pkey = tmp_wallets[txParams.from].getPrivateKey();
} else {
cb('Account not found');
}
const tx = new Transaction(txParams);
tx.sign(pkey);
const rawTx = `0x${tx.serialize().toString('hex')}`;
cb(null, rawTx);
},
}));
(!shareNonce)
? this.engine.addProvider(new NonceSubProvider())
: this.engine.addProvider(singletonNonceSubProvider);
this.engine.addProvider(new FiltersSubprovider());
this.engine.addProvider(new ProviderSubprovider(new Web3.providers.HttpProvider(providerUrl)));
this.engine.start(); // Required by the provider engine.
}
engine.addProvider(new FixtureSubprovider({
web3_clientVersion: 'ProviderEngine/v0.0.0/javascript',
net_listening: true,
eth_hashrate: '0x00',
eth_mining: false,
eth_syncing: true,
}));
// cache layer
engine.addProvider(new CacheSubprovider());
// filters
engine.addProvider(new FilterSubprovider())
// pending nonce
engine.addProvider(new NonceSubprovider());
// vm
//engine.addProvider(new VmSubprovider());
if (secretSeed) {
var ks = new wallet.keystore(secretSeed, pwDerivedKey);
ks.generateNewAddress(pwDerivedKey, 1);
var addr = '0x' + ks.getAddresses()[0];
engine.addProvider(new HookedWalletSubprovider({
getAccounts: function(cb) {
cb(null, [addr]);
},
approveTransaction: function(txParams, cb) {
cb(null, true);
},
/* eslint-disable */
const ProviderEngine = require('web3-provider-engine');
const FiltersSubprovider = require('web3-provider-engine/subproviders/filters.js');
const NonceSubProvider = require('web3-provider-engine/subproviders/nonce-tracker.js');
const HookedSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js');
const ProviderSubprovider = require('web3-provider-engine/subproviders/provider.js');
const Web3 = require('web3');
const Transaction = require('ethereumjs-tx');
const ethereumjsWallet = require('ethereumjs-wallet');
// This line shares nonce state across multiple provider instances. Necessary
// because within truffle the wallet is repeatedly newed if it's declared in the config within a
// function, resetting nonce from tx to tx. An instance can opt out
// of this behavior by passing `shareNonce=false` to the constructor.
// See issue #65 for more
const singletonNonceSubProvider = new NonceSubProvider();
// FIXME: Hack. See https://github.com/ethereum/web3.js/issues/1119
Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send;
/* The following provider is a patch over truffle-hdwallet-provider 0.0.7-beta.0
to support importing accounts by private keys instead of by mnemonic.
The idea was taken from: https://github.com/rhlsthrm/truffle-hdwallet-provider-privkey
This is useful when using wallets created on MyEtherWallet.
It's not even close to an ideal solution, let's discuss following steps at:
https://github.com/wibsonorg/wibson-core/issues/63
*/
function PrivKeyWalletProvider(privateKeys, providerUrl, shareNonce = true) {
this.wallets = {};
this.addresses = [];
// from https://github.com/trufflesuite/truffle-hdwallet-provider/pull/25/commits
initWeb3Provider () {
//filters
//this.engine.addProvider(new FilterSubprovider());
//pending nonce
this.engine.addProvider(new NonceSubprovider());
//accounts management
this.engine.addProvider(new HookedWalletSubprovider({
getAccounts: (cb) => {
this.getVault()
.then(vault => {
if(vault) {
cb(null, vault.getAddresses());
}else {
cb('no keys available, login frst', null);
}
});
},
signMessage: (options, cb) => {
this.getVault().then(vault => {
var secretKey = vault.exportPrivateKey(options.from, this.pwDerivedKey);
},
signMessage({ data, from }, cb) {
core.signMsg(data, from)
.then(signature => cb(null, signature))
.catch(err => cb(err));
},
signPersonalMessage({ data, from }, cb) {
core.signMsg(data, from)
.then(signature => cb(null, signature))
.catch(err => cb(err));
}
})
);
//this.engine.addProvider(new GaspriceProvider());
this.engine.addProvider(new NonceSubProvider());
this.engine.addProvider(new FiltersSubprovider());
this.engine.addProvider({
handleRequest(payload, next, end) {
core.handleRequest(network, payload)
.then(result => {
try {
end(null, result);
} catch (err) {
if (err.message !== 'Could not find block') {
throw err;
}
}
})
.catch(err => end(err));
},