From d5621e52622efe518fe4a95f9ec191c76719a3e5 Mon Sep 17 00:00:00 2001 From: Keith Date: Tue, 13 Aug 2019 18:09:27 +0800 Subject: [PATCH] feat(neuron-ui): check the JSON format before keystore validation --- packages/neuron-wallet/src/controllers/wallets/index.ts | 6 ++++++ packages/neuron-wallet/src/exceptions/common.ts | 7 +++++++ packages/neuron-wallet/src/locales/en.ts | 1 + packages/neuron-wallet/src/locales/zh.ts | 1 + 4 files changed, 15 insertions(+) diff --git a/packages/neuron-wallet/src/controllers/wallets/index.ts b/packages/neuron-wallet/src/controllers/wallets/index.ts index 2fcfe40a63..b0ad9055d0 100644 --- a/packages/neuron-wallet/src/controllers/wallets/index.ts +++ b/packages/neuron-wallet/src/controllers/wallets/index.ts @@ -15,6 +15,7 @@ import { ServiceHasNoResponse, EmptyPassword, IncorrectPassword, + InvalidJSON, } from 'exceptions' import i18n from 'utils/i18n' import AddressService from 'services/addresses' @@ -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( diff --git a/packages/neuron-wallet/src/exceptions/common.ts b/packages/neuron-wallet/src/exceptions/common.ts index 8412e78eb5..840ddc4d63 100644 --- a/packages/neuron-wallet/src/exceptions/common.ts +++ b/packages/neuron-wallet/src/exceptions/common.ts @@ -40,6 +40,12 @@ export class ShouldBeTypeOf extends Error { } } +export class InvalidJSON extends Error { + constructor() { + super(i18n.t('messages.invalid-JSON')) + } +} + export default { InvalidName, UsedName, @@ -47,4 +53,5 @@ export default { MissingRequiredArgument, ServiceHasNoResponse, ShouldBeTypeOf, + InvalidJSON, } diff --git a/packages/neuron-wallet/src/locales/en.ts b/packages/neuron-wallet/src/locales/en.ts index 50f62f7656..16d76ef658 100644 --- a/packages/neuron-wallet/src/locales/en.ts +++ b/packages/neuron-wallet/src/locales/en.ts @@ -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', diff --git a/packages/neuron-wallet/src/locales/zh.ts b/packages/neuron-wallet/src/locales/zh.ts index c357f638fd..d5b08b5704 100644 --- a/packages/neuron-wallet/src/locales/zh.ts +++ b/packages/neuron-wallet/src/locales/zh.ts @@ -92,6 +92,7 @@ export default { 'capacity-too-small': '金额小于最低金额', 'should-be-type-of': '{{field}} 应该为 {{type}} 类型', 'invalid-keystore': 'Keystore 格式不正确', + 'invalid-JSON': 'JSON 文件格式不正确', }, contextMenu: { select: '选择',