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.
Improvement
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.
E.g:
123 % 10 = 3
3 + 0x30 = 0x33 (ASCII of “3”)
Here is my implementation without place holder:
Result of truffle test
:
The result and whole code could be found here:
Conclusion
- My implementation cost
5846 Gas
, RicMoo’s implementation cost6440 Gas
- New feature to verify serialized signature cost
6160 Gas
- In theory, It could verify message with length equal to 10³²-1
Also, Read
- The Best Crypto Trading Bot
- Crypto Copy Trading Platforms
- The Best Crypto Tax Software
- Best Crypto Trading Platforms
- Best Crypto Lending Platforms
- Best Blockchain Analysis Tools
- Crypto arbitrage guide: How to make money as a beginner
- Best Crypto Charting Tool
- Ledger vs Trezor
- What are the best books to learn about Bitcoin?
- 3Commas Review
- AAX Exchange Review | Referral Code, Trading Fee, Pros and Cons
- Deribit Review | Options, Fees, APIs and Testnet
- FTX Crypto Exchange Review
- NGRAVE ZERO review
- Bybit Exchange Review
- 3Commas vs Cryptohopper
- The Best Bitcoin Hardware wallet
- Best monero wallet
- ledger nano s vs x
- Bitsgap vs 3Commas vs Quadency
- Ledger Nano S vs Trezor one vs Trezor T vs Ledger Nano X
- BlockFi vs Celsius vs Hodlnaut
- Bitsgap review — A Crypto Trading Bot That Makes Easy Money
- Quadency Review- A Crypto Trading Bot Made For Professionals
- PrimeXBT Review | Leverage Trading, Fee and Covesting
- Ellipal Titan Review
- SecuX Stone Review
- BlockFi Review | Earn up to 8.6% interests on your Crypto