-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
254 lines (217 loc) · 6.83 KB
/
helpers.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
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/anypb"
"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/auth"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/consensus"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
cosmostx "cosmossdk.io/api/cosmos/tx/v1beta1"
txsigning "cosmossdk.io/x/tx/signing"
"github.com/cosmos/cosmos-sdk/codec"
oldtxsigning "github.com/cosmos/cosmos-sdk/types/tx/signing"
sdksigning "github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
)
// WalletSigner is a struct that holds the basics for signing a transaction.
// This is not required, but is nice to have instead of passing through tons of method arguments.
type WalletSigner struct {
Ctx context.Context
TxBuilder client.TxBuilder
EncCfg moduletestutil.TestEncodingConfig
Keyring keyring.Keyring
GrpcConn *grpc.ClientConn
}
// SetupWalletSigner sets up the wallet signer basics and returns a pointer to
// the WalletSigner struct.
func SetupWalletSigner(gRPCAddr string) *WalletSigner {
// To sign a transaction, the AppModuleBasic must be provided here. This
// is for the protobuf (so we can encode/decode the transaction bytes)
encCfg := moduletestutil.MakeTestEncodingConfig(
auth.AppModuleBasic{},
bank.AppModuleBasic{},
staking.AppModuleBasic{},
mint.AppModuleBasic{},
params.AppModuleBasic{},
slashing.AppModuleBasic{},
consensus.AppModuleBasic{},
)
// Setup a gRPC connection
grpcConn, err := grpc.Dial(
gRPCAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
panic(err)
}
// Setup a struct to share data with helper methods.
w := &WalletSigner{
Ctx: context.Background(),
GrpcConn: grpcConn,
EncCfg: encCfg,
Keyring: keyring.NewInMemory(encCfg.Codec),
}
// Set the TxBuilder to be empty
w.ResetTxBuilder()
return w
}
// ResetTxBuilder resets the TxBuilder to a new TxBuilder.
func (w *WalletSigner) ResetTxBuilder() {
w.TxBuilder = w.EncCfg.TxConfig.NewTxBuilder()
}
// LoadKeyFromMnemonic loads a key from a mnemonic and returns the keyring record and the address.
func (w *WalletSigner) LoadKeyFromMnemonic(keyName, mnemonic, password string) (*keyring.Record, sdk.AccAddress) {
path := sdk.GetConfig().GetFullBIP44Path()
r, err := w.Keyring.NewAccount(keyName, mnemonic, password, path, hd.Secp256k1)
if err != nil {
panic(err)
}
a, err := r.GetAddress()
if err != nil {
panic(err)
}
return r, a
}
// GetAccountInfo returns an SDK account from the chain.
// NOTE: the account must have at least 0.000001 tokens in it first to work.
func (w *WalletSigner) GetAccountInfo(addr sdk.AccAddress) (authtypes.BaseAccount, error) {
var acc authtypes.BaseAccount
// Query account info from the chain (required for sequence & account number)
res, err := authtypes.NewQueryClient(w.GrpcConn).Account(w.Ctx, &authtypes.QueryAccountRequest{
Address: addr.String(),
})
if err != nil {
return acc, err
}
if err := w.EncCfg.Codec.Unmarshal(res.Account.Value, &acc); err != nil {
return acc, err
}
return acc, nil
}
func (w *WalletSigner) BroadcastTx() *cosmostx.BroadcastTxResponse {
// Generated Protobuf-encoded bytes.
txBytes, err := w.EncCfg.TxConfig.TxEncoder()(w.TxBuilder.GetTx())
if err != nil {
panic(err)
}
// JSON String (not required, just showing for reference)
txBytesJson, err := w.EncCfg.TxConfig.TxJSONEncoder()(w.TxBuilder.GetTx())
if err != nil {
panic(err)
}
fmt.Println("txBytesJson", string(txBytesJson))
// Submit the Tx to the gRPC server
txClient := cosmostx.NewServiceClient(w.GrpcConn)
grpcRes, err := txClient.BroadcastTx(
w.Ctx,
&cosmostx.BroadcastTxRequest{
Mode: cosmostx.BroadcastMode_BROADCAST_MODE_SYNC,
TxBytes: txBytes,
},
)
if err != nil {
panic(err)
}
return grpcRes
}
func (w *WalletSigner) SignTx(keyName string) error {
protoTxCfg := authtx.NewTxConfig(codec.NewProtoCodec(w.EncCfg.InterfaceRegistry), authtx.DefaultSignModes)
signModeHandler := protoTxCfg.SignModeHandler().DefaultMode()
signMode := oldtxsigning.SignMode(signModeHandler)
k, err := w.Keyring.Key(keyName)
if err != nil {
return err
}
krAcc, err := k.GetAddress()
if err != nil {
return err
}
pubKey, err := k.GetPubKey()
if err != nil {
return err
}
// requires chain connection (gRPC). You could also just hardcode this
acc, err := w.GetAccountInfo(krAcc)
if err != nil {
return err
}
accSeq := acc.Sequence
accNum := acc.AccountNumber
// First round: we gather all the signer infos. We use the "set empty signature" hack to do that.
if err := w.TxBuilder.SetSignatures(oldtxsigning.SignatureV2{
PubKey: pubKey,
Data: &oldtxsigning.SingleSignatureData{
SignMode: signMode,
Signature: nil,
},
Sequence: accSeq,
}); err != nil {
panic(err)
}
signerData := SignerData(pubKey, accNum, accSeq)
// 2nd round: with all signer infos set, signer can sign really.
txData := TxSigningData(w.TxBuilder.GetTx())
signBz, err := protoTxCfg.SignModeHandler().GetSignBytes(w.Ctx, signModeHandler, signerData, txData)
if err != nil {
return err
}
sigBz, _, err := w.Keyring.Sign(keyName, signBz, oldtxsigning.SignMode_SIGN_MODE_DIRECT)
if err != nil {
return err
}
if err = w.TxBuilder.SetSignatures(oldtxsigning.SignatureV2{
PubKey: pubKey,
Data: &sdksigning.SingleSignatureData{
SignMode: sdksigning.SignMode(signMode),
Signature: sigBz,
},
Sequence: acc.Sequence,
}); err != nil {
return err
}
return nil
}
func SignerData(pubKey cryptotypes.PubKey, accNum, accSeq uint64) txsigning.SignerData {
anyPubKey, err := PubKeyToAny(pubKey)
if err != nil {
panic(err)
}
return txsigning.SignerData{
ChainID: ChainId,
AccountNumber: accNum,
Sequence: accSeq,
PubKey: anyPubKey,
}
}
func TxSigningData(tx authsigning.Tx) txsigning.TxData {
adaptableTx, ok := tx.(authsigning.V2AdaptableTx)
if !ok {
panic(fmt.Sprintf("%T does not implement the authsigning.V2AdaptableTx interface", tx))
}
return adaptableTx.GetSigningTxData()
}
func PubKeyToAny(pubKey cryptotypes.PubKey) (*anypb.Any, error) {
anyPk, err := codectypes.NewAnyWithValue(pubKey)
if err != nil {
return nil, err
}
return &anypb.Any{
TypeUrl: anyPk.TypeUrl,
Value: anyPk.Value,
}, nil
}