This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
file_storage.go
77 lines (63 loc) · 1.49 KB
/
file_storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package wallet
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"os"
)
const (
defaultFilename = "wallet.dat"
defaultKeysAmount = 5
)
// fileStorage represents a file storage of a wallet
type fileStorage struct {
f *os.File
}
// NewFileStorage creates new Storage.
// It creates new filename or uses existed one
func NewFileStorage(filename string) Storage {
if filename == "" {
filename = defaultFilename
}
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
log.Fatalf(err.Error())
}
return &fileStorage{f}
}
//Init creates a wallet or reads existed one
func (f *fileStorage) Init(ctx context.Context) (*Wallet, error) {
data, err := ioutil.ReadAll(f.f)
if err != nil {
return nil, err
}
if len(data) == 0 {
return Init(defaultKeysAmount)
}
var w Wallet
err = json.Unmarshal(data, &w)
if err != nil {
return nil, err
}
if len(w.Keys()) == 0 {
return Init(defaultKeysAmount)
}
return &w, nil
}
// Write writes a wallet data to a file
// It's not the best way to store private keys :)
// You can read more about it here: https://stackoverflow.com/questions/21322182/how-to-store-ecdsa-private-key-in-go
func (f *fileStorage) Write(ctx context.Context, w *Wallet) error {
data, err := json.Marshal(w)
if err != nil {
return err
}
// since we have only wallet.AddKeys behavior, we'll rewrite the file every time
_, err = f.f.WriteAt(data, 0)
return err
}
// Close closes a file
func (f *fileStorage) Close() error {
return f.f.Close()
}