-
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): URN address parameters (relative to
orch:/
)
- Loading branch information
1 parent
823c8d1
commit 9e1f37e
Showing
4 changed files
with
185 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,41 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
const ParameterizedAddressScheme = "orch" | ||
|
||
// ExtractBaseAddress extracts the base address from a parameterized address | ||
// encoded as a URN. | ||
func ExtractBaseAddressFromURN(relativeURN, scheme string) (string, error) { | ||
relativeTo := url.URL{Scheme: scheme} | ||
|
||
u, err := relativeTo.Parse(relativeURN) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if u.Scheme != scheme { | ||
return "", fmt.Errorf("unsupported address scheme %s", u.Scheme) | ||
} | ||
|
||
splits := strings.SplitN(u.Path, "/", 3) | ||
if len(splits) < 2 || splits[0] != "" { | ||
return "", fmt.Errorf("base address path must have at least one leading slash, not %s", u.Path) | ||
} | ||
|
||
base := splits[1] | ||
if len(base) == 0 { | ||
return "", fmt.Errorf("base address cannot be empty") | ||
} | ||
|
||
return base, nil | ||
} | ||
|
||
// ExtractBaseAddress extracts the base address from a parameterized address. | ||
func ExtractBaseAddress(fullAddr string) (string, error) { | ||
return ExtractBaseAddressFromURN(fullAddr, ParameterizedAddressScheme) | ||
} |
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, false}, | ||
{"orch:/", false, false}, | ||
{"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) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} |