From 16fa191ccb79ef7e0ba053353726b7da8e7def53 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 21 Apr 2021 15:22:13 +0300 Subject: [PATCH] cli: move combining signers and accounts to a separate package This code will be reused in other packages. --- cli/cmdargs/parser.go | 20 ++++++++++++++++++++ cli/smartcontract/smart_contract.go | 12 +++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cli/cmdargs/parser.go b/cli/cmdargs/parser.go index c1829a877..252ca4cfc 100644 --- a/cli/cmdargs/parser.go +++ b/cli/cmdargs/parser.go @@ -7,7 +7,10 @@ import ( "github.com/nspcc-dev/neo-go/cli/flags" "github.com/nspcc-dev/neo-go/pkg/core/transaction" + "github.com/nspcc-dev/neo-go/pkg/encoding/address" + "github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/smartcontract" + "github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/urfave/cli" ) @@ -128,3 +131,20 @@ func ParseParams(args []string, calledFromMain bool) (int, []smartcontract.Param } return 0, []smartcontract.Parameter{}, errors.New("invalid array syntax: missing closing bracket") } + +// GetSignersAccounts returns the list of signers combined with the corresponding +// accounts from the provided wallet. +func GetSignersAccounts(wall *wallet.Wallet, signers []transaction.Signer) ([]client.SignerAccount, error) { + signersAccounts := make([]client.SignerAccount, len(signers)) + for i := range signers { + signerAcc := wall.GetAccount(signers[i].Account) + if signerAcc == nil { + return nil, fmt.Errorf("no account was found in the wallet for signer #%d (%s)", i, address.Uint160ToString(signers[i].Account)) + } + signersAccounts[i] = client.SignerAccount{ + Signer: signers[i], + Account: signerAcc, + } + } + return signersAccounts, nil +} diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index 3fc97f41d..cb95e8627 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -555,15 +555,9 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error { if err != nil { return err } - for i := range cosigners { - cosignerAcc := wall.GetAccount(cosigners[i].Account) - if cosignerAcc == nil { - return cli.NewExitError(fmt.Errorf("can't calculate network fee: no account was found in the wallet for cosigner #%d", i), 1) - } - cosignersAccounts = append(cosignersAccounts, client.SignerAccount{ - Signer: cosigners[i], - Account: cosignerAcc, - }) + cosignersAccounts, err = cmdargs.GetSignersAccounts(wall, cosigners) + if err != nil { + return cli.NewExitError(fmt.Errorf("failed to calculate network fee: %w", err), 1) } } gctx, cancel := options.GetTimeoutContext(ctx)