Skip to content

Commit

Permalink
fix after review
Browse files Browse the repository at this point in the history
  • Loading branch information
schimih committed Dec 7, 2022
1 parent fe06bbb commit 8f4ba22
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
15 changes: 9 additions & 6 deletions core/check/ifHrp.go
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions core/pubkeyConverter/bech32PubkeyConverter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()

Expand Down
14 changes: 13 additions & 1 deletion core/pubkeyConverter/hexPubkeyConverter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 8f4ba22

Please sign in to comment.