ethereum / ethereumj

DEPRECATED! Java implementation of the Ethereum yellowpaper. For JSON-RPC and other client features check Ethereum Harmony
GNU Lesser General Public License v3.0
2.18k stars 1.1k forks source link

signature error due to the toByteArray methed in ECDSASignature #952

Open zhumingu opened 6 years ago

zhumingu commented 6 years ago

`public byte[] toByteArray() { final byte fixedV = this.v >= 27 ? (byte) (this.v - 27) :this.v;

return ByteUtil.merge(
        ByteUtil.bigIntegerToBytes(this.r),
        ByteUtil.bigIntegerToBytes(this.s),
        new byte[]{fixedV});

}

public static byte[] bigIntegerToBytes(BigInteger value) { if (value == null) return null;

byte[] data = value.toByteArray();

if (data.length != 1 && data[0] == 0) {
    byte[] tmp = new byte[data.length - 1];
    System.arraycopy(data, 1, tmp, 0, tmp.length);
    data = tmp;
}
return data;

} Whendata[0] == 0`, the signature is error.

mkalinin commented 6 years ago

could you be more specific, pls? What exactly causes the error and why?

zhumingu commented 6 years ago

When I sign() tx with ECKey and toByteArray(), the result is error because of the bigIntegerToBytes function.

public static byte[] bigIntegerToBytes(BigInteger value) {
        if (value == null)
            return null;

        byte[] data = value.toByteArray();

        if (data.length != 1 && data[0] == 0) {  // when the data[0] == 0, the result is error
            byte[] tmp = new byte[data.length - 1];
            System.arraycopy(data, 1, tmp, 0, tmp.length);
            data = tmp;
        }
        return data;
}

That's the way I use it.

mkalinin commented 6 years ago

would it be possible to share the pKey and the data you're trying to sign?