forked from pactus-project/pactus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
51 lines (43 loc) · 1 KB
/
store.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
package wallet
import (
"encoding/json"
"hash/crc32"
"time"
"github.com/google/uuid"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/wallet/vault"
)
type store struct {
Version int `json:"version"`
UUID uuid.UUID `json:"uuid"`
CreatedAt time.Time `json:"created_at"`
Network genesis.ChainType `json:"network"`
VaultCRC uint32 `json:"crc"`
Vault *vault.Vault `json:"vault"`
History history `json:"history"`
}
func (s *store) Load() ([]byte, error) {
s.VaultCRC = s.calcVaultCRC()
return json.MarshalIndent(s, " ", " ")
}
func (s *store) Save(bs []byte) error {
err := json.Unmarshal(bs, s)
if err != nil {
return err
}
crc := s.calcVaultCRC()
if s.VaultCRC != crc {
return CRCNotMatchError{
Expected: crc,
Got: s.VaultCRC,
}
}
return nil
}
func (s *store) calcVaultCRC() uint32 {
d, err := json.Marshal(s.Vault)
if err != nil {
return 0
}
return crc32.ChecksumIEEE(d)
}