neo-go/cli/cmdargs/parser.go
Anna Shaleva 2ab420ed18 cli: move cosigners parsing to a separate function
We have a lot of common code which is shared between `smartcontract` and
`wallet` cli packages. It's convinient to keep it in a separate helper
package in order to avoid functional cli packages dependencies.
2021-04-23 10:11:01 +03:00

49 lines
1.1 KiB
Go

package cmdargs
import (
"fmt"
"strings"
"github.com/nspcc-dev/neo-go/cli/flags"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/urfave/cli"
)
// GetSignersFromContext returns signers parsed from context args starting
// from the specified offset.
func GetSignersFromContext(ctx *cli.Context, offset int) ([]transaction.Signer, *cli.ExitError) {
args := ctx.Args()
var signers []transaction.Signer
if args.Present() && len(args) > offset {
for i, c := range args[offset:] {
cosigner, err := parseCosigner(c)
if err != nil {
return nil, cli.NewExitError(fmt.Errorf("failed to parse signer #%d: %w", i, err), 1)
}
signers = append(signers, cosigner)
}
}
return signers, nil
}
func parseCosigner(c string) (transaction.Signer, error) {
var (
err error
res = transaction.Signer{
Scopes: transaction.CalledByEntry,
}
)
data := strings.SplitN(c, ":", 2)
s := data[0]
res.Account, err = flags.ParseAddress(s)
if err != nil {
return res, err
}
if len(data) > 1 {
res.Scopes, err = transaction.ScopesFromString(data[1])
if err != nil {
return transaction.Signer{}, err
}
}
return res, nil
}