-
Notifications
You must be signed in to change notification settings - Fork 28
/
rpc.go
842 lines (730 loc) · 27.2 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
package nkn
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"sync"
"time"
"github.com/nknorg/nkn/v2/common"
nknConfig "github.com/nknorg/nkn/v2/config"
"github.com/nknorg/nkn/v2/program"
"github.com/nknorg/nkn/v2/transaction"
"github.com/nknorg/nkngomobile"
)
// Signer is the interface that can sign transactions.
type signer interface {
PubKey() []byte
SignTransaction(tx *transaction.Transaction) error
}
// RPCClient is the RPC client interface that implements most RPC methods
// (except a few ones that is supposed to call with seed node only).
type rpcClient interface {
GetNonce(txPool bool) (int64, error)
GetNonceContext(ctx context.Context, txPool bool) (int64, error)
GetNonceByAddress(address string, txPool bool) (int64, error)
GetNonceByAddressContext(ctx context.Context, address string, txPool bool) (int64, error)
Balance() (*Amount, error)
BalanceContext(ctx context.Context) (*Amount, error)
BalanceByAddress(address string) (*Amount, error)
BalanceByAddressContext(ctx context.Context, address string) (*Amount, error)
GetHeight() (int32, error)
GetHeightContext(ctx context.Context) (int32, error)
GetSubscribers(topic string, offset, limit int, meta, txPool bool, subscriberHashPrefix []byte) (*Subscribers, error)
GetSubscribersContext(ctx context.Context, topic string, offset, limit int, meta, txPool bool, subscriberHashPrefix []byte) (*Subscribers, error)
GetSubscription(topic string, subscriber string) (*Subscription, error)
GetSubscriptionContext(ctx context.Context, topic string, subscriber string) (*Subscription, error)
GetSubscribersCount(topic string, subscriberHashPrefix []byte) (int, error)
GetSubscribersCountContext(ctx context.Context, topic string, subscriberHashPrefix []byte) (int, error)
GetRegistrant(name string) (*Registrant, error)
GetRegistrantContext(ctx context.Context, name string) (*Registrant, error)
SendRawTransaction(txn *transaction.Transaction) (string, error)
SendRawTransactionContext(ctx context.Context, txn *transaction.Transaction) (string, error)
}
// SignerRPCClient is a RPCClient that can also sign transactions and made RPC
// requests that requires signatures.
type signerRPCClient interface {
signer
rpcClient
Transfer(address, amount string, config *TransactionConfig) (string, error)
TransferContext(ctx context.Context, address, amount string, config *TransactionConfig) (string, error)
RegisterName(name string, config *TransactionConfig) (string, error)
RegisterNameContext(ctx context.Context, name string, config *TransactionConfig) (string, error)
TransferName(name string, recipientPubKey []byte, config *TransactionConfig) (string, error)
TransferNameContext(ctx context.Context, name string, recipientPubKey []byte, config *TransactionConfig) (string, error)
DeleteName(name string, config *TransactionConfig) (string, error)
DeleteNameContext(ctx context.Context, name string, config *TransactionConfig) (string, error)
Subscribe(identifier, topic string, duration int, meta string, config *TransactionConfig) (string, error)
SubscribeContext(ctx context.Context, identifier, topic string, duration int, meta string, config *TransactionConfig) (string, error)
Unsubscribe(identifier, topic string, config *TransactionConfig) (string, error)
UnsubscribeContext(ctx context.Context, identifier, topic string, config *TransactionConfig) (string, error)
}
type dialConfigInterface interface {
RPCGetHttpDialContext() func(ctx context.Context, network, addr string) (net.Conn, error)
}
// RPCConfigInterface is the config interface for making rpc call. ClientConfig,
// WalletConfig and RPCConfig all implement this interface and thus can be used
// directly. RPC prefix is added to all public methods to avoid gomobile compile
// error.
type RPCConfigInterface interface {
RPCGetSeedRPCServerAddr() *nkngomobile.StringArray
RPCGetRPCTimeout() int32
RPCGetConcurrency() int32
}
// Node struct contains the information of the node that a client connects to.
type Node struct {
Addr string `json:"addr"`
RPCAddr string `json:"rpcAddr"`
PubKey string `json:"pubkey"`
ID string `json:"id"`
Sdp string `json:"sdp"`
}
// NodeState struct contains the state of a NKN full node.
type NodeState struct {
Addr string `json:"addr"`
CurrTimeStamp int64 `json:"currTimeStamp"`
Height int32 `json:"height"` // Changed to signed int for gomobile compatibility
ID string `json:"id"`
JSONRPCPort int32 `json:"jsonRpcPort"`
ProposalSubmitted int32 `json:"proposalSubmitted"`
ProtocolVersion int32 `json:"protocolVersion"`
PublicKey string `json:"publicKey"`
RelayMessageCount int64 `json:"relayMessageCount"`
SyncState string `json:"syncState"`
TLSJSONRpcDomain string `json:"tlsJsonRpcDomain"`
TLSJSONRpcPort int32 `json:"tlsJsonRpcPort"`
TLSWebsocketDomain string `json:"tlsWebsocketDomain"`
TLSWebsocketPort int32 `json:"tlsWebsocketPort"`
Uptime int64 `json:"uptime"`
Version string `json:"version"`
WebsocketPort int32 `json:"websocketPort"`
}
// Subscription contains the information of a subscriber to a topic.
type Subscription struct {
Meta string `json:"meta"`
ExpiresAt int32 `json:"expiresAt"` // Changed to signed int for gomobile compatibility
}
// Registrant contains the information of a name registrant
type Registrant struct {
Registrant string `json:"registrant"`
ExpiresAt int32 `json:"expiresAt"` // Changed to signed int for gomobile compatibility
}
type balance struct {
Amount string `json:"amount"`
}
type nonce struct {
Nonce uint64 `json:"nonce"`
NonceInTxPool uint64 `json:"nonceInTxPool"`
}
type errResp struct {
Code int32
Message string
Data string
}
type rpcResp struct {
Error errResp `json:"error"`
Result interface{} `json:"result"`
}
func httpPost(ctx context.Context, addr string, reqBody []byte, timeout time.Duration, dialContext func(ctx context.Context, network, addr string) (net.Conn, error)) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "POST", addr, bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
client := &http.Client{
Timeout: timeout,
Transport: &http.Transport{
DialContext: dialContext,
},
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("rpc call failed with %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
return io.ReadAll(resp.Body)
}
// RPCCall makes a RPC call and put results to result passed in.
func RPCCall(parentCtx context.Context, method string, params interface{}, result interface{}, config RPCConfigInterface) error {
if config == nil {
config = GetDefaultRPCConfig()
}
req, err := json.Marshal(map[string]interface{}{
"id": "nkn-sdk-go",
"method": method,
"params": params,
})
if err != nil {
return &errorWithCode{err: err, code: errCodeEncodeError}
}
var wg sync.WaitGroup
var respLock sync.Mutex
rpcAddrChan := make(chan string)
respErrChan := make(chan *errResp, 1)
ctx, cancel := context.WithCancel(parentCtx)
defer cancel()
n := int(config.RPCGetConcurrency())
if n == 0 {
n = config.RPCGetSeedRPCServerAddr().Len()
}
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for rpcAddr := range rpcAddrChan {
cfg, ok := config.(dialConfigInterface)
if !ok {
log.Println("type conversion failed")
return
}
body, err := httpPost(ctx, rpcAddr, req, time.Duration(config.RPCGetRPCTimeout())*time.Millisecond, cfg.RPCGetHttpDialContext())
if err != nil {
select {
case <-ctx.Done():
return
default:
log.Println(err)
}
continue
}
resp := &rpcResp{
Result: result,
}
respLock.Lock()
select {
case <-ctx.Done():
respLock.Unlock()
return
default:
}
err = json.Unmarshal(body, &resp)
if err != nil {
log.Println("RPCCall json.Unmarshal(resp) error: ", err)
log.Println("RPCCall resp body: ", string(body))
respLock.Unlock()
continue
}
if resp.Error.Code != 0 || len(resp.Error.Data) > 0 || len(resp.Error.Message) > 0 {
select {
case respErrChan <- &resp.Error:
cancel()
case <-ctx.Done():
}
respLock.Unlock()
return
}
select {
case respErrChan <- nil:
cancel()
case <-ctx.Done():
}
respLock.Unlock()
return
}
}()
}
for _, rpcAddr := range config.RPCGetSeedRPCServerAddr().Elems() {
select {
case rpcAddrChan <- rpcAddr:
case <-ctx.Done():
break
}
}
close(rpcAddrChan)
wg.Wait()
close(respErrChan)
err = parentCtx.Err()
if err != nil {
return &errorWithCode{err: err, code: errCodeCanceled}
}
er, ok := <-respErrChan
if !ok {
return &errorWithCode{err: errors.New("all rpc request failed"), code: errCodeNetworkError}
}
if er != nil {
msg := er.Message
if len(er.Data) > 0 {
msg += ": " + er.Data
}
return &errorWithCode{err: errors.New(msg), code: er.Code}
}
return nil
}
// GetWsAddr wraps GetWsAddrContext with background context.
func GetWsAddr(clientAddr string, config RPCConfigInterface) (*Node, error) {
return GetWsAddrContext(context.Background(), clientAddr, config)
}
// GetWsAddrContext RPC gets the node that a client address should connect to
// using ws.
func GetWsAddrContext(ctx context.Context, clientAddr string, config RPCConfigInterface) (*Node, error) {
node := &Node{}
err := RPCCall(ctx, "getwsaddr", map[string]interface{}{"address": clientAddr}, node, config)
if err != nil {
return nil, err
}
return node, nil
}
// GetWssAddr wraps GetWssAddrContext with background context.
func GetWssAddr(clientAddr string, config RPCConfigInterface) (*Node, error) {
return GetWssAddrContext(context.Background(), clientAddr, config)
}
// GetWssAddrContext RPC gets the node that a client address should connect to
// using wss.
func GetWssAddrContext(ctx context.Context, clientAddr string, config RPCConfigInterface) (*Node, error) {
node := &Node{}
err := RPCCall(ctx, "getwssaddr", map[string]interface{}{"address": clientAddr}, node, config)
if err != nil {
return nil, err
}
return node, nil
}
// GetNonce wraps GetNonceContext with background context.
func GetNonce(address string, txPool bool, config RPCConfigInterface) (int64, error) {
return GetNonceContext(context.Background(), address, txPool, config)
}
// GetNonceContext RPC gets the next nonce to use of an address. If txPool is
// false, result only counts transactions in ledger; if txPool is true,
// transactions in txPool are also counted.
//
// Nonce is changed to signed int for gomobile compatibility.
func GetNonceContext(ctx context.Context, address string, txPool bool, config RPCConfigInterface) (int64, error) {
n := &nonce{}
err := RPCCall(ctx, "getnoncebyaddr", map[string]interface{}{"address": address}, n, config)
if err != nil {
return 0, err
}
if txPool && n.NonceInTxPool > n.Nonce {
return int64(n.NonceInTxPool), nil
}
return int64(n.Nonce), nil
}
// GetBalance wraps GetBalanceContext with background context.
func GetBalance(address string, config RPCConfigInterface) (*Amount, error) {
return GetBalanceContext(context.Background(), address, config)
}
// GetBalanceContext RPC returns the balance of a wallet address.
func GetBalanceContext(ctx context.Context, address string, config RPCConfigInterface) (*Amount, error) {
balance := &balance{}
err := RPCCall(ctx, "getbalancebyaddr", map[string]interface{}{"address": address}, balance, config)
if err != nil {
return nil, err
}
return NewAmount(balance.Amount)
}
// GetHeight wraps GetHeightContext with background context.
func GetHeight(config RPCConfigInterface) (int32, error) {
return GetHeightContext(context.Background(), config)
}
// GetHeightContext RPC returns the latest block height.
func GetHeightContext(ctx context.Context, config RPCConfigInterface) (int32, error) {
var height uint32
err := RPCCall(ctx, "getlatestblockheight", map[string]interface{}{}, &height, config)
if err != nil {
return 0, err
}
return int32(height), nil
}
// GetSubscribers wraps GetSubscribersContext with background context.
func GetSubscribers(topic string, offset, limit int, meta, txPool bool, subscriberHashPrefix []byte, config RPCConfigInterface) (*Subscribers, error) {
return GetSubscribersContext(context.Background(), topic, offset, limit, meta, txPool, subscriberHashPrefix, config)
}
// GetSubscribersContext gets the subscribers of a topic with a offset and max
// number of results (limit). If meta is true, results contain each subscriber's
// metadata. If txPool is true, results contain subscribers in txPool. Enabling
// this will get subscribers sooner after they send subscribe transactions, but
// might affect the correctness of subscribers because transactions in txpool is
// not guaranteed to be packed into a block. If subscriberHashPrefix is not
// empty, only subscriber whose sha256(pubkey+identifier) contains this prefix
// will be returned. Each prefix byte will reduce result count to about 1/256,
// and also reduce response time to about 1/256 if there are a lot of
// subscribers. This is a good way to sample subscribers randomly with low cost.
//
// Offset and limit are changed to signed int for gomobile compatibility
func GetSubscribersContext(ctx context.Context, topic string, offset, limit int, meta, txPool bool, subscriberHashPrefix []byte, config RPCConfigInterface) (*Subscribers, error) {
var result map[string]interface{}
err := RPCCall(ctx, "getsubscribers", map[string]interface{}{
"topic": topic,
"offset": offset,
"limit": limit,
"meta": meta,
"txPool": txPool,
"subscriberHashPrefix": hex.EncodeToString(subscriberHashPrefix),
}, &result, config)
if err != nil {
return nil, err
}
subscribers := make(map[string]string)
subscribersInTxPool := make(map[string]string)
if meta {
for subscriber, meta := range result["subscribers"].(map[string]interface{}) {
subscribers[subscriber] = meta.(string)
}
if txPool {
for subscriber, meta := range result["subscribersInTxPool"].(map[string]interface{}) {
subscribersInTxPool[subscriber] = meta.(string)
}
}
} else {
for _, subscriber := range result["subscribers"].([]interface{}) {
subscribers[subscriber.(string)] = ""
}
if txPool {
for _, subscriber := range result["subscribersInTxPool"].([]interface{}) {
subscribersInTxPool[subscriber.(string)] = ""
}
}
}
return &Subscribers{
Subscribers: nkngomobile.NewStringMap(subscribers),
SubscribersInTxPool: nkngomobile.NewStringMap(subscribersInTxPool),
}, nil
}
// GetSubscription wraps GetSubscriptionContext with background context.
func GetSubscription(topic string, subscriber string, config RPCConfigInterface) (*Subscription, error) {
return GetSubscriptionContext(context.Background(), topic, subscriber, config)
}
// GetSubscriptionContext RPC gets the subscription details of a subscriber in a topic.
func GetSubscriptionContext(ctx context.Context, topic string, subscriber string, config RPCConfigInterface) (*Subscription, error) {
subscription := &Subscription{}
err := RPCCall(ctx, "getsubscription", map[string]interface{}{
"topic": topic,
"subscriber": subscriber,
}, subscription, config)
if err != nil {
return nil, err
}
return subscription, nil
}
// GetSubscribersCount wraps GetSubscribersCountContext with background context.
func GetSubscribersCount(topic string, subscriberHashPrefix []byte, config RPCConfigInterface) (int, error) {
return GetSubscribersCountContext(context.Background(), topic, subscriberHashPrefix, config)
}
// GetSubscribersCountContext RPC returns the number of subscribers of a topic
// (not including txPool). If subscriberHashPrefix is not empty, only subscriber
// whose sha256(pubkey+identifier) contains this prefix will be counted. Each
// prefix byte will reduce result count to about 1/256, and also reduce response
// time to about 1/256 if there are a lot of subscribers. This is a good way to
// sample subscribers randomly with low cost.
//
// Count is changed to signed int for gomobile compatibility
func GetSubscribersCountContext(ctx context.Context, topic string, subscriberHashPrefix []byte, config RPCConfigInterface) (int, error) {
var count int
err := RPCCall(ctx, "getsubscriberscount", map[string]interface{}{
"topic": topic,
"subscriberHashPrefix": hex.EncodeToString(subscriberHashPrefix),
}, &count, config)
if err != nil {
return 0, err
}
return count, nil
}
// GetRegistrant wraps GetRegistrantContext with background context.
func GetRegistrant(name string, config RPCConfigInterface) (*Registrant, error) {
return GetRegistrantContext(context.Background(), name, config)
}
// GetRegistrantContext RPC gets the registrant of a name.
func GetRegistrantContext(ctx context.Context, name string, config RPCConfigInterface) (*Registrant, error) {
registrant := &Registrant{}
err := RPCCall(ctx, "getregistrant", map[string]interface{}{"name": name}, registrant, config)
if err != nil {
return nil, err
}
return registrant, nil
}
// SendRawTransaction wraps SendRawTransactionContext with background context.
func SendRawTransaction(txn *transaction.Transaction, config RPCConfigInterface) (string, error) {
return SendRawTransactionContext(context.Background(), txn, config)
}
// SendRawTransactionContext RPC sends a signed transaction to chain and returns
// txn hash hex string.
func SendRawTransactionContext(ctx context.Context, txn *transaction.Transaction, config RPCConfigInterface) (string, error) {
b, err := txn.Marshal()
if err != nil {
return "", err
}
var txnHash string
err = RPCCall(ctx, "sendrawtransaction", map[string]interface{}{"tx": hex.EncodeToString(b)}, &txnHash, config)
if err != nil {
return "", err
}
return txnHash, nil
}
// Transfer wraps TransferContext with background context.
func Transfer(s signerRPCClient, address, amount string, config *TransactionConfig) (string, error) {
return TransferContext(context.Background(), s, address, amount, config)
}
// TransferContext sends asset to a wallet address with a transaction fee.
// Amount is the string representation of the amount in unit of NKN to avoid
// precision loss. For example, "0.1" will be parsed as 0.1 NKN. The
// signerRPCClient can be a client, multiclient or wallet.
func TransferContext(ctx context.Context, s signerRPCClient, address, amount string, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
}
sender, err := program.CreateProgramHash(s.PubKey())
if err != nil {
return "", err
}
amountFixed64, err := common.StringToFixed64(amount)
if err != nil {
return "", err
}
recipient, err := common.ToScriptHash(address)
if err != nil {
return "", err
}
fee, err := common.StringToFixed64(config.Fee)
if err != nil {
return "", err
}
nonce := config.Nonce
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonceContext(ctx, true)
if err != nil {
return "", err
}
}
tx, err := transaction.NewTransferAssetTransaction(sender, recipient, uint64(nonce), amountFixed64, fee)
if err != nil {
return "", err
}
if len(config.Attributes) > 0 {
tx.Transaction.UnsignedTx.Attributes = config.Attributes
}
if err := s.SignTransaction(tx); err != nil {
return "", err
}
return s.SendRawTransactionContext(ctx, tx)
}
// RegisterName wraps RegisterNameContext with background context.
func RegisterName(s signerRPCClient, name string, config *TransactionConfig) (string, error) {
return RegisterNameContext(context.Background(), s, name, config)
}
// RegisterNameContext registers a name for this signer's public key at the cost
// of 10 NKN with a given transaction fee. The name will be valid for 1,576,800
// blocks (around 1 year). Register name currently owned by this pubkey will
// extend the duration of the name to current block height + 1,576,800.
// Registration will fail if the name is currently owned by another account. The
// signerRPCClient can be a client, multiclient or wallet.
func RegisterNameContext(ctx context.Context, s signerRPCClient, name string, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
}
fee, err := common.StringToFixed64(config.Fee)
if err != nil {
return "", err
}
nonce := config.Nonce
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonceContext(ctx, true)
if err != nil {
return "", err
}
}
tx, err := transaction.NewRegisterNameTransaction(s.PubKey(), name, uint64(nonce), nknConfig.MinNameRegistrationFee, fee)
if err != nil {
return "", err
}
if len(config.Attributes) > 0 {
tx.Transaction.UnsignedTx.Attributes = config.Attributes
}
if err := s.SignTransaction(tx); err != nil {
return "", err
}
return s.SendRawTransactionContext(ctx, tx)
}
// TransferName wraps TransferNameContext with background context.
func TransferName(s signerRPCClient, name string, recipientPubKey []byte, config *TransactionConfig) (string, error) {
return TransferNameContext(context.Background(), s, name, recipientPubKey, config)
}
// TransferNameContext transfers a name owned by this signer's pubkey to another
// public key with a transaction fee. The expiration height of the name will not
// be changed. The signerRPCClient can be a client, multiclient or wallet.
func TransferNameContext(ctx context.Context, s signerRPCClient, name string, recipientPubKey []byte, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
}
fee, err := common.StringToFixed64(config.Fee)
if err != nil {
return "", err
}
nonce := config.Nonce
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonceContext(ctx, true)
if err != nil {
return "", err
}
}
tx, err := transaction.NewTransferNameTransaction(s.PubKey(), recipientPubKey, name, uint64(nonce), fee)
if err != nil {
return "", err
}
if len(config.Attributes) > 0 {
tx.Transaction.UnsignedTx.Attributes = config.Attributes
}
if err := s.SignTransaction(tx); err != nil {
return "", err
}
return s.SendRawTransactionContext(ctx, tx)
}
// DeleteName wraps DeleteNameContext with background context.
func DeleteName(s signerRPCClient, name string, config *TransactionConfig) (string, error) {
return DeleteNameContext(context.Background(), s, name, config)
}
// DeleteNameContext deletes a name owned by this signer's pubkey with a given
// transaction fee. The signerRPCClient can be a client, multiclient or wallet.
func DeleteNameContext(ctx context.Context, s signerRPCClient, name string, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
}
fee, err := common.StringToFixed64(config.Fee)
if err != nil {
return "", err
}
nonce := config.Nonce
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonceContext(ctx, true)
if err != nil {
return "", err
}
}
tx, err := transaction.NewDeleteNameTransaction(s.PubKey(), name, uint64(nonce), fee)
if err != nil {
return "", err
}
if len(config.Attributes) > 0 {
tx.Transaction.UnsignedTx.Attributes = config.Attributes
}
if err := s.SignTransaction(tx); err != nil {
return "", err
}
return s.SendRawTransactionContext(ctx, tx)
}
// Subscribe wraps SubscribeContext with background context.
func Subscribe(s signerRPCClient, identifier, topic string, duration int, meta string, config *TransactionConfig) (string, error) {
return SubscribeContext(context.Background(), s, identifier, topic, duration, meta, config)
}
// SubscribeContext to a topic with an identifier for a number of blocks. Client
// using the same key pair and identifier will be able to receive messages from
// this topic. If this (identifier, public key) pair is already subscribed to
// this topic, the subscription expiration will be extended to current block
// height + duration. The signerRPCClient can be a client, multiclient or
// wallet.
//
// Duration is changed to signed int for gomobile compatibility.
func SubscribeContext(ctx context.Context, s signerRPCClient, identifier, topic string, duration int, meta string, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
}
fee, err := common.StringToFixed64(config.Fee)
if err != nil {
return "", err
}
nonce := config.Nonce
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonceContext(ctx, true)
if err != nil {
return "", err
}
}
tx, err := transaction.NewSubscribeTransaction(
s.PubKey(),
identifier,
topic,
uint32(duration),
meta,
uint64(nonce),
fee,
)
if err != nil {
return "", err
}
if len(config.Attributes) > 0 {
tx.Transaction.UnsignedTx.Attributes = config.Attributes
}
if err := s.SignTransaction(tx); err != nil {
return "", err
}
return s.SendRawTransactionContext(ctx, tx)
}
// Unsubscribe wraps UnsubscribeContext with background context.
func Unsubscribe(s signerRPCClient, identifier, topic string, config *TransactionConfig) (string, error) {
return UnsubscribeContext(context.Background(), s, identifier, topic, config)
}
// UnsubscribeContext from a topic for an identifier. Client using the same key
// pair and identifier will no longer receive messages from this topic. The
// signerRPCClient can be a client, multiclient or wallet.
func UnsubscribeContext(ctx context.Context, s signerRPCClient, identifier, topic string, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
}
fee, err := common.StringToFixed64(config.Fee)
if err != nil {
return "", err
}
nonce := config.Nonce
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonceContext(ctx, true)
if err != nil {
return "", err
}
}
tx, err := transaction.NewUnsubscribeTransaction(
s.PubKey(),
identifier,
topic,
uint64(nonce),
fee,
)
if err != nil {
return "", err
}
if len(config.Attributes) > 0 {
tx.Transaction.UnsignedTx.Attributes = config.Attributes
}
if err := s.SignTransaction(tx); err != nil {
return "", err
}
return s.SendRawTransactionContext(ctx, tx)
}
// GetNodeState wraps GetNodeStateContext with background context.
func GetNodeState(config RPCConfigInterface) (*NodeState, error) {
return GetNodeStateContext(context.Background(), config)
}
// GetNodeStateContext returns the state of the RPC node.
func GetNodeStateContext(ctx context.Context, config RPCConfigInterface) (*NodeState, error) {
nodeState := &NodeState{}
err := RPCCall(ctx, "getnodestate", map[string]interface{}{}, nodeState, config)
if err != nil {
return nil, err
}
return nodeState, nil
}
// GetWsAddr wraps GetWsAddrContext with background context.
func GetPeerAddr(clientAddr string, offer string, config RPCConfigInterface) (*Node, error) {
return GetPeerAddrContext(context.Background(), clientAddr, offer, config)
}
// GetPeerAddrContext RPC gets the node that a client address should connect to
// using ws.
func GetPeerAddrContext(ctx context.Context, clientAddr string, offer string, config RPCConfigInterface) (*Node, error) {
node := &Node{}
err := RPCCall(ctx, "getpeeraddr", map[string]interface{}{"address": clientAddr, "offer": offer}, node, config)
if err != nil {
return nil, err
}
return node, nil
}