From 8f196c8222f356fbd8721108418d48a28d96cc21 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Thu, 29 Jul 2021 16:00:20 +0300 Subject: [PATCH] wallet: marshal before writing to file Signed-off-by: Evgeniy Stratonikov --- pkg/wallet/wallet.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/wallet/wallet.go b/pkg/wallet/wallet.go index c42e65ef1..a4694bf4a 100644 --- a/pkg/wallet/wallet.go +++ b/pkg/wallet/wallet.go @@ -143,20 +143,31 @@ 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 err := w.rewind(); err != nil { + data, err := json.Marshal(w) + if err != nil { return err } - return json.NewEncoder(w.rw).Encode(w) + + return w.writeRaw(data) } // savePretty saves wallet in a beautiful JSON. func (w *Wallet) savePretty() error { + data, err := json.MarshalIndent(w, "", " ") + if err != nil { + return err + } + + return w.writeRaw(data) +} + +func (w *Wallet) writeRaw(data []byte) error { if err := w.rewind(); err != nil { return err } - enc := json.NewEncoder(w.rw) - enc.SetIndent("", " ") - return enc.Encode(w) + + _, err := w.rw.Write(data) + return err } func (w *Wallet) rewind() error {