-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathaccount_mixer.go
334 lines (268 loc) · 9.53 KB
/
account_mixer.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
332
333
334
package dcrlibwallet
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"net"
"decred.org/dcrwallet/v2/ticketbuyer"
w "decred.org/dcrwallet/v2/wallet"
"decred.org/dcrwallet/v2/wallet/udb"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/dcrutil/v4"
"github.com/planetdecred/dcrlibwallet/internal/certs"
)
const (
smalletSplitPoint = 000.00262144
ShuffleServer = "mix.decred.org"
MainnetShufflePort = "5760"
TestnetShufflePort = "15760"
MixedAccountBranch = int32(udb.ExternalBranch)
)
func (mw *MultiWallet) AddAccountMixerNotificationListener(accountMixerNotificationListener AccountMixerNotificationListener, uniqueIdentifier string) error {
mw.notificationListenersMu.Lock()
defer mw.notificationListenersMu.Unlock()
if _, ok := mw.accountMixerNotificationListener[uniqueIdentifier]; ok {
return errors.New(ErrListenerAlreadyExist)
}
mw.accountMixerNotificationListener[uniqueIdentifier] = accountMixerNotificationListener
return nil
}
func (mw *MultiWallet) RemoveAccountMixerNotificationListener(uniqueIdentifier string) {
mw.notificationListenersMu.Lock()
defer mw.notificationListenersMu.Unlock()
delete(mw.accountMixerNotificationListener, uniqueIdentifier)
}
// CreateMixerAccounts creates the two accounts needed for the account mixer. This function
// is added to ease unlocking the wallet before creating accounts. This function should be
// used with auto cspp mixer setup.
func (wallet *Wallet) CreateMixerAccounts(mixedAccount, unmixedAccount, privPass string) error {
accountMixerConfigSet := wallet.ReadBoolConfigValueForKey(AccountMixerConfigSet, false)
if accountMixerConfigSet {
return errors.New(ErrInvalid)
}
if wallet.HasAccount(mixedAccount) || wallet.HasAccount(unmixedAccount) {
return errors.New(ErrExist)
}
err := wallet.UnlockWallet([]byte(privPass))
if err != nil {
return err
}
defer wallet.LockWallet()
mixedAccountNumber, err := wallet.NextAccount(mixedAccount)
if err != nil {
return err
}
unmixedAccountNumber, err := wallet.NextAccount(unmixedAccount)
if err != nil {
return err
}
wallet.SetInt32ConfigValueForKey(AccountMixerMixedAccount, mixedAccountNumber)
wallet.SetInt32ConfigValueForKey(AccountMixerUnmixedAccount, unmixedAccountNumber)
wallet.SetBoolConfigValueForKey(AccountMixerConfigSet, true)
return nil
}
// SetAccountMixerConfig sets the config for mixed and unmixed account. Private passphrase is verifed
// for security even if not used. This function should be used with manual cspp mixer setup.
func (wallet *Wallet) SetAccountMixerConfig(mixedAccount, unmixedAccount int32, privPass string) error {
if mixedAccount == unmixedAccount {
return errors.New(ErrInvalid)
}
// Verify that account numbers are correct
_, err := wallet.GetAccount(mixedAccount)
if err != nil {
return errors.New(ErrNotExist)
}
_, err = wallet.GetAccount(unmixedAccount)
if err != nil {
return errors.New(ErrNotExist)
}
err = wallet.UnlockWallet([]byte(privPass))
if err != nil {
return err
}
wallet.LockWallet()
wallet.SetInt32ConfigValueForKey(AccountMixerMixedAccount, mixedAccount)
wallet.SetInt32ConfigValueForKey(AccountMixerUnmixedAccount, unmixedAccount)
wallet.SetBoolConfigValueForKey(AccountMixerConfigSet, true)
return nil
}
func (wallet *Wallet) AccountMixerMixChange() bool {
return wallet.ReadBoolConfigValueForKey(AccountMixerMixTxChange, false)
}
func (wallet *Wallet) AccountMixerConfigIsSet() bool {
return wallet.ReadBoolConfigValueForKey(AccountMixerConfigSet, false)
}
func (wallet *Wallet) MixedAccountNumber() int32 {
return wallet.ReadInt32ConfigValueForKey(AccountMixerMixedAccount, -1)
}
func (wallet *Wallet) UnmixedAccountNumber() int32 {
return wallet.ReadInt32ConfigValueForKey(AccountMixerUnmixedAccount, -1)
}
func (wallet *Wallet) ClearMixerConfig() {
wallet.SetInt32ConfigValueForKey(AccountMixerMixedAccount, -1)
wallet.SetInt32ConfigValueForKey(AccountMixerUnmixedAccount, -1)
wallet.SetBoolConfigValueForKey(AccountMixerConfigSet, false)
}
func (mw *MultiWallet) ReadyToMix(walletID int) (bool, error) {
wallet := mw.WalletWithID(walletID)
if wallet == nil {
return false, errors.New(ErrNotExist)
}
unmixedAccount := wallet.ReadInt32ConfigValueForKey(AccountMixerUnmixedAccount, -1)
hasMixableOutput, err := wallet.accountHasMixableOutput(unmixedAccount)
if err != nil {
return false, translateError(err)
}
return hasMixableOutput, nil
}
// StartAccountMixer starts the automatic account mixer
func (mw *MultiWallet) StartAccountMixer(walletID int, walletPassphrase string) error {
if !mw.IsConnectedToDecredNetwork() {
return errors.New(ErrNotConnected)
}
wallet := mw.WalletWithID(walletID)
if wallet == nil {
return errors.New(ErrNotExist)
}
cfg := wallet.readCSPPConfig()
if cfg == nil {
return errors.New(ErrFailedPrecondition)
}
hasMixableOutput, err := wallet.accountHasMixableOutput(int32(cfg.ChangeAccount))
if err != nil {
return translateError(err)
} else if !hasMixableOutput {
return errors.New(ErrNoMixableOutput)
}
tb := ticketbuyer.New(wallet.Internal())
tb.AccessConfig(func(c *ticketbuyer.Config) {
c.MixedAccountBranch = cfg.MixedAccountBranch
c.MixedAccount = cfg.MixedAccount
c.ChangeAccount = cfg.ChangeAccount
c.CSPPServer = cfg.CSPPServer
c.DialCSPPServer = cfg.DialCSPPServer
c.TicketSplitAccount = cfg.TicketSplitAccount
c.BuyTickets = false
c.MixChange = true
// c.VotingAccount = 0 // TODO: VotingAccount should be configurable.
})
err = wallet.UnlockWallet([]byte(walletPassphrase))
if err != nil {
return translateError(err)
}
go func() {
log.Info("Running account mixer")
if mw.accountMixerNotificationListener != nil {
mw.publishAccountMixerStarted(walletID)
}
ctx, cancel := mw.contextWithShutdownCancel()
wallet.cancelAccountMixer = cancel
err = tb.Run(ctx, []byte(walletPassphrase))
if err != nil {
log.Errorf("AccountMixer instance errored: %v", err)
}
wallet.cancelAccountMixer = nil
if mw.accountMixerNotificationListener != nil {
mw.publishAccountMixerEnded(walletID)
}
}()
return nil
}
func (wallet *Wallet) readCSPPConfig() *CSPPConfig {
mixedAccount := wallet.ReadInt32ConfigValueForKey(AccountMixerMixedAccount, -1)
unmixedAccount := wallet.ReadInt32ConfigValueForKey(AccountMixerUnmixedAccount, -1)
if mixedAccount == -1 || unmixedAccount == -1 {
// not configured for mixing
return nil
}
var shufflePort = TestnetShufflePort
var dialCSPPServer func(ctx context.Context, network, addr string) (net.Conn, error)
if wallet.chainParams.Net == chaincfg.MainNetParams().Net {
shufflePort = MainnetShufflePort
pool := x509.NewCertPool()
pool.AppendCertsFromPEM([]byte(certs.CSPP))
csppTLSConfig := new(tls.Config)
csppTLSConfig.ServerName = ShuffleServer
csppTLSConfig.RootCAs = pool
dailer := new(net.Dialer)
dialCSPPServer = func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := dailer.DialContext(context.Background(), network, addr)
if err != nil {
return nil, err
}
conn = tls.Client(conn, csppTLSConfig)
return conn, nil
}
}
return &CSPPConfig{
CSPPServer: ShuffleServer + ":" + shufflePort,
DialCSPPServer: dialCSPPServer,
MixedAccount: uint32(mixedAccount),
MixedAccountBranch: uint32(MixedAccountBranch),
ChangeAccount: uint32(unmixedAccount),
TicketSplitAccount: uint32(mixedAccount), // upstream desc: Account to derive fresh addresses from for mixed ticket splits; uses mixedaccount if unset
}
}
// StopAccountMixer stops the active account mixer
func (mw *MultiWallet) StopAccountMixer(walletID int) error {
wallet := mw.WalletWithID(walletID)
if wallet == nil {
return errors.New(ErrNotExist)
}
if wallet.cancelAccountMixer == nil {
return errors.New(ErrInvalid)
}
wallet.cancelAccountMixer()
wallet.cancelAccountMixer = nil
return nil
}
func (wallet *Wallet) accountHasMixableOutput(accountNumber int32) (bool, error) {
policy := w.OutputSelectionPolicy{
Account: uint32(accountNumber),
RequiredConfirmations: wallet.RequiredConfirmations(),
}
// fetch all utxos in account to extract details for the utxos selected by user
// use targetAmount = 0 to fetch ALL utxos in account
inputDetail, err := wallet.Internal().SelectInputs(wallet.shutdownContext(), dcrutil.Amount(0), policy)
if err != nil {
return false, nil
}
hasMixableOutput := false
for _, input := range inputDetail.Inputs {
if AmountCoin(input.ValueIn) > smalletSplitPoint {
hasMixableOutput = true
break
}
}
if !hasMixableOutput {
accountName, err := wallet.AccountName(accountNumber)
if err != nil {
return hasMixableOutput, nil
}
lockedOutpoints, err := wallet.Internal().LockedOutpoints(wallet.shutdownContext(), accountName)
if err != nil {
return hasMixableOutput, nil
}
hasMixableOutput = len(lockedOutpoints) > 0
}
return hasMixableOutput, nil
}
// IsAccountMixerActive returns true if account mixer is active
func (wallet *Wallet) IsAccountMixerActive() bool {
return wallet.cancelAccountMixer != nil
}
func (mw *MultiWallet) publishAccountMixerStarted(walletID int) {
mw.notificationListenersMu.RLock()
defer mw.notificationListenersMu.RUnlock()
for _, accountMixerNotificationListener := range mw.accountMixerNotificationListener {
accountMixerNotificationListener.OnAccountMixerStarted(walletID)
}
}
func (mw *MultiWallet) publishAccountMixerEnded(walletID int) {
mw.notificationListenersMu.RLock()
defer mw.notificationListenersMu.RUnlock()
for _, accountMixerNotificationListener := range mw.accountMixerNotificationListener {
accountMixerNotificationListener.OnAccountMixerEnded(walletID)
}
}