Skip to content

Commit

Permalink
change address prefix to shentu
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-yuhh committed Jul 5, 2023
1 parent 704cdaa commit 725de9b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
40 changes: 39 additions & 1 deletion common/prefix.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package common

import (
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
)

const (
// Bech32MainPrefix is the common prefix of all prefixes.
Bech32MainPrefix = "certik"
Bech32MainPrefix = "shentu"

// Bech32PrefixAccAddr is the prefix of account addresses.
Bech32PrefixAccAddr = Bech32MainPrefix
Expand All @@ -21,3 +25,37 @@ const (
// Bech32PrefixConsPub is the prefix of consensus node public keys.
Bech32PrefixConsPub = Bech32MainPrefix + sdk.PrefixValidator + sdk.PrefixConsensus + sdk.PrefixPublic
)

// PrefixToCertik convert shentu prefix address to certik prefix address
func PrefixToCertik(address string) (string, error) {
hrp, data, err := bech32.DecodeAndConvert(address)
if err != nil {
return "", err
}

Check warning on line 34 in common/prefix.go

View check run for this annotation

Codecov / codecov/patch

common/prefix.go#L33-L34

Added lines #L33 - L34 were not covered by tests
if !strings.HasPrefix(hrp, "shentu") {
return "", fmt.Errorf("invalid address:%s", address)
}

certikAddr, err := bech32.ConvertAndEncode("certik", data)
if err != nil {
return "", err
}

Check warning on line 42 in common/prefix.go

View check run for this annotation

Codecov / codecov/patch

common/prefix.go#L41-L42

Added lines #L41 - L42 were not covered by tests
return certikAddr, nil
}

// PrefixToShentu convert certik prefix address to shentu prefix address
func PrefixToShentu(address string) (string, error) {
hrp, data, err := bech32.DecodeAndConvert(address)
if err != nil {
return "", err
}

Check warning on line 51 in common/prefix.go

View check run for this annotation

Codecov / codecov/patch

common/prefix.go#L50-L51

Added lines #L50 - L51 were not covered by tests
if !strings.HasPrefix(hrp, "certik") {
return "", fmt.Errorf("invalid address:%s", address)
}

shentuAddr, err := bech32.ConvertAndEncode("shentu", data)
if err != nil {
return "", err
}

Check warning on line 59 in common/prefix.go

View check run for this annotation

Codecov / codecov/patch

common/prefix.go#L58-L59

Added lines #L58 - L59 were not covered by tests
return shentuAddr, nil
}
44 changes: 44 additions & 0 deletions common/prefix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package common

import (
"crypto/sha256"
"github.com/stretchr/testify/require"
"strings"
"testing"

"github.com/cosmos/cosmos-sdk/types/bech32"
)

func TestPrefixToCertik(t *testing.T) {
sum := sha256.Sum256([]byte("hello world\n"))
ss := "shentu"

address, err := bech32.ConvertAndEncode(ss, sum[:])
require.NoError(t, err)

certikAddr, err := PrefixToCertik(address)
require.NoError(t, err)
require.True(t, strings.HasPrefix(certikAddr, "certik"))

address, err = bech32.ConvertAndEncode("certik", sum[:])
require.NoError(t, err)
_, err = PrefixToCertik(address)
require.Error(t, err)
}

func TestPrefixToShentu(t *testing.T) {
sum := sha256.Sum256([]byte("hello world\n"))
ss := "certik"

address, err := bech32.ConvertAndEncode(ss, sum[:])
require.NoError(t, err)

shentuAddr, err := PrefixToShentu(address)
require.NoError(t, err)
require.True(t, strings.HasPrefix(shentuAddr, "shentu"))

address, err = bech32.ConvertAndEncode("shentu", sum[:])
require.NoError(t, err)
_, err = PrefixToShentu(address)
require.Error(t, err)
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ require (
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.0.0-20210204194344-59a8610d2b87 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
Expand Down Expand Up @@ -304,3 +304,5 @@ require (
replace google.golang.org/grpc => google.golang.org/grpc v1.33.2

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.2-alpha.regen.4

replace github.com/cosmos/cosmos-sdk => github.com/shentufoundation/cosmos-sdk v0.45.11-1
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ github.com/cosmos/btcutil v1.0.4 h1:n7C2ngKXo7UC9gNyMNLbzqz7Asuf+7Qv4gnX/rOdQ44=
github.com/cosmos/btcutil v1.0.4/go.mod h1:Ffqc8Hn6TJUdDgHBwIZLtrLQC1KdJ9jGJl/TvgUaxbU=
github.com/cosmos/cosmos-proto v1.0.0-beta.3 h1:VitvZ1lPORTVxkmF2fAp3IiA61xVwArQYKXTdEcpW6o=
github.com/cosmos/cosmos-proto v1.0.0-beta.3/go.mod h1:t8IASdLaAq+bbHbjq4p960BvcTqtwuAxid3b/2rOD6I=
github.com/cosmos/cosmos-sdk v0.45.11 h1:Pc44fFEkai0KXFND5Ys/2ZJkfVdstMIBzKBN8MY7Ll0=
github.com/cosmos/cosmos-sdk v0.45.11/go.mod h1:45z8Q1Ah4iypFycu2Kl4kBPIsQKUiND8G2CUX+HTtPM=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
Expand Down Expand Up @@ -631,8 +629,9 @@ github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
Expand Down Expand Up @@ -1056,6 +1055,8 @@ github.com/securego/gosec/v2 v2.13.1/go.mod h1:EO1sImBMBWFjOTFzMWfTRrZW6M15gm60l
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c h1:W65qqJCIOVP4jpqPQ0YvHYKwcMEMVWIzWC5iNQQfBTU=
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c/go.mod h1:/PevMnwAxekIXwN8qQyfc5gl2NlkB3CQlkizAbOkeBs=
github.com/shentufoundation/cosmos-sdk v0.45.11-1 h1:qAK+5ymbfuCdnaVikZSYRpTV+HHtVupHPf1WuNpD/2E=
github.com/shentufoundation/cosmos-sdk v0.45.11-1/go.mod h1:45z8Q1Ah4iypFycu2Kl4kBPIsQKUiND8G2CUX+HTtPM=
github.com/shirou/gopsutil v2.20.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
Expand Down

0 comments on commit 725de9b

Please sign in to comment.