wallet: marshal before writing to file

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-07-29 16:00:20 +03:00
parent 50d99464e0
commit 8f196c8222

View file

@ -143,20 +143,31 @@ func (w *Wallet) Path() string {
// that is responsible for saving the data. This can // that is responsible for saving the data. This can
// be a buffer, file, etc.. // be a buffer, file, etc..
func (w *Wallet) Save() error { func (w *Wallet) Save() error {
if err := w.rewind(); err != nil { data, err := json.Marshal(w)
if err != nil {
return err return err
} }
return json.NewEncoder(w.rw).Encode(w)
return w.writeRaw(data)
} }
// savePretty saves wallet in a beautiful JSON. // savePretty saves wallet in a beautiful JSON.
func (w *Wallet) savePretty() error { 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 { if err := w.rewind(); err != nil {
return err return err
} }
enc := json.NewEncoder(w.rw)
enc.SetIndent("", " ") _, err := w.rw.Write(data)
return enc.Encode(w) return err
} }
func (w *Wallet) rewind() error { func (w *Wallet) rewind() error {