diff --git a/packages/chains/chains.go b/packages/chains/chains.go index fc11db7ee7..3bbfa85a79 100644 --- a/packages/chains/chains.go +++ b/packages/chains/chains.go @@ -478,7 +478,7 @@ func (c *Chains) Get(chainID isc.ChainID) (chain.Chain, error) { ret, exists := c.allChains.Get(chainID) if !exists { - return nil, interfaces.NewChainNotFoundError(chainID) + return nil, interfaces.ErrChainNotFound } return ret.chain, nil } diff --git a/packages/isc/chainid.go b/packages/isc/chainid.go index c769f40175..b3805782a4 100644 --- a/packages/isc/chainid.go +++ b/packages/isc/chainid.go @@ -45,19 +45,13 @@ func ChainIDFromBytes(data []byte) (ret ChainID, err error) { } func ChainIDFromString(bech32 string) (ChainID, error) { - netPrefix, addr, err := iotago.ParseBech32(bech32) + _, addr, err := iotago.ParseBech32(bech32) if err != nil { return ChainID{}, err } if addr.Type() != iotago.AddressAlias { return ChainID{}, fmt.Errorf("chainID must be an alias address (%s)", bech32) } - - expectedNetPrefix := parameters.L1().Protocol.Bech32HRP - if netPrefix != expectedNetPrefix { - return ChainID{}, fmt.Errorf("invalid network prefix: %s", netPrefix) - } - return ChainIDFromAddress(addr.(*iotago.AliasAddress)), nil } diff --git a/packages/webapi/controllers/chain/chain.go b/packages/webapi/controllers/chain/chain.go index 1325d4a7fb..b01f7f967a 100644 --- a/packages/webapi/controllers/chain/chain.go +++ b/packages/webapi/controllers/chain/chain.go @@ -65,7 +65,7 @@ func (c *Controller) getChainInfo(e echo.Context) error { } chainInfo, err := c.chainService.GetChainInfoByChainID(chainID, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) - if errors.As(err, &interfaces.ChainNotFoundError{}) { + if errors.Is(err, interfaces.ErrChainNotFound) { return e.NoContent(http.StatusNotFound) } else if err != nil { return err @@ -97,7 +97,7 @@ func (c *Controller) getChainList(e echo.Context) error { chainInfo, err := c.chainService.GetChainInfoByChainID(chainID, "") c.log.Infof("getchaininfo %v", err) - if errors.As(err, &interfaces.ChainNotFoundError{}) { + if errors.Is(err, interfaces.ErrChainNotFound) { // TODO: Validate this logic here. Is it possible to still get more chain info? chainList = append(chainList, models.ChainInfoResponse{ IsActive: false, diff --git a/packages/webapi/controllers/corecontracts/controller.go b/packages/webapi/controllers/corecontracts/controller.go index 8f96f2229e..936bc4393b 100644 --- a/packages/webapi/controllers/corecontracts/controller.go +++ b/packages/webapi/controllers/corecontracts/controller.go @@ -27,11 +27,9 @@ func (c *Controller) Name() string { } func (c *Controller) handleViewCallError(err error, chainID isc.ChainID) error { - var chainNotFound interfaces.ChainNotFoundError - if errors.As(err, &chainNotFound) { - return apierrors.ChainNotFoundError(chainNotFound.ChainID.String()) + if errors.Is(err, interfaces.ErrChainNotFound) { + return apierrors.ChainNotFoundError(chainID.String()) } - return apierrors.ContractExecutionError(err) } diff --git a/packages/webapi/interfaces/errors.go b/packages/webapi/interfaces/errors.go deleted file mode 100644 index c136acf3cc..0000000000 --- a/packages/webapi/interfaces/errors.go +++ /dev/null @@ -1,31 +0,0 @@ -package interfaces - -import ( - "errors" - - "github.com/iotaledger/wasp/packages/isc" -) - -var ( - ErrCantDeleteLastUser = errors.New("you can't delete the last user") -) - -func NewChainNotFoundError(chainID isc.ChainID) error { - return ChainNotFoundError{ - ChainID: chainID, - } -} - -type ChainNotFoundError struct { - ChainID isc.ChainID -} - -func (e ChainNotFoundError) Error() string { - errStr := "Chain not found" - - if !e.ChainID.Empty() { - errStr = errStr + ": " + e.ChainID.String() - } - - return errStr -} diff --git a/packages/webapi/interfaces/interfaces.go b/packages/webapi/interfaces/interfaces.go index b5b6a3f965..ef7f5f2335 100644 --- a/packages/webapi/interfaces/interfaces.go +++ b/packages/webapi/interfaces/interfaces.go @@ -17,6 +17,11 @@ import ( "github.com/iotaledger/wasp/packages/webapi/models" ) +var ( + ErrChainNotFound = errors.New("chain not found") + ErrCantDeleteLastUser = errors.New("you can't delete the last user") +) + type APIController interface { Name() string RegisterPublic(publicAPI echoswagger.ApiGroup, mocker Mocker) diff --git a/packages/webapi/services/chain.go b/packages/webapi/services/chain.go index 3d8c83995e..c8b4d2b774 100644 --- a/packages/webapi/services/chain.go +++ b/packages/webapi/services/chain.go @@ -163,7 +163,7 @@ func (c *ChainService) GetChainInfoByChainID(chainID isc.ChainID, blockIndexOrTr governanceChainInfo, err := corecontracts.GetChainInfo(ch, blockIndexOrTrieRoot) if err != nil { - if chainRecord != nil && errors.As(err, &interfaces.ChainNotFoundError{}) { + if chainRecord != nil && errors.Is(err, interfaces.ErrChainNotFound) { return &dto.ChainInfo{ChainID: chainID, IsActive: false}, nil }