Created
September 1, 2025 16:13
-
-
Save Amxx/ae43dc9dc24427f364c9d5789ad65896 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const { ethers } = require("ethers"); | |
| const max = (...values) => values.slice(1).reduce((x, y) => (x > y ? x : y), values.at(0)); | |
| const min = (...values) => values.slice(1).reduce((x, y) => (x < y ? x : y), values.at(0)); | |
| const buildLookup = (...tables) => { | |
| const bTables = tables.map(table => Array.from(ethers.toUtf8Bytes(table))); | |
| const MINIMUM = min(...bTables.flatMap(x => x)); | |
| const MAXIMUM = max(...bTables.flatMap(x => x)); | |
| const filter = [].concat(...bTables).reduce((acc, x) => acc | (1n << BigInt(x - MINIMUM)), 0n); | |
| const lookup = Uint8Array.from(Array.from({ length: MAXIMUM - MINIMUM + 1 }).map((_, i) => bTables.map(table => table.indexOf(i + MINIMUM)).find(x => x != -1) ?? 0xff)); | |
| const valid = tables.every(table => Object.entries(table).every(([ i, c]) => i == lookup.at(c.charCodeAt(0) - MINIMUM))); | |
| return valid ? { tables, lookup: ethers.hexlify(lookup), filter: ethers.toBeHex(filter), MINIMUM, MAXIMUM } : undefined; | |
| } | |
| console.log(buildLookup( | |
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", // base64 | |
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", // base64url | |
| "=", // padding | |
| )); | |
| console.log(buildLookup( | |
| "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", // base32 | |
| "=", // padding | |
| )); | |
| console.log(buildLookup( | |
| "0123456789ABCDEFGHIJKLMNOPQRSTUV", // base32 | |
| "=", // padding | |
| )); | |
| console.log(buildLookup( | |
| "0123456789abcdef", // base16 / hex | |
| "0123456789ABDCEF", // base16 / hex | |
| )); | |
| console.log(buildLookup( | |
| "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz", // base58 | |
| )); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output is