|
| 1 | +// Given a Hash |
| 2 | +// Represent it in a sequence of emojis |
| 3 | + |
| 4 | +// Manually gone and looked at different shapes |
| 5 | +// Skipping emojis that look too similar |
| 6 | +// NOTE: I could have searched way more |
| 7 | +// I have yet to go through https://emojipedia.org/food-drink and onwards |
| 8 | +const ARBITRARY_LIST_OF_EMOJIS = [ |
| 9 | + "🌲", |
| 10 | + "🍄", |
| 11 | + "🌸", |
| 12 | + "🌹", |
| 13 | + "🌓", |
| 14 | + "🪐", |
| 15 | + "⭐", |
| 16 | + "🌤️", |
| 17 | + "🌀", |
| 18 | + "🌈", |
| 19 | + "🌊", |
| 20 | + "🤠", |
| 21 | + "🥳", |
| 22 | + "😎", |
| 23 | + "🤯", |
| 24 | + "🤮", |
| 25 | + "😈", |
| 26 | + "💀", |
| 27 | + "💩", |
| 28 | + "🤡", |
| 29 | + "👺", |
| 30 | + "👻", |
| 31 | + "👽", |
| 32 | + "👾", |
| 33 | + "🤖", |
| 34 | + "😻", |
| 35 | + "🙈", |
| 36 | + "👍", |
| 37 | + "👊", |
| 38 | + "🤝", |
| 39 | + "🙏", |
| 40 | + "💅", |
| 41 | + "💪", |
| 42 | + "🦵", |
| 43 | + "🦶", |
| 44 | + "🧠", |
| 45 | + "🦷", |
| 46 | + "👀", |
| 47 | + "👅", |
| 48 | + "👄", |
| 49 | + "🩸", |
| 50 | + "🤰", |
| 51 | + "🐵", |
| 52 | + "🦍", |
| 53 | + "🐶", |
| 54 | + "🐕", |
| 55 | + "🐺", |
| 56 | + "🦊", |
| 57 | + "🦁", |
| 58 | + "🦄", |
| 59 | + "🐮", |
| 60 | + "🐷", |
| 61 | + "🐏", |
| 62 | + "🦃", |
| 63 | + "🐥", |
| 64 | + "🦉", |
| 65 | + "🦆", |
| 66 | + "🦩", |
| 67 | + "😂", |
| 68 | + "🌍", |
| 69 | + "🚗", |
| 70 | + "🗿", |
| 71 | + "🌋", |
| 72 | + "🗽" |
| 73 | +] |
| 74 | + |
| 75 | +console.log("len", ARBITRARY_LIST_OF_EMOJIS.length) |
| 76 | + |
| 77 | +// Dumb af |
| 78 | + |
| 79 | +// Replace 16 with the first 16 digits |
| 80 | +// Demo string |
| 81 | +// 0x5ac989f52ea4c399343f6c0cf5a4810fc1bdac5773de37ca0cd0a8287f75a5c6 |
| 82 | + |
| 83 | +const NIBBLE_LETTERS_TO_NUMBER = { |
| 84 | + "a": 10, |
| 85 | + "b": 11, |
| 86 | + "c": 12, |
| 87 | + "d": 13, |
| 88 | + "e": 14, |
| 89 | + "f": 15 |
| 90 | +} |
| 91 | + |
| 92 | +function fromNibbleCharToNumber(nibble: string): number { |
| 93 | + if(NIBBLE_LETTERS_TO_NUMBER[nibble]) { |
| 94 | + return NIBBLE_LETTERS_TO_NUMBER[nibble] |
| 95 | + } |
| 96 | + |
| 97 | + return Number(nibble) |
| 98 | +} |
| 99 | + |
| 100 | +function fromHexStringToNibbles(hash: string): number[] { |
| 101 | + // Check and remove 0x |
| 102 | + // Copy then replace |
| 103 | + // Copy so we avoid any weirdness |
| 104 | + const trimmed = JSON.parse(JSON.stringify(hash)).replace(/^0x/, "") |
| 105 | + let converted: string[] = Array.from(trimmed) |
| 106 | + |
| 107 | + return converted.map(nibble => fromNibbleCharToNumber(nibble)) |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +function fromNibbleNumberToEmoji(value: number): string { |
| 113 | + return ARBITRARY_LIST_OF_EMOJIS[value] |
| 114 | +} |
| 115 | + |
| 116 | +export function fromHexStringToEmojis(hash: string): string[] { |
| 117 | + const asNibblesNumbers = fromHexStringToNibbles(hash) |
| 118 | + return asNibblesNumbers.map(nibble => fromNibbleNumberToEmoji(nibble)) |
| 119 | +} |
| 120 | +export default function fromHexStringToEmojiString(hash: string): string { |
| 121 | + const asNibblesNumbers = fromHexStringToNibbles(hash) |
| 122 | + return asNibblesNumbers.map(nibble => fromNibbleNumberToEmoji(nibble)).join("") |
| 123 | +} |
| 124 | + |
| 125 | +console.log(fromHexStringToNibbles("0x5ac989f52ea4c399343f6c0cf5a4810fc1bdac5773de37ca0cd0a8287f75a5c6")) |
| 126 | +console.log(fromHexStringToEmojis("0x5ac989f52ea4c399343f6c0cf5a4810fc1bdac5773de37ca0cd0a8287f75a5c6")) |
0 commit comments