Skip to content

fix(utils): replace Buffer base64 usage with uint8arrays for browser …#7178

Open
Sushil-19 wants to merge 4 commits intoWalletConnect:v2.0from
Sushil-19:fix/utils-remove-buffer-base64
Open

fix(utils): replace Buffer base64 usage with uint8arrays for browser …#7178
Sushil-19 wants to merge 4 commits intoWalletConnect:v2.0from
Sushil-19:fix/utils-remove-buffer-base64

Conversation

@Sushil-19
Copy link

Summary

Replaces Node.js Buffer base64 decoding usage in @walletconnect/utils
with uint8arrays helpers to improve browser compatibility.

Changes

  • Replaced Buffer.from(..., "base64") with fromString(..., BASE64)
  • Normalized base64url decoding to base64pad format
  • Preserved existing signature and hashing behavior
  • No breaking changes

Testing

  • All unit tests pass (117/117)
  • Verified EIP-1271, EIP-191, JWT, and crypto utilities
  • Confirmed RPC verification works with TEST_PROJECT_ID

@github-actions
Copy link
Contributor

github-actions bot commented Mar 1, 2026

All contributors have signed the CTA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@Sushil-19
Copy link
Author

I have read the CTA Document and I hereby sign the CTA

@ganchoradkov
Copy link
Member

ganchoradkov commented Mar 2, 2026

hey, thanks for tackling the issue. There are few ways to improve though

  • The PR only addresses crypto.ts, but Buffer is used extensively across the utils package
  • No browser-environment tests
    The PR claims "All unit tests pass (117/117)", but these tests run in Node.js where Buffer is globally available. The tests would pass even without the changes.

Ideally, a test should either:

  • Run in a browser-like environment (jsdom/happy-dom), or
  • Assert that the changed functions don't reference Buffer (e.g., a bundler analysis)

@Sushil-19
Copy link
Author

Thanks for the feedback — that makes sense.

You're right that the current PR only replaces the Buffer usage in crypto.ts, while there are still several Buffer references across the @walletconnect/utils package.

I'll extend the changes to replace all Buffer usage with browser-compatible alternatives (e.g., Uint8Array and utilities from uint8arrays where appropriate).

I'll also add a browser-like test environment (e.g., jsdom) to ensure the code works without Buffer being globally available, since the current Node.js tests would not catch this issue.

I'll push an update shortly addressing these points.

@Sushil-19 Sushil-19 force-pushed the fix/utils-remove-buffer-base64 branch from 0d51690 to 70a12f2 Compare March 5, 2026 07:36
@Sushil-19
Copy link
Author

Following up on the earlier changes in this PR.

We updated the implementation across the utils files to remove usage of Node.js Buffer and replaced it with uint8arrays helpers (fromString, toString, and concat) to ensure browser compatibility and avoid relying on Node-specific APIs.

Additionally, the concat import was corrected to use the proper named export:

import { concat } from "uint8arrays/concat";

I also re-ran the full test suite locally. The two failing EIP-1271 signature tests appear to fail on the base v2.0 branch as well, so they are not introduced by the changes in this PR.

No other behavior was modified — the goal of this PR remains limited to removing Buffer usage and improving browser compatibility within the utils package.

@Sushil-19
Copy link
Author

hello @ganchoradkov , can you please check this? If any changes needed, I can work on it asap so that I can work on next issue.

@rnbrady
Copy link

rnbrady commented Mar 9, 2026

Suggestions:

  • Commit a test that fails, then commit a fix that makes the test pass. Then you have a confirmed fix and a regression test.
  • No need to install to top level package.json, this is a monorepo and utils package already has uint8arrays. Also the lockfile is now out of sync.
  • Check padding behaviour, for example in misc.ts:
import { toString } from "uint8arrays";

export function old_toBase64(input: string, removePadding = false): string {
  const encoded = Buffer.from(input).toString("base64");
  return removePadding ? encoded.replace(/[=]/g, "") : encoded;
}

export function new_toBase64(input: string, removePadding = false): string {
  const encoded = toString(new TextEncoder().encode(input), "base64");
  return removePadding ? encoded.replace(/[=]/g, "") : encoded;
}

console.log(old_toBase64("1", false), "should equal", new_toBase64("1", false));
// Outputs: MQ== should equal MQ

console.log(old_toBase64("1", true), "should equal", new_toBase64("1", true));
// Outputs: MQ should equal MQ

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.

3 participants