-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
177 lines (143 loc) · 4.04 KB
/
database.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
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package wallet
import (
"fmt"
"sync"
"github.com/FactomProject/factom"
"github.com/FactomProject/factomd/common/factoid"
)
// Wallet is a connection to a Factom Wallet Database
type Wallet struct {
*WalletDatabaseOverlay
Encrypted bool
DBPath string
txlock sync.Mutex
transactions map[string]*factoid.Transaction
txdb *TXDatabaseOverlay
}
func (w *Wallet) InitWallet() error {
dbSeed, err := w.GetOrCreateDBSeed()
if err != nil {
return err
}
if dbSeed == nil {
return fmt.Errorf("dbSeed not present in DB")
}
return nil
}
func NewOrOpenLevelDBWallet(path string) (*Wallet, error) {
w := new(Wallet)
w.transactions = make(map[string]*factoid.Transaction)
db, err := NewLevelDB(path)
if err != nil {
return nil, err
}
w.WalletDatabaseOverlay = db
if err = w.InitWallet(); err != nil {
return nil, err
}
return w, nil
}
func NewOrOpenBoltDBWallet(path string) (*Wallet, error) {
w := new(Wallet)
w.transactions = make(map[string]*factoid.Transaction)
db, err := NewBoltDB(path)
if err != nil {
return nil, err
}
w.WalletDatabaseOverlay = db
if err = w.InitWallet(); err != nil {
return nil, err
}
return w, nil
}
func NewEncryptedBoltDBWallet(path, password string) (*Wallet, error) {
w := new(Wallet)
w.transactions = make(map[string]*factoid.Transaction)
db, err := NewEncryptedBoltDB(path, password)
if err != nil {
return nil, err
}
w.WalletDatabaseOverlay = db
if err = w.InitWallet(); err != nil {
return nil, err
}
return w, nil
}
func NewEncryptedBoltDBWalletAwaitingPassphrase(path string) (*Wallet, error) {
w := new(Wallet)
w.transactions = make(map[string]*factoid.Transaction)
w.Encrypted = true
w.DBPath = path
return w, nil
}
func NewMapDBWallet() (*Wallet, error) {
w := new(Wallet)
w.transactions = make(map[string]*factoid.Transaction)
w.WalletDatabaseOverlay = NewMapDB()
if err := w.InitWallet(); err != nil {
return nil, err
}
return w, nil
}
// Close closes a Factom Wallet Database
func (w *Wallet) Close() error {
if w.WalletDatabaseOverlay == nil {
return nil
}
return w.DBO.Close()
}
// AddTXDB allows the wallet api to read from a local transaction cashe.
func (w *Wallet) AddTXDB(t *TXDatabaseOverlay) {
w.txdb = t
}
// TXDB returns a handle for the Transaction Database.
func (w *Wallet) TXDB() *TXDatabaseOverlay {
return w.txdb
}
// GenerateECAddress creates and stores a new Entry Credit Address in the
// Wallet. The address can be reproduced in the future using the Wallet Seed.
func (w *Wallet) GenerateECAddress() (*factom.ECAddress, error) {
return w.GetNextECAddress()
}
// GenerateFCTAddress creates and stores a new Factoid Address in the Wallet.
// The address can be reproduced in the future using the Wallet Seed.
func (w *Wallet) GenerateFCTAddress() (*factom.FactoidAddress, error) {
return w.GetNextFCTAddress()
}
// GenerateIdentityKey creates and stores a new Identity Key in the Wallet.
func (w *Wallet) GenerateIdentityKey() (*factom.IdentityKey, error) {
return w.GetNextIdentityKey()
}
// GetAllAddresses retrieves all Entry Credit and Factoid Addresses from the
// Wallet Database.
func (w *Wallet) GetAllAddresses() ([]*factom.FactoidAddress, []*factom.ECAddress, error) {
fcs, err := w.GetAllFCTAddresses()
if err != nil {
return nil, nil, err
}
ecs, err := w.GetAllECAddresses()
if err != nil {
return nil, nil, err
}
return fcs, ecs, nil
}
// GetSeed returns the string representaion of the Wallet Seed. The Wallet Seed
// can be used to regenerate the Factoid and Entry Credit Addresses previously
// generated by the wallet. Note that Addresses that are imported into the
// Wallet cannot be regenerated using the Wallet Seed.
func (w *Wallet) GetSeed() (string, error) {
seed, err := w.GetDBSeed()
if err != nil {
return "", err
}
return seed.MnemonicSeed, nil
}
func (w *Wallet) GetVersion() string {
return WalletVersion
}
func (w *Wallet) GetApiVersion() string {
return ApiVersion
}