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

Fix/QB-1659: Bech32 sds p2pPubKey correction #223

Merged
merged 1 commit into from
Feb 21, 2023
Merged
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
28 changes: 6 additions & 22 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/codec/legacy"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -46,33 +46,17 @@ var _ yaml.Marshaler = SdsAddress{}

type SdsAddress []byte

// SdsPubKeyFromBech32 returns a SdsPublicKey from a Bech32 string.
// SdsPubKeyFromBech32 returns an ed25519 SdsPublicKey from a Bech32 string.
func SdsPubKeyFromBech32(pubkeyStr string) (cryptotypes.PubKey, error) {
bech32Prefix := GetConfig().GetBech32SdsNodeP2PPubPrefix()
bz, err := sdk.GetFromBech32(pubkeyStr, bech32Prefix)
_, sdsPubKeyBytes, err := bech32.DecodeAndConvert(pubkeyStr)
if err != nil {
return nil, err
}

pk, err := legacy.PubKeyFromBytes(bz)
if err != nil {
return nil, err
}

return pk, nil
}

// SdsPubKeyFromBytes returns a SdsPublicKey from a byte array.
func SdsPubKeyFromByteArr(bytes []byte) (cryptotypes.PubKey, error) {
bech32PubPrefix := GetConfig().GetBech32SdsNodeP2PPubPrefix()
pubStr, err := sdk.Bech32ifyAddressBytes(bech32PubPrefix, bytes)
if err != nil {
return nil, err
}
return SdsPubKeyFromBech32(pubStr)
pubKey := ed25519.PubKey{Key: sdsPubKeyBytes}
return &pubKey, nil
}

// SdsPubKeyFromBech32 convert a SdsPublicKey to a Bech32 string.
// SdsPubKeyToBech32 convert a SdsPublicKey to a Bech32 string.
func SdsPubKeyToBech32(pubkey cryptotypes.PubKey) (string, error) {
bech32PubPrefix := GetConfig().GetBech32SdsNodeP2PPubPrefix()
bech32Pub, err := bech32.ConvertAndEncode(bech32PubPrefix, pubkey.Bytes())
Expand Down
75 changes: 75 additions & 0 deletions types/address_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,84 @@
package types

import (
"encoding/base64"
"testing"

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

"github.com/stratosnet/stratos-chain/app"

"github.com/stretchr/testify/require"
)

func initCodec() codec.Codec {
encodingConfig := app.MakeTestEncodingConfig()
cdc := encodingConfig.Marshaler
return cdc
}

// go test -v address_test.go address.go config.go coin.go -run TestSdsPubKeyToBech32
func TestSdsPubKeyToBech32(t *testing.T) {
tests := []struct {
name string
pubkeyJson string
expectedBech32 string
wantErr bool
}{
{"test1", "{\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"2OAeLO0+KrBkSxuFKU1ofJqGb4RtA8GpD8XCZlMYw2A=\"}",
"stsdspub1mrsput8d8c4tqeztrwzjjntg0jdgvmuyd5pur2g0chpxv5cccdsqvayhan", false},
}
cdc := initCodec()
cfg := GetConfig()
cfg.Seal()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

var pubKey cryptotypes.PubKey
err := cdc.UnmarshalInterfaceJSON([]byte(tt.pubkeyJson), &pubKey)
if (err != nil) != tt.wantErr {
t.Errorf(err.Error())
}

bech32Str, err := SdsPubKeyToBech32(pubKey)
if (err != nil) != tt.wantErr {
t.Errorf(err.Error())
}

require.Equal(t, bech32Str, tt.expectedBech32)
})
}
}

// go test -v address_test.go address.go config.go coin.go -run TestSdsPubKeyFromBech32
func TestSdsPubKeyFromBech32(t *testing.T) {
tests := []struct {
name string
bech32PubKey string
expectedBase64 string
wantErr bool
}{
{"test1", "stsdspub1mrsput8d8c4tqeztrwzjjntg0jdgvmuyd5pur2g0chpxv5cccdsqvayhan",
"2OAeLO0+KrBkSxuFKU1ofJqGb4RtA8GpD8XCZlMYw2A=", false},
}

cfg := GetConfig()
cfg.Seal()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pubKey, err := SdsPubKeyFromBech32(tt.bech32PubKey)
if (err != nil) != tt.wantErr {
t.Errorf(err.Error())
}
base64Str := base64.StdEncoding.EncodeToString(pubKey.Bytes())
require.Equal(t, base64Str, tt.expectedBase64)
})
}

}

func TestSdsAddress_Unmarshal(t *testing.T) {

tests := []struct {
Expand Down
73 changes: 0 additions & 73 deletions types/bech32/legacybech32/pk.go

This file was deleted.

6 changes: 5 additions & 1 deletion x/register/types/meta_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

stratos "github.com/stratosnet/stratos-chain/types"
)

Expand Down Expand Up @@ -37,7 +38,10 @@ func (v MetaNode) ConvertToString() string {
if err != nil {
return ErrUnknownPubKey.Error()
}
pubKey, err := stratos.SdsPubKeyFromBech32(pkAny.String())
cachedPubkey := pkAny.GetCachedValue()
pk := cachedPubkey.(cryptotypes.PubKey)

pubKey, err := stratos.SdsPubKeyToBech32(pk)
if err != nil {
return ErrUnknownPubKey.Error()
}
Expand Down
5 changes: 4 additions & 1 deletion x/register/types/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ func (v ResourceNode) ConvertToString() string {
if err != nil {
return ErrUnknownPubKey.Error()
}
pubKey, err := stratos.SdsPubKeyFromBech32(pkAny.String())
cachedPubkey := pkAny.GetCachedValue()
pk := cachedPubkey.(cryptotypes.PubKey)

pubKey, err := stratos.SdsPubKeyToBech32(pk)
if err != nil {
return ErrUnknownPubKey.Error()
}
Expand Down