Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/keys/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default class Key {
.toString('hex')
.replace('0x', '')
return {
version: 0,
version: 3,
id: uuid(),
crypto: {
ciphertext: ciphertext.toString('hex'),
Expand Down
33 changes: 21 additions & 12 deletions packages/neuron-wallet/src/services/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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

Expand All @@ -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 => {
Expand All @@ -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
}
}
Expand All @@ -62,13 +58,22 @@ class FileKeystoreWallet implements Wallet {
}

public loadKeystore = (): Keystore => {
// TODO: handle fs error
const data = this.keyStore.service.readFileSync(MODULE_NAME, `${this.id}.json`)
const data = fileService.readFileSync(MODULE_NAME, this.keystoreFileName())
return JSON.parse(data) as Keystore
}

clear = () => {
this.keyStore.clear()
saveKeystore = (keystore: Keystore) => {
const keystoreToSave = keystore
keystoreToSave.id = this.id
fileService.writeFileSync(MODULE_NAME, this.keystoreFileName(), JSON.stringify(keystoreToSave))
}

deleteKeystore = () => {
fileService.deleteFileSync(MODULE_NAME, this.keystoreFileName())
}

keystoreFileName = () => {
return `${this.id}.json`
}
}

Expand Down Expand Up @@ -99,6 +104,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)
Expand All @@ -115,6 +121,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)
}
Expand All @@ -132,7 +141,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) {
Expand Down Expand Up @@ -177,7 +186,7 @@ export default class WalletService {
public clearAll = () => {
this.getAll().forEach(w => {
const wallet = FileKeystoreWallet.fromJSON(w)
wallet.clear()
wallet.deleteKeystore()
})
this.listStore.clear()
}
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-wallet/tests/services/wallets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('wallet service', () => {
wallet1 = {
name: 'wallet-test1',
keystore: {
version: 0,
version: 3,
id: '0',
crypto: {
cipher: 'wallet1',
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('wallet service', () => {
wallet2 = {
name: 'wallet-test2',
keystore: {
version: 0,
version: 3,
id: '1',
crypto: {
cipher: 'wallet2',
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('wallet service', () => {
wallet3 = {
name: 'wallet-test3',
keystore: {
version: 0,
version: 3,
id: '1',
crypto: {
cipher: 'wallet3',
Expand Down