/
store.go
331 lines (293 loc) · 7.06 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package wallet
import (
"crypto/aes"
"crypto/cipher"
"encoding/json"
"github.com/awnumar/memguard"
"time"
)
// Abstract interface embeddable in other types that you wish to convert to other encodings such as JSON and potentially any type of desired encoding, as well as to enable encryption of sensitive data while it is not being utilised
type Serializable struct {
armed *MasterKey
masterKey []*MasterKey
safe []*memguard.LockedBuffer
ckey *memguard.LockedBuffer
iv []byte
}
type serializable interface {
Lock()
Unlock(p *memguard.LockedBuffer)
Copy() (S *Serializable)
}
// Locks the sensitive data that may have been created for an serializable object
func (s *Serializable) Lock() {
if s.safe != nil {
DeleteBuffers(s.safe...)
DeleteBuffer(s.ckey)
}
}
// Converts an object into a JSON string
func ToJSON(p interface{}) (j string) {
J, _ := json.MarshalIndent(p, "", " ")
j = string(J)
return
}
// Converts a JSON string into the data to fill the parent type
func (s *Serializable) FromJSON(j string) (J string, err error) {
err = json.Unmarshal([]byte(j), J)
return J, err
}
// Stores the details of an encryption master key for a wallet
type MasterKey struct {
MKeyID int64
EncryptedKey []byte
Salt []byte
Method uint32
Iterations uint32
Other []byte
}
// Encrypts plaintext using the masterkey and a password
func (m *MasterKey) Encrypt(ckey *memguard.LockedBuffer, iv []byte, b ...*memguard.LockedBuffer) (r []*memguard.LockedBuffer, err error) {
return encDec(true, ckey, iv, b...)
}
//Decrypts ciphertext using the masterkey and a password
func (m *MasterKey) Decrypt(ckey *memguard.LockedBuffer, iv []byte, b ...[]byte) (r []*memguard.LockedBuffer, err error) {
B := make([]*memguard.LockedBuffer, len(b))
for i := range b {
B[i], err = NewBufferFromBytes(b[i])
if err != nil {
return
}
}
r, err = encDec(false, ckey, iv, B...)
DeleteBuffers(B...)
return
}
func encDec(enc bool, ckey *memguard.LockedBuffer, iv []byte, b ...*memguard.LockedBuffer) (r []*memguard.LockedBuffer, err error) {
if block, err := aes.NewCipher(ckey.Buffer()[:32]); err != nil {
return nil, err
} else {
var blockmode cipher.BlockMode
if enc {
blockmode = cipher.NewCBCEncrypter(block, iv[:block.BlockSize()])
} else {
blockmode = cipher.NewCBCDecrypter(block, iv[:block.BlockSize()])
}
for i := range b {
if enc {
b[i], err = PKCS7.Padding(b[i], blockmode.BlockSize())
r = append(r, b[i])
}
blockmode.CryptBlocks(r[i].Buffer(), b[i].Buffer())
if !enc {
if r[i], err = PKCS7.Unpadding(r[i], blockmode.BlockSize()); err != nil {
return nil, err
}
}
// DeleteBuffers(b...)
}
}
return
}
// Creates a new MasterKey
func NewMasterKey() (e MasterKey) {
return
}
// Stores the details of human readable labels for own and others addresses
type AddressBook struct {
*Serializable
Pub []byte
Label []byte
}
type addressBook interface {
Decrypt() (a *AddressBook)
Encrypt() (a *AddressBook)
}
// Decrypt an addressbook record
func (a *AddressBook) Decrypt() (A AddressBook) {
d := make([]*memguard.LockedBuffer, 2)
d[0], _ = NewBuffer(len(a.Pub))
d[1], _ = NewBuffer(len(a.Label))
A.Serializable = a.Serializable
A.safe = d
r, _ := a.armed.Decrypt(a.ckey, a.iv, d[0].Buffer(), d[1].Buffer())
A.Pub = r[0].Buffer()
A.Label = r[1].Buffer()
return
}
// Encrypt an addressbook record
func (a *AddressBook) Encrypt() (A AddressBook) {
if a.armed != nil {
p, _ := NewBufferFromBytes(a.Pub)
l, _ := NewBufferFromBytes(a.Label)
r, _ := a.armed.Encrypt(a.ckey, a.iv, p, l)
r[0].Copy(A.Pub)
r[1].Copy(A.Label)
} else {
A.Pub = a.Pub
A.Label = a.Label
}
return
}
// Creates a new AddressBook
func NewAddressBook(s *Serializable) (a *AddressBook) {
a = new(AddressBook)
a.Serializable = s
return
}
// Stores version and create time for keys
type Metadata struct {
*Serializable
Pub []byte
Version uint32
CreateTime []byte
}
type metadata interface {
Decrypt() (a Metadata)
}
// Decrypt an metadata record
func (m *Metadata) Decrypt() (M *Metadata) {
d := make([]*memguard.LockedBuffer, 2)
d[0], _ = NewBuffer(len(m.Pub))
d[1], _ = NewBuffer(len(m.CreateTime))
M.safe = d
d, _ = m.armed.Decrypt(m.ckey, m.iv, m.Pub, m.CreateTime)
M.Pub = d[0].Buffer()
M.CreateTime = d[1].Buffer()
return
}
// Creates a new
func NewMetadata(s *Serializable) (m *Metadata) {
m = new(Metadata)
m.Serializable = s
return
}
// A public/private key pair corresponding to a wallet address
type Key struct {
*Serializable
Pub []byte
Priv []byte
}
type ikey interface {
Decrypt() (a Metadata)
}
// Decrypt an Key record
func (k *Key) Decrypt() (K Key) {
d := make([]*memguard.LockedBuffer, 2)
d[0], _ = NewBuffer(len(k.Pub))
d[1], _ = NewBuffer(len(k.Priv))
K.safe = d
d, _ = k.armed.Decrypt(k.ckey, k.iv, k.Pub, k.Priv)
K.Pub = d[0].Buffer()
K.Priv = d[1].Buffer()
return
}
// Creates a new Key
func NewKey(s *Serializable) (k *Key) {
k = new(Key)
k.Serializable = s
return
}
// Extra metadata for key expiry management
type Wdata struct {
*Serializable
Pub []byte
Created []byte
Expires []byte
Comment []byte
}
// Creates a new Wdata
func NewWdata(s *Serializable) (w *Wdata) {
w.Serializable = s
return
}
// A raw transaction related to a key pair in the wallet
type Tx struct {
*Serializable
TxHash []byte
TxData []byte
}
// Creates a new Tx
func NewTx(s *Serializable) (t *Tx) {
t = new(Tx)
t.Serializable = s
return
}
// Keys in reserve for future change operations
type Pool struct {
*Serializable
Index uint64
Version uint32
Time []byte
Pub []byte
}
// Creates a new Pool
func NewPool(s *Serializable) (p *Pool) {
p = new(Pool)
p.Serializable = s
return
}
// A payment script related to key pairs in the wallet
type Script struct {
*Serializable
ID []byte
Data []byte
}
// Creates a new Script
func NewScript(s *Serializable) (S *Script) {
S = new(Script)
S.Serializable = s
return
}
// I'm not sure what this is used for
type Account struct {
*Serializable
Account []byte
Version int32
Pub []byte
}
// Creates a new Account
func NewAccount(s *Serializable) (a *Account) {
a.Serializable = s
return
}
// Stores settings related to the user's wallet
type Setting struct {
*Serializable
Name string
Value []byte
}
// Creates a new Setting
func NewSetting(s *Serializable) (S *Setting) {
S = new(Setting)
S.Serializable = s
return
}
// A store of all the data related to a wallet with the ability to be encrypted and exported to other data formats
type EncryptedStore struct {
*Serializable
MasterKey []*MasterKey
LastLocked time.Time
AddressBook []*AddressBook
Metadata []*Metadata
Key []*Key
Wdata []*Wdata
Tx []*Tx
Pool []*Pool
Script []*Script
Account []*Account
Setting []*Setting
DefaultKey []byte
BestBlock []byte
OrderPosNext int64
Version uint32
MinVersion uint32
}
type encryptedStore interface {
}
// Creates a new EncryptedStore
func NewEncryptedStore() (e *EncryptedStore) {
e = new(EncryptedStore)
e.Serializable = new(Serializable)
return
}