Skip to content

Commit 09506fa

Browse files
authored
Merge pull request #687 from Keith-CY/verify-mnemonic
feat(neuron-ui): validate mnemonic words before setting password
2 parents 09a2e00 + f92b0b8 commit 09506fa

File tree

6 files changed

+60
-10
lines changed

6 files changed

+60
-10
lines changed

packages/neuron-ui/src/components/WalletWizard/index.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import withWizard, { WizardElementProps, WithWizardState } from 'components/with
88
import { MnemonicAction, BUTTON_GAP } from 'utils/const'
99
import { verifyWalletSubmission } from 'utils/validators'
1010
import { helpersCall, walletsCall } from 'services/UILayer'
11+
import { validateMnemonic, showErrorMessage } from 'services/remote'
1112
import { registerIcons, buttonGrommetIconStyles } from 'utils/icons'
1213

1314
export enum WalletWizardPath {
@@ -138,13 +139,18 @@ const Mnemonic = ({
138139
if (isCreate) {
139140
history.push(`${rootPath}${WalletWizardPath.Mnemonic}/${MnemonicAction.Verify}`)
140141
} else {
141-
history.push(
142-
`${rootPath}${WalletWizardPath.Submission}/${
143-
type === MnemonicAction.Verify ? MnemonicAction.Create : MnemonicAction.Import
144-
}`
145-
)
142+
const isMnemonicValid = validateMnemonic(imported)
143+
if (isMnemonicValid) {
144+
history.push(
145+
`${rootPath}${WalletWizardPath.Submission}/${
146+
type === MnemonicAction.Verify ? MnemonicAction.Create : MnemonicAction.Import
147+
}`
148+
)
149+
} else {
150+
showErrorMessage(t('messages.error'), t('messages.invalid-mnemonic'))
151+
}
146152
}
147-
}, [isCreate, history, rootPath, type])
153+
}, [isCreate, history, rootPath, type, imported, t])
148154

149155
return (
150156
<Stack verticalFill verticalAlign="center" horizontalAlign="stretch" tokens={{ childrenGap: 15 }}>

packages/neuron-ui/src/locales/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@
205205
"wallet-created-successfully": "{{name}} created successfully",
206206
"wallet-updated-successfully": "{{name}} updated successfully",
207207
"wallet-not-found": "Wallet not found",
208-
"no-transactions": "No transactions"
208+
"no-transactions": "No transactions",
209+
"error": "Error",
210+
"invalid-mnemonic": "Invalid mnemonic words"
209211
},
210212
"sync": {
211213
"syncing": "Syncing",

packages/neuron-ui/src/locales/zh.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@
205205
"wallet-created-successfully": "{{name}} 创建成功",
206206
"wallet-updated-successfully": "{{name}} 更新成功",
207207
"wallet-not-found": "未找到钱包",
208-
"no-transactions": "没有交易"
208+
"no-transactions": "没有交易",
209+
"error": "错误",
210+
"invalid-mnemonic": "助记词不合法"
209211
},
210212
"sync": {
211213
"syncing": "同步中",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export const validateMnemonic = (mnemonic: string): boolean => {
2+
if (!window.remote) {
3+
console.warn('remote is not supported')
4+
return true
5+
}
6+
const { validateMnemonic: remoteValidateMnemonic } = window.remote.require('./models/keys/mnemonic')
7+
return remoteValidateMnemonic(mnemonic)
8+
}
9+
10+
export const showMessage = (options: any, callback: Function) => {
11+
if (!window.remote) {
12+
console.warn('remote is not supported')
13+
window.alert(options.message)
14+
} else {
15+
window.remote.require('electron').dialog.showMessageBox(options, callback)
16+
}
17+
}
18+
19+
export const showErrorMessage = (title: string, content: string) => {
20+
if (!window.remote) {
21+
console.warn('remote is not supported')
22+
window.alert(`${title}: ${content}`)
23+
} else {
24+
window.remote.require('electron').dialog.showErrorBox(title, content)
25+
}
26+
}
27+
28+
export default {
29+
validateMnemonic,
30+
showMessage,
31+
showErrorMessage,
32+
}

packages/neuron-ui/src/types/global/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
declare interface Window {
22
clipboard: any
3-
remote: any
3+
remote: {
4+
getCurrentWebContents: Function
5+
getCurrentWindow: Function
6+
getGlobal: (name: string) => any
7+
require: (module: string) => any
8+
process?: any
9+
}
410
require: any
511
bridge: any
612
nativeImage: any

packages/neuron-wallet/src/startup/preload.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { ipcRenderer, clipboard, nativeImage } from 'electron'
1+
import { remote, ipcRenderer, clipboard, nativeImage } from 'electron'
22

33
declare global {
44
interface Window {
55
bridge: any
66
clipboard: Electron.Clipboard
77
nativeImage: any
8+
remote: Electron.Remote
89
}
910
}
1011

@@ -34,3 +35,4 @@ if (process.env.NODE_ENV === 'development') {
3435
window.clipboard = clipboard
3536
window.bridge = window.bridge || bridge
3637
window.nativeImage = nativeImage
38+
window.remote = remote

0 commit comments

Comments
 (0)