Skip to content

Commit d5621e5

Browse files
committed
feat(neuron-ui): check the JSON format before keystore validation
1 parent f8a4796 commit d5621e5

4 files changed

Lines changed: 15 additions & 0 deletions

File tree

packages/neuron-wallet/src/controllers/wallets/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ServiceHasNoResponse,
1616
EmptyPassword,
1717
IncorrectPassword,
18+
InvalidJSON,
1819
} from 'exceptions'
1920
import i18n from 'utils/i18n'
2021
import AddressService from 'services/addresses'
@@ -161,6 +162,11 @@ export default class WalletsController {
161162
throw new IsRequired('Password')
162163
}
163164
const keystore = fs.readFileSync(keystorePath, 'utf8')
165+
try {
166+
JSON.parse(keystore)
167+
} catch {
168+
throw new InvalidJSON()
169+
}
164170
const keystoreObject = Keystore.fromJson(keystore)
165171
const masterPrivateKey = keystoreObject.extendedPrivateKey(password)
166172
const masterKeychain = new Keychain(

packages/neuron-wallet/src/exceptions/common.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,18 @@ export class ShouldBeTypeOf extends Error {
4040
}
4141
}
4242

43+
export class InvalidJSON extends Error {
44+
constructor() {
45+
super(i18n.t('messages.invalid-JSON'))
46+
}
47+
}
48+
4349
export default {
4450
InvalidName,
4551
UsedName,
4652
IsRequired,
4753
MissingRequiredArgument,
4854
ServiceHasNoResponse,
4955
ShouldBeTypeOf,
56+
InvalidJSON,
5057
}

packages/neuron-wallet/src/locales/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export default {
9393
'capacity-too-small': 'Capacity less than min',
9494
'should-be-type-of': '{{field}} should be type of {{type}}',
9595
'invalid-keystore': 'Keystore is invalid',
96+
'invalid-JSON': 'Invalid JSON file',
9697
},
9798
contextMenu: {
9899
select: 'Select',

packages/neuron-wallet/src/locales/zh.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export default {
9292
'capacity-too-small': '金额小于最低金额',
9393
'should-be-type-of': '{{field}} 应该为 {{type}} 类型',
9494
'invalid-keystore': 'Keystore 格式不正确',
95+
'invalid-JSON': 'JSON 文件格式不正确',
9596
},
9697
contextMenu: {
9798
select: '选择',

0 commit comments

Comments
 (0)