From ea30122a09ba6d1a7a25627484af89cb6b8d8c6d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 20 Feb 2020 12:55:03 +0300 Subject: [PATCH] wallet: rewrite file on save When we are opening a file, it is expected that it will be rewritten, not appended to a already existing wallet. --- pkg/wallet/wallet.go | 5 +++++ pkg/wallet/wallet_test.go | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/pkg/wallet/wallet.go b/pkg/wallet/wallet.go index fd9c9ab61..8a6826f5b 100644 --- a/pkg/wallet/wallet.go +++ b/pkg/wallet/wallet.go @@ -105,6 +105,11 @@ func (w *Wallet) Path() string { // that is responsible for saving the data. This can // be a buffer, file, etc.. func (w *Wallet) Save() error { + if s, ok := w.rw.(io.Seeker); ok { + if _, err := s.Seek(0, 0); err != nil { + return err + } + } return json.NewEncoder(w.rw).Encode(w) } diff --git a/pkg/wallet/wallet_test.go b/pkg/wallet/wallet_test.go index c22972713..eee01b199 100644 --- a/pkg/wallet/wallet_test.go +++ b/pkg/wallet/wallet_test.go @@ -90,6 +90,17 @@ func TestSave(t *testing.T) { openedWallet, err := NewWalletFromFile(wallet.path) require.NoError(t, err) require.Equal(t, wallet.Accounts, openedWallet.Accounts) + + t.Run("change and rewrite", func(t *testing.T) { + err := openedWallet.CreateAccount("test", "pass") + require.NoError(t, err) + + w2, err := NewWalletFromFile(openedWallet.path) + require.NoError(t, err) + require.Equal(t, 2, len(w2.Accounts)) + require.NoError(t, w2.Accounts[1].Decrypt("pass")) + require.Equal(t, openedWallet.Accounts, w2.Accounts) + }) } func TestJSONMarshallUnmarshal(t *testing.T) {