diff --git a/core/check/ifHrp.go b/core/check/ifHrp.go index 37acffeb6..4fc9c2077 100644 --- a/core/check/ifHrp.go +++ b/core/check/ifHrp.go @@ -1,12 +1,15 @@ package check -import ( - "regexp" -) - // IfHrp tests if the provided string is human readable - does contain only alphabetic characters func IfHrp(s string) bool { - isHrp := regexp.MustCompile(`^[a-zA-Z]+$`).MatchString(s) + if s == "" { + return false + } - return isHrp + for _, r := range s { + if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') { + return false + } + } + return true } diff --git a/core/pubkeyConverter/bech32PubkeyConverter_test.go b/core/pubkeyConverter/bech32PubkeyConverter_test.go index 34453afef..97494534d 100644 --- a/core/pubkeyConverter/bech32PubkeyConverter_test.go +++ b/core/pubkeyConverter/bech32PubkeyConverter_test.go @@ -9,6 +9,7 @@ import ( "github.com/ElrondNetwork/elrond-go-core/core" "github.com/ElrondNetwork/elrond-go-core/core/check" + "github.com/ElrondNetwork/elrond-go-core/core/mock" "github.com/ElrondNetwork/elrond-go-core/core/pubkeyConverter" "github.com/stretchr/testify/assert" ) @@ -76,6 +77,30 @@ func TestBech32PubkeyConverter_DecodeWrongSizeShouldErr(t *testing.T) { assert.True(t, errors.Is(err, pubkeyConverter.ErrWrongSize)) } +func TestBech32PubkeyConverter_SilentEncodeShouldWork(t *testing.T) { + t.Parallel() + + addressLen := 32 + bpc, _ := pubkeyConverter.NewBech32PubkeyConverter(addressLen, core.DefaultAddressPrefix) + + buff := []byte("12345678901234567890123456789012") + str := bpc.SilentEncode(buff, &mock.LoggerMock{}) + + assert.Equal(t, 0, strings.Index(str, pubkeyConverter.Prefix)) +} + +func TestBech32PubkeyConverter_SilentEncodeShouldNotWork(t *testing.T) { + t.Parallel() + + addressLen := 32 + bpc, _ := pubkeyConverter.NewBech32PubkeyConverter(addressLen, core.DefaultAddressPrefix) + + buff := []byte("1234") + str := bpc.SilentEncode(buff, &mock.LoggerMock{}) + + assert.Equal(t, "", str) +} + func TestBech32PubkeyConverter_EncodeDecodeShouldWork(t *testing.T) { t.Parallel() diff --git a/core/pubkeyConverter/hexPubkeyConverter_test.go b/core/pubkeyConverter/hexPubkeyConverter_test.go index f5d8617ae..2d30eb7ba 100644 --- a/core/pubkeyConverter/hexPubkeyConverter_test.go +++ b/core/pubkeyConverter/hexPubkeyConverter_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/ElrondNetwork/elrond-go-core/core/check" + "github.com/ElrondNetwork/elrond-go-core/core/mock" "github.com/ElrondNetwork/elrond-go-core/core/pubkeyConverter" "github.com/stretchr/testify/assert" ) @@ -69,7 +70,18 @@ func TestHexPubkeyConverter_EncodeShouldWork(t *testing.T) { addressLen := 4 hpc, _ := pubkeyConverter.NewHexPubkeyConverter(addressLen) - str, _ := hpc.Encode([]byte{170, 255}) + str, err := hpc.Encode([]byte{170, 255}) + assert.Nil(t, err) + assert.Equal(t, "aaff", str) +} + +func TestHexPubkeyConverter_SilentEncodeShouldWork(t *testing.T) { + t.Parallel() + + addressLen := 4 + hpc, _ := pubkeyConverter.NewHexPubkeyConverter(addressLen) + + str := hpc.SilentEncode([]byte{170, 255}, &mock.LoggerMock{}) assert.Equal(t, "aaff", str) }