-
Notifications
You must be signed in to change notification settings - Fork 47
/
readable.go
196 lines (164 loc) · 4.43 KB
/
readable.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package wallet
import (
"fmt"
"strconv"
"github.com/SkycoinProject/cx-chains/src/cipher"
"github.com/SkycoinProject/cx-chains/src/util/file"
)
// ReadableEntry wallet entry with json tags
type ReadableEntry struct {
Address string `json:"address"`
Public string `json:"public_key"`
Secret string `json:"secret_key"`
}
// NewReadableEntry creates readable wallet entry
func NewReadableEntry(coinType CoinType, w Entry) ReadableEntry {
re := ReadableEntry{}
if !w.Address.Null() {
re.Address = w.Address.String()
}
if !w.Public.Null() {
re.Public = w.Public.Hex()
}
if !w.Secret.Null() {
switch coinType {
case CoinTypeSkycoin:
re.Secret = w.Secret.Hex()
case CoinTypeBitcoin:
re.Secret = cipher.BitcoinWalletImportFormatFromSeckey(w.Secret)
default:
logger.Panicf("Invalid coin type %q", coinType)
}
}
return re
}
// ReadableEntries array of ReadableEntry
type ReadableEntries []ReadableEntry
// ToWalletEntries convert readable entries to entries
// converts base on the wallet version.
func (res ReadableEntries) toWalletEntries(coinType CoinType, isEncrypted bool) ([]Entry, error) {
entries := make([]Entry, len(res))
for i, re := range res {
e, err := newEntryFromReadable(coinType, &re)
if err != nil {
return []Entry{}, err
}
// Verify the wallet if it's not encrypted
if !isEncrypted && re.Secret != "" {
if err := e.Verify(); err != nil {
return nil, err
}
}
entries[i] = *e
}
return entries, nil
}
// newEntryFromReadable creates WalletEntry base one ReadableWalletEntry
func newEntryFromReadable(coinType CoinType, w *ReadableEntry) (*Entry, error) {
var a cipher.Addresser
var err error
switch coinType {
case CoinTypeSkycoin:
a, err = cipher.DecodeBase58Address(w.Address)
case CoinTypeBitcoin:
a, err = cipher.DecodeBase58BitcoinAddress(w.Address)
default:
logger.Panicf("Invalid coin type %q", coinType)
}
if err != nil {
return nil, err
}
p, err := cipher.PubKeyFromHex(w.Public)
if err != nil {
return nil, err
}
// Decodes the secret hex string if any
var secret cipher.SecKey
if w.Secret != "" {
switch coinType {
case CoinTypeSkycoin:
secret, err = cipher.SecKeyFromHex(w.Secret)
case CoinTypeBitcoin:
secret, err = cipher.SecKeyFromBitcoinWalletImportFormat(w.Secret)
default:
logger.Panicf("Invalid coin type %q", coinType)
}
if err != nil {
return nil, err
}
}
return &Entry{
Address: a,
Public: p,
Secret: secret,
}, nil
}
// ReadableWallet used for [de]serialization of a Wallet
type ReadableWallet struct {
Meta map[string]string `json:"meta"`
Entries ReadableEntries `json:"entries"`
}
// NewReadableWallet creates readable wallet
func NewReadableWallet(w *Wallet) *ReadableWallet {
readable := make(ReadableEntries, len(w.Entries))
for i, e := range w.Entries {
readable[i] = NewReadableEntry(w.coin(), e)
}
meta := make(map[string]string, len(w.Meta))
for k, v := range w.Meta {
meta[k] = v
}
return &ReadableWallet{
Meta: meta,
Entries: readable,
}
}
// LoadReadableWallet loads a ReadableWallet from disk
func LoadReadableWallet(filename string) (*ReadableWallet, error) {
w := &ReadableWallet{}
if err := w.Load(filename); err != nil {
return nil, fmt.Errorf("load wallet %s failed: %v", filename, err)
}
return w, nil
}
// ToWallet convert readable wallet to Wallet
func (rw *ReadableWallet) ToWallet() (*Wallet, error) {
w := &Wallet{
Meta: rw.Meta,
}
if err := w.Validate(); err != nil {
return nil, fmt.Errorf("invalid wallet %s: %v", w.Filename(), err)
}
ets, err := rw.Entries.toWalletEntries(w.coin(), w.IsEncrypted())
if err != nil {
return nil, err
}
w.Entries = ets
return w, nil
}
// Save saves to filename
func (rw *ReadableWallet) Save(filename string) error {
return file.SaveJSON(filename, rw, 0600)
}
// Load loads from filename
func (rw *ReadableWallet) Load(filename string) error {
return file.LoadJSON(filename, rw)
}
func (rw *ReadableWallet) timestamp() int64 {
// Intentionally ignore the error when parsing the timestamp,
// if it isn't valid or is missing it will be set to 0
x, _ := strconv.ParseInt(rw.Meta[metaTimestamp], 10, 64) // nolint: errcheck
return x
}
func (rw *ReadableWallet) filename() string {
return rw.Meta[metaFilename]
}
// Erase remove sensitive data
func (rw *ReadableWallet) Erase() {
delete(rw.Meta, metaSeed)
delete(rw.Meta, metaLastSeed)
delete(rw.Meta, metaSecrets)
for i := range rw.Entries {
rw.Entries[i].Secret = ""
}
}