-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathportal.go
149 lines (122 loc) · 4.06 KB
/
portal.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
package main
import (
"github.com/incognitochain/go-incognito-sdk-v2/incclient"
"github.com/urfave/cli/v2"
"log"
)
// getPortalDepositAddress generates the portal depositing (i.e, shielding) address for a payment address and a tokenID.
func getPortalDepositAddress(c *cli.Context) error {
address := c.String(addressFlag)
if !isValidAddress(address) {
return newAppError(InvalidPaymentAddressError)
}
tokenIDStr := c.String(tokenIDFlag)
if !isValidTokenID(tokenIDStr) {
return newAppError(InvalidTokenIDError)
}
shieldAddress, err := cfg.incClient.GeneratePortalShieldingAddress(address, tokenIDStr)
if err != nil {
return newAppError(GenerateShieldingAddressError, err)
}
return jsonPrintWithKey("ShieldAddress", shieldAddress)
}
// portalShield deposits a portal token (e.g, BTC) into the Incognito chain.
func portalShield(c *cli.Context) error {
if cfg.btcClient == nil {
return newAppError(BTCClientNotFoundError)
}
privateKey := c.String(privateKeyFlag)
if !isValidPrivateKey(privateKey) {
return newAppError(InvalidPrivateKeyError)
}
address := c.String(addressFlag)
if address == "" {
address = incclient.PrivateKeyToPaymentAddress(privateKey, -1)
}
if !isValidAddress(address) {
return newAppError(InvalidPaymentAddressError)
}
portalTxHashStr := c.String(externalTxIDFlag)
tokenIDStr := c.String(tokenIDFlag)
if !isValidTokenID(tokenIDStr) {
return newAppError(InvalidTokenIDError)
}
// check if the transaction has enough confirmations.
isConfirmed, blkHeight, err := cfg.btcClient.IsConfirmedTx(portalTxHashStr)
if err != nil {
return newAppError(GetBTCConfirmationError, err)
}
if !isConfirmed {
return newAppError(NotEnoughBTCConfirmationError)
}
// generate the shielding proof.
shieldingProof, err := cfg.btcClient.BuildProof(portalTxHashStr, blkHeight)
if err != nil {
return newAppError(BuildBTCProofError, err)
}
// create an Incognito transaction to submit the proof.
txHash, err := cfg.incClient.CreateAndSendPortalShieldTransaction(privateKey, tokenIDStr, address, shieldingProof, nil, nil)
if err != nil {
return newAppError(CreatePortalShieldingTransactionError, err)
}
return jsonPrintWithKey("TxHash", txHash)
}
// getPortalShieldStatus returns the status of a portal shielding request.
func getPortalShieldStatus(c *cli.Context) error {
txHash := c.String(txHashFlag)
if txHash == "" {
return newAppError(InvalidIncognitoTxHashError)
}
status, err := cfg.incClient.GetPortalShieldingRequestStatus(txHash)
if err != nil {
return newAppError(GetPortalShieldingStatusError)
}
return jsonPrint(status)
}
// portalUnShield creates and sends a port un-shielding transaction.
func portalUnShield(c *cli.Context) error {
privateKey := c.String(privateKeyFlag)
if !isValidPrivateKey(privateKey) {
return newAppError(InvalidPrivateKeyError)
}
tokenIDStr := c.String(tokenIDFlag)
if !isValidTokenID(tokenIDStr) {
return newAppError(InvalidTokenIDError)
}
unShieldAmount := c.Uint64(amountFlag)
if unShieldAmount == 0 {
return newAppError(InvalidAmountError)
}
remoteAddress := c.String(externalAddressFlag)
if remoteAddress == "" {
return newAppError(InvalidExternalAddressError)
}
// create a transaction to burn the Incognito token.
txHash, err := cfg.incClient.CreateAndSendPortalUnShieldTransaction(
privateKey,
tokenIDStr,
remoteAddress,
unShieldAmount,
nil,
nil,
)
if err != nil {
return newAppError(CreatePortalUnShieldingTransactionError, err)
}
log.Printf("TxHash: %v\n", txHash)
log.Println("Please wait for ~ 30-60 minutes for the fund to be released!!")
log.Println("Use command `portalunshieldstatus` to check the status of the request.")
return nil
}
// getPortalUnShieldStatus returns the status of a portal un-shielding request.
func getPortalUnShieldStatus(c *cli.Context) error {
txHash := c.String(txHashFlag)
if !isValidIncTxHash(txHash) {
return newAppError(InvalidIncognitoTxHashError)
}
status, err := cfg.incClient.GetPortalUnShieldingRequestStatus(txHash)
if err != nil {
return newAppError(GetPortalUnShieldingStatusError, err)
}
return jsonPrint(status)
}