-
Notifications
You must be signed in to change notification settings - Fork 13
/
rpc.go
1590 lines (1312 loc) · 38.7 KB
/
rpc.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019, Keychain Foundation Ltd.
// This file is part of the dipperin-core library.
//
// The dipperin-core library is free software: you can redistribute
// it and/or modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// The dipperin-core library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package commands
import (
"context"
"errors"
"fmt"
"github.com/dipperin/dipperin-core/common"
"github.com/dipperin/dipperin-core/common/consts"
"github.com/dipperin/dipperin-core/common/hexutil"
"github.com/dipperin/dipperin-core/common/util"
"github.com/dipperin/dipperin-core/core/accounts"
"github.com/dipperin/dipperin-core/core/accounts/soft-wallet"
"github.com/dipperin/dipperin-core/core/chain"
"github.com/dipperin/dipperin-core/core/chain-config"
"github.com/dipperin/dipperin-core/core/rpc-interface"
"github.com/dipperin/dipperin-core/third-party/log"
"github.com/dipperin/dipperin-core/third-party/rpc"
"github.com/urfave/cli"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"sync/atomic"
"time"
)
var (
l log.Logger
client RpcClient
defaultAccount common.Address
defaultWallet accounts.WalletIdentifier
osExit = os.Exit
)
const (
VerifierStatusNoRegistered = "Not Registered"
VerifierStatusRegistered = "Registered"
VerifiedStatusCanceled = "Canceled"
VerifiedStatusUnstaked = "Unstaked"
)
//go:generate mockgen -destination=./rpc_client_mock_test.go -package=commands github.com/dipperin/dipperin-core/cmd/dipperincli/commands RpcClient
type RpcClient interface {
Call(result interface{}, method string, args ...interface{}) error
Subscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) (*rpc.ClientSubscription, error)
}
func init() {
l = log.New()
l.SetHandler(log.MultiHandler(log.CliOutHandler))
}
func InitRpcClient(port int) {
l.Info("init rpc client", "port", port)
var err error
//if client, err = rpc.Dial(fmt.Sprintf("http://%v:%d", "127.0.0.1", port)); err != nil {
// panic("init rpc client failed: " + err.Error())
//}
wsURL := fmt.Sprintf("ws://%v:%d", "127.0.0.1", port)
//l.Info("init rpc client", "wsURL", wsURL)
if client, err = rpc.Dial(wsURL); err != nil {
panic("init rpc client failed: " + err.Error())
}
}
func InitAccountInfo(nodeType int, path, password, passPharse string) {
// get default account
if nodeType == chain_config.NodeTypeOfNormal {
err := initWallet(path, password, passPharse)
if err != nil {
l.Error("init Wallet Error", "err", err)
osExit(1)
}
}
defaultAccount = getDefaultAccount()
defaultWallet = getDefaultWallet()
}
// CsSubscribe registers a subscripion under the "cs" namespace.
//func csSubscribe(c RpcClient, ctx context.Context, channel interface{}, args ...interface{}) (*rpc.ClientSubscription, error) {
// return c.Subscribe(ctx, "dipperin", channel, args...)
//}
//
//func Subscribe(nodeType string) {
//
// if nodeType != "mine master" {
// return
// }
//
// blockCh := make(chan string)
//
// sub, err := csSubscribe(client, context.Background(), blockCh, "newBlock")
//
// if err != nil {
// l.Error("can't subscribe server", "err", err)
// return
// }
//
// l.Info("start subscribe node server msg")
//
// for {
// select {
// case err := <-sub.Err():
// l.Info("sub result", "err", err)
// case <-blockCh:
// // todo Make another prompt, otherwise it is difficult to operate the command.
// //l.Info(b)
// }
// }
//
//}
var (
CheckSyncStatusDuration = time.Second * 3
)
var SyncStatus atomic.Value
//check downloader Sync Status
func CheckDownloaderSyncStatus() {
timer := time.NewTimer(CheckSyncStatusDuration)
SyncStatus.Store(false)
var status bool
if err := client.Call(&status, getDipperinRpcMethodByName("GetSyncStatus")); err != nil {
l.Error("call GetSyncStatus", "err", err)
panic("call GetSyncStatus err")
}
//l.Info("the status is:","status",status)
if !status {
SyncStatus.Store(true)
return
}
var resp bool
for {
select {
case <-timer.C:
if err := client.Call(&resp, getDipperinRpcMethodByName("GetSyncStatus")); err != nil {
l.Error("call GetSyncStatus", "err", err)
}
//l.Info("the GetSyncStatus resp is:","resp",resp)
if !resp {
SyncStatus.Store(true)
return
}
timer.Reset(CheckSyncStatusDuration)
}
}
}
type rpcCaller struct{}
var callerRv = reflect.ValueOf(&rpcCaller{})
func RpcCall(c *cli.Context) {
if client == nil {
panic("rpc client not initialized")
}
method := c.String("m")
if method == "" {
l.Error("Please specify -m")
return
}
rvf := callerRv.MethodByName(method)
if rvf.Kind() != reflect.Func {
l.Error("not found method", "method_name", c.String("m"))
return
}
// call method
rvf.Call([]reflect.Value{reflect.ValueOf(c)})
}
// get rpc method from name
func getDipperinRpcMethodByName(mName string) string {
lm := strings.ToLower(string(mName[0])) + mName[1:]
return "dipperin_" + lm
}
// get rpc parameters from map, return string list, delete space at end
func getRpcParamFromString(cParam string) []string {
if cParam == "" {
return []string{}
}
lm := strings.Split(cParam, ",")
l.Info("the lm is:", "lm", lm)
return lm
}
func getRpcMethodAndParam(c *cli.Context) (mName string, cParams []string, err error) {
mName = c.String("m")
if mName == "" {
return "", []string{}, errors.New("the method name is nil")
}
params := c.String("p")
l.Info("the params is:", "params", params)
cParams = getRpcParamFromString(params)
return mName, cParams, nil
}
func checkSync() bool {
if !SyncStatus.Load().(bool) {
l.Error("the block downloader isn't finished")
var respBlock rpc_interface.BlockResp
if err := client.Call(&respBlock, getDipperinRpcMethodByName("CurrentBlock")); err != nil {
l.Error("get current Block error", "err", err)
return true
}
l.Info("the current block number is:", "number", respBlock.Header.Number)
return true
}
return false
}
func (caller *rpcCaller) GetDefaultAccountBalance(c *cli.Context) {
var resp rpc_interface.CurBalanceResp
if err := client.Call(&resp, getDipperinRpcMethodByName("CurrentBalance"), defaultAccount); err != nil {
l.Error("call current balance error", "err", err)
return
}
balance, err := CSCoinToMoneyValue(resp.Balance)
if err != nil {
l.Error("the address isn't on the block chain balance=0")
} else {
l.Info("address current Balance is:", "balance", balance+consts.CoinDIPName)
}
}
func (caller *rpcCaller) CurrentBalance(c *cli.Context) {
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
if len(cParams) != 0 && len(cParams) != 1 {
l.Error("parameter error")
return
}
var addr common.Address
if len(cParams) == 0 {
addr = getDefaultAccount()
} else {
addr, err = CheckAndChangeHexToAddress(cParams[0])
if err != nil {
l.Error("the input address is invalid", "err", err)
return
}
}
var resp rpc_interface.CurBalanceResp
if err := client.Call(&resp, getDipperinRpcMethodByName(mName), addr); err != nil {
l.Error("call current balance error", "err", err)
return
}
balance, err := CSCoinToMoneyValue(resp.Balance)
if err != nil {
l.Error("the address isn't on the block chain balance=0")
} else {
l.Info("address current Balance is:", "balance", balance+consts.CoinDIPName)
}
}
func printBlockInfo(respBlock rpc_interface.BlockResp) {
fmt.Printf(respBlock.Header.String())
//l.Info("the current Block header is:","header",respBlock.Header.String())
fmt.Printf("\r\n[the current Block txs is]:")
for _, tx := range respBlock.Body.Txs {
fmt.Printf("\r\ntxID:%v", tx.CalTxId().Hex())
}
fmt.Printf("\r\n[the current Block commit address is]:")
for _, ver := range respBlock.Body.Vers {
fmt.Printf("\r\n[commit address]:%v", ver.GetAddress().Hex())
}
fmt.Printf("\r\n")
}
func printTransactionInfo(respTx rpc_interface.TransactionResp) {
if respTx.Transaction == nil {
fmt.Printf("the tx isn't on the block chain\r\n")
return
}
fmt.Printf("\r\n[the tx info is:]")
fmt.Printf("\r\n%v", respTx.Transaction.String())
fmt.Printf("\r\nthe blockHash is:%v", respTx.BlockHash.Hex())
fmt.Printf("\r\nthe BlockNumber is:%v", respTx.BlockNumber)
fmt.Printf("\r\nthe TxIndex is:%v\r\n", respTx.TxIndex)
}
func (caller *rpcCaller) CurrentBlock(c *cli.Context) {
mName, _, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
var respBlock rpc_interface.BlockResp
if err := client.Call(&respBlock, getDipperinRpcMethodByName(mName)); err != nil {
l.Error("call current block error", "err", err)
return
}
printBlockInfo(respBlock)
}
func (caller *rpcCaller) GetGenesis(c *cli.Context) {
mName, _, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
var respBlock rpc_interface.BlockResp
if err := client.Call(&respBlock, getDipperinRpcMethodByName(mName)); err != nil {
l.Error("call genesis block error", "err", err)
return
}
printBlockInfo(respBlock)
}
// GetBlockByNumber get block information according to block num
func (caller *rpcCaller) GetBlockByNumber(c *cli.Context) {
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
if len(cParams) == 0 {
l.Error("parameter includes:blockNumber")
return
}
blockNum, err := strconv.Atoi(cParams[0])
if err != nil {
l.Error("the blockNumber error")
}
l.Info("the blockNum is:", "blockNum", blockNum)
var respBlock rpc_interface.BlockResp
if err := client.Call(&respBlock, getDipperinRpcMethodByName(mName), blockNum); err != nil {
l.Error("call block error", "err", err)
return
}
printBlockInfo(respBlock)
}
// GetBlockByHash get block information based on block hash
func (caller *rpcCaller) GetBlockByHash(c *cli.Context) {
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
if len(cParams) == 0 {
l.Error("parameter includes:blockHash")
return
}
var respBlock rpc_interface.BlockResp
if err := client.Call(&respBlock, getDipperinRpcMethodByName(mName), cParams[0]); err != nil {
l.Error("call block error", "err", err)
return
}
printBlockInfo(respBlock)
}
func (caller *rpcCaller) StartMine(c *cli.Context) {
if checkSync() {
return
}
mName, _, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
var resp interface{}
if err := client.Call(&resp, getDipperinRpcMethodByName(mName)); err != nil {
l.Error("start mining error", "err", err)
return
}
l.Debug("Mining Started" )
}
func (caller *rpcCaller) StopMine(c *cli.Context) {
mName, _, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
var resp interface{}
if err := client.Call(&resp, getDipperinRpcMethodByName(mName)); err != nil {
l.Error("stop mining error", "err", err)
return
}
l.Debug("stop mining")
}
// SetMineCoinBase set the mining coinbase address
func (caller *rpcCaller) SetMineCoinBase(c *cli.Context) {
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
if len(cParams) < 1 {
l.Error("setting CoinBase needs:address")
return
}
address, err := CheckAndChangeHexToAddress(cParams[0])
if err != nil {
l.Error("the input address is invalid", "err", err)
return
}
var resp interface{}
if err := client.Call(&resp, getDipperinRpcMethodByName(mName), address); err != nil {
l.Error("setting CoinBase", "err", err)
return
}
l.Debug("setting CoinBase complete")
}
func (caller *rpcCaller) SendTx(c *cli.Context) {
if checkSync() {
return
}
_, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error", "err", err)
return
}
if len(cParams) < 3 {
l.Error("parameter includes:to value transactionFee")
return
}
toAddress, err := CheckAndChangeHexToAddress(cParams[0])
if err != nil {
l.Error("the to address is invalid", "err", err)
return
}
value, err := MoneyValueToCSCoin(cParams[1])
if err != nil {
l.Error("the parameter value invalid", "err", err)
return
}
txFee, err := MoneyValueToCSCoin(cParams[2])
if err != nil {
l.Error("the parameter transactionFee invalid", "err", err)
return
}
extraData := make([]byte, 0)
if len(cParams) >= 4 {
extraData = []byte(cParams[3])
}
var resp common.Hash
l.Info("the from is: ", "from", defaultAccount.Hex())
l.Info("the to is: ", "to", toAddress.Hex())
l.Info("the value is:", "value", cParams[1]+consts.CoinDIPName)
l.Info("the TransactionFee is:", "TransactionFee", cParams[2]+consts.CoinDIPName)
l.Info("the ExtraData is: ", "ExtraData", extraData)
if err := client.Call(&resp, getDipperinRpcMethodByName("SendTransaction"), defaultAccount, toAddress, value, txFee, extraData, nil); err != nil {
l.Error("call send transaction error", "err", err)
return
}
l.Info("SendTransaction result", "txId", resp.Hex())
}
//send transaction
func (caller *rpcCaller) SendTransaction(c *cli.Context) {
if checkSync() {
return
}
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error", "err", err)
return
}
if len(cParams) != 4 && len(cParams) != 5 {
l.Error("parameter includes:from to value transactionFee")
return
}
From, err := CheckAndChangeHexToAddress(cParams[0])
if err != nil {
l.Error("the from address is invalid", "err", err)
l.Error(err.Error())
return
}
To, err := CheckAndChangeHexToAddress(cParams[1])
if err != nil {
l.Error("the to address is invalid", "err", err)
return
}
Value, err := MoneyValueToCSCoin(cParams[2])
if err != nil {
l.Error("the parameter value invalid")
return
}
TransactionFee, err := MoneyValueToCSCoin(cParams[3])
if err != nil {
l.Error("the parameter transactionFee invalid")
return
}
ExtraData := make([]byte, 0)
if len(cParams) == 5 {
ExtraData = []byte(cParams[4])
}
var resp common.Hash
l.Info("the From is: ", "From", From.Hex())
l.Info("the To is: ", "To", To.Hex())
l.Info("the Value is:", "Value", cParams[2]+consts.CoinDIPName)
l.Info("the TransactionFee is:", "TransactionFee", cParams[3]+consts.CoinDIPName)
l.Info("the ExtraData is: ", "ExtraData", ExtraData)
if err := client.Call(&resp, getDipperinRpcMethodByName(mName), From, To, Value, TransactionFee, ExtraData, nil); err != nil {
l.Error("call send transaction", "err", err)
return
}
l.Info("SendTransaction result", "txId", resp.Hex())
}
//check transaction from transaction hash
func (caller *rpcCaller) Transaction(c *cli.Context) {
if checkSync() {
return
}
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
if len(cParams) < 1 {
l.Error("Transaction need:txHash")
return
}
tmpHash, err := hexutil.Decode(cParams[0])
if err != nil {
l.Error("the err is:", "err", err)
l.Error("Transaction decode error")
return
}
var hash common.Hash
_ = copy(hash[:], tmpHash)
var resp rpc_interface.TransactionResp
if err := client.Call(&resp, getDipperinRpcMethodByName(mName), hash); err != nil {
l.Error("Call Transaction", "err", err)
return
}
printTransactionInfo(resp)
}
//List Wallet
func (caller *rpcCaller) ListWallet(c *cli.Context) {
mName, _, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
l.Debug(getDipperinRpcMethodByName(mName))
var resp []accounts.WalletIdentifier
if err := client.Call(&resp, getDipperinRpcMethodByName(mName)); err != nil {
l.Error("Call ListWallet", "err", err)
return
}
l.Info("Call ListWallet", "resp wallet", resp)
}
//List Wallet Account
func (caller *rpcCaller) ListWalletAccount(c *cli.Context) {
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
var identifier accounts.WalletIdentifier
if len(cParams) == 0 {
identifier = defaultWallet
} else if len(cParams) == 2 {
identifier.Path, identifier.WalletName = ParseWalletPathAndName(cParams[1])
if cParams[0] == "SoftWallet" {
identifier.WalletType = accounts.SoftWallet
} else if cParams[0] == "LedgerWallet" {
identifier.WalletType = accounts.TrezorWallet
} else if cParams[0] == "TrezorWallet" {
identifier.WalletType = accounts.TrezorWallet
} else {
l.Error("Wallet Type error")
return
}
} else {
l.Error("list Wallet Account need:Type Path")
return
}
l.Debug(getDipperinRpcMethodByName(mName))
var resp []accounts.Account
if err := client.Call(&resp, getDipperinRpcMethodByName(mName), identifier); err != nil {
l.Error("Call ListWallet", "err", err)
return
}
l.Info("Call ListWalletAccount", "resp wallet account", resp)
for _, account := range resp {
l.Info("the account address is", "address", account.Address.Hex())
}
}
//Establish Wallet
func (caller *rpcCaller) EstablishWallet(c *cli.Context) {
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
if len(cParams) != 3 {
l.Error("EstablishWallet need:Type Path password")
return
}
var identifier accounts.WalletIdentifier
identifier.Path, identifier.WalletName = ParseWalletPathAndName(cParams[1])
if cParams[0] == "SoftWallet" {
identifier.WalletType = accounts.SoftWallet
} else if cParams[0] == "LedgerWallet" {
identifier.WalletType = accounts.TrezorWallet
} else if cParams[0] == "TrezorWallet" {
identifier.WalletType = accounts.TrezorWallet
} else {
l.Error("Wallet Type error")
return
}
password := cParams[2]
/* var passPhrase string
if len(cParams) == 4 {
passPhrase = cParams[3]
} else {
passPhrase = ""
}*/
var resp string
/* l.Info("the identifier is: ","identifier",identifier)
l.Info("the password is: ","password",password)
l.Info("the passPhrase is: ","passPhrase",passPhrase)
l.Info("call EstablishWallet")*/
if err := client.Call(&resp, getDipperinRpcMethodByName(mName), password, "", identifier); err != nil {
l.Error("Call EstablishWallet", "err", err)
return
}
resp = strings.Replace(resp, " ", ",", -1)
l.Info("Call EstablishWallet", "resp mnemonic", resp)
}
//Restore Wallet
func (caller *rpcCaller) RestoreWallet(c *cli.Context) {
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
if len(cParams) < 4 {
l.Error("RestoreWallet need:Type Path password mnemonic")
return
}
var identifier accounts.WalletIdentifier
identifier.Path, identifier.WalletName = ParseWalletPathAndName(cParams[1])
if cParams[0] == "SoftWallet" {
identifier.WalletType = accounts.SoftWallet
} else if cParams[0] == "LedgerWallet" {
identifier.WalletType = accounts.TrezorWallet
} else if cParams[0] == "TrezorWallet" {
identifier.WalletType = accounts.TrezorWallet
} else {
l.Error("Wallet Type error")
return
}
var mnemonic string
for i := 3; i < len(cParams); i++ {
mnemonic += cParams[i]
if i != len(cParams)-1 {
mnemonic += " "
}
}
password := cParams[2]
//passPhrase := cParams[3]
l.Debug(getDipperinRpcMethodByName(mName))
var resp interface{}
l.Info("the identifier is: ", "identifier", identifier)
l.Info("the password is: ", "password", password)
//l.Info("the passPhrase is: ", "passPhrase", passPhrase)
l.Info("the mnemonic is: ", "mnemonic", mnemonic)
if err := client.Call(resp, getDipperinRpcMethodByName(mName), password, mnemonic, "", identifier); err != nil {
l.Error("Call RestoreWallet", "err", err)
return
}
l.Info("Call RestoreWallet success")
}
//Open Wallet
func (caller *rpcCaller) OpenWallet(c *cli.Context) {
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
var identifier accounts.WalletIdentifier
var password string
if len(cParams) == 1 {
identifier = defaultWallet
password = cParams[0]
} else if len(cParams) == 3 {
identifier.Path, identifier.WalletName = ParseWalletPathAndName(cParams[1])
if cParams[0] == "SoftWallet" {
identifier.WalletType = accounts.SoftWallet
} else if cParams[0] == "LedgerWallet" {
identifier.WalletType = accounts.LedgerWallet
} else if cParams[0] == "TrezorWallet" {
identifier.WalletType = accounts.TrezorWallet
} else {
l.Error("Wallet Type error")
return
}
password = cParams[2]
} else {
l.Error("OpenWallet need:Type Path password")
return
}
l.Debug(getDipperinRpcMethodByName(mName))
var resp interface{}
if err := client.Call(&resp, getDipperinRpcMethodByName(mName), password, identifier); err != nil {
l.Error("Call OpenWallet", "err", err)
return
}
l.Info("Call OpenWallet success")
}
//Close Wallet
func (caller *rpcCaller) CloseWallet(c *cli.Context) {
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
var identifier accounts.WalletIdentifier
if len(cParams) == 0 {
identifier = defaultWallet
} else if len(cParams) == 2 {
identifier.Path, identifier.WalletName = ParseWalletPathAndName(cParams[1])
if cParams[0] == "SoftWallet" {
identifier.WalletType = accounts.SoftWallet
} else if cParams[0] == "LedgerWallet" {
identifier.WalletType = accounts.TrezorWallet
} else if cParams[0] == "TrezorWallet" {
identifier.WalletType = accounts.TrezorWallet
} else {
l.Error("Wallet Type error")
return
}
} else {
l.Error("CloseWallet need:Type Path")
return
}
l.Debug(getDipperinRpcMethodByName(mName))
var resp interface{}
if err := client.Call(&resp, getDipperinRpcMethodByName(mName), identifier); err != nil {
l.Error("Call CloseWallet", "err", err)
return
}
l.Info("Call CloseWallet success")
}
//AddAccount
func (caller *rpcCaller) AddAccount(c *cli.Context) {
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
var identifier accounts.WalletIdentifier
if len(cParams) == 0 {
identifier = defaultWallet
} else if len(cParams) == 2 {
identifier.Path, identifier.WalletName = ParseWalletPathAndName(cParams[1])
if cParams[0] == "SoftWallet" {
identifier.WalletType = accounts.SoftWallet
} else if cParams[0] == "LedgerWallet" {
identifier.WalletType = accounts.TrezorWallet
} else if cParams[0] == "TrezorWallet" {
identifier.WalletType = accounts.TrezorWallet
} else {
l.Error("Wallet Type error")
return
}
} else {
l.Error("AddAccount need:Type Path")
return
}
derivationPath := ""
l.Debug(getDipperinRpcMethodByName(mName))
var resp accounts.Account
if err := client.Call(&resp, getDipperinRpcMethodByName(mName), derivationPath, identifier); err != nil {
l.Error("Call AddAccount", "err", err)
return
}
l.Info("Call AddAccount", "resp AddAccount", resp.Address.Hex())
}
func (caller *rpcCaller) SendRegisterTx(c *cli.Context) {
if checkSync() {
return
}
_, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error", "err", err)
return
}
if len(cParams) != 2 {
l.Error("SendRegisterTransaction need:stake transactionFee")
return
}
stake, err := MoneyValueToCSCoin(cParams[0])
if err != nil {
l.Error("the parameter stake invalid")
return
}
txFee, err := MoneyValueToCSCoin(cParams[1])
if err != nil {
l.Error("the parameter transactionFee invalid")
return
}
var resp common.Hash
if err := client.Call(&resp, getDipperinRpcMethodByName("SendRegisterTransaction"), defaultAccount, stake, txFee, nil); err != nil {
l.Error("call send transaction", "err", err)
return
}
l.Info("SendRegisterTransaction result", "txId", resp.Hex())
addTrackingAccount(defaultAccount)
RecordRegistration(resp.Hex())
}
//send Register transaction
func (caller *rpcCaller) SendRegisterTransaction(c *cli.Context) {
if checkSync() {
return
}
mName, cParams, err := getRpcMethodAndParam(c)
if err != nil {
l.Error("getRpcMethodAndParam error")
return
}
if len(cParams) != 3 {
l.Error("SendRegisterTransaction need:from stake transactionFee")
return
}
var resp common.Hash
From, err := CheckAndChangeHexToAddress(cParams[0])
if err != nil {
l.Error("the from address is invalid", "err", err)
return
}
stake, err := MoneyValueToCSCoin(cParams[1])
if err != nil {
l.Error("the parameter stake invalid")
return
}
TransactionFee, err := MoneyValueToCSCoin(cParams[2])
if err != nil {
l.Error("the parameter transactionFee invalid")
return