Skip to content

Commit

Permalink
add Remove0xPrefix() (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Nov 18, 2021
1 parent 6183a89 commit 492a27d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
21 changes: 4 additions & 17 deletions crypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/iotexproject/iotex-address/address"
"github.com/pkg/errors"

"github.com/iotexproject/go-pkgs/util"
)

// const
Expand Down Expand Up @@ -71,21 +73,9 @@ func GenerateKeySm2() (PrivateKey, error) {
return newP256sm2PrvKey()
}

func has0xPrefix(s string) bool {
if len(s) > 1 {
if s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
return true
}
}
return false
}

// HexStringToPublicKey decodes a string to PublicKey
func HexStringToPublicKey(pubKey string) (PublicKey, error) {
if has0xPrefix(pubKey) {
pubKey = pubKey[2:]
}
b, err := hex.DecodeString(pubKey)
b, err := hex.DecodeString(util.Remove0xPrefix(pubKey))
if err != nil {
return nil, errors.Wrapf(err, "failed to decode public key %s", pubKey)
}
Expand All @@ -94,10 +84,7 @@ func HexStringToPublicKey(pubKey string) (PublicKey, error) {

// HexStringToPrivateKey decodes a string to PrivateKey
func HexStringToPrivateKey(prvKey string) (PrivateKey, error) {
if has0xPrefix(prvKey) {
prvKey = prvKey[2:]
}
b, err := hex.DecodeString(prvKey)
b, err := hex.DecodeString(util.Remove0xPrefix(prvKey))
if err != nil {
return nil, errors.Wrapf(err, "failed to decode public key %s", prvKey)
}
Expand Down
6 changes: 4 additions & 2 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"encoding/hex"

"github.com/ethereum/go-ethereum/crypto"

"github.com/iotexproject/go-pkgs/util"
)

var (
Expand Down Expand Up @@ -66,7 +68,7 @@ func BytesToHash160(b []byte) Hash160 {

// HexStringToHash256 decodes the hex string, then copy byte slice into hash
func HexStringToHash256(s string) (Hash256, error) {
b, err := hex.DecodeString(s)
b, err := hex.DecodeString(util.Remove0xPrefix(s))
if err != nil {
return ZeroHash256, err
}
Expand All @@ -75,7 +77,7 @@ func HexStringToHash256(s string) (Hash256, error) {

// HexStringToHash160 decodes the hex string, then copy byte slice into hash
func HexStringToHash160(s string) (Hash160, error) {
b, err := hex.DecodeString(s)
b, err := hex.DecodeString(util.Remove0xPrefix(s))
if err != nil {
return ZeroHash160, err
}
Expand Down
4 changes: 2 additions & 2 deletions hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestHash(t *testing.T) {
msg string
hash string
}{
{"", "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},
{"abc", "4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45"},
{"", "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},
{"abc", "0X4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45"},
}

for _, test := range tests {
Expand Down
10 changes: 10 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package util

func Remove0xPrefix(s string) string {
if len(s) > 1 {
if s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
return s[2:]
}
}
return s
}

0 comments on commit 492a27d

Please sign in to comment.