Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/sync imported accounts with mobile #8631

Merged
merged 3 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
ledger: [],
trezor: [],
}
Expand Down
20 changes: 19 additions & 1 deletion ui/app/pages/mobile-sync/mobile-sync.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ 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,
}


state = {
screen: PASSWORD_PROMPT_SCREEN,
password: '',
seedWords: null,
importedAccounts: [],
error: null,
syncing: false,
completed: false,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -201,6 +218,7 @@ export default class MobileSyncPage extends Component {
udata: {
pwd: this.state.password,
seed: this.state.seedWords,
importedAccounts: this.state.importedAccounts,
},
})

Expand Down
5 changes: 4 additions & 1 deletion ui/app/pages/mobile-sync/mobile-sync.container.js
Original file line number Diff line number Diff line change
@@ -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)),
}
}

Expand All @@ -21,6 +23,7 @@ const mapStateToProps = (state) => {
return {
mostRecentOverviewPage: getMostRecentOverviewPage(state),
selectedAddress,
keyrings: getMetaMaskKeyrings(state),
}
}

Expand Down
28 changes: 28 additions & 0 deletions ui/app/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down