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.
This commit is contained in:
Evgenii Stratonikov 2020-02-20 12:55:03 +03:00
parent ad6fa2aea9
commit ea30122a09
2 changed files with 16 additions and 0 deletions

View file

@ -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)
}

View file

@ -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) {