mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
feature/sync imported accounts with mobile (#8631)
* sync imported accounts * Update app/scripts/metamask-controller.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> * exportAccounts action Co-authored-by: Mark Stacey <markjstacey@gmail.com>
This commit is contained in:
parent
2dbb151e0b
commit
73257b1cd1
@ -725,10 +725,15 @@ export default class MetamaskController extends EventEmitter {
|
||||
|
||||
// Accounts
|
||||
const hdKeyring = this.keyringController.getKeyringsByType('HD Key Tree')[0]
|
||||
const simpleKeyPairKeyrings = this.keyringController.getKeyringsByType('Simple Key Pair')
|
||||
const hdAccounts = await hdKeyring.getAccounts()
|
||||
const simpleKeyPairKeyringAccounts = await Promise.all(
|
||||
simpleKeyPairKeyrings.map((keyring) => keyring.getAccounts())
|
||||
)
|
||||
const simpleKeyPairAccounts = simpleKeyPairKeyringAccounts.reduce((acc, accounts) => [...acc, ...accounts], [])
|
||||
const accounts = {
|
||||
hd: hdAccounts.filter((item, pos) => (hdAccounts.indexOf(item) === pos)).map((address) => ethUtil.toChecksumAddress(address)),
|
||||
simpleKeyPair: [],
|
||||
simpleKeyPair: simpleKeyPairAccounts.filter((item, pos) => (simpleKeyPairAccounts.indexOf(item) === pos)).map((address) => ethUtil.toChecksumAddress(address)),
|
||||
ledger: [],
|
||||
trezor: [],
|
||||
}
|
||||
|
@ -25,6 +25,8 @@ export default class MobileSyncPage extends Component {
|
||||
fetchInfoToSync: PropTypes.func.isRequired,
|
||||
mostRecentOverviewPage: PropTypes.string.isRequired,
|
||||
requestRevealSeedWords: PropTypes.func.isRequired,
|
||||
exportAccounts: PropTypes.func.isRequired,
|
||||
keyrings: PropTypes.array,
|
||||
}
|
||||
|
||||
|
||||
@ -32,6 +34,7 @@ export default class MobileSyncPage extends Component {
|
||||
screen: PASSWORD_PROMPT_SCREEN,
|
||||
password: '',
|
||||
seedWords: null,
|
||||
importedAccounts: [],
|
||||
error: null,
|
||||
syncing: false,
|
||||
completed: false,
|
||||
@ -62,11 +65,25 @@ export default class MobileSyncPage extends Component {
|
||||
.then((seedWords) => {
|
||||
this.startKeysGeneration()
|
||||
this.startIdleTimeout()
|
||||
this.setState({ seedWords, screen: REVEAL_SEED_SCREEN })
|
||||
this.exportAccounts()
|
||||
.then((importedAccounts) => {
|
||||
this.setState({ seedWords, importedAccounts, screen: REVEAL_SEED_SCREEN })
|
||||
})
|
||||
})
|
||||
.catch((error) => this.setState({ error: error.message }))
|
||||
}
|
||||
|
||||
async exportAccounts () {
|
||||
const addresses = []
|
||||
this.props.keyrings.forEach((keyring) => {
|
||||
if (keyring.type === 'Simple Key Pair') {
|
||||
addresses.push(keyring.accounts[0])
|
||||
}
|
||||
})
|
||||
const importedAccounts = await this.props.exportAccounts(this.state.password, addresses)
|
||||
return importedAccounts
|
||||
}
|
||||
|
||||
startKeysGeneration () {
|
||||
this.keysGenerationTimeout && clearTimeout(this.keysGenerationTimeout)
|
||||
this.disconnectWebsockets()
|
||||
@ -201,6 +218,7 @@ export default class MobileSyncPage extends Component {
|
||||
udata: {
|
||||
pwd: this.state.password,
|
||||
seed: this.state.seedWords,
|
||||
importedAccounts: this.state.importedAccounts,
|
||||
},
|
||||
})
|
||||
|
||||
|
@ -1,13 +1,15 @@
|
||||
import { connect } from 'react-redux'
|
||||
import { displayWarning, requestRevealSeedWords, fetchInfoToSync } from '../../store/actions'
|
||||
import { displayWarning, requestRevealSeedWords, fetchInfoToSync, exportAccounts } from '../../store/actions'
|
||||
import MobileSyncPage from './mobile-sync.component'
|
||||
import { getMostRecentOverviewPage } from '../../ducks/history/history'
|
||||
import { getMetaMaskKeyrings } from '../../selectors'
|
||||
|
||||
const mapDispatchToProps = (dispatch) => {
|
||||
return {
|
||||
requestRevealSeedWords: (password) => dispatch(requestRevealSeedWords(password)),
|
||||
fetchInfoToSync: () => dispatch(fetchInfoToSync()),
|
||||
displayWarning: (message) => dispatch(displayWarning(message || null)),
|
||||
exportAccounts: (password, addresses) => dispatch(exportAccounts(password, addresses)),
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,6 +23,7 @@ const mapStateToProps = (state) => {
|
||||
return {
|
||||
mostRecentOverviewPage: getMostRecentOverviewPage(state),
|
||||
selectedAddress,
|
||||
keyrings: getMetaMaskKeyrings(state),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1708,6 +1708,34 @@ export function exportAccount (password, address) {
|
||||
}
|
||||
}
|
||||
|
||||
export function exportAccounts (password, addresses) {
|
||||
return function (dispatch) {
|
||||
log.debug(`background.submitPassword`)
|
||||
return new Promise((resolve, reject) => {
|
||||
background.submitPassword(password, function (err) {
|
||||
if (err) {
|
||||
log.error('Error in submitting password.')
|
||||
return reject(err)
|
||||
}
|
||||
log.debug(`background.exportAccounts`)
|
||||
const accountPromises = addresses.map((address) =>
|
||||
new Promise(
|
||||
(resolve, reject) => background.exportAccount(address, function (err, result) {
|
||||
if (err) {
|
||||
log.error(err)
|
||||
dispatch(displayWarning('Had a problem exporting the account.'))
|
||||
return reject(err)
|
||||
}
|
||||
return resolve(result)
|
||||
})
|
||||
)
|
||||
)
|
||||
return resolve(Promise.all(accountPromises))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function showPrivateKey (key) {
|
||||
return {
|
||||
type: actionConstants.SHOW_PRIVATE_KEY,
|
||||
|
Loading…
Reference in New Issue
Block a user