Skip to content

Conversation

@r-near
Copy link
Contributor

@r-near r-near commented Nov 20, 2025

Changes Made:

  1. package.json: Replaced borsh: 1.0.0 with @zorsh/zorsh: ^0.4.0
  2. src/transactions/schema.ts: Converted all 25+ schema definitions from old borsh format to zorsh's builder API:
  • Old: { struct: { field: 'type' } } → New: b.struct({ field: b.type() })
  • Updated all serialize() and deserialize() calls to use schema methods
  1. src/signers/signer.ts: Updated NEP-413 message schema and serialization calls
  2. Test files: Updated test imports and API usage

With regards to the as any casts, Zorsh generates very strict types from schemas. For enums, zorsh expects discriminated unions like { ed25519Key: X } | { secp256k1Key: Y }, but the existing PublicKey class has a different structure (both properties exist, one is undefined).
They only appear at serialization boundaries in:

  • encodeDelegateAction() (src/transactions/schema.ts:21)
  • encodeSignedDelegate() (src/transactions/schema.ts:30)
  • encodeTransaction() (src/transactions/schema.ts:40)
  • One test (test/transactions/serialize.test.ts:78)

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • Commit messages follow the conventional commits spec
  • If this is a code change: I have written unit tests.
  • If this changes code in a published package: I have run pnpm changeset to create a changeset JSON document appropriate for this change.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

Test Plan

Related issues/PRs

Closes #1699

@changeset-bot
Copy link

changeset-bot bot commented Nov 20, 2025

🦋 Changeset detected

Latest commit: 0946ae7

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

This PR includes changesets to release 1 package
Name Type
near-api-js Patch

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

@github-project-automation github-project-automation bot moved this to NEW❗ in DevTools Nov 20, 2025
@r-near r-near changed the base branch from master to pre-release/v7 November 20, 2025 01:50
@r-near r-near marked this pull request as draft November 20, 2025 01:54
@chatgpt-codex-connector
Copy link

💡 Codex Review

near-api-js/package.json

Lines 15 to 21 in df68e3b

"type": "module",
"exports": {
".": {
"import": "./lib/index.js",
"types": "./lib/index.d.ts"
}
},

P1 Badge Convert tests config to ESM after enabling type: module

The package is now marked as ESM ("type": "module"), which makes every .js file load as an ES module. The test helpers still import test/config.js using CommonJS exports (require/module.exports in that file), so as soon as the tests or E2E suites load the config, Node will throw ReferenceError: require is not defined rather than running the suite. Please convert the config file to ESM (or rename it to .cjs) so it can be executed under the new module type.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Replace the borsh library with @zorsh/zorsh throughout the codebase:

- Update package.json to use @zorsh/zorsh ^0.4.0 instead of borsh 1.0.0
- Convert all schema definitions in src/transactions/schema.ts to use
  zorsh's builder API (b.struct, b.enum, b.bytes, etc.)
- Update serialize/deserialize calls to use schema methods instead of
  standalone functions
- Fix type compatibility issues with type assertions at serialization
  boundaries where needed
- Update test files to use zorsh API

The binary serialization format remains identical as both libraries
implement the Borsh specification.
- Add ensureBigInt() helper for number-to-BigInt conversion
- Update all encoding functions to handle BigInt requirements
- Fix test imports for vitest migration
- Remove unused Test class and update test expectations
@r-near r-near force-pushed the claude/switch-borsh-to-zorsh-01GsCsbvcWe2f6oc2mHxwGPt branch from e7c9782 to abdca41 Compare November 20, 2025 22:08
- Organize imports in schema.ts and key_pair_signer.test.ts
- Fix formatting in schema.ts (constructor parameters, imports)
- Apply template literals in serialize.test.ts
- Format long lines in test files
- Exclude keyType field from BigInt conversion (it's an enum: 0 or 1)
- Convert undefined to null for allowance field (zorsh expects null for optional values)
- Prevent mixing BigInt with regular numbers in serialization

This fixes the "serialize multi-action tx" test failure.
- Convert all undefined values to null for optional numeric fields
  (not just allowance, but also maxBlockHeight and other optional fields)
- Zorsh's b.option() expects null, not undefined, for "no value"
- Add comprehensive unit tests for real-world transaction scenarios
  including large numbers, mixed numeric types, and JSON-parsed values

This ensures compatibility with actual RPC responses where numeric
values may be numbers, strings, or BigInt depending on their size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: NEW❗

Development

Successfully merging this pull request may close these issues.

3 participants