// implementation referenced from https://github.com/ChainSafe/web3.js/blob/edcd215bf657a4bba62fabaafd08e6e70040976e/packages/web3-utils/src/utils.js#L165
funcUtf8ToHex(strstring)(string,error){
str,err:=Utf8encode(str)
iferr!=nil{
return"",err
}
str=removeUnicodeZeroPadding(str)
varhex=""
for_,r:=rangestr{
n:=fmt.Sprintf("%x",r)
iflen(n)<2{
hex+="0"+n
}else{
hex+=n
}
}
return"0x"+hex,nil
}
// implementation referenced from https://github.com/ChainSafe/web3.js/blob/edcd215bf657a4bba62fabaafd08e6e70040976e/packages/web3-utils/src/utils.js#L193
funcHexToUtf8(hexStringstring)(string,error){
hexString=removeHexPrefix(hexString)
if!hexStringPattern.MatchString(hexString){
return"",fmt.Errorf("the parameter '%s' must be a valid HEX string",hexString)
}
iflen(hexString)%2!=0{
return"",fmt.Errorf("the parameter '%s' must have a even number of digits",hexString)
}
hexString=removeHexZeroPadding(hexString)
n:=big.NewInt(0)
varbytes[]byte
fori:=0;i<len(hexString);i+=2{
hex:=hexString[i:i+2]
n,success:=n.SetString(hex,16)
if!success{
return"",fmt.Errorf("invalid hex value %s",hex)
}
r:=rune(n.Int64())
bs:=stringFromCharCode(r)
bytes=appendBytes(bytes,bs)
}
bytes,err:=Utf8decode(string(bytes))
iferr!=nil{
return"",err
}
returnstring(bytes),nil
}
funcremoveHexPrefix(strstring)string{
found:=hexPrefixPattern.FindString(str)
returnstrings.Replace(str,found,"",1)
}
// implementation referenced from https://github.com/ChainSafe/web3.js/blob/edcd215bf657a4bba62fabaafd08e6e70040976e/packages/web3-utils/src/utils.js#L107
// implementation referenced from https://github.com/ChainSafe/web3.js/blob/edcd215bf657a4bba62fabaafd08e6e70040976e/packages/web3-utils/src/utils.js#L85
funcIsAddress(addressstring)(bool,error){
// check if it has the basic requirements of an address
// implementation referenced from https://github.com/ChainSafe/web3.js/blob/2022b17d52d31ce95559d18d5530d18c83eb4d1c/packages/web3-utils/src/index.js#L283