Skip to content

Commit

Permalink
Merge pull request #687 from Keith-CY/verify-mnemonic
Browse files Browse the repository at this point in the history
feat(neuron-ui): validate mnemonic words before setting password
  • Loading branch information
ashchan authored Jul 18, 2019
2 parents 09a2e00 + f92b0b8 commit 09506fa
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
18 changes: 12 additions & 6 deletions packages/neuron-ui/src/components/WalletWizard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import withWizard, { WizardElementProps, WithWizardState } from 'components/with
import { MnemonicAction, BUTTON_GAP } from 'utils/const'
import { verifyWalletSubmission } from 'utils/validators'
import { helpersCall, walletsCall } from 'services/UILayer'
import { validateMnemonic, showErrorMessage } from 'services/remote'
import { registerIcons, buttonGrommetIconStyles } from 'utils/icons'

export enum WalletWizardPath {
Expand Down Expand Up @@ -138,13 +139,18 @@ const Mnemonic = ({
if (isCreate) {
history.push(`${rootPath}${WalletWizardPath.Mnemonic}/${MnemonicAction.Verify}`)
} else {
history.push(
`${rootPath}${WalletWizardPath.Submission}/${
type === MnemonicAction.Verify ? MnemonicAction.Create : MnemonicAction.Import
}`
)
const isMnemonicValid = validateMnemonic(imported)
if (isMnemonicValid) {
history.push(
`${rootPath}${WalletWizardPath.Submission}/${
type === MnemonicAction.Verify ? MnemonicAction.Create : MnemonicAction.Import
}`
)
} else {
showErrorMessage(t('messages.error'), t('messages.invalid-mnemonic'))
}
}
}, [isCreate, history, rootPath, type])
}, [isCreate, history, rootPath, type, imported, t])

return (
<Stack verticalFill verticalAlign="center" horizontalAlign="stretch" tokens={{ childrenGap: 15 }}>
Expand Down
4 changes: 3 additions & 1 deletion packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@
"wallet-created-successfully": "{{name}} created successfully",
"wallet-updated-successfully": "{{name}} updated successfully",
"wallet-not-found": "Wallet not found",
"no-transactions": "No transactions"
"no-transactions": "No transactions",
"error": "Error",
"invalid-mnemonic": "Invalid mnemonic words"
},
"sync": {
"syncing": "Syncing",
Expand Down
4 changes: 3 additions & 1 deletion packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@
"wallet-created-successfully": "{{name}} 创建成功",
"wallet-updated-successfully": "{{name}} 更新成功",
"wallet-not-found": "未找到钱包",
"no-transactions": "没有交易"
"no-transactions": "没有交易",
"error": "错误",
"invalid-mnemonic": "助记词不合法"
},
"sync": {
"syncing": "同步中",
Expand Down
32 changes: 32 additions & 0 deletions packages/neuron-ui/src/services/remote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const validateMnemonic = (mnemonic: string): boolean => {
if (!window.remote) {
console.warn('remote is not supported')
return true
}
const { validateMnemonic: remoteValidateMnemonic } = window.remote.require('./models/keys/mnemonic')
return remoteValidateMnemonic(mnemonic)
}

export const showMessage = (options: any, callback: Function) => {
if (!window.remote) {
console.warn('remote is not supported')
window.alert(options.message)
} else {
window.remote.require('electron').dialog.showMessageBox(options, callback)
}
}

export const showErrorMessage = (title: string, content: string) => {
if (!window.remote) {
console.warn('remote is not supported')
window.alert(`${title}: ${content}`)
} else {
window.remote.require('electron').dialog.showErrorBox(title, content)
}
}

export default {
validateMnemonic,
showMessage,
showErrorMessage,
}
8 changes: 7 additions & 1 deletion packages/neuron-ui/src/types/global/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
declare interface Window {
clipboard: any
remote: any
remote: {
getCurrentWebContents: Function
getCurrentWindow: Function
getGlobal: (name: string) => any
require: (module: string) => any
process?: any
}
require: any
bridge: any
nativeImage: any
Expand Down
4 changes: 3 additions & 1 deletion packages/neuron-wallet/src/startup/preload.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ipcRenderer, clipboard, nativeImage } from 'electron'
import { remote, ipcRenderer, clipboard, nativeImage } from 'electron'

declare global {
interface Window {
bridge: any
clipboard: Electron.Clipboard
nativeImage: any
remote: Electron.Remote
}
}

Expand Down Expand Up @@ -34,3 +35,4 @@ if (process.env.NODE_ENV === 'development') {
window.clipboard = clipboard
window.bridge = window.bridge || bridge
window.nativeImage = nativeImage
window.remote = remote

0 comments on commit 09506fa

Please sign in to comment.