aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts
diff options
context:
space:
mode:
authorBruno Barbieri <bruno.barbieri@consensys.net>2018-08-18 01:45:46 +0800
committerGitHub <noreply@github.com>2018-08-18 01:45:46 +0800
commitbd904c86f6f27eb50e0633f46af6831869b99893 (patch)
treea92c329bc53f99f88fdd0a72e522dc94f4dfb83e /app/scripts
parent0391ec37c84376e320fcd55d64118a39035ab8ca (diff)
parent755369e4e48e71b04b4dbb9cb702353c09b66e28 (diff)
downloadtangerine-wallet-browser-bd904c86f6f27eb50e0633f46af6831869b99893.tar
tangerine-wallet-browser-bd904c86f6f27eb50e0633f46af6831869b99893.tar.gz
tangerine-wallet-browser-bd904c86f6f27eb50e0633f46af6831869b99893.tar.bz2
tangerine-wallet-browser-bd904c86f6f27eb50e0633f46af6831869b99893.tar.lz
tangerine-wallet-browser-bd904c86f6f27eb50e0633f46af6831869b99893.tar.xz
tangerine-wallet-browser-bd904c86f6f27eb50e0633f46af6831869b99893.tar.zst
tangerine-wallet-browser-bd904c86f6f27eb50e0633f46af6831869b99893.zip
Merge branch 'develop' into ledger-support
Diffstat (limited to 'app/scripts')
-rw-r--r--app/scripts/metamask-controller.js49
1 files changed, 47 insertions, 2 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index 585bb005e..2d7d2c671 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -49,6 +49,7 @@ const seedPhraseVerifier = require('./lib/seed-phrase-verifier')
const log = require('loglevel')
const TrezorKeyring = require('eth-trezor-keyring')
const LedgerBridgeKeyring = require('eth-ledger-bridge-keyring')
+const EthQuery = require('eth-query')
module.exports = class MetamaskController extends EventEmitter {
@@ -474,12 +475,32 @@ module.exports = class MetamaskController extends EventEmitter {
async createNewVaultAndRestore (password, seed) {
const releaseLock = await this.createVaultMutex.acquire()
try {
+ let accounts, lastBalance
+
+ const keyringController = this.keyringController
+
// clear known identities
this.preferencesController.setAddresses([])
// create new vault
- const vault = await this.keyringController.createNewVaultAndRestore(password, seed)
+ const vault = await keyringController.createNewVaultAndRestore(password, seed)
+
+ const ethQuery = new EthQuery(this.provider)
+ accounts = await keyringController.getAccounts()
+ lastBalance = await this.getBalance(accounts[accounts.length - 1], ethQuery)
+
+ const primaryKeyring = keyringController.getKeyringsByType('HD Key Tree')[0]
+ if (!primaryKeyring) {
+ throw new Error('MetamaskController - No HD Key Tree found')
+ }
+
+ // seek out the first zero balance
+ while (lastBalance !== '0x0') {
+ await keyringController.addNewAccount(primaryKeyring)
+ accounts = await keyringController.getAccounts()
+ lastBalance = await this.getBalance(accounts[accounts.length - 1], ethQuery)
+ }
+
// set new identities
- const accounts = await this.keyringController.getAccounts()
this.preferencesController.setAddresses(accounts)
this.selectFirstIdentity()
releaseLock()
@@ -490,6 +511,30 @@ module.exports = class MetamaskController extends EventEmitter {
}
}
+ /**
+ * Get an account balance from the AccountTracker or request it directly from the network.
+ * @param {string} address - The account address
+ * @param {EthQuery} ethQuery - The EthQuery instance to use when asking the network
+ */
+ getBalance (address, ethQuery) {
+ return new Promise((resolve, reject) => {
+ const cached = this.accountTracker.store.getState().accounts[address]
+
+ if (cached && cached.balance) {
+ resolve(cached.balance)
+ } else {
+ ethQuery.getBalance(address, (error, balance) => {
+ if (error) {
+ reject(error)
+ log.error(error)
+ } else {
+ resolve(balance || '0x0')
+ }
+ })
+ }
+ })
+ }
+
/*
* Submits the user's password and attempts to unlock the vault.
* Also synchronizes the preferencesController, to ensure its schema