forked from bpeters/react-native-truffle-hdwallet-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (32 loc) · 1.47 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
const bip39 = require('react-native-bip39');
const hdkey = require('ethereumjs-wallet/hdkey');
const ProviderEngine = require('web3-provider-engine');
const FiltersSubprovider = require('web3-provider-engine/subproviders/filters.js');
const WalletSubprovider = require('web3-provider-engine/subproviders/wallet.js');
const Web3Subprovider = require('web3-provider-engine/subproviders/web3.js');
const Web3 = require('web3');
function HDWalletProvider(mnemonic, provider_url, address_index) {
this.mnemonic = mnemonic;
this.hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(mnemonic));
if (address_index == null) {
address_index = 0;
}
this.wallet_hdpath = "m/44'/60'/0'/0/";
this.wallet = this.hdwallet.derivePath(this.wallet_hdpath + address_index).getWallet();
this.address = "0x" + this.wallet.getAddress().toString("hex");
this.engine = new ProviderEngine();
this.engine.addProvider(new WalletSubprovider(this.wallet, {}));
this.engine.addProvider(new FiltersSubprovider());
this.engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(provider_url)));
this.engine.start(); // Required by the provider engine.
};
HDWalletProvider.prototype.sendAsync = function() {
this.engine.sendAsync.apply(this.engine, arguments);
};
HDWalletProvider.prototype.send = function() {
return this.engine.send.apply(this.engine, arguments);
};
HDWalletProvider.prototype.getAddress = function() {
return this.address;
};
module.exports = HDWalletProvider;