-
-
Notifications
You must be signed in to change notification settings - Fork 960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SignTypedData incorrectly handling bigints when BigInt.toJSON is defined #2395
Comments
I don't think patching the I would suggest to create a custom const replacer = (key, value) =>
typeof value === "bigint" ? { $bigint: value.toString() } : value;
const data = {
number: 1,
big: 18014398509481982n,
};
const stringified = JSON.stringify(data, replacer);
console.log(stringified);
// {"number":1,"big":{"$bigint":"18014398509481982"}} |
Affecting things like The alternative is to patch What I am confused about is why is viem able to stringify bigints correctly (using |
Patching the
Strongly recommend the second approach on the MDN docs: |
We can not just override the reviver function everywhere, as then it'd break Next.js. I cannot tell Next.js to use a specific reviver for example, meaning that passing bigints between server and client components would not be possible without first stringifying them and then (probably using a slow zod schema) parsing them again. Which would also move typechecks to runtime instead of compile time, which makes no sense. We have had 0 issues with our approach until we came across this viem one. Doing However, from what I can see from testing, Soo dw about it 😅 |
This issue has been locked since it has been closed for more than 14 days. If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Viem version. If you have any questions or comments you can create a new discussion thread. |
Check existing issues
Viem Version
2.10.11
Current Behavior
To support
bigint
s across our repo, including Next.js server <-> client component communication, we've decided to overrideJSON.parse
and defineBigInt.prototype.toJSON
.This has worked quite well so far, but we've found that our permits had stopped working, due to viem using the
toJSON
function we defined instead of justtoString()
:This is how
BigInt.prototype.toJSON
is defined:Expected Behavior
Viem should serialize bigints using
toString
instead of (I'm guessing?) first tryingtoJSON
and then if that failstoString
.Steps To Reproduce
No response
Link to Minimal Reproducible Example
https://stackblitz.com/edit/viem-getting-started-hevaba?file=index.js
Anything else?
The linked repro sandbox is broken, had to change the index file over to .js
The text was updated successfully, but these errors were encountered: