komputing / KEthereum

Kotlin library for Ethereum
MIT License
344 stars 72 forks source link

signMessage produces incorrect output #150

Closed krisbitney closed 1 year ago

krisbitney commented 1 year ago

There seems to be a bug somewhere in signMessage. I am trying to replicate the output from libraries in a few other languages and I'm getting a different result.

Expected: 0xa4708243bf782c6769ed04d83e7192dbcf4fc131aa54fde9d889d8633ae39dab03d7babd2392982dff6bc20177f7d887e27e50848c851320ee89c6c63d18ca761c

Received: 0x6b7d68156f3ca0c0f6b5b603922dd41d62097d5131656c5eb46fd1c71731a45447f964bbfe462f5a3e9ed56ec0f157cd011ab08b8cfa1e129e30ced0bb3486191c

Provider: "https://bsc-dataseed1.binance.org/" Private Key: "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d" Message: "Hello World".encodeToByteArray()

Code:

private fun locallySignMessage(message: ByteArray, signer: ECKeyPair): String {
    return "0x" + signer.signMessage(message).toHex()
}

Test: https://github.com/polywrap/ethereum-wallet/blob/a7dd7132ba2dac6938734fe547a375413c68204b/implementations/kt/src/test/kotlin/io/polywrap/plugins/ethereum/EthereumWalletPluginTest.kt#L64

krisbitney commented 1 year ago

This is resolved. It was my mistake. I wasn't adding the prefix!

private fun locallySignMessage(message: ByteArray, signer: ECKeyPair): String {
    val len = message.size.toString().encodeToByteArray()
    return "0x" + signer.signMessage(MESSAGE_PREFIX + len + message).toHex()
}
companion object {
    val MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n".encodeToByteArray()
}