/
tx.go
112 lines (92 loc) · 2.5 KB
/
tx.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
package main
import (
"fmt"
"github.com/incognitochain/go-incognito-sdk-v2/common"
"github.com/urfave/cli/v2"
)
// send creates and sends a transaction from one wallet to another w.r.t a tokenID.
func send(c *cli.Context) error {
err := initNetWork()
if err != nil {
return err
}
privateKey := c.String("privateKey")
if !isValidPrivateKey(privateKey) {
return fmt.Errorf("private key is invalid")
}
address := c.String("address")
if !isValidAddress(address) {
return fmt.Errorf("receiver address is not valid")
}
tokenIDStr := c.String("tokenID")
if !isValidTokenID(tokenIDStr) {
return fmt.Errorf("tokenID is invalid")
}
amount := c.Uint64("amount")
if amount == 0 {
return fmt.Errorf("amount cannot be zero")
}
fee := c.Uint64("fee")
if fee == 0 {
return fmt.Errorf("fee cannot be zero")
}
version := c.Int("version")
if !isSupportedVersion(int8(version)) {
return fmt.Errorf("version is not supported")
}
fmt.Printf("Send %v of token %v from %v to %v with version %v\n", amount, tokenIDStr, privateKey, address, version)
var txHash string
if tokenIDStr == common.PRVIDStr {
txHash, err = cfg.incClient.CreateAndSendRawTransaction(privateKey,
[]string{address},
[]uint64{amount},
int8(version), nil)
} else {
txHash, err = cfg.incClient.CreateAndSendRawTokenTransaction(privateKey,
[]string{address},
[]uint64{amount},
tokenIDStr,
int8(version), nil)
}
if err != nil {
return err
}
fmt.Printf("Success!! TxHash %v\n", txHash)
return nil
}
// checkReceiver if a user is a receiver of a transaction.
func checkReceiver(c *cli.Context) error {
err := initNetWork()
if err != nil {
return err
}
txHash := c.String(txHashFlag)
if txHash == "" {
return fmt.Errorf("%v is invalid", txHashFlag)
}
otaKey := c.String(otaKeyFlag)
if !isValidOtaKey(otaKey) {
return fmt.Errorf("%v is invalid", otaKeyFlag)
}
readonlyKey := c.String(readonlyKeyFlag)
if readonlyKey != "" && !isValidReadonlyKey(otaKey) {
return fmt.Errorf("%v is invalid", readonlyKeyFlag)
}
var received bool
var res map[string]uint64
if readonlyKey == "" {
received, res, err = cfg.incClient.GetReceivingInfo(txHash, otaKey)
} else {
received, res, err = cfg.incClient.GetReceivingInfo(txHash, otaKey, readonlyKey)
}
if err != nil {
return err
}
if !received {
fmt.Printf("OTAKey %v is not a receiver of tx %v\n", otaKeyFlag, txHash)
} else {
fmt.Printf("OTAKey %v is a receiver of tx %v\n", otaKeyFlag, txHash)
fmt.Printf("Receiving info: %v\n", res)
}
return nil
}