Skip to content

Commit

Permalink
feat(neuron-ui): check the JSON format before keystore validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Aug 13, 2019
1 parent f8a4796 commit d5621e5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/neuron-wallet/src/controllers/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ServiceHasNoResponse,
EmptyPassword,
IncorrectPassword,
InvalidJSON,
} from 'exceptions'
import i18n from 'utils/i18n'
import AddressService from 'services/addresses'
Expand Down Expand Up @@ -161,6 +162,11 @@ export default class WalletsController {
throw new IsRequired('Password')
}
const keystore = fs.readFileSync(keystorePath, 'utf8')
try {
JSON.parse(keystore)
} catch {
throw new InvalidJSON()
}
const keystoreObject = Keystore.fromJson(keystore)
const masterPrivateKey = keystoreObject.extendedPrivateKey(password)
const masterKeychain = new Keychain(
Expand Down
7 changes: 7 additions & 0 deletions packages/neuron-wallet/src/exceptions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ export class ShouldBeTypeOf extends Error {
}
}

export class InvalidJSON extends Error {
constructor() {
super(i18n.t('messages.invalid-JSON'))
}
}

export default {
InvalidName,
UsedName,
IsRequired,
MissingRequiredArgument,
ServiceHasNoResponse,
ShouldBeTypeOf,
InvalidJSON,
}
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default {
'capacity-too-small': 'Capacity less than min',
'should-be-type-of': '{{field}} should be type of {{type}}',
'invalid-keystore': 'Keystore is invalid',
'invalid-JSON': 'Invalid JSON file',
},
contextMenu: {
select: 'Select',
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export default {
'capacity-too-small': '金额小于最低金额',
'should-be-type-of': '{{field}} 应该为 {{type}} 类型',
'invalid-keystore': 'Keystore 格式不正确',
'invalid-JSON': 'JSON 文件格式不正确',
},
contextMenu: {
select: '选择',
Expand Down

0 comments on commit d5621e5

Please sign in to comment.