Skip to content

Conversation

@Amxx
Copy link
Collaborator

@Amxx Amxx commented Jun 20, 2025

Fixes #5760
Includes (depends on) #5733

This probably deserves some assembly. It feels like the decoding could be optimized a lot. Note that base58 is notoriously loop (and memory ops) heavy.

PR Checklist

  • Tests
  • Documentation
  • Changeset entry (run npx changeset add)

Summary by CodeRabbit

  • New Features

    • On-chain Base58 encoding/decoding utility added.
    • Bytes utilities enhanced with in-place splice variants and helpers to count leading/consecutive bytes; splice parity with slicing ensured.
  • Documentation

    • Base58 added to docs; Base64 descriptions clarified.
  • Tests

    • Comprehensive Base58 tests (round-trip, vectors, invalid-input handling).
    • Expanded Bytes tests to cover new helpers and splice.
    • Updated Base64 tests with improved assertion style.

@Amxx Amxx requested a review from a team as a code owner June 20, 2025 21:16
@changeset-bot
Copy link

changeset-bot bot commented Jun 20, 2025

🦋 Changeset detected

Latest commit: 23221e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openzeppelin-solidity Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR


bytes internal constant _TABLE = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
bytes internal constant _LOOKUP_TABLE =
hex"000102030405060708ffffffffffffff090a0b0c0d0e0f10ff1112131415ff161718191a1b1c1d1e1f20ffffffffffff2122232425262728292a2bff2c2d2e2f30313233343536373839";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For anyone curious, here is how you build these lookup tables:

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 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), MINIMUM, MAXIMUM } : undefined;
}

console.log(buildLookup(
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", // base64
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", // base64url
));

console.log(buildLookup(
    "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz", // base58
));

@Amxx Amxx force-pushed the feature/base58 branch from dbee56c to d4a573b Compare June 22, 2025 13:10
@Amxx Amxx force-pushed the feature/base58 branch from d4a573b to a25bd11 Compare June 22, 2025 13:13
@Amxx Amxx requested a review from ernestognw September 24, 2025 09:35
Copy link
Member

@ernestognw ernestognw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will keep reviewing

ernestognw
ernestognw previously approved these changes Sep 25, 2025
Copy link
Member

@ernestognw ernestognw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lfg 🚀

ernestognw
ernestognw previously approved these changes Sep 25, 2025
Co-authored-by: Hadrien Croubois <[email protected]>
@Amxx Amxx merged commit 5eb047a into OpenZeppelin:master Sep 26, 2025
17 of 19 checks passed
@Amxx Amxx deleted the feature/base58 branch September 26, 2025 13:03
PeterAdedokun1 pushed a commit to PeterAdedokun1/openzeppelin-contracts that referenced this pull request Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider adding a Base58 library

4 participants