Published in


Ethereum: Improving message verification in Solidity

I read this article, Signatures: Verifying Messages in Solidity. It’s such a good article but this code made me uncomfortable. It’s too wasting and dirty then I implement mine so you could do compare.

Message signing in Ethereum

Almost messages on Ethereum will be sign with a given format:

"\x19Ethereum Signed Message:\n" + message.length + messageE.g: “\x19Ethereum Signed Message:\n16Hello, I’m Chiro”

This prefix "\x19Ethereum Signed Message:\n" was used to make sure signed message won't be "reused" somewhere else.


As you see, the only thing that prevent us to archive onchain verification is message.length. You need to encode a number to string, here are RicMoo’s code:

The basic concept could be simplify:

function uintToStr(value) {
let result = [];
for (let i = value; i > 0; i = (i / 10) >>> 0) {
result.push(String.fromCharCode((i % 10) + 0x30));
return result.reverse().join('');

Radix is 10, so we need modulo for 10 to get the digit and add the result with 0x30 to get its ASCII code.

123 % 10 = 3
3 + 0x30 = 0x33 (ASCII of “3”)

Here is my implementation without place holder:

Result of truffle test:

Test result

The result and whole code could be found here:


  • My implementation cost 5846 Gas, RicMoo’s implementation cost 6440 Gas
  • New feature to verify serialized signature cost 6160 Gas
  • In theory, It could verify message with length equal to 10³²-1

Also, Read



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store