feat: create shared serialization helpers - #197
Conversation
There was a problem hiding this comment.
Pull request overview
Adds shared serialization utilities to @metamask/snap-networks-utils so network snaps can round-trip non-JSON-native values through snap state / interface context using a consistent wire format.
Changes:
- Introduces
serialize,deserialize, andSerializablesupportingundefined,BigNumber,bigint, andUint8Arrayvia{ __type, value? }. - Exposes the new helpers from the package root export surface.
- Adds Jest coverage and a changelog entry; introduces needed dependencies (
bignumber.js,lodash,@types/lodash).
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| yarn.lock | Adds lockfile entries for newly introduced dependencies. |
| packages/snap-networks-utils/src/serialization/types.ts | Defines the Serializable type used by the helpers. |
| packages/snap-networks-utils/src/serialization/serialize.ts | Implements JSON-safe serialization with a stable { __type, value? } wire format. |
| packages/snap-networks-utils/src/serialization/serialize.test.ts | Adds unit tests covering primitives, nested structures, and Uint8Array encoding. |
| packages/snap-networks-utils/src/serialization/deserialize.ts | Implements deserialization back to original runtime types. |
| packages/snap-networks-utils/src/serialization/deserialize.test.ts | Adds unit tests for deserialization, including falsy-value handling and Uint8Array. |
| packages/snap-networks-utils/src/index.ts | Exports serialize, deserialize, and Serializable from the package entrypoint. |
| packages/snap-networks-utils/package.json | Adds runtime/dev dependencies required by the new helpers. |
| packages/snap-networks-utils/CHANGELOG.md | Documents the addition of the new serialization helpers. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| @@ -0,0 +1,95 @@ | |||
| /* eslint-disable jest/prefer-strict-equal */ | |||
There was a problem hiding this comment.
missing enable after disable
True better this way, I'll change it. |
Explanation
Solana, Tron, and Stellar each keep a local
{ __type, value }serialize/deserialize pair so snap state can round-trip values JSON cannot represent (undefined,BigNumber,bigint,Uint8Array). This PR adds that helper to@metamask/snap-networks-utilsso the snaps can share one implementation.serialize/deserialize/Serializableare exported from the package.{ __type: 'undefined' | 'BigNumber' | 'bigint' | 'Uint8Array', value? }.Uint8Arrayuses portablebtoa/atob(same as Tron/Stellar), not Solana’s@solana/kitcodec. Encoded bytes still match ([1, 2, 3]→AQID).This PR is utils-only. Follow-up PRs will switch Solana, Tron, and Stellar to the shared helpers.
References
Checklist