Skip to content

Commit 3f108cc

Browse files
author
gregorgololicic
committed
account address validation
1 parent da6e08f commit 3f108cc

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

gateway/grpc.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,23 @@ func (g *GRPC) SetAPIURL(url string) {
2323
}
2424

2525
// GetAccount gets account by the address via grpc call
26-
func (g *GRPC) GetAccount(address string) (*models.Account, error) {
26+
func (g *GRPC) GetAccount(addr string) (*models.Account, error) {
2727
flowClient, err := client.New(g.APIURL, grpc.WithInsecure())
2828

2929
if err != nil {
3030
return nil, err
3131
}
3232

33-
// validation of params
33+
chainID := flow.ChainID("flow-emulator") // todo refactor this to config if used on other networks
34+
address := flow.HexToAddress(addr)
35+
36+
if !address.IsValid(chainID) {
37+
return nil, &InvalidAddress{}
38+
}
3439

3540
account, err := flowClient.GetAccountAtLatestBlock(
3641
context.Background(),
37-
flow.HexToAddress(address),
42+
address,
3843
)
3944

4045
if err != nil {
@@ -43,3 +48,10 @@ func (g *GRPC) GetAccount(address string) (*models.Account, error) {
4348

4449
return &models.Account{account}, nil
4550
}
51+
52+
// InvalidAddress error for wrong account address
53+
type InvalidAddress struct{}
54+
55+
func (m *InvalidAddress) Error() string {
56+
return "Invalid Address"
57+
}

util/util.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/manifoldco/promptui"
99
"github.com/onflow/flow-go-sdk/client"
10+
"github.com/sideninja/flow-cli/gateway"
1011
"github.com/spf13/viper"
1112
"google.golang.org/grpc/codes"
1213
)
@@ -44,13 +45,20 @@ func PromptURL() {
4445
// HandleError main error handler for commands
4546
func HandleError(err error) {
4647
if err != SilentErr {
48+
// check if is address error
49+
_, isInvalidAddress := err.(*gateway.InvalidAddress)
50+
if isInvalidAddress {
51+
fmt.Printf("\n\033[31mInvalid Address: Invalid Account Address. \nPlease check Flow documentation: \n \nhttps://docs.onflow.org/cadence/language/values-and-types/#addresses\033[0m\n\n")
52+
os.Exit(1)
53+
}
54+
4755
// check if error is grpc error
48-
rpcError := client.RPCError{}
49-
if errors.As(err, &rpcError) {
56+
rpcError, isRPCError := err.(client.RPCError)
57+
if isRPCError {
5058
handleGrpcError(rpcError)
51-
} else { // unhandeled error - can be improved
52-
fmt.Printf("\n\033[31mUnhandeled Error: %s\033[0m\n\n", err)
5359
}
60+
61+
fmt.Printf("\n\033[31mUnhandeled Error: %s\033[0m\n\n", err)
5462
}
5563

5664
os.Exit(1)

0 commit comments

Comments
 (0)