-
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): account address parameters
- Loading branch information
1 parent
b4480d8
commit 82be26d
Showing
4 changed files
with
187 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,43 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// ExtractBaseAddress extracts the base address from a parameterized address | ||
// encoded as a relative URI. | ||
func ExtractBaseAddress(relativeURI string) (string, error) { | ||
u, err := url.Parse(relativeURI) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if u.Scheme != "" { | ||
return "", fmt.Errorf("unsupported address scheme %s", u.Scheme) | ||
} | ||
|
||
if u.Opaque != "" { | ||
return "", fmt.Errorf("unsupported address opaque %s", u.Opaque) | ||
} | ||
|
||
if u.User != nil { | ||
return "", fmt.Errorf("unsupported address user %q", u.User) | ||
} | ||
|
||
if u.Host != "" { | ||
return "", fmt.Errorf("unsupported address host %s", u.Host) | ||
} | ||
|
||
if strings.HasPrefix(u.Path, "/") { | ||
return "", fmt.Errorf("address path must be relative, got %s", u.Path) | ||
} | ||
|
||
baseAddr, _, _ := strings.Cut(u.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) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} |