-
Notifications
You must be signed in to change notification settings - Fork 214
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 address from parameterized address
- Loading branch information
1 parent
d122756
commit 2edf161
Showing
4 changed files
with
190 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,46 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// OptionalAddressScheme is the scheme that can be prefixed on an address. | ||
const OptionalAddressScheme = "orch" | ||
|
||
// 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{ | ||
Scheme: rawParsed.Scheme, | ||
Opaque: strings.TrimPrefix(rawParsed.Opaque, "/"), | ||
Path: strings.TrimPrefix(rawParsed.Path, "/"), | ||
RawPath: strings.TrimPrefix(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) | ||
} | ||
|
||
path := parsed.Path | ||
if parsed.Scheme == OptionalAddressScheme { | ||
path = parsed.Opaque | ||
} else if parsed.Scheme != "" { | ||
return "", fmt.Errorf("address scheme must be empty or %s, got %s", OptionalAddressScheme, parsed.Scheme) | ||
} | ||
|
||
baseAddr, _, _ := strings.Cut(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, false}, | ||
{"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) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} |