-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
356 lines (301 loc) · 8.08 KB
/
client.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// This is a temporary shadow of the Client struct to be provided by https://github.com/gnolang/gno/pull/1047 .
package service
import (
"fmt"
"github.com/gnolang/gno/tm2/pkg/amino"
rpc_client "github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/sdk/vm"
"github.com/gnolang/gno/tm2/pkg/std"
)
// Client represents the Gno.land RPC API client.
type Client struct {
remote string
chainID string
keybase keys.Keybase
nameOrBech32 string
password string
}
type Opts struct {
Remote string
ChainID string
NameOrBech32 string
Password string
}
func (o *Opts) ApplyDefaults() {
if o.Remote == "" {
o.Remote = "127.0.0.1:26657"
}
if o.ChainID == "" {
o.ChainID = "dev"
}
}
func NewClient(opts Opts) *Client {
opts.ApplyDefaults()
return &Client{
remote: opts.Remote,
chainID: opts.ChainID,
nameOrBech32: opts.NameOrBech32,
password: opts.Password,
}
}
func (c *Client) SetRemote(remote string) {
c.remote = remote
}
func (c *Client) SetChainID(chainID string) {
c.chainID = chainID
}
func (c *Client) InitKeyBaseFromDir(rootDir string) error {
var err error
if c.keybase, err = keys.NewKeyBaseFromDir(rootDir); err != nil {
return err
}
return nil
}
func (c *Client) SetNameOrBech32(nameOrBech32 string) {
c.nameOrBech32 = nameOrBech32
}
func (c *Client) SetPassword(password string) {
c.password = password
}
func (c *Client) GetKeys() ([]keys.Info, error) {
keyList, err := c.keybase.List()
if err != nil {
return nil, err
}
return keyList, nil
}
func (c *Client) GetKeyByNameOrBech32(nameOrBech32 string) (keys.Info, error) {
return c.keybase.GetByNameOrAddress(nameOrBech32)
}
func (c *Client) CreateAccount(nameOrBech32 string, mnemonic string, bip39Passwd string, password string, account uint32, index uint32) (keys.Info, error) {
info, err := c.keybase.CreateAccount(nameOrBech32, mnemonic, bip39Passwd, password, account, index)
if err != nil {
return nil, err
}
return info, err
}
// TODO: port existing code, i.e. faucet?
// TODO: create right now a tm2 generic go client and a gnovm generic go client?
// TODO: Command: Send
// TODO: Command: AddPkg
// TODO: Command: Query
// TODO: Command: Eval
// TODO: Command: Exec
// TODO: Command: Package
// TODO: Command: QFile
// TODO: examples and unit tests
// TODO: Mock
// TODO: alternative configuration (pass existing websocket?)
// TODO: minimal go.mod to make it light to import
func (c *Client) Call(pkgPath string, fnc string, args commands.StringArr, gasFee string, gasWanted int64, send string, nameOrBech32 string, password string) error {
info, err := c.keybase.GetByNameOrAddress(nameOrBech32)
if err != nil {
return err
}
caller := info.GetAddress()
// Parse send amount.
sendCoins, err := std.ParseCoins(send)
if err != nil {
return errors.Wrap(err, "parsing send coins")
}
// parse gas wanted & fee.
gasFeeCoins, err := std.ParseCoin(gasFee)
if err != nil {
return errors.Wrap(err, "parsing gas fee coin")
}
// construct msg & tx and marshal.
msg := vm.MsgCall{
Caller: caller,
Send: sendCoins,
PkgPath: pkgPath,
Func: fnc,
Args: args,
}
tx := std.Tx{
Msgs: []std.Msg{msg},
Fee: std.NewFee(gasWanted, gasFeeCoins),
Signatures: nil,
Memo: "",
}
qopts := &queryCfg{
remote: c.remote,
path: fmt.Sprintf("auth/accounts/%s", caller),
}
qres, err := queryHandler(qopts)
if err != nil {
return errors.Wrap(err, "query account")
}
var qret struct{ BaseAccount std.BaseAccount }
err = amino.UnmarshalJSON(qres.Response.Data, &qret)
if err != nil {
return err
}
// sign tx
accountNumber := qret.BaseAccount.AccountNumber
sequence := qret.BaseAccount.Sequence
sopts := &signCfg{
kb: c.keybase,
sequence: sequence,
accountNumber: accountNumber,
chainID: c.chainID,
nameOrBech32: nameOrBech32,
txJSON: amino.MustMarshalJSON(tx),
pass: password,
}
signedTx, err := SignHandler(sopts)
if err != nil {
return errors.Wrap(err, "sign tx")
}
// broadcast signed tx
bopts := &broadcastCfg{
remote: c.remote,
tx: signedTx,
}
bres, err := broadcastHandler(bopts)
if err != nil {
return errors.Wrap(err, "broadcast tx")
}
if bres.CheckTx.IsErr() {
return errors.Wrap(bres.CheckTx.Error, "check transaction failed: log:%s", bres.CheckTx.Log)
}
if bres.DeliverTx.IsErr() {
return errors.Wrap(bres.DeliverTx.Error, "deliver transaction failed: log:%s", bres.DeliverTx.Log)
}
return nil
}
// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/query.go
type queryCfg struct {
remote string
data string
height int64
prove bool
// internal
path string
}
// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/sign.go
type signCfg struct {
kb keys.Keybase
txPath string
chainID string
accountNumber uint64
sequence uint64
showSignBytes bool
// internal flags, when called programmatically
nameOrBech32 string
txJSON []byte
pass string
}
// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/broadcast.go
type broadcastCfg struct {
remote string
dryRun bool
// internal
tx *std.Tx
}
// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/query.go
func queryHandler(cfg *queryCfg) (*ctypes.ResultABCIQuery, error) {
remote := cfg.remote
if remote == "" || remote == "y" {
return nil, errors.New("missing remote url")
}
data := []byte(cfg.data)
opts2 := rpc_client.ABCIQueryOptions{
// Height: height, XXX
// Prove: false, XXX
}
cli := rpc_client.NewHTTP(remote, "/websocket")
qres, err := cli.ABCIQueryWithOptions(
cfg.path, data, opts2)
if err != nil {
return nil, errors.Wrap(err, "querying")
}
return qres, nil
}
// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/broadcast.go
func broadcastHandler(cfg *broadcastCfg) (*ctypes.ResultBroadcastTxCommit, error) {
if cfg.tx == nil {
return nil, errors.New("invalid tx")
}
remote := cfg.remote
if remote == "" || remote == "y" {
return nil, errors.New("missing remote url")
}
bz, err := amino.Marshal(cfg.tx)
if err != nil {
return nil, errors.Wrap(err, "remarshaling tx binary bytes")
}
cli := rpc_client.NewHTTP(remote, "/websocket")
/*
if cfg.dryRun {
return simulateTx(cli, bz)
}
*/
bres, err := cli.BroadcastTxCommit(bz)
if err != nil {
return nil, errors.Wrap(err, "broadcasting bytes")
}
return bres, nil
}
// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/sign.go
// (Even though the original SignHandler is public, its argument signCfg is private.)
func SignHandler(cfg *signCfg) (*std.Tx, error) {
var err error
var tx std.Tx
if cfg.txJSON == nil {
return nil, errors.New("invalid tx content")
}
err = amino.UnmarshalJSON(cfg.txJSON, &tx)
if err != nil {
return nil, err
}
// fill tx signatures.
signers := tx.GetSigners()
if tx.Signatures == nil {
for range signers {
tx.Signatures = append(tx.Signatures, std.Signature{
PubKey: nil, // zero signature
Signature: nil, // zero signature
})
}
}
// validate document to sign.
err = tx.ValidateBasic()
if err != nil {
return nil, err
}
// derive sign doc bytes.
chainID := cfg.chainID
accountNumber := cfg.accountNumber
sequence := cfg.sequence
signbz := tx.GetSignBytes(chainID, accountNumber, sequence)
if cfg.showSignBytes {
fmt.Printf("sign bytes: %X\n", signbz)
return nil, nil
}
sig, pub, err := cfg.kb.Sign(cfg.nameOrBech32, cfg.pass, signbz)
if err != nil {
return nil, err
}
addr := pub.Address()
found := false
for i := range tx.Signatures {
// override signature for matching slot.
if signers[i] == addr {
found = true
tx.Signatures[i] = std.Signature{
PubKey: pub,
Signature: sig,
}
}
}
if !found {
return nil, errors.New(
fmt.Sprintf("addr %v (%s) not in signer set", addr, cfg.nameOrBech32),
)
}
return &tx, nil
}