-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
43 lines (32 loc) · 1.48 KB
/
index.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
const ProviderEngine = require("web3-provider-engine");
const FiltersSubprovider = require('web3-provider-engine/subproviders/filters');
const WalletSubprovider = require('web3-provider-engine/subproviders/wallet');
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
const Wallet = require('ethereumjs-wallet').default;
const NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker');
function PrivateKeyProvider(privateKey, providerUrl) {
if (!privateKey) {
throw new Error(`Private Key missing, non-empty string expected, got "${privateKey}"`);
}
if (!providerUrl) {
throw new Error(`Provider URL missing, non-empty string expected, got "${providerUrl}"`);
}
if (privateKey.startsWith('0x')) {
privateKey = privateKey.substr(2, privateKey.length);
}
this.wallet = new Wallet(new Buffer(privateKey, "hex"));
this.address = "0x" + this.wallet.getAddress().toString("hex");
this.engine = new ProviderEngine();
this.engine.addProvider(new FiltersSubprovider());
this.engine.addProvider(new NonceSubprovider());
this.engine.addProvider(new WalletSubprovider(this.wallet, {}));
this.engine.addProvider(new RpcSubprovider({ rpcUrl: providerUrl }));
this.engine.start();
}
PrivateKeyProvider.prototype.sendAsync = function() {
this.engine.sendAsync.apply(this.engine, arguments);
};
PrivateKeyProvider.prototype.send = function() {
return this.engine.send.apply(this.engine, arguments);
};
module.exports = PrivateKeyProvider;