-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwallet.go
228 lines (163 loc) · 4.89 KB
/
wallet.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
package tronWallet
import (
"crypto/ecdsa"
"errors"
"fmt"
"github.com/Amirilidan78/tron-wallet/enums"
"github.com/Amirilidan78/tron-wallet/grpcClient"
"github.com/Amirilidan78/tron-wallet/util"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"math/big"
)
type TronWallet struct {
Node enums.Node
Address string
AddressBase58 string
PrivateKey string
PublicKey string
}
// generating
func GenerateTronWallet(node enums.Node) *TronWallet {
privateKey, _ := generatePrivateKey()
privateKeyHex := convertPrivateKeyToHex(privateKey)
publicKey, _ := getPublicKeyFromPrivateKey(privateKey)
publicKeyHex := convertPublicKeyToHex(publicKey)
address := getAddressFromPublicKey(publicKey)
addressBase58 := util.HexToBase58(address)
return &TronWallet{
Node: node,
Address: address,
AddressBase58: addressBase58,
PrivateKey: privateKeyHex,
PublicKey: publicKeyHex,
}
}
func CreateTronWallet(node enums.Node, privateKeyHex string) (*TronWallet, error) {
privateKey, err := privateKeyFromHex(privateKeyHex)
if err != nil {
return nil, err
}
publicKey, _ := getPublicKeyFromPrivateKey(privateKey)
publicKeyHex := convertPublicKeyToHex(publicKey)
address := getAddressFromPublicKey(publicKey)
addressBase58 := util.HexToBase58(address)
return &TronWallet{
Node: node,
Address: address,
AddressBase58: addressBase58,
PrivateKey: privateKeyHex,
PublicKey: publicKeyHex,
}, nil
}
// struct functions
func (t *TronWallet) PrivateKeyRCDSA() (*ecdsa.PrivateKey, error) {
return privateKeyFromHex(t.PrivateKey)
}
func (t *TronWallet) PrivateKeyBytes() ([]byte, error) {
priv, err := t.PrivateKeyRCDSA()
if err != nil {
return []byte{}, err
}
return crypto.FromECDSA(priv), nil
}
// private key
func generatePrivateKey() (*ecdsa.PrivateKey, error) {
return crypto.GenerateKey()
}
func convertPrivateKeyToHex(privateKey *ecdsa.PrivateKey) string {
privateKeyBytes := crypto.FromECDSA(privateKey)
return hexutil.Encode(privateKeyBytes)[2:]
}
func privateKeyFromHex(hex string) (*ecdsa.PrivateKey, error) {
return crypto.HexToECDSA(hex)
}
// public key
func getPublicKeyFromPrivateKey(privateKey *ecdsa.PrivateKey) (*ecdsa.PublicKey, error) {
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return nil, errors.New("error in getting public key")
}
return publicKeyECDSA, nil
}
func convertPublicKeyToHex(publicKey *ecdsa.PublicKey) string {
privateKeyBytes := crypto.FromECDSAPub(publicKey)
return hexutil.Encode(privateKeyBytes)[2:]
}
// address
func getAddressFromPublicKey(publicKey *ecdsa.PublicKey) string {
address := crypto.PubkeyToAddress(*publicKey).Hex()
address = "41" + address[2:]
return address
}
// balance
func (t *TronWallet) Balance() (int64, error) {
c, err := grpcClient.GetGrpcClient(t.Node)
if err != nil {
return 0, err
}
b, err := c.GetAccount(t.AddressBase58)
if err != nil {
return 0, err
}
return b.Balance, nil
}
func (t *TronWallet) BalanceTRC20(token *Token) (int64, error) {
balance, err := token.GetBalance(t.Node, t.AddressBase58)
if err != nil {
return 0, err
}
return balance.Int64(), nil
}
// transaction
func (t *TronWallet) Transfer(toAddressBase58 string, amountInSun int64) (string, error) {
privateRCDSA, err := t.PrivateKeyRCDSA()
if err != nil {
return "", fmt.Errorf("RCDSA private key error: %v", err)
}
tx, err := createTransactionInput(t.Node, t.AddressBase58, toAddressBase58, amountInSun)
if err != nil {
return "", fmt.Errorf("creating tx pb error: %v", err)
}
tx, err = signTransaction(tx, privateRCDSA)
if err != nil {
return "", fmt.Errorf("signing transaction error: %v", err)
}
err = broadcastTransaction(t.Node, tx)
if err != nil {
return "", fmt.Errorf("broadcast transaction error: %v", err)
}
return hexutil.Encode(tx.GetTxid())[2:], nil
}
func (t *TronWallet) TransferTRC20(token *Token, toAddressBase58 string, amountInTRC20 *big.Int) (string, error) {
c, err := grpcClient.GetGrpcClient(t.Node)
if err != nil {
return "", err
}
toAddress, err := util.Base58ToAddress(toAddressBase58)
if err != nil {
return "", err
}
ab := common.LeftPadBytes(amountInTRC20.Bytes(), 32)
req := trc20TransferMethodSignature + "0000000000000000000000000000000000000000000000000000000000000000"[len(toAddress.Hex())-4:] + toAddress.Hex()[4:]
req += common.Bytes2Hex(ab)
tx, err := c.TRC20Call(t.AddressBase58, token.ContractAddress.Base58(), req, false, trc20FeeLimit)
if err != nil {
return "", err
}
privateKey, err := t.PrivateKeyRCDSA()
if err != nil {
return "", err
}
signedTx, err := signTransaction(tx, privateKey)
if err != nil {
return "", err
}
err = broadcastTransaction(t.Node, signedTx)
if err != nil {
return "", err
}
return hexutil.Encode(tx.GetTxid())[2:], nil
}