Skip to content

Instantly share code, notes, and snippets.

@JasoonS
Created May 7, 2018 00:19
Show Gist options
  • Save JasoonS/11fca1a98f1eb41a79987a041541e8c8 to your computer and use it in GitHub Desktop.
Save JasoonS/11fca1a98f1eb41a79987a041541e8c8 to your computer and use it in GitHub Desktop.
Private Key ProviderEngine for Ethereum
// as used here: https://github.com/andytudhope/SeekerBackend/blob/e529f586c0b91c6591d05e90309d1d5e0553e514/utils/privateKeyProviderEngine.js
const Wallet = require('ethereumjs-wallet')
const ethUtils = require('ethereumjs-util')
// var bip39 = require("bip39");
// var hdkey = require('ethereumjs-wallet/hdkey');
var ProviderEngine = require("web3-provider-engine");
var FiltersSubprovider = require('web3-provider-engine/subproviders/filters.js');
var HookedSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js');
var Web3Subprovider = require("web3-provider-engine/subproviders/web3.js");
var Web3 = require("web3");
var Transaction = require('ethereumjs-tx');
function privateKeyProvider(privateKey, provider_url) {
this.wallets = {};
this.addresses = [];
const privateKeyBuffer = ethUtils.toBuffer(privateKey)
const wallet = Wallet.fromPrivateKey(privateKeyBuffer)
const addr = wallet.getAddressString()
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) { cb(null, tmp_accounts) },
getPrivateKey: function(address, cb) {
if (!tmp_wallets[address]) { return cb('Account not found'); }
else { cb(null, tmp_wallets[address].getPrivateKey().toString('hex')); }
},
signTransaction: function(txParams, cb) {
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);
},
signMessage: function(a, cb) {
let pkey;
if (tmp_wallets[a.from]) { pkey = tmp_wallets[a.from].getPrivateKey(); }
else { cb('Account not found'); }
const web3 = new Web3()
const prefix = "\x19Ethereum Signed Message:\n32";
const prefixedBytes = web3.fromAscii(prefix) + a.data.slice(2)
const prefixedHash = web3.sha3(prefixedBytes, { encoding: 'hex' })
var echash = Buffer.from(prefixedHash.slice(2), 'hex')
const ecresult = ethUtils.ecsign(echash, pkey)
const result = ethUtils.toRpcSig(ecresult.v, ecresult.r, ecresult.s)
cb(null, result)
}
}));
this.engine.addProvider(new FiltersSubprovider());
this.engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(provider_url)));
this.engine.start(); // Required by the provider engine.
};
privateKeyProvider.prototype.sendAsync = function() {
this.engine.sendAsync.apply(this.engine, arguments);
};
privateKeyProvider.prototype.send = function() {
return this.engine.send.apply(this.engine, arguments);
};
// returns the address of the given address_index, first checking the cache
privateKeyProvider.prototype.getAddress = function(idx) {
console.log('getting addresses', this.addresses[0], idx)
if (!idx) { return this.addresses[0]; }
else { return this.addresses[idx]; }
}
// returns the addresses cache
privateKeyProvider.prototype.getAddresses = function() {
return this.addresses;
}
module.exports = privateKeyProvider;const Wallet = require('ethereumjs-wallet')
const ethUtils = require('ethereumjs-util')
// var bip39 = require("bip39");
// var hdkey = require('ethereumjs-wallet/hdkey');
var ProviderEngine = require("web3-provider-engine");
var FiltersSubprovider = require('web3-provider-engine/subproviders/filters.js');
var HookedSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js');
var Web3Subprovider = require("web3-provider-engine/subproviders/web3.js");
var Web3 = require("web3");
var Transaction = require('ethereumjs-tx');
function privateKeyProvider(privateKey, provider_url) {
this.wallets = {};
this.addresses = [];
const privateKeyBuffer = ethUtils.toBuffer(privateKey)
const wallet = Wallet.fromPrivateKey(privateKeyBuffer)
const addr = wallet.getAddressString()
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) { cb(null, tmp_accounts) },
getPrivateKey: function(address, cb) {
if (!tmp_wallets[address]) { return cb('Account not found'); }
else { cb(null, tmp_wallets[address].getPrivateKey().toString('hex')); }
},
signTransaction: function(txParams, cb) {
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);
},
signMessage: function(a, cb) {
let pkey;
if (tmp_wallets[a.from]) { pkey = tmp_wallets[a.from].getPrivateKey(); }
else { cb('Account not found'); }
const web3 = new Web3()
const prefix = "\x19Ethereum Signed Message:\n32";
const prefixedBytes = web3.fromAscii(prefix) + a.data.slice(2)
const prefixedHash = web3.sha3(prefixedBytes, { encoding: 'hex' })
var echash = Buffer.from(prefixedHash.slice(2), 'hex')
const ecresult = ethUtils.ecsign(echash, pkey)
const result = ethUtils.toRpcSig(ecresult.v, ecresult.r, ecresult.s)
cb(null, result)
}
}));
this.engine.addProvider(new FiltersSubprovider());
this.engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(provider_url)));
this.engine.start(); // Required by the provider engine.
};
privateKeyProvider.prototype.sendAsync = function() {
this.engine.sendAsync.apply(this.engine, arguments);
};
privateKeyProvider.prototype.send = function() {
return this.engine.send.apply(this.engine, arguments);
};
// returns the address of the given address_index, first checking the cache
privateKeyProvider.prototype.getAddress = function(idx) {
console.log('getting addresses', this.addresses[0], idx)
if (!idx) { return this.addresses[0]; }
else { return this.addresses[idx]; }
}
// returns the addresses cache
privateKeyProvider.prototype.getAddresses = function() {
return this.addresses;
}
module.exports = privateKeyProvider;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment