From e29708625467d848b8633562655c730a39b5c900 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 14 May 2019 11:58:28 +0900 Subject: [PATCH 1/4] refactor: Fix keystore version to 3 altough we don't guarantee the compatibility with V3 Keystore format used by Ethereum and some other implementations --- packages/neuron-wallet/src/keys/key.ts | 2 +- packages/neuron-wallet/tests/services/wallets.test.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/neuron-wallet/src/keys/key.ts b/packages/neuron-wallet/src/keys/key.ts index 020f7f3108..bd3102f95a 100644 --- a/packages/neuron-wallet/src/keys/key.ts +++ b/packages/neuron-wallet/src/keys/key.ts @@ -192,7 +192,7 @@ export default class Key { .toString('hex') .replace('0x', '') return { - version: 0, + version: 3, id: uuid(), crypto: { ciphertext: ciphertext.toString('hex'), diff --git a/packages/neuron-wallet/tests/services/wallets.test.ts b/packages/neuron-wallet/tests/services/wallets.test.ts index c4b16ea115..21d7883e3b 100644 --- a/packages/neuron-wallet/tests/services/wallets.test.ts +++ b/packages/neuron-wallet/tests/services/wallets.test.ts @@ -12,7 +12,7 @@ describe('wallet service', () => { wallet1 = { name: 'wallet-test1', keystore: { - version: 0, + version: 3, id: '0', crypto: { cipher: 'wallet1', @@ -64,7 +64,7 @@ describe('wallet service', () => { wallet2 = { name: 'wallet-test2', keystore: { - version: 0, + version: 3, id: '1', crypto: { cipher: 'wallet2', @@ -116,7 +116,7 @@ describe('wallet service', () => { wallet3 = { name: 'wallet-test3', keystore: { - version: 0, + version: 3, id: '1', crypto: { cipher: 'wallet3', From 06f165e861665ed72603214df787ba1db2dc9f6e Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 15 May 2019 13:33:46 +0900 Subject: [PATCH 2/4] refactor: Replace Store with FileSerice when accessing keystore file Keystore JSON is always read and written as a while so Store's kv access is not suitable. --- .../neuron-wallet/src/services/wallets.ts | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/neuron-wallet/src/services/wallets.ts b/packages/neuron-wallet/src/services/wallets.ts index cd32e150aa..96553c9827 100644 --- a/packages/neuron-wallet/src/services/wallets.ts +++ b/packages/neuron-wallet/src/services/wallets.ts @@ -1,5 +1,6 @@ import { v4 as uuid } from 'uuid' import TransactionsService from './transactions' +import FileService from './file' import Key, { Addresses } from '../keys/key' import { Keystore } from '../keys/keystore' import Store from '../utils/store' @@ -28,13 +29,10 @@ class FileKeystoreWallet implements Wallet { public name: string public addresses: Addresses - private keyStore: Store - - constructor(id: string, { name, addresses, keystore }: WalletProperties) { + constructor(id: string, { name, addresses }: WalletProperties) { this.id = id this.name = name this.addresses = addresses - this.keyStore = new Store(MODULE_NAME, `${id}.json`, JSON.stringify(keystore || {})) } static fromJSON = (json: { id: string; name: string; addresses: Addresses }): FileKeystoreWallet => { @@ -44,11 +42,9 @@ class FileKeystoreWallet implements Wallet { public update = ({ name, addresses }: WalletProperties) => { if (name) { - this.keyStore.writeSync('name', name) this.name = name } if (addresses) { - this.keyStore.writeSync('addresses', JSON.stringify(addresses)) this.addresses = addresses } } @@ -62,13 +58,20 @@ class FileKeystoreWallet implements Wallet { } public loadKeystore = (): Keystore => { - // TODO: handle fs error - const data = this.keyStore.service.readFileSync(MODULE_NAME, `${this.id}.json`) + const data = new FileService().readFileSync(MODULE_NAME, this.keystoreFileName()) return JSON.parse(data) as Keystore } - clear = () => { - this.keyStore.clear() + saveKeystore = (keystore: Keystore) => { + new FileService().writeFileSync(MODULE_NAME, this.keystoreFileName(), JSON.stringify(keystore)) + } + + deleteKeystore = () => { + new FileService().deleteFileSync(MODULE_NAME, this.keystoreFileName()) + } + + keystoreFileName = () => { + return `${this.id}.json` } } @@ -99,6 +102,7 @@ export default class WalletService { throw Error('Wallet name existed') } const wallet = new FileKeystoreWallet(uuid(), props) + wallet.saveKeystore(props.keystore!) this.listStore.writeSync(this.walletsKey, this.getAll().concat(wallet.toJSON())) if (this.getAll().length === 1) { this.setCurrent(wallet.id) @@ -115,6 +119,9 @@ export default class WalletService { throw Error('Wallet name existed') } wallet.update(props) + if (props.keystore) { + wallet.saveKeystore(props.keystore) + } wallets[index] = wallet.toJSON() this.listStore.writeSync(this.walletsKey, wallets) } @@ -132,7 +139,7 @@ export default class WalletService { const wallet = FileKeystoreWallet.fromJSON(wallets[index]) wallets.splice(index, 1) this.listStore.writeSync(this.walletsKey, wallets) - wallet.clear() + wallet.deleteKeystore() const newWallets = this.getAll() if (currentId === id) { @@ -177,7 +184,7 @@ export default class WalletService { public clearAll = () => { this.getAll().forEach(w => { const wallet = FileKeystoreWallet.fromJSON(w) - wallet.clear() + wallet.deleteKeystore() }) this.listStore.clear() } From f2ab20f5f1045b2d45d11a5f0b554b43a11ced15 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 15 May 2019 13:56:38 +0900 Subject: [PATCH 3/4] fix: Keystore id to always be identical as attached wallet's --- packages/neuron-wallet/src/services/wallets.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/neuron-wallet/src/services/wallets.ts b/packages/neuron-wallet/src/services/wallets.ts index 96553c9827..9abcf82543 100644 --- a/packages/neuron-wallet/src/services/wallets.ts +++ b/packages/neuron-wallet/src/services/wallets.ts @@ -63,7 +63,9 @@ class FileKeystoreWallet implements Wallet { } saveKeystore = (keystore: Keystore) => { - new FileService().writeFileSync(MODULE_NAME, this.keystoreFileName(), JSON.stringify(keystore)) + const keystoreToSave = keystore + keystoreToSave.id = this.id + new FileService().writeFileSync(MODULE_NAME, this.keystoreFileName(), JSON.stringify(keystoreToSave)) } deleteKeystore = () => { From c88bac38d01b1e2af1cfc000657c6c53cf191eaf Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 15 May 2019 14:46:06 +0900 Subject: [PATCH 4/4] refactor: Import startup/fileService as singleton instead of initializing FileService instance --- packages/neuron-wallet/src/services/wallets.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/neuron-wallet/src/services/wallets.ts b/packages/neuron-wallet/src/services/wallets.ts index 9abcf82543..dee16cbd9b 100644 --- a/packages/neuron-wallet/src/services/wallets.ts +++ b/packages/neuron-wallet/src/services/wallets.ts @@ -1,10 +1,10 @@ import { v4 as uuid } from 'uuid' import TransactionsService from './transactions' -import FileService from './file' import Key, { Addresses } from '../keys/key' import { Keystore } from '../keys/keystore' import Store from '../utils/store' import nodeService from '../startup/nodeService' +import fileService from '../startup/fileService' const { core } = nodeService @@ -58,18 +58,18 @@ class FileKeystoreWallet implements Wallet { } public loadKeystore = (): Keystore => { - const data = new FileService().readFileSync(MODULE_NAME, this.keystoreFileName()) + const data = fileService.readFileSync(MODULE_NAME, this.keystoreFileName()) return JSON.parse(data) as Keystore } saveKeystore = (keystore: Keystore) => { const keystoreToSave = keystore keystoreToSave.id = this.id - new FileService().writeFileSync(MODULE_NAME, this.keystoreFileName(), JSON.stringify(keystoreToSave)) + fileService.writeFileSync(MODULE_NAME, this.keystoreFileName(), JSON.stringify(keystoreToSave)) } deleteKeystore = () => { - new FileService().deleteFileSync(MODULE_NAME, this.keystoreFileName()) + fileService.deleteFileSync(MODULE_NAME, this.keystoreFileName()) } keystoreFileName = () => {