-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR shares the same goal as #1438: to make `tm2` completely unaware of `gnovm` and `gno.land`. However, it adopts a different strategy to achieve this, following #1438 (comment). It will: - Exposed almost everything from the `tm2/crypto/keys/client` package: - `NewCommandXXX`, along with their specific structure configurations and fields, are now exposed to allow the creation of composable CLI tools. - `XXXHandler` (including Sign, Verify, Broadcast, etc.) are now exposed to enable external packages to reuse some logic from the `keys/client` package. - Moved specific `gnovm`/`gno.land` commands to the `gnokey` package: `run`, `call`, and `addpkg`. - In an effort to avoid duplicate code, an `ExecSignAndBroadcast` method has been exposed. However, it appears too specific, and I am considering duplicating it regardless. I would appreciate thoughts on this. <details><summary>Contributors' checklist...</summary> - [ ] Added new tests, or not needed, or not feasible - [ ] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [ ] Updated the official documentation or not needed - [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [ ] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md). </details> --------- Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
- Loading branch information
Showing
32 changed files
with
745 additions
and
593 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## keycli | ||
|
||
`keycli` is an extension of `tm2/keys/client`, enhancing its functionality. It provides the following features: | ||
|
||
- **addpkg**: Allows you to upload a new package to the blockchain. | ||
- **run**: Execute Gno code by invoking the main() function from the target package. | ||
- **call**: Executes a single function call within a Realm. | ||
- **maketx**: Compose a transaction (tx) document to sign (and possibly broadcast). | ||
|
||
--- | ||
|
||
Most of these features have been extracted from `tm2/keys/client` to ensure that `tm2` remains completely independent of `gnovm` and `gno.land`. For more detailed information regarding this change, please refer to [PR#1483](https://github.com/gnolang/gno/pull/1483) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package keyscli | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/gnolang/gno/gno.land/pkg/sdk/vm" | ||
gno "github.com/gnolang/gno/gnovm/pkg/gnolang" | ||
"github.com/gnolang/gno/tm2/pkg/amino" | ||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/crypto/keys" | ||
"github.com/gnolang/gno/tm2/pkg/crypto/keys/client" | ||
"github.com/gnolang/gno/tm2/pkg/errors" | ||
"github.com/gnolang/gno/tm2/pkg/std" | ||
) | ||
|
||
type MakeAddPkgCfg struct { | ||
RootCfg *client.MakeTxCfg | ||
|
||
PkgPath string | ||
PkgDir string | ||
Deposit string | ||
} | ||
|
||
func NewMakeAddPkgCmd(rootCfg *client.MakeTxCfg, io commands.IO) *commands.Command { | ||
cfg := &MakeAddPkgCfg{ | ||
RootCfg: rootCfg, | ||
} | ||
|
||
return commands.NewCommand( | ||
commands.Metadata{ | ||
Name: "addpkg", | ||
ShortUsage: "addpkg [flags] <key-name>", | ||
ShortHelp: "Uploads a new package", | ||
}, | ||
cfg, | ||
func(_ context.Context, args []string) error { | ||
return execMakeAddPkg(cfg, args, io) | ||
}, | ||
) | ||
} | ||
|
||
func (c *MakeAddPkgCfg) RegisterFlags(fs *flag.FlagSet) { | ||
fs.StringVar( | ||
&c.PkgPath, | ||
"pkgpath", | ||
"", | ||
"package path (required)", | ||
) | ||
|
||
fs.StringVar( | ||
&c.PkgDir, | ||
"pkgdir", | ||
"", | ||
"path to package files (required)", | ||
) | ||
|
||
fs.StringVar( | ||
&c.Deposit, | ||
"deposit", | ||
"", | ||
"deposit coins", | ||
) | ||
} | ||
|
||
func execMakeAddPkg(cfg *MakeAddPkgCfg, args []string, io commands.IO) error { | ||
if cfg.PkgPath == "" { | ||
return errors.New("pkgpath not specified") | ||
} | ||
if cfg.PkgDir == "" { | ||
return errors.New("pkgdir not specified") | ||
} | ||
|
||
if len(args) != 1 { | ||
return flag.ErrHelp | ||
} | ||
|
||
// read account pubkey. | ||
nameOrBech32 := args[0] | ||
kb, err := keys.NewKeyBaseFromDir(cfg.RootCfg.RootCfg.Home) | ||
if err != nil { | ||
return err | ||
} | ||
info, err := kb.GetByNameOrAddress(nameOrBech32) | ||
if err != nil { | ||
return err | ||
} | ||
creator := info.GetAddress() | ||
// info.GetPubKey() | ||
|
||
// parse deposit. | ||
deposit, err := std.ParseCoins(cfg.Deposit) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// open files in directory as MemPackage. | ||
memPkg := gno.ReadMemPackage(cfg.PkgDir, cfg.PkgPath) | ||
if memPkg.IsEmpty() { | ||
panic(fmt.Sprintf("found an empty package %q", cfg.PkgPath)) | ||
} | ||
|
||
// precompile and validate syntax | ||
err = gno.PrecompileAndCheckMempkg(memPkg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// parse gas wanted & fee. | ||
gaswanted := cfg.RootCfg.GasWanted | ||
gasfee, err := std.ParseCoin(cfg.RootCfg.GasFee) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// construct msg & tx and marshal. | ||
msg := vm.MsgAddPackage{ | ||
Creator: creator, | ||
Package: memPkg, | ||
Deposit: deposit, | ||
} | ||
tx := std.Tx{ | ||
Msgs: []std.Msg{msg}, | ||
Fee: std.NewFee(gaswanted, gasfee), | ||
Signatures: nil, | ||
Memo: cfg.RootCfg.Memo, | ||
} | ||
|
||
if cfg.RootCfg.Broadcast { | ||
err := client.ExecSignAndBroadcast(cfg.RootCfg, args, tx, io) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
fmt.Println(string(amino.MustMarshalJSON(tx))) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.