github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/subkey/hex.go (about)

     1  package subkey
     2  
     3  import (
     4  	"encoding/hex"
     5  	"strings"
     6  )
     7  
     8  // DecodeHex decodes the hex string to bytes.
     9  // `0x` prefix is accepted.
    10  func DecodeHex(uri string) ([]byte, bool) {
    11  	uri = strings.TrimPrefix(uri, "0x")
    12  	res, err := hex.DecodeString(uri)
    13  	return res, err == nil
    14  }
    15  
    16  // EncodeHex encodes bytes to hex
    17  // `0x` prefix is added.
    18  func EncodeHex(b []byte) string {
    19  	res := hex.EncodeToString(b)
    20  	if !strings.HasPrefix(res, "0x") {
    21  		res = "0x" + res
    22  	}
    23  
    24  	return res
    25  }