forked from YuraFjrosr/xfsgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keystoredb.go
98 lines (86 loc) · 2.61 KB
/
keystoredb.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
// Copyright 2018 The xfsgo Authors
// This file is part of the xfsgo library.
//
// The xfsgo library is free software: you can redistribute it and/or modify
// it under the terms of the MIT Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The xfsgo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// MIT Lesser General Public License for more details.
//
// You should have received a copy of the MIT Lesser General Public License
// along with the xfsgo library. If not, see <https://mit-license.org/>.
package xfsgo
import (
"crypto/ecdsa"
"crypto/x509"
"xfsgo/common"
"xfsgo/storage/badger"
)
var (
addrKeyPre = []byte("addr:")
defaultAddressKey = []byte("default")
)
type keyStoreDB struct {
storage *badger.Storage
}
func newKeyStoreDB(storage *badger.Storage) *keyStoreDB {
return &keyStoreDB{
storage: storage,
}
}
func (db *keyStoreDB) GetDefaultAddress() (common.Address, error) {
data, err := db.storage.GetData(defaultAddressKey)
if err != nil {
return noneAddress, err
}
return common.Bytes2Address(data), nil
}
func (db *keyStoreDB) Foreach(fn func(address common.Address, key *ecdsa.PrivateKey)) {
_ = db.storage.PrefixForeachData(addrKeyPre, func(k []byte, v []byte) error {
privateKey, err := x509.ParseECPrivateKey(v)
if err != nil {
return err
}
addr := common.Bytes2Address(k)
fn(addr, privateKey)
return nil
})
}
func (db *keyStoreDB) GetPrivateKey(address common.Address) (*ecdsa.PrivateKey, error) {
key := append(addrKeyPre, address.Bytes()...)
keyDer, err := db.storage.GetData(key)
if err != nil {
return nil, err
}
return x509.ParseECPrivateKey(keyDer)
}
func (db *keyStoreDB) PutPrivateKey(addr common.Address, key *ecdsa.PrivateKey) error {
bs, err := x509.MarshalECPrivateKey(key)
if err != nil {
return err
}
sKey := append(addrKeyPre, addr.Bytes()...)
return db.storage.SetData(sKey, bs)
}
func (db *keyStoreDB) SetDefaultAddress(address common.Address) error {
return db.storage.SetData(defaultAddressKey, address.Bytes())
}
func (db *keyStoreDB) RemoveAddress(address common.Address) error {
key := append(addrKeyPre, address.Bytes()...)
_, err := db.storage.GetData(key)
if err != nil {
return err
}
return db.storage.DelData(key)
}
func (db *keyStoreDB) DelDefault() error {
_, err := db.storage.GetData(defaultAddressKey)
if err != nil {
return err
}
return db.storage.DelData(defaultAddressKey)
}