-
Notifications
You must be signed in to change notification settings - Fork 311
/
command_identities.go
642 lines (536 loc) · 16.4 KB
/
command_identities.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
* Copyright (C) 2021 The "MysteriumNetwork/node" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cli
import (
"errors"
"fmt"
"math/big"
"os"
"strconv"
"strings"
"time"
"github.com/shopspring/decimal"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/mysteriumnetwork/node/cmd/commands/cli/clio"
"github.com/mysteriumnetwork/node/config"
"github.com/mysteriumnetwork/node/core/beneficiary"
"github.com/mysteriumnetwork/node/core/node"
"github.com/mysteriumnetwork/node/identity"
"github.com/mysteriumnetwork/node/money"
"github.com/mysteriumnetwork/payments/units"
)
func (c *cliApp) identities(args []string) (err error) {
usage := strings.Join([]string{
"Usage: identities <action> [args]",
"Available actions:",
" " + usageListIdentities,
" " + usageGetIdentity,
" " + usageGetBalance,
" " + usageNewIdentity,
" " + usageUnlockIdentity,
" " + usageRegisterIdentity,
" " + usageSettle,
" " + usageSetBeneficiary,
" " + usageSetBeneficiaryStatus,
" " + usageGetReferralCode,
" " + usageExportIdentity,
" " + usageImportIdentity,
" " + usageWithdraw,
" " + usageLastWithdrawal,
" " + usageMigrateHermesStatus,
" " + usageMigrateHermes,
}, "\n")
if len(args) == 0 {
clio.Info(usage)
return
}
action := args[0]
actionArgs := args[1:]
switch action {
case "list":
return c.listIdentities(actionArgs)
case "get":
return c.getIdentity(actionArgs)
case "balance":
return c.getBalance(actionArgs)
case "new":
return c.newIdentity(actionArgs)
case "unlock":
return c.unlockIdentity(actionArgs)
case "register":
return c.registerIdentity(actionArgs)
case "settle":
return c.settle(actionArgs)
case "beneficiary-set":
return c.setBeneficiary(actionArgs)
case "beneficiary-status":
return c.getBeneficiaryStatus(actionArgs)
case "referralcode":
return c.getReferralCode(actionArgs)
case "export":
return c.exportIdentity(actionArgs)
case "import":
return c.importIdentity(actionArgs)
case "withdraw":
return c.withdraw(actionArgs)
case "last-withdrawal":
return c.lastWithdrawal(actionArgs)
case "migrate-hermes":
return c.migrateHermes(actionArgs)
case "migrate-hermes-status":
return c.migrateHermesStatus(actionArgs)
default:
fmt.Println(usage)
return errUnknownSubCommand(args[0])
}
}
const usageListIdentities = "list"
func (c *cliApp) listIdentities(args []string) (err error) {
if len(args) > 0 {
clio.Info("Usage: " + usageListIdentities)
return errWrongArgumentCount
}
ids, err := c.tequilapi.GetIdentities()
if err != nil {
return err
}
for _, id := range ids {
clio.Status("+", id.Address)
}
return nil
}
const usageGetBalance = "balance <identity>"
func (c *cliApp) getBalance(actionArgs []string) (err error) {
if len(actionArgs) != 1 {
clio.Info("Usage: " + usageGetBalance)
return errWrongArgumentCount
}
address := actionArgs[0]
balance, err := c.tequilapi.BalanceRefresh(address)
if err != nil {
return err
}
clio.Info(fmt.Sprintf("Balance: %s MYST", balance.BalanceTokens))
return nil
}
const usageGetIdentity = "get <identity>"
func (c *cliApp) getIdentity(actionArgs []string) (err error) {
if len(actionArgs) != 1 {
clio.Info("Usage: " + usageGetIdentity)
return errWrongArgumentCount
}
address := actionArgs[0]
identityStatus, err := c.tequilapi.Identity(address)
if err != nil {
return err
}
clio.Info("Registration Status: ", identityStatus.RegistrationStatus)
clio.Info("Channel address: ", identityStatus.ChannelAddress)
clio.Info(fmt.Sprintf("Balance: %s MYST", identityStatus.BalanceTokens))
clio.Info(fmt.Sprintf("Earnings: %s", money.New(identityStatus.Earnings)))
clio.Info(fmt.Sprintf("Earnings total: %s", money.New(identityStatus.EarningsTotal)))
return nil
}
const usageNewIdentity = "new [passphrase]"
func (c *cliApp) newIdentity(args []string) (err error) {
if len(args) > 1 {
clio.Info("Usage: " + usageNewIdentity)
return errWrongArgumentCount
}
passphrase := identityDefaultPassphrase
if len(args) == 1 {
passphrase = args[0]
}
id, err := c.tequilapi.NewIdentity(passphrase)
if err != nil {
return err
}
clio.Success("New identity created:", id.Address)
return nil
}
const usageUnlockIdentity = "unlock <identity> [passphrase]"
func (c *cliApp) unlockIdentity(actionArgs []string) (err error) {
if len(actionArgs) < 1 {
clio.Info("Usage: " + usageUnlockIdentity)
return errWrongArgumentCount
}
address := actionArgs[0]
var passphrase string
if len(actionArgs) >= 2 {
passphrase = actionArgs[1]
}
clio.Info("Unlocking ", address)
err = c.tequilapi.Unlock(address, passphrase)
if err != nil {
return err
}
clio.Success(fmt.Sprintf("Identity %s unlocked.", address))
return nil
}
const usageRegisterIdentity = "register <identity> [beneficiary] [referralcode]"
func (c *cliApp) registerIdentity(actionArgs []string) error {
if len(actionArgs) < 1 || len(actionArgs) > 3 {
clio.Info("Usage: " + usageRegisterIdentity)
return errWrongArgumentCount
}
address := actionArgs[0]
var beneficiary string
if len(actionArgs) >= 2 {
beneficiary = actionArgs[1]
}
var token *string
if len(actionArgs) >= 3 {
token = &actionArgs[2]
}
err := c.tequilapi.RegisterIdentity(address, beneficiary, token)
if err != nil {
return fmt.Errorf("could not register identity: %w", err)
}
msg := "Registration started. Top up the identities channel to finish it."
clio.Info(msg)
clio.Info(fmt.Sprintf("To explore additional information about the identity use: identities %s", usageGetIdentity))
return nil
}
const usageSettle = "settle <providerIdentity> [hermesID,hermesID2]"
func (c *cliApp) settle(args []string) (err error) {
if len(args) == 0 || len(args) > 2 {
clio.Info("Usage: " + usageSettle)
fees, err := c.tequilapi.GetTransactorFees()
if err != nil {
clio.Warn("Could not get transactor fee:", err)
}
clio.Info(fmt.Sprintf("Transactor fee: %s MYST", fees.SettlementTokens))
hermesPct, err := decimal.NewFromString(fees.HermesPercent)
if err != nil {
clio.Info("Could not retrieve hermes fee:", err)
}
clio.Info(fmt.Sprintf("Hermes fee: %s%%", hermesPct.Mul(decimal.NewFromInt(100)).StringFixed(2)))
return errWrongArgumentCount
}
hermesID, err := c.config.GetHermesID()
if err != nil {
return fmt.Errorf("could not get Hermes ID: %w", err)
}
hermesIDs := make([]common.Address, 0)
if len(args) == 2 {
ids := strings.Split(args[1], ",")
for _, h := range ids {
hermesIDs = append(hermesIDs, common.HexToAddress(h))
}
} else {
hermesIDs = append(hermesIDs, common.HexToAddress(hermesID))
}
clio.Infof("Will settle with hermes: %v\n", hermesIDs)
clio.Info("Waiting for settlement to complete")
errChan := make(chan error)
go func() {
errChan <- c.tequilapi.Settle(identity.FromAddress(args[0]), hermesIDs, true)
}()
timeout := time.After(time.Minute * 5)
for {
select {
case <-timeout:
fmt.Println()
return errTimeout
case <-time.After(time.Millisecond * 500):
fmt.Print(".")
case err := <-errChan:
fmt.Println()
if err != nil {
return fmt.Errorf("settlement failed: %w", err)
}
clio.Info("settlement succeeded")
return nil
}
}
}
const usageSetBeneficiary = "beneficiary-set <providerIdentity> <beneficiary> [hermesID]"
func (c *cliApp) setBeneficiary(actionArgs []string) error {
if len(actionArgs) < 2 || len(actionArgs) > 3 {
clio.Info("Usage: " + usageSetBeneficiary)
return errors.New("malformed args")
}
address := actionArgs[0]
benef := actionArgs[1]
hermesID := ""
if len(actionArgs) == 3 {
hermesID = actionArgs[2]
}
err := c.tequilapi.SettleWithBeneficiary(address, benef, hermesID)
if err != nil {
return err
}
timeout := time.After(30 * time.Second)
for {
select {
case <-timeout:
clio.Info("Beneficiary change in progress")
clio.Info(fmt.Sprintf("To get additional information use command: \"%s\"", usageSetBeneficiaryStatus))
return nil
case <-time.After(time.Second):
st, err := c.tequilapi.SettleWithBeneficiaryStatus(address)
if err != nil {
break
}
if !strings.EqualFold(st.ChangeTo, benef) || st.State != beneficiary.Completed {
break
}
if st.Error != "" {
return fmt.Errorf("could not set new beneficiary address: %s", st.Error)
}
data, err := c.tequilapi.Beneficiary(address)
if err != nil {
break
}
if strings.EqualFold(data.Beneficiary, benef) {
clio.Success("New beneficiary address set")
return nil
}
}
}
}
const usageSetBeneficiaryStatus = "beneficiary-status <identity>"
func (c *cliApp) getBeneficiaryStatus(actionArgs []string) error {
if len(actionArgs) != 1 {
return errors.New("malformed args")
}
address := actionArgs[0]
data, err := c.tequilapi.Beneficiary(address)
if err != nil {
return fmt.Errorf("could not get current beneficiary: %w", err)
}
clio.Info(fmt.Sprintf("Current beneficiary: %s", data.Beneficiary))
clio.Info(fmt.Sprintf("Is a channel address: %t", data.IsChannelAddress))
st, err := c.tequilapi.SettleWithBeneficiaryStatus(address)
if err != nil {
return fmt.Errorf("could not get beneficiary change status: %w", err)
}
if st.State == beneficiary.NotFound {
clio.Info("No last change request information")
}
clio.Info("Last change request information:")
clio.Info(fmt.Sprintf("Change to: %s", st.ChangeTo))
clio.Info(fmt.Sprintf("State: %s", st.State))
if st.Error != "" {
clio.Warn(fmt.Sprintf("Error: %s", st.Error))
}
return nil
}
const usageWithdraw = "withdraw <providerIdentity> <beneficiary> <toChain> [amount]"
func (c *cliApp) withdraw(args []string) error {
if len(args) < 3 {
clio.Info("Usage: " + usageWithdraw)
fees, err := c.tequilapi.GetTransactorFees()
if err != nil {
clio.Warn("Could not get transactor fee:", err)
}
clio.Info(fmt.Sprintf("Transactor fee: %s MYST", fees.SettlementTokens))
hermesPct, err := decimal.NewFromString(fees.HermesPercent)
if err != nil {
clio.Info("Could not retrieve hermes fee:", err)
}
clio.Info(fmt.Sprintf("Hermes fee: %s%%", hermesPct.Mul(decimal.NewFromInt(100)).StringFixed(2)))
return errWrongArgumentCount
}
hermesID, err := c.config.GetHermesID()
if err != nil {
return fmt.Errorf("could not get Hermes ID: %w", err)
}
errChan := make(chan error)
fromChain := c.config.GetInt64ByFlag(config.FlagChain2ChainID)
providerIdentity := args[0]
if !common.IsHexAddress(providerIdentity) {
return errors.New("a valid provider identity must be provided")
}
beneficiaryAddr := args[1]
if !common.IsHexAddress(beneficiaryAddr) {
return errors.New("a valid beneficiary address must be provided")
}
toChain, err := strconv.Atoi(args[2])
if err != nil {
return fmt.Errorf("could not parse chain id %w", err)
}
var amount *big.Int
if len(args) == 4 {
amf, err := strconv.ParseFloat(args[3], 64)
if err != nil {
return fmt.Errorf("%v is not a valid number", args[3])
}
if amf > 99 {
return errors.New("max withdrawal amount is 99 MYST")
}
amount = units.FloatEthToBigIntWei(amf)
}
clio.Info("Waiting for withdrawal to complete")
go func() {
errChan <- c.tequilapi.Withdraw(identity.FromAddress(providerIdentity), common.HexToAddress(hermesID), common.HexToAddress(beneficiaryAddr), amount, fromChain, int64(toChain))
}()
timeout := time.After(time.Minute * 2)
for {
select {
case <-timeout:
return errors.New("withdrawal timed out")
case <-time.After(time.Millisecond * 500):
fmt.Print(".")
case err := <-errChan:
fmt.Println()
if err != nil {
return fmt.Errorf("withdrawal failed: %w", err)
}
clio.Info("withdrawal succeeded")
return nil
}
}
}
const usageGetReferralCode = "referralcode <identity>"
func (c *cliApp) getReferralCode(actionArgs []string) error {
if len(actionArgs) != 1 {
clio.Info("Usage: " + usageGetReferralCode)
return errWrongArgumentCount
}
address := actionArgs[0]
res, err := c.tequilapi.IdentityReferralCode(address)
if err != nil {
return fmt.Errorf("could not get referral token: %w", err)
}
clio.Success(fmt.Sprintf("Your referral token is: %q", res.Token))
return nil
}
const usageExportIdentity = "export <identity> <new_passphrase> [file]"
func (c *cliApp) exportIdentity(actionsArgs []string) (err error) {
if len(actionsArgs) < 2 || len(actionsArgs) > 3 {
clio.Info("Usage: " + usageExportIdentity)
return errWrongArgumentCount
}
id := actionsArgs[0]
passphrase := actionsArgs[1]
dataDir := c.config.GetStringByFlag(config.FlagDataDir)
if dataDir == "" {
return errors.New("could not get data directory")
}
ksdir := node.GetOptionsDirectoryKeystore(dataDir)
ks := keystore.NewKeyStore(ksdir, keystore.LightScryptN, keystore.LightScryptP)
ex := identity.NewExporter(identity.NewKeystoreFilesystem(ksdir, ks))
blob, err := ex.Export(id, "", passphrase)
if err != nil {
return fmt.Errorf("failed to export identity: %w", err)
}
if len(actionsArgs) == 3 {
filepath := actionsArgs[2]
write := func() error {
f, err := os.Create(filepath)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(blob)
return err
}
err := write()
if err != nil {
return fmt.Errorf("failed to write exported key to file: %s reason: %w", filepath, err)
}
clio.Success("Identity exported to file:", filepath)
return nil
}
clio.Success("Private key exported: ")
quoted := strconv.Quote(string(blob))
fmt.Println(quoted[1 : len(quoted)-1])
return nil
}
const usageImportIdentity = "import <passphrase> <key-string/key-file>"
func (c *cliApp) importIdentity(actionsArgs []string) (err error) {
if len(actionsArgs) != 2 {
clio.Info("Usage: " + usageImportIdentity)
return errWrongArgumentCount
}
key := actionsArgs[1]
passphrase := actionsArgs[0]
blob := []byte(key)
if _, err := os.Stat(key); err == nil {
blob, err = os.ReadFile(key)
if err != nil {
return fmt.Errorf("can't read provided file: %s reason: %w", key, err)
}
blob = []byte(strings.ReplaceAll(string(blob), `\"`, `"`))
}
id, err := c.tequilapi.ImportIdentity(blob, passphrase, true)
if err != nil {
return fmt.Errorf("failed to import identity: %w", err)
}
clio.Success("Identity imported:", id.Address)
return nil
}
const usageLastWithdrawal = "last-withdrawal <identity>"
func (c *cliApp) lastWithdrawal(actionArgs []string) error {
if len(actionArgs) != 1 {
clio.Info("Usage: " + usageLastWithdrawal)
return errWrongArgumentCount
}
address := actionArgs[0]
history, err := c.tequilapi.WithdrawalHistory(address)
if err != nil {
return fmt.Errorf("could not get last withdrawal: %w", err)
}
if history.TotalItems == 0 {
clio.Info("No withdrawals found")
return nil
}
lastWithdrawal := history.Items[0]
clio.Info("Tx hash: ", lastWithdrawal.TxHash)
clio.Info("DateTime: ", lastWithdrawal.SettledAt)
clio.Info("Beneficiary: ", lastWithdrawal.Beneficiary)
clio.Info("Amount: ", money.New(lastWithdrawal.Amount))
clio.Info("Fees: ", money.New(lastWithdrawal.Fees))
clio.Info("Blockchain explorer URL: ", lastWithdrawal.BlockExplorerURL)
if lastWithdrawal.Error != "" {
clio.Warnf("Error: %s", lastWithdrawal.Error)
} else {
clio.Info("Error: none")
}
return nil
}
const usageMigrateHermesStatus = "migrate-hermes-status <identity>"
func (c *cliApp) migrateHermesStatus(actionArgs []string) error {
if len(actionArgs) != 1 {
clio.Info("Usage: " + usageMigrateHermesStatus)
return errWrongArgumentCount
}
address := actionArgs[0]
response, err := c.tequilapi.MigrateHermesStatus(address)
if err != nil {
return err
}
clio.Info("Migration status: ", response.Status)
return nil
}
const usageMigrateHermes = "migrate-hermes <identity>"
func (c *cliApp) migrateHermes(actionArgs []string) error {
if len(actionArgs) != 1 {
clio.Info("Usage: " + usageMigrateHermes)
return errWrongArgumentCount
}
address := actionArgs[0]
clio.Info("Migration started")
err := c.tequilapi.MigrateHermes(address)
if err != nil {
return err
}
clio.Info("Migration finished successfully")
return nil
}