bc-java icon indicating copy to clipboard operation
bc-java copied to clipboard

invalid characters encountered in Hex string while decoding SolidityType string

Open aayushga opened this issue 2 years ago • 1 comments

I am trying to parse a RawTransaction Data using : https://github.com/CoboVault/cobo-vault-cold/blob/master/coinlib/src/main/java/com/cobo/coinlib/coins/ETH/SolidityType.java#L303

But when I try to pass 08414243445f454647 - it throws "org.bouncycastle.util.encoders.DecoderException: exception decoding Hex string: invalid characters encountered in Hex string". This should evaluate to "ABCD_EFG"

It's not able to understand 08 as length of the string. If I pass 00 instead of 08 at the beginning, it's able to parse but gives empty string.

I tried with following versions:

    <dependency>
       <groupId>com.madgag.spongycastle</groupId>
       <artifactId>core</artifactId>
       <version>1.58.0.0</version>
    </dependency>

and

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.68</version>
    </dependency>

Please let me know if you need any further details

aayushga avatar Aug 01 '22 03:08 aayushga

The line that you point to in StringType reads

return new String(Hex.decode(super.decode(encoded, offset)), StandardCharsets.UTF_8);

The underlying decode method in class BytesType has as its return statement

return "0x"+Hex.toHexString(Arrays.copyOfRange(encoded, offset, offset + len));

and there you have your problem.

The characters "0x" are being prefixed to the hex string that is passed to Hex.decode()

the letter x is not a valid hex character and is therefore rejected.

tonywasher avatar Aug 12 '22 07:08 tonywasher

Regarded as dealt with.

dghgit avatar Sep 25 '22 01:09 dghgit