-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
285 lines (236 loc) · 7.39 KB
/
main.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
package main
import (
"context"
"fmt"
"math"
"time"
"github.com/ExocoreNetwork/exocore/app"
"github.com/evmos/evmos/v14/encoding"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"cosmossdk.io/simapp/params"
// "github.com/cosmos/cosmos-sdk/codec"
cmdcfg "github.com/ExocoreNetwork/exocore/cmd/config"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
chainID = "exocoretestnet_233-1"
homeDir = "/Users/linqing/.tmp-exocored"
appName = "exocore"
)
var encCfg params.EncodingConfig
var txCfg client.TxConfig
var kr keyring.Keyring
var gasPrice uint64
var blockMaxGas uint64
func init() {
config := sdk.GetConfig()
cmdcfg.SetBech32Prefixes(config)
encCfg = encoding.MakeConfig(app.ModuleBasics)
txCfg = encCfg.TxConfig
var err error
if kr, err = keyring.New(appName, keyring.BackendTest, homeDir, nil, encCfg.Codec); err != nil {
panic(err)
}
gasPrice = uint64(7)
blockMaxGas = 10000000
}
func main() {
cc := createGrpcConn()
defer cc.Close()
sendTx(cc)
}
func createGrpcConn() *grpc.ClientConn {
grpcConn, err := grpc.Dial(
"127.0.0.1:9090",
grpc.WithTransportCredentials(insecure.NewCredentials()), // The Cosmos SDK doesn't support any transport security mechanism.
grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(encCfg.InterfaceRegistry).GRPCCodec())),
)
if err != nil {
panic(err)
}
return grpcConn
}
func simulateTx(cc *grpc.ClientConn, txBytes []byte) (uint64, error) {
// Simulate the tx via gRPC. We create a new client for the Protobuf Tx service
txClient := sdktx.NewServiceClient(cc)
// call the Simulate method on this client.
grpcRes, err := txClient.Simulate(
context.Background(),
&sdktx.SimulateRequest{
TxBytes: txBytes,
},
)
if err != nil {
fmt.Println("debug-simulateTx-err:", err)
return 0, err
}
fmt.Println("debug-simulateTx-gasinfo:", grpcRes.GasInfo) // Prints estimated gas used.
return grpcRes.GasInfo.GasUsed, nil
}
func signMsg(cc *grpc.ClientConn, name string, msgs ...sdk.Msg) authsigning.Tx {
txBuilder := txCfg.NewTxBuilder()
_ = txBuilder.SetMsgs(msgs...)
txBuilder.SetGasLimit(blockMaxGas)
txBuilder.SetFeeAmount(sdk.Coins{types.NewInt64Coin("aexo", math.MaxInt64)})
info, _ := kr.Key(name)
fromAddr, _ := info.GetAddress()
number, sequence, err := queryAccount(cc, fromAddr)
if err != nil {
fmt.Println("debug-queryAccount-err:", err)
panic(err)
}
txf := tx.Factory{}.
WithChainID(chainID).
WithKeybase(kr).
WithTxConfig(txCfg).
WithAccountNumber(number).
WithSequence(sequence)
if err = tx.Sign(txf, "dev1", txBuilder, true); err != nil {
panic(err)
}
//simulate and sign again
fmt.Println("sign again with simulated gas")
signedTx := txBuilder.GetTx()
txBytes, _ := txCfg.TxEncoder()(signedTx)
gasLimit, _ := simulateTx(cc, txBytes)
fee := gasLimit * gasPrice
if fee > math.MaxInt64 {
panic("fee exceeds maxInt64")
}
txBuilder.SetGasLimit(gasLimit)
txBuilder.SetFeeAmount(sdk.Coins{types.NewInt64Coin("aexo", int64(fee))})
//sign agin with simulated gas used
if err = tx.Sign(txf, "dev1", txBuilder, true); err != nil {
panic(err)
}
return txBuilder.GetTx()
}
func verifySignature(cc *grpc.ClientConn, signerName string, signedTx authsigning.Tx, signatureBytes []byte) bool {
info, _ := kr.Key(signerName)
fromAddr, _ := info.GetAddress()
pub, _ := info.GetPubKey()
number, _, err := queryAccount(cc, fromAddr)
if err != nil {
panic(err)
}
bytesToSign, err := txCfg.SignModeHandler().GetSignBytes(
txCfg.SignModeHandler().DefaultMode(),
authsigning.SignerData{
ChainID: chainID,
AccountNumber: number,
// Sequence: sequence,
PubKey: pub,
Address: sdk.AccAddress(pub.Address()).String(),
},
signedTx,
)
if err != nil {
fmt.Println("debug-getBytesToSign-err:", err)
}
verified := pub.VerifySignature(bytesToSign, signatureBytes)
return verified
}
func sendTx(cc *grpc.ClientConn) {
info, _ := kr.Key("dev1")
fromAddr, _ := info.GetAddress()
info2, _ := kr.Key("dev2")
toAddr, _ := info2.GetAddress()
msg := banktypes.NewMsgSend(fromAddr, toAddr, types.NewCoins(types.NewInt64Coin("aexo", 10000)))
fmt.Println("debug:signMsg-----")
signedTx := signMsg(cc, "dev1", msg)
signatures, err := signedTx.GetSignaturesV2()
if err != nil {
panic(err)
}
sigSingle, isSingle := signatures[0].Data.(*signing.SingleSignatureData)
if !isSingle {
panic("extract signature fail")
}
signatureBytes := sigSingle.Signature
fmt.Println("debug:verifySignature-----")
verified := verifySignature(cc, "dev1", signedTx, signatureBytes)
fmt.Println("debug-VerifySignature:", verified)
//-----broadcast
fmt.Println("check balance before send")
balance, _ := queryBalance(cc, fromAddr)
fmt.Println("debug-balance:", balance)
txBytes, err := txCfg.TxEncoder()(signedTx)
if err != nil {
panic(err)
}
ccRes := broadcastTxBytes(cc, txBytes)
_ = ccRes
// fmt.Println("debug-braodResponse:", ccRes.TxResponse)
time.Sleep(5 * time.Second)
fmt.Println("check balance after send")
balance, _ = queryBalance(cc, fromAddr)
fmt.Println("debug-balacne:", balance)
}
func broadcastTxBytes(cc *grpc.ClientConn, txBytes []byte) *sdktx.BroadcastTxResponse {
txClient := sdktx.NewServiceClient(cc)
ccRes, err := txClient.BroadcastTx(
context.Background(),
&sdktx.BroadcastTxRequest{
Mode: sdktx.BroadcastMode_BROADCAST_MODE_SYNC,
TxBytes: txBytes,
},
)
if err != nil {
panic(err)
}
return ccRes
}
func queryBalance(grpcConn *grpc.ClientConn, myAddress sdk.AccAddress) (balance *types.Coin, err error) {
// This creates a gRPC client to query the x/bank service.
bankClient := banktypes.NewQueryClient(grpcConn)
bankRes, err := bankClient.Balance(
context.Background(),
&banktypes.QueryBalanceRequest{Address: myAddress.String(), Denom: "aexo"},
)
if err != nil {
fmt.Println("debug-queryBalance:", err)
return
}
balance = bankRes.GetBalance()
return
}
func queryAccount(grpcConn *grpc.ClientConn, myAddress sdk.AccAddress) (number, sequence uint64, err error) {
fmt.Println("debug-queryAccount-myAddress:", myAddress.String())
authClient := authTypes.NewQueryClient(grpcConn)
var accountRes *authTypes.QueryAccountResponse
accountRes, err = authClient.Account(context.Background(), &authTypes.QueryAccountRequest{
Address: myAddress.String(),
})
if err != nil {
fmt.Println("debug-queryAccount-err:", err)
return
}
var account authTypes.AccountI
encCfg.Codec.UnpackAny(accountRes.Account, &account)
number = account.GetAccountNumber()
sequence = account.GetSequence()
fmt.Println("debug-queryAccount-number/sequence:", number, sequence)
fmt.Println("===")
//AccountInfo has to be used with cosmossdk version >= 0.47.5
accountResponse, err := authClient.AccountInfo(context.Background(), &authTypes.QueryAccountInfoRequest{
Address: myAddress.String(),
})
if err != nil {
return
}
number = accountResponse.Info.AccountNumber
sequence = accountResponse.Info.Sequence
fmt.Println("debug-queryAccountInfo-addressNumberSequence:", number, sequence)
return
}