-
Notifications
You must be signed in to change notification settings - Fork 84
/
account.go
218 lines (190 loc) · 5.54 KB
/
account.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
package core
import (
"encoding/hex"
"errors"
"fmt"
"decred.org/dcrdex/client/db"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
// AccountDisable is used to disable an account by given host and application
// password.
func (c *Core) AccountDisable(pw []byte, addr string) error {
// Validate password.
_, err := c.encryptionKey(pw)
if err != nil {
return codedError(passwordErr, err)
}
// Get dex connection by host.
dc, _, err := c.dex(addr)
if err != nil {
return newError(unknownDEXErr, "error retrieving dex conn: %w", err)
}
// Check active orders.
if dc.hasActiveOrders() {
return fmt.Errorf("cannot disable account with active orders")
}
err = c.db.DisableAccount(dc.acct.host)
if err != nil {
return newError(accountDisableErr, "error disabling account: %w", err)
}
// Stop dexConnection books.
dc.cfgMtx.RLock()
for _, m := range dc.cfg.Markets {
// Empty bookie's feeds map, close feeds' channels & stop close timers.
dc.booksMtx.Lock()
if b, found := dc.books[m.Name]; found {
b.closeFeeds()
if b.closeTimer != nil {
b.closeTimer.Stop()
}
}
dc.booksMtx.Unlock()
dc.stopBook(m.Base, m.Quote)
}
dc.cfgMtx.RUnlock()
// Disconnect and delete connection from map.
dc.connMaster.Disconnect()
c.connMtx.Lock()
delete(c.conns, dc.acct.host)
c.connMtx.Unlock()
return nil
}
// AccountExport is used to retrieve account by host for export.
func (c *Core) AccountExport(pw []byte, host string) (*Account, error) {
crypter, err := c.encryptionKey(pw)
if err != nil {
return nil, codedError(passwordErr, err)
}
defer crypter.Close()
host, err = addrHost(host)
if err != nil {
return nil, newError(addressParseErr, "error parsing address: %w", err)
}
// Get the dexConnection and the dex.Asset for each asset.
c.connMtx.RLock()
dc, found := c.conns[host]
c.connMtx.RUnlock()
if !found {
return nil, newError(unknownDEXErr, "DEX: %s", host)
}
// Unlock account if it is locked so that account id and privKey can be retrieved.
if err = dc.acct.unlock(crypter); err != nil {
return nil, codedError(acctKeyErr, err)
}
dc.acct.keyMtx.RLock()
privKey := hex.EncodeToString(dc.acct.privKey.Serialize())
dc.acct.keyMtx.RUnlock()
feeProofSig := ""
var feeProofStamp uint64
if dc.acct.isPaid {
accountProof, err := c.db.AccountProof(host)
if err != nil {
return nil, codedError(accountProofErr, err)
}
feeProofSig = hex.EncodeToString(accountProof.Sig)
feeProofStamp = accountProof.Stamp
}
// Account ID is exported for informational purposes only, it is not used during import.
acct := &Account{
Host: host,
AccountID: dc.acct.id.String(),
// PrivKey: Note that we don't differentiate between legacy and
// hierarchical private keys here. On import, all keys are treated as
// legacy keys.
PrivKey: privKey,
DEXPubKey: hex.EncodeToString(dc.acct.dexPubKey.SerializeCompressed()),
Cert: hex.EncodeToString(dc.acct.cert),
FeeCoin: hex.EncodeToString(dc.acct.feeCoin),
FeeProofSig: feeProofSig,
FeeProofStamp: feeProofStamp,
}
return acct, nil
}
// AccountImport is used import an existing account into the db.
func (c *Core) AccountImport(pw []byte, acct Account) error {
crypter, err := c.encryptionKey(pw)
if err != nil {
return codedError(passwordErr, err)
}
host, err := addrHost(acct.Host)
if err != nil {
return newError(addressParseErr, "error parsing address: %w", err)
}
accountInfo := db.AccountInfo{Host: host}
DEXpubKey, err := hex.DecodeString(acct.DEXPubKey)
if err != nil {
return codedError(decodeErr, err)
}
accountInfo.DEXPubKey, err = secp256k1.ParsePubKey(DEXpubKey)
if err != nil {
return codedError(parseKeyErr, err)
}
accountInfo.Cert, err = hex.DecodeString(acct.Cert)
if err != nil {
return codedError(decodeErr, err)
}
accountInfo.FeeCoin, err = hex.DecodeString(acct.FeeCoin)
if err != nil {
return codedError(decodeErr, err)
}
privKey, err := hex.DecodeString(acct.PrivKey)
if err != nil {
return codedError(decodeErr, err)
}
accountInfo.LegacyEncKey, err = crypter.Encrypt(privKey)
if err != nil {
return codedError(encryptionErr, err)
}
accountInfo.Paid = acct.FeeProofSig != "" && acct.FeeProofStamp != 0
// Make a connection to the DEX.
if dc, connected := c.connectAccount(&accountInfo); !connected {
if dc != nil {
dc.connMaster.Disconnect() // stop reconnect loop
c.connMtx.Lock()
delete(c.conns, dc.acct.host)
c.connMtx.Unlock()
}
return newError(accountVerificationErr, "Account not verified for host: %s", host)
}
err = c.db.CreateAccount(&accountInfo)
if err != nil {
return codedError(dbErr, err)
}
if accountInfo.Paid {
sig, err := hex.DecodeString(acct.FeeProofSig)
if err != nil {
return codedError(decodeErr, err)
}
accountProof := db.AccountProof{
Host: host,
Stamp: acct.FeeProofStamp,
Sig: sig,
}
err = c.db.AccountPaid(&accountProof)
if err != nil {
return codedError(dbErr, err)
}
}
return nil
}
// UpdateCert attempts to connect to a server using a new TLS certificate. If
// the connection is successful, then the cert in the database is updated.
func (c *Core) UpdateCert(host string, cert []byte) error {
account, err := c.db.Account(host)
if err != nil {
return err
}
if account == nil {
return fmt.Errorf("account does not exist for host: %v", host)
}
account.Cert = cert
_, connected := c.connectAccount(account)
if !connected {
return errors.New("failed to connect using new cert")
}
err = c.db.UpdateAccountInfo(account)
if err != nil {
return fmt.Errorf("failed to update account info: %w", err)
}
return nil
}