-
Notifications
You must be signed in to change notification settings - Fork 97
fix(identity): handle ArrayBuffer in delegation chain serialization #1152
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
fix(identity): handle ArrayBuffer in delegation chain serialization #1152
Conversation
Dear @DrJesseGlass, In order to potentially merge your code in this open-source repository and therefore proceed with your contribution, we need to have your approval on DFINITY's CLA. If you decide to agree with it, please visit this issue and read the instructions there. Once you have signed it, re-trigger the workflow on this PR to see if your code can be merged. — The DFINITY Foundation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DrJesseGlass thanks for the contribution and the detailed explanation in the PR!
I'm wondering: shouldn't the usage of ArrayBuffer
instead of Uint8Array
be caught by the TypeScript checks before runtime? In fact, in the tests that you added, you explicitly cast the type to any
to avoid TS errors.
I think the proper fix is to convert to Uint8Array
downstream, like you did in kristoferlund/ic-siws#6, leaving the @icp-sdk/core/identity
code untouched.
Thank you @ilbertt for the review and architectural discussion. You're correct that the test uses The runtime failure occurs when ArrayBuffer objects enter delegation chains through:
The test simulates the exact runtime condition where ArrayBuffer objects end up in delegation chain serialization paths. While TypeScript's structural typing doesn't catch this at compile time, Core library type conversion is an established strategy:
DelegationChain.toJSON() is fundamental to ICP authentication patterns:
The fix is indeed minimal:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've thought more about this and I think it makes sense to avoid developers having to manually check for the proper buffer type.
I left a few minor comments that I think can be resolved easily.
… the number of assertions at two points in test; coverted one off Hex format regexes to expect.extend custom matcher
Review dismissed by automation script.
@DrJesseGlass the PR looks good. Can you please fix the linter error? So that we can merge it |
Review dismissed by automation script.
@DrJesseGlass merged! Thanks a lot for the contribution and the iterations on the feedback. A patch containing the fix will be released soon. |
Description
Fixes 'Uint8Array expected' error in
DelegationChain.toJSON()
caused by@noble/hashes
v1.8+ requiring strictUint8Array
inputs.Root Cause: The upgrade to
@noble/hashes
v1.8+ introduced stricter type checking wherebytesToHex()
only acceptsUint8Array
, but delegation serialization can encounterArrayBuffer
objects from:ArrayBuffer
instead ofUint8Array
Impact: Applications using delegation chain persistence (like
ic-siws-js
) crash with "Uint8Array expected" when callingtoJSON()
on delegation chains containingArrayBuffer
data.Solution: Added
safeBytesToHex()
wrapper that convertsArrayBuffer
toUint8Array
before callingbytesToHex()
. This maintains backward compatibility while working with the stricter noble library requirements.Changed Methods:
Delegation.toJSON()
- handlesArrayBuffer
inpubkey
fieldDelegationChain.toJSON()
- handlesArrayBuffer
insignature
andpublicKey
fieldsHow Has This Been Tested?
ArrayBuffer
conditions that caused the original failureic-siws-js
that triggered this investigationUint8Array
andArrayBuffer
inputs seamlesslytoJSON()
→fromJSON()
→toJSON()
cycles work correctlyTest command:
pnpm --filter @dfinity/identity test
Integration test: Verified fix resolves the original issue in
ic-siws-js
component that triggered this investigation.Checklist: