【EIP】ERC55 混合大小寫校驗和地址編碼
目的是為了減少因為誤輸入字符而導致轉賬到錯誤地址的情況。通過這種校驗和大小寫校正機制,可以提高轉帳的準確性和安全性。
文章目錄
1.ERC55
Mixed-case checksum address encoding
2.換算方式
a.將地址轉換為小寫
b.利用 keccak256 把地址轉換成 hash
c.循環每個地址的字元,當 index 的 hash 大於等於8,字母轉成大寫
3.校驗地址
const createKeccakHash = require('keccak')
function toChecksumAddress (address) {
// a.將地址轉換為小寫
address = address.toLowerCase().replace('0x', '')
// b.利用 keccak256 把地址轉換成 hash
const hash = createKeccakHash('keccak256').update(address).digest('hex');
// c.循環每個地址的字元,當 index 的 hash 大於等於8,字母轉成大寫
return '0x' + address.split('').map((char, index) => {
return parseInt(hash[index], 16) >= 8 ? char.toUpperCase() : char;
}).join('');
}