-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Toshihiko Okubo <[email protected]>
- Loading branch information
1 parent
b6bc61b
commit 51c3295
Showing
6 changed files
with
122 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Tests | ||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: 'go.mod' | ||
go-version: '1.20' | ||
- name: test | ||
run: | | ||
go test -v ./... |
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
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,30 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
// ref https://github.com/hyperledger-labs/yui-ibc-solidity/blob/v0.3.21/contracts/apps/20-transfer/ICS20Lib.sol#L210 | ||
func ToCanonicalICS20Denom(denom string) (string, error) { | ||
if common.IsHexAddress(denom) { | ||
return strings.ToLower(denom), nil | ||
} | ||
|
||
if strings.Contains(denom, "/") { | ||
identifiers := strings.Split(denom, "/") | ||
// If a prefix exists, at least one port-id and one channel-id must be specified | ||
// ex) ${PortID}/${ChannelID}/${Denom} | ||
if len(identifiers) < 3 { | ||
return "", fmt.Errorf("invalid denom format: %s", denom) | ||
} | ||
|
||
return strings.Join( | ||
append(identifiers[0:len(identifiers)-1], strings.ToLower(identifiers[len(identifiers)-1])), | ||
"/"), nil | ||
} | ||
|
||
return "", fmt.Errorf("invalid denom format: %s", denom) | ||
} |
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,57 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestToLowerICS20Denom(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
in string | ||
want string | ||
expectedErr bool | ||
}{ | ||
{ | ||
name: "HexAddress", | ||
in: "0x4639F884305273E856dBa51AF60c10a5b5E0F482", | ||
want: "0x4639f884305273e856dba51af60c10a5b5e0f482", | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "WithPrefix", | ||
in: "Port/channel-0/0x4639F884305273E856dBa51AF60c10a5b5E0F482", | ||
want: "Port/channel-0/0x4639f884305273e856dba51af60c10a5b5e0f482", | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "WithMultiplePrefix", | ||
in: "Port-0/channel-0/Port-1/channel-1/0x4639F884305273E856dBa51AF60c10a5b5E0F482", | ||
want: "Port-0/channel-0/Port-1/channel-1/0x4639f884305273e856dba51af60c10a5b5e0f482", | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "Invalid Address Format", | ||
in: "invalid address format", | ||
expectedErr: true, | ||
}, | ||
{ | ||
name: "Invalid Prefix Format", | ||
in: "Portchannel-0/0x4639F884305273E856dBa51AF60c10a5b5E0F482", | ||
expectedErr: true, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
ret, err := ToCanonicalICS20Denom(c.in) | ||
if c.expectedErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, c.want, ret) | ||
} | ||
}) | ||
} | ||
} |