-
Notifications
You must be signed in to change notification settings - Fork 32
/
cmdLinePKProvider.js
57 lines (47 loc) · 1.98 KB
/
cmdLinePKProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const Web3 = require("web3");
// eslint-disable-next-line
const Accounts = require("web3-eth-accounts");
const ProviderEngine = require("web3-provider-engine");
// eslint-disable-next-line
const Web3Subprovider = require("web3-provider-engine/subproviders/web3.js");
const HookedWalletEthTxSubprovider = require("web3-provider-engine/subproviders/hooked-wallet-ethtx");
const inherits = require("util").inherits;
const commandLineArgs = require("command-line-args");
// https://github.com/ethereumjs/ethereumjs-wallet/blob/master/src/provider-engine.js
// https://github.com/MetaMask/web3-provider-engine/blob/master/subproviders/hooked-wallet.js
inherits(CmdLinePKSubprovider, HookedWalletEthTxSubprovider);
function CmdLinePKSubprovider(address, privateKey, params) {
const opts = params || {};
const lcAddress = address.toLowerCase();
opts.getAccounts = cb => {
cb(null, [address]);
};
opts.getPrivateKey = (a, cb) => {
const lowercasedAddress = a.toLowerCase();
if (lowercasedAddress !== lcAddress) {
cb(new Error(`Account ${lowercasedAddress} not found`));
} else {
cb(null, privateKey);
}
};
CmdLinePKSubprovider.super_.call(this, opts);
}
export function cmdLinePKProvider(nodeUrl) {
const web3HttpProvider = new Web3.providers.HttpProvider(nodeUrl);
const engine = new ProviderEngine();
const optionDefinitions = [{ name: "pk", type: String }];
const options = commandLineArgs(optionDefinitions, { partial: true });
if (!options.pk) {
throw new Error("Private key wasn't provided. Use --pk paremeter");
}
const privateKey = Buffer.from(options.pk.substr(2), "hex");
const accounts = new Accounts(web3HttpProvider);
const address = accounts.privateKeyToAccount(options.pk).address;
// eslint-disable-next-line
console.log(`Recovered address ${address}`);
engine.addProvider(new CmdLinePKSubprovider(address, privateKey));
engine.addProvider(new Web3Subprovider(web3HttpProvider));
engine.start();
engine.stop();
return engine;
}