Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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())
// vm
engine.addProvider(new VmSubprovider())
// id mgmt
engine.addProvider(new HookedWalletSubprovider({
getAccounts: opts.getAccounts,
approveTx: opts.approveTx,
signTransaction: opts.signTransaction,
}))
// data source
engine.addProvider(new RpcSubprovider({
rpcUrl: opts.rpcUrl,
}))
// log new blocks
engine.on('block', function(block){
// console.log('================================')
console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
// console.log('================================')
})
this.wallets = {};
this.addresses = [];
// from https://github.com/trufflesuite/truffle-hdwallet-provider/pull/25/commits
for (const key of privateKeys) {
const wallet = ethereumjsWallet.fromPrivateKey(new Buffer(key, 'hex'));
const addr = `0x${wallet.getAddress().toString('hex')}`;
this.addresses.push(addr);
this.wallets[addr] = wallet;
}
const tmp_accounts = this.addresses;
const tmp_wallets = this.wallets;
this.engine = new ProviderEngine();
this.engine.addProvider(new HookedSubprovider({
getAccounts(cb) {
cb(null, tmp_accounts);
},
getPrivateKey(address, cb) {
if (!tmp_wallets[address]) {
return cb('Account not found');
}
cb(null, tmp_wallets[address].getPrivateKey().toString('hex'));
},
signTransaction(txParams, cb) {
let pkey;
if (tmp_wallets[txParams.from]) {
pkey = tmp_wallets[txParams.from].getPrivateKey();
} else {
cb('Account not found');
}
// 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);
},
signTransaction: function(txData, cb) {
txData.gasPrice = parseInt(txData.gasPrice, 16);
txData.nonce = parseInt(txData.nonce, 16);
txData.gasLimit = txData.gas;
var tx = wallet.txutils.createContractTx(addr, txData);
var signed = wallet.signing.signTx(ks, pwDerivedKey, tx.tx, addr);
cb(null, signed);
}
}));
}
constructor(options, syncOptions) {
super();
const engine = this
const web3 = new Web3(this)
const { rpcUrl } = options
context.web3 = web3
globalSyncOptions = syncOptions
engine.addProvider(new CacheSubprovider())
engine.addProvider(new SubscriptionsSubprovider())
engine.addProvider(new FilterSubprovider())
engine.addProvider(hookedSubProvider = new HookedWalletSubprovider(options))
engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(rpcUrl)))
engine.on('error', err => console.error(err.stack))
engine.isTrust = true
engine.start()
}
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);
var msg = new Buffer(options.data.replace('0x', ''), 'hex');
let msgHash = Utils.hashPersonalMessage(msg);
if (payload.method === 'eth_sendTransaction'
&& core.shouldSkipSigning(network, payload.params[0])) {
const result = await core.handleRequest(network, payload);
end(null, result);
} else {
next();
}
} catch (err) {
end(err);
}
},
setEngine() {},
});
this.engine.addProvider(
new HookedSubprovider({
async getAccounts(cb) {
try {
cb(null, await core.getAccounts());
} catch (err) {
cb(err)
}
},
signTransaction(txParams, cb) {
txParams.chainId = +network;
core.signTx(txParams)
.then(signedTx => cb(null, signedTx))
.catch(err => cb(err));
},
signMessage({ data, from }, cb) {
core.signMsg(data, from)
.then(signature => cb(null, signature))
this.wallet_hdpath = "m/44'/60'/0'/0/";
this.wallets = {};
this.addresses = [];
for (let i = address_index; i < address_index + num_addresses; i++){
var wallet = this.hdwallet.derivePath(this.wallet_hdpath + i).getWallet();
var addr = '0x' + wallet.getAddress().toString('hex');
this.addresses.push(addr);
this.wallets[addr] = wallet;
}
const tmp_accounts = this.addresses;
const tmp_wallets = this.wallets;
this.engine = new ProviderEngine();
this.engine.addProvider(new HookedSubprovider({
getAccounts: function(cb : Function) { cb(null, tmp_accounts) },
getPrivateKey: function(address: string, cb: Function) {
if (!tmp_wallets[address]) { return cb('Account not found'); }
else { cb(null, tmp_wallets[address].getPrivateKey().toString('hex')); }
},
signTransaction: function(txParams: any, cb: Function) {
let pkey;
if (tmp_wallets[txParams.from]) { pkey = tmp_wallets[txParams.from].getPrivateKey(); }
else { cb('Account not found'); }
var tx = new Transaction(txParams);
tx.sign(pkey);
var rawTx = '0x' + tx.serialize().toString('hex');
cb(null, rawTx);
}
}));
this.engine.addProvider(new FiltersSubprovider());