-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vtransfer): extract base account from parameterized account
- Loading branch information
1 parent
b4480d8
commit 833445e
Showing
4 changed files
with
177 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// ExtractBaseAddress extracts the base address from a parameterized address. | ||
func ExtractBaseAddress(addr string) (string, error) { | ||
rawParsed, err := url.Parse(addr) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
parsed := url.URL{ | ||
Path: strings.TrimPrefix(rawParsed.Path, "/"), | ||
RawPath: rawParsed.RawPath, | ||
RawQuery: rawParsed.RawQuery, | ||
Fragment: rawParsed.Fragment, | ||
RawFragment: rawParsed.RawFragment, | ||
} | ||
if *rawParsed != parsed { | ||
return "", fmt.Errorf("address must be relative path with optional query and fragment, got %s", addr) | ||
} | ||
|
||
baseAddr, _, _ := strings.Cut(parsed.Path, "/") | ||
if baseAddr == "" { | ||
return "", fmt.Errorf("base address cannot be empty") | ||
} | ||
|
||
return baseAddr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/Agoric/agoric-sdk/golang/cosmos/x/vtransfer/types" | ||
) | ||
|
||
func TestExtractBaseAddress(t *testing.T) { | ||
bases := []struct { | ||
name string | ||
addr string | ||
}{ | ||
{"agoric address", "agoric1abcdefghiteaneas"}, | ||
{"cosmos address", "cosmos1abcdeffiharceuht"}, | ||
{"hex address", "0xabcdef198189818c93839ibia"}, | ||
} | ||
|
||
prefixes := []struct { | ||
prefix string | ||
baseIsWrong bool | ||
isErr bool | ||
}{ | ||
{"", false, false}, | ||
{"/", false, true}, | ||
{"orch:/", false, true}, | ||
{"unexpected", true, false}, | ||
{"norch:/", false, true}, | ||
{"orch:", false, true}, | ||
{"norch:", false, true}, | ||
{"\x01", false, true}, | ||
} | ||
|
||
suffixes := []struct { | ||
suffix string | ||
baseIsWrong bool | ||
isErr bool | ||
}{ | ||
{"", false, false}, | ||
{"/", false, false}, | ||
{"/sub/account", false, false}, | ||
{"?query=something&k=v&k2=v2", false, false}, | ||
{"?query=something&k=v&k2=v2#fragment", false, false}, | ||
{"unexpected", true, false}, | ||
{"\x01", false, true}, | ||
} | ||
|
||
for _, b := range bases { | ||
b := b | ||
for _, p := range prefixes { | ||
p := p | ||
for _, s := range suffixes { | ||
s := s | ||
t.Run(b.name+" "+p.prefix+" "+s.suffix, func(t *testing.T) { | ||
addr := p.prefix + b.addr + s.suffix | ||
addr, err := types.ExtractBaseAddress(addr) | ||
if p.isErr || s.isErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
if p.baseIsWrong || s.baseIsWrong { | ||
require.NotEqual(t, b.addr, addr) | ||
} else { | ||
require.Equal(t, b.addr, addr) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} |