Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chain not found error #3481

Merged
merged 10 commits into from
Jul 25, 2024
12 changes: 11 additions & 1 deletion packages/isc/chainid.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ func ChainIDFromBytes(data []byte) (ret ChainID, err error) {
}

func ChainIDFromString(bech32 string) (ChainID, error) {
_, addr, err := iotago.ParseBech32(bech32)
netPrefix, 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
}

Expand Down Expand Up @@ -121,6 +127,10 @@ func (id ChainID) ShortString() string {

// String human-readable form (bech32)
func (id ChainID) String() string {
if id.Empty() {
return ""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably breaks the invariant:

ChainIDFromString(chainID.String()) == chainID

Maybe it's better to panic if the chainID is "empty"?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just return the bech32 as before. Why is the check needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks, actually I reverted it. It was accidentally pushed

}

return id.AsAddress().Bech32(parameters.L1().Protocol.Bech32HRP)
}

Expand Down
8 changes: 8 additions & 0 deletions packages/isc/chainid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/iotaledger/wasp/packages/util/rwutil"
"github.com/stretchr/testify/require"
)

func TestChainIDSerialization(t *testing.T) {
Expand All @@ -12,3 +13,10 @@ func TestChainIDSerialization(t *testing.T) {
rwutil.BytesTest(t, chainID, ChainIDFromBytes)
rwutil.StringTest(t, chainID, ChainIDFromString)
}

func TestIncorrectPrefix(t *testing.T) {
chainID := "rms1prxunz807j39nmhzy3gre4hwdlzvdjyrkfn59d27x6xh426y8ajt205mh9g"
_, err := ChainIDFromString(chainID)

require.ErrorContains(t, err, "invalid network prefix: rms")
}
Loading