Last active
June 19, 2024 07:14
-
-
Save ardislu/c2626b5f524cfc94142e33e055bb4516 to your computer and use it in GitHub Desktop.
Minimal JavaScript implementation of ERC-7673.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Utility functions to convert an Ethereum hexadecimal address to base256emoji and vice versa. | |
// https://ercs.ethereum.org/ERCS/erc-7673 | |
const toEmoji = [ | |
'๐', '๐ช', 'โ', '๐ฐ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', | |
'๐', 'โ', '๐ป', '๐ฅ', '๐พ', '๐ฟ', '๐', 'โค', '๐', '๐คฃ', '๐', '๐', '๐', '๐ญ', '๐', '๐', | |
'๐ ', '๐', '๐', '๐ฅ', '๐ฅฐ', '๐', '๐', '๐', '๐ข', '๐ค', '๐', '๐', '๐ช', '๐', 'โบ', '๐', | |
'๐ค', '๐', '๐', '๐', '๐', '๐น', '๐คฆ', '๐', '๐', 'โ', 'โจ', '๐คท', '๐ฑ', '๐', '๐ธ', '๐', | |
'๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐คฉ', '๐', '๐', '๐ค', '๐', '๐ฏ', '๐', '๐', '๐ถ', | |
'๐', '๐คญ', 'โฃ', '๐', '๐', '๐', '๐ช', '๐', '๐ฅ', '๐', '๐', '๐ฉ', '๐ก', '๐คช', '๐', '๐ฅณ', | |
'๐ฅ', '๐คค', '๐', '๐', '๐ณ', 'โ', '๐', '๐', '๐ด', '๐', '๐ฌ', '๐', '๐', '๐ท', '๐ป', '๐', | |
'โญ', 'โ ', '๐ฅบ', '๐', '๐', '๐ค', '๐ฆ', 'โ', '๐ฃ', '๐', '๐', 'โน', '๐', '๐', '๐ ', 'โ', | |
'๐', '๐บ', '๐', '๐ป', '๐', '๐', '๐', '๐', '๐น', '๐ฃ', '๐ซ', '๐', '๐', '๐ต', '๐ค', '๐', | |
'๐ด', '๐ค', '๐ผ', '๐ซ', 'โฝ', '๐ค', 'โ', '๐', '๐คซ', '๐', '๐ฎ', '๐', '๐ป', '๐', '๐ถ', '๐', | |
'๐ฒ', '๐ฟ', '๐งก', '๐', 'โก', '๐', '๐', 'โ', 'โ', '๐', '๐ฐ', '๐คจ', '๐ถ', '๐ค', '๐ถ', '๐ฐ', | |
'๐', '๐ข', '๐ค', '๐', '๐จ', '๐จ', '๐คฌ', 'โ', '๐', '๐บ', '๐ค', '๐', '๐', '๐ฑ', '๐', '๐ถ', | |
'๐ฅด', 'โถ', 'โก', 'โ', '๐', '๐ธ', 'โฌ', '๐จ', '๐', '๐ฆ', '๐ท', '๐บ', 'โ ', '๐ ', '๐', '๐ต', | |
'๐', '๐คฒ', '๐ค ', '๐คง', '๐', '๐ต', '๐ ', '๐ง', '๐พ', '๐', '๐', '๐ค', '๐', '๐คฏ', '๐ท', 'โ', | |
'๐ง', '๐ฏ', '๐', '๐', '๐ค', '๐', '๐', 'โ', '๐ด', '๐ฃ', '๐ธ', '๐', '๐', '๐ฅ', '๐คข', '๐ ', | |
'๐ก', '๐ฉ', '๐', '๐ธ', '๐ป', '๐ค', '๐คฎ', '๐ผ', '๐ฅต', '๐ฉ', '๐', '๐', '๐ผ', '๐', '๐ฃ', '๐ฅ' | |
]; | |
const fromEmoji = {}; | |
for (const [index, emoji] of toEmoji.entries()) { | |
fromEmoji[emoji] = index.toString(16).padStart(2, '0'); | |
} | |
function encodeAddress(hex) { | |
return hex.replace('0x', '').match(/.{2}/g).map(i => toEmoji[parseInt(i, 16)]).join(''); | |
} | |
// emoji.split('') will split the string into an array of UTF-16 code units, which are neither | |
// grapheme clusters nor Unicode code points. Use Intl.Segmenter() to split into grapheme clusters. | |
function decodeAddress(emoji) { | |
const segmenter = new Intl.Segmenter(); | |
return `0x${[...segmenter.segment(emoji)].map(e => fromEmoji[e.segment]).join('')}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment