forked from iotaledger/goshimmer
/
wallets.go
443 lines (377 loc) · 12.1 KB
/
wallets.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package evilwallet
import (
"sync"
"github.com/cockroachdb/errors"
"github.com/izuc/zipp.foundation/core/generics/lo"
"github.com/izuc/zipp.foundation/core/types"
"go.uber.org/atomic"
"github.com/izuc/zipp/client/wallet/packages/address"
"github.com/izuc/zipp/client/wallet/packages/seed"
"github.com/izuc/zipp/packages/core/ledger/utxo"
"github.com/izuc/zipp/packages/core/ledger/vm/devnetvm"
)
type walletID int
// WalletType is the type of the wallet.
type WalletType int8
type WalletStatus int8
const (
Other WalletType = iota
// Fresh is used for automatic Faucet Requests, outputs are returned one by one
Fresh
// Reuse stores resulting outputs of double spends or transactions issued by the evilWallet,
// outputs from this wallet are reused in spamming scenario with flag reuse set to true and no RestrictedReuse wallet provided.
// Reusing spammed outputs allows for creation of deeper UTXO DAG structure.
Reuse
// RestrictedReuse it is a reuse wallet, that will be available only to spamming scenarios that
// will provide RestrictedWallet for the reuse spamming.
RestrictedReuse
)
// region Wallets ///////////////////////////////////////////////////////////////////////////////////////////////////////
// Wallets is a container of wallets.
type Wallets struct {
wallets map[walletID]*Wallet
// we store here non-empty wallets ids of wallets with Fresh faucet outputs.
faucetWallets []walletID
// reuse wallets are stored without an order, so they are picked up randomly.
// Boolean flag indicates if wallet is ready - no new addresses will be generated, so empty wallets can be deleted.
reuseWallets map[walletID]bool
mu sync.RWMutex
lastWalletID atomic.Int64
}
// NewWallets creates and returns a new Wallets container.
func NewWallets() *Wallets {
return &Wallets{
wallets: make(map[walletID]*Wallet),
faucetWallets: make([]walletID, 0),
reuseWallets: make(map[walletID]bool),
lastWalletID: *atomic.NewInt64(-1),
}
}
// NewWallet adds a new wallet to Wallets and returns the created wallet.
func (w *Wallets) NewWallet(walletType WalletType) *Wallet {
wallet := NewWallet(walletType)
wallet.ID = walletID(w.lastWalletID.Add(1))
w.addWallet(wallet)
if walletType == Reuse {
w.addReuseWallet(wallet)
}
return wallet
}
// GetWallet returns the wallet with the specified ID.
func (w *Wallets) GetWallet(walletID walletID) *Wallet {
return w.wallets[walletID]
}
// GetNextWallet get next non-empty wallet based on provided type.
func (w *Wallets) GetNextWallet(walletType WalletType, minOutputsLeft int) (*Wallet, error) {
w.mu.Lock()
defer w.mu.Unlock()
switch walletType {
case Fresh:
if !w.IsFaucetWalletAvailable() {
return nil, errors.New("no faucet wallets available, need to request more funds")
}
wallet := w.wallets[w.faucetWallets[0]]
if wallet.IsEmpty() {
return nil, errors.New("wallet is empty, need to request more funds")
}
return wallet, nil
case Reuse:
for id, ready := range w.reuseWallets {
wal := w.wallets[id]
if wal.UnspentOutputsLeft() > minOutputsLeft {
// if are solid
return wal, nil
}
// no outputs will be ever added to this wallet, so it can be deleted
if wal.IsEmpty() && ready {
w.removeReuseWallet(id)
}
}
return nil, errors.New("no reuse wallets available")
}
return nil, errors.New("wallet type not supported for ordered usage, use GetWallet by ID instead")
}
func (w *Wallets) UnspentOutputsLeft(walletType WalletType) int {
w.mu.RLock()
defer w.mu.RUnlock()
outputsLeft := 0
switch walletType {
case Fresh:
for _, wID := range w.faucetWallets {
outputsLeft += w.wallets[wID].UnspentOutputsLeft()
}
case Reuse:
for wID := range w.reuseWallets {
outputsLeft += w.wallets[wID].UnspentOutputsLeft()
}
}
return outputsLeft
}
// addWallet stores newly created wallet.
func (w *Wallets) addWallet(wallet *Wallet) {
w.mu.Lock()
defer w.mu.Unlock()
w.wallets[wallet.ID] = wallet
}
// addReuseWallet stores newly created wallet.
func (w *Wallets) addReuseWallet(wallet *Wallet) {
w.mu.Lock()
defer w.mu.Unlock()
w.reuseWallets[wallet.ID] = false
}
// GetUnspentOutput gets first found unspent output for a given walletType
func (w *Wallets) GetUnspentOutput(wallet *Wallet) *Output {
if wallet == nil {
return nil
}
return wallet.GetUnspentOutput()
}
// IsFaucetWalletAvailable checks if there is any faucet wallet left.
func (w *Wallets) IsFaucetWalletAvailable() bool {
return len(w.faucetWallets) > 0
}
// freshWallet returns the first non-empty wallet from the faucetWallets queue. If current wallet is empty,
// it is removed and the next enqueued one is returned.
func (w *Wallets) freshWallet() (*Wallet, error) {
wallet, err := w.GetNextWallet(Fresh, 1)
if err != nil {
w.removeFreshWallet()
wallet, err = w.GetNextWallet(Fresh, 1)
if err != nil {
return nil, err
}
}
return wallet, nil
}
// reuseWallet returns the first non-empty wallet from the reuseWallets queue. If current wallet is empty,
// it is removed and the next enqueued one is returned.
func (w *Wallets) reuseWallet(outputsNeeded int) *Wallet {
wallet, err := w.GetNextWallet(Reuse, outputsNeeded)
if err != nil {
return nil
}
return wallet
}
// removeWallet removes wallet, for Fresh wallet it will be the first wallet in a queue.
func (w *Wallets) removeFreshWallet() {
w.mu.Lock()
defer w.mu.Unlock()
if w.IsFaucetWalletAvailable() {
removedID := w.faucetWallets[0]
w.faucetWallets = w.faucetWallets[1:]
delete(w.wallets, removedID)
}
return
}
// removeWallet removes wallet, for Fresh wallet it will be the first wallet in a queue.
func (w *Wallets) removeReuseWallet(walletID walletID) {
if _, ok := w.reuseWallets[walletID]; ok {
delete(w.reuseWallets, walletID)
delete(w.wallets, walletID)
}
return
}
// SetWalletReady makes wallet ready to use, Fresh wallet is added to freshWallets queue.
func (w *Wallets) SetWalletReady(wallet *Wallet) {
w.mu.Lock()
defer w.mu.Unlock()
if wallet.IsEmpty() {
return
}
wType := wallet.walletType
switch wType {
case Fresh:
w.faucetWallets = append(w.faucetWallets, wallet.ID)
case Reuse:
w.reuseWallets[wallet.ID] = true
}
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////
// region Wallet ///////////////////////////////////////////////////////////////////////////////////////////////////////
// Wallet is the definition of a wallet.
type Wallet struct {
ID walletID
walletType WalletType
unspentOutputs map[string]*Output // maps addr to its unspentOutput
indexAddrMap map[uint64]string
addrIndexMap map[string]uint64
inputTransactions map[string]types.Empty
reuseAddressPool map[string]types.Empty
seed *seed.Seed
lastAddrIdxUsed atomic.Int64 // used during filling in wallet with new outputs
lastAddrSpent atomic.Int64 // used during spamming with outputs one by one
*sync.RWMutex
}
// NewWallet creates a wallet of a given type.
func NewWallet(wType ...WalletType) *Wallet {
walletType := Other
if len(wType) > 0 {
walletType = wType[0]
}
idxSpent := atomic.NewInt64(-1)
addrUsed := atomic.NewInt64(-1)
wallet := &Wallet{
walletType: walletType,
ID: -1,
seed: seed.NewSeed(),
unspentOutputs: make(map[string]*Output),
indexAddrMap: make(map[uint64]string),
addrIndexMap: make(map[string]uint64),
inputTransactions: make(map[string]types.Empty),
lastAddrSpent: *idxSpent,
lastAddrIdxUsed: *addrUsed,
RWMutex: &sync.RWMutex{},
}
if walletType == Reuse {
wallet.reuseAddressPool = make(map[string]types.Empty)
}
return wallet
}
// Type returns the wallet type.
func (w *Wallet) Type() WalletType {
return w.walletType
}
// Address returns a new and unused address of a given wallet.
func (w *Wallet) Address() address.Address {
w.Lock()
defer w.Unlock()
index := uint64(w.lastAddrIdxUsed.Add(1))
addr := w.seed.Address(index)
w.indexAddrMap[index] = addr.Base58()
w.addrIndexMap[addr.Base58()] = index
return addr
}
// UnspentOutput returns the unspent output on the address.
func (w *Wallet) UnspentOutput(addr string) *Output {
w.RLock()
defer w.RUnlock()
return w.unspentOutputs[addr]
}
// UnspentOutputs returns all unspent outputs on the wallet.
func (w *Wallet) UnspentOutputs() (outputs map[string]*Output) {
w.RLock()
defer w.RUnlock()
outputs = make(map[string]*Output)
for addr, out := range w.unspentOutputs {
outputs[addr] = out
}
return outputs
}
// IndexAddrMap returns the address for the index specified.
func (w *Wallet) IndexAddrMap(outIndex uint64) string {
w.RLock()
defer w.RUnlock()
return w.indexAddrMap[outIndex]
}
// AddrIndexMap returns the index for the address specified.
func (w *Wallet) AddrIndexMap(outIndex string) uint64 {
w.RLock()
defer w.RUnlock()
return w.addrIndexMap[outIndex]
}
// AddUnspentOutput adds an unspentOutput of a given wallet.
func (w *Wallet) AddUnspentOutput(addr devnetvm.Address, addrIdx uint64, outputID utxo.OutputID, balance *devnetvm.ColoredBalances) *Output {
w.Lock()
defer w.Unlock()
out := &Output{
OutputID: outputID,
Address: addr,
Index: addrIdx,
Balance: balance,
}
w.unspentOutputs[addr.Base58()] = out
return out
}
// UnspentOutputBalance returns the balance on the unspent output sitting on the address specified.
func (w *Wallet) UnspentOutputBalance(addr string) *devnetvm.ColoredBalances {
w.RLock()
defer w.RUnlock()
if out, ok := w.unspentOutputs[addr]; ok {
return out.Balance
}
return &devnetvm.ColoredBalances{}
}
// IsEmpty returns true if the wallet is empty.
func (w *Wallet) IsEmpty() (empty bool) {
switch w.walletType {
case Reuse:
empty = len(w.reuseAddressPool) == 0
default:
empty = w.lastAddrSpent.Load() == w.lastAddrIdxUsed.Load() || w.UnspentOutputsLength() == 0
}
return
}
// UnspentOutputsLeft returns how many unused outputs are available in wallet.
func (w *Wallet) UnspentOutputsLeft() (left int) {
switch w.walletType {
case Reuse:
left = len(w.reuseAddressPool)
default:
left = int(w.lastAddrIdxUsed.Load() - w.lastAddrSpent.Load())
}
return
}
// AddReuseAddress adds address to the reuse ready outputs' addresses pool for a Reuse wallet.
func (w *Wallet) AddReuseAddress(addr string) {
w.Lock()
defer w.Unlock()
if w.walletType == Reuse {
w.reuseAddressPool[addr] = types.Void
}
}
// GetReuseAddress get random address from reuse addresses reuseOutputsAddresses pool. Address is removed from the pool after selecting.
func (w *Wallet) GetReuseAddress() string {
w.Lock()
defer w.Unlock()
if w.walletType == Reuse {
if len(w.reuseAddressPool) > 0 {
for addr := range w.reuseAddressPool {
delete(w.reuseAddressPool, addr)
return addr
}
}
}
return ""
}
// GetUnspentOutput returns an unspent output on the oldest address ordered by index.
func (w *Wallet) GetUnspentOutput() *Output {
switch w.walletType {
case Reuse:
addr := w.GetReuseAddress()
return w.UnspentOutput(addr)
default:
if w.lastAddrSpent.Load() < w.lastAddrIdxUsed.Load() {
idx := w.lastAddrSpent.Add(1)
addr := w.IndexAddrMap(uint64(idx))
out := w.UnspentOutput(addr)
return out
}
}
return nil
}
// Sign signs the tx essence.
func (w *Wallet) Sign(addr devnetvm.Address, txEssence *devnetvm.TransactionEssence) *devnetvm.ED25519Signature {
w.RLock()
defer w.RUnlock()
index := w.AddrIndexMap(addr.Base58())
kp := w.seed.KeyPair(index)
return devnetvm.NewED25519Signature(kp.PublicKey, kp.PrivateKey.Sign(lo.PanicOnErr(txEssence.Bytes())))
}
// UpdateUnspentOutputID updates the unspent output on the address specified.
func (w *Wallet) UpdateUnspentOutputID(addr string, outputID utxo.OutputID) error {
w.RLock()
walletOutput, ok := w.unspentOutputs[addr]
w.RUnlock()
if !ok {
return errors.Newf("could not find unspent output under provided address in the wallet, outID:%s, addr: %s", outputID.Base58(), addr)
}
w.Lock()
walletOutput.OutputID = outputID
w.Unlock()
return nil
}
// UnspentOutputsLength returns the number of unspent outputs on the wallet.
func (w *Wallet) UnspentOutputsLength() int {
return len(w.unspentOutputs)
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////