cli: allow to create accounts for deployed contracts

This commit is contained in:
Evgenii Stratonikov 2020-08-27 10:50:55 +03:00
parent 9c7168e4e8
commit cdfb2429f2

View file

@ -13,6 +13,7 @@ import (
"github.com/nspcc-dev/neo-go/cli/options"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/urfave/cli"
@ -166,6 +167,20 @@ func NewCommands() []cli.Command {
},
},
},
{
Name: "import-deployed",
Usage: "import deployed contract",
UsageText: "import-multisig --wallet <path> --wif <wif> --contract <hash>",
Action: importDeployed,
Flags: append([]cli.Flag{
walletPathFlag,
wifFlag,
cli.StringFlag{
Name: "contract, c",
Usage: "Contract hash",
},
}, options.RPC...),
},
{
Name: "remove",
Usage: "remove an account from the wallet",
@ -390,7 +405,7 @@ func importMultisig(ctx *cli.Context) error {
return nil
}
func importWallet(ctx *cli.Context) error {
func importDeployed(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
@ -398,6 +413,56 @@ func importWallet(ctx *cli.Context) error {
defer wall.Close()
rawHash := strings.TrimPrefix("0x", ctx.String("contract"))
h, err := util.Uint160DecodeStringLE(rawHash)
if err != nil {
return cli.NewExitError(fmt.Errorf("invalid contract hash: %w", err), 1)
}
acc, err := newAccountFromWIF(ctx.String("wif"))
if err != nil {
return cli.NewExitError(err, 1)
}
gctx, cancel := options.GetTimeoutContext(ctx)
defer cancel()
c, err := options.GetRPCClient(gctx, ctx)
if err != nil {
return err
}
cs, err := c.GetContractState(h)
if err != nil {
return cli.NewExitError(fmt.Errorf("can't fetch contract info: %w", err), 1)
}
md := cs.Manifest.ABI.GetMethod(manifest.MethodVerify)
if md == nil {
return cli.NewExitError("contract has no `verify` method", 1)
}
acc.Contract.Script = cs.Script
for _, p := range md.Parameters {
acc.Contract.Parameters = append(acc.Contract.Parameters, wallet.ContractParam{
Name: p.Name,
Type: p.Type,
})
}
acc.Contract.Deployed = true
if err := addAccountAndSave(wall, acc); err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
func importWallet(ctx *cli.Context) error {
wall, err := openWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
defer wall.Close()
acc, err := newAccountFromWIF(ctx.String("wif"))
if err != nil {
return cli.NewExitError(err, 1)