Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check canonical denom #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
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 ./...
9 changes: 8 additions & 1 deletion cmd/ics20/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"log"
"math/big"

"github.com/datachainlab/ethereum-ics20-cli/chains/geth"
"github.com/ethereum/go-ethereum/common"
"github.com/hyperledger-labs/yui-ibc-solidity/pkg/contract/erc20"
"github.com/hyperledger-labs/yui-ibc-solidity/pkg/contract/ics20bank"
"github.com/hyperledger-labs/yui-ibc-solidity/pkg/contract/ics20transferbank"
"github.com/spf13/cobra"

"github.com/datachainlab/ethereum-ics20-cli/chains/geth"
"github.com/datachainlab/ethereum-ics20-cli/util"
)

func transferCmd() *cobra.Command {
Expand Down Expand Up @@ -79,6 +81,11 @@ func transfer(ctx context.Context, rpcAddress string, mnemonic, ics20BankAddress
return err
}

denom, err = util.ToCanonicalICS20Denom(denom)
if err != nil {
return err
}

if common.IsHexAddress(denom) {
tx, err := erc20Token.Approve(chain.TxOpts(ctx, fromIndex), common.HexToAddress(ics20BankAddress), big.NewInt(amount))
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/ethereum/go-ethereum v1.11.6
github.com/hyperledger-labs/yui-ibc-solidity v0.3.17
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
)

require (
Expand All @@ -20,6 +21,7 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand All @@ -29,6 +31,7 @@ require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand All @@ -38,4 +41,5 @@ require (
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/sys v0.10.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI=
github.com/deckarep/golang-set/v2 v2.1.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0=
Expand Down Expand Up @@ -91,6 +92,7 @@ github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM=
Expand All @@ -109,6 +111,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/status-im/keycard-go v0.2.0 h1:QDLFswOQu1r5jsycloeQh3bVU8n/NatHHaZobtDnDzA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d h1:vfofYNRScrDdvS342BElfbETmL1Aiz3i2t0zfRj16Hs=
github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw=
github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk=
Expand Down Expand Up @@ -141,6 +144,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af h1:Yx9k8YCG3dvF87UAn2tu2HQLf2dt/eR1bXxpLMWeH+Y=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
Expand Down
30 changes: 30 additions & 0 deletions util/util.go
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])),
Copy link
Collaborator

@siburu siburu Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@toshihiko-okubo You need to check if the last element is in hex format.

Plus, I think that IsHexAddress is not enough.
ibc-solidity uses 0x as prefix, but IsHexAddress also accepts 0X.

"/"), nil
}

return "", fmt.Errorf("invalid denom format: %s", denom)
}
57 changes: 57 additions & 0 deletions util/util_test.go
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)
}
})
}
}
Loading