Skip to content

Commit df68e3b

Browse files
committed
Switch from borsh to @zorsh/zorsh
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.
1 parent 6692c2e commit df68e3b

File tree

7 files changed

+242
-231
lines changed

7 files changed

+242
-231
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"near-api-js": patch
3+
---
4+
5+
Replace borsh with @zorsh/zorsh for Borsh serialization (maintains identical binary format)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"dependencies": {
2323
"depd": "2.0.0",
2424
"@scure/base": "1.2.4",
25-
"borsh": "1.0.0",
25+
"@zorsh/zorsh": "^0.4.0",
2626
"@noble/hashes": "1.7.1",
2727
"@noble/curves": "1.8.1",
2828
"secp256k1": "5.0.1",

pnpm-lock.yaml

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/signers/signer.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
encodeTransaction,
99
encodeDelegateAction,
1010
} from '../transactions/index.js';
11-
import { Schema, serialize } from 'borsh';
11+
import { b } from '@zorsh/zorsh';
1212
import { sha256 } from '@noble/hashes/sha256';
1313

1414
export interface SignMessageParams {
@@ -25,14 +25,12 @@ export interface SignedMessage {
2525
state?: string; // Optional, applicable to browser wallets (e.g. MyNearWallet). The same state passed in SignMessageParams.
2626
}
2727

28-
export const Nep413MessageSchema: Schema = {
29-
struct: {
30-
message: 'string',
31-
nonce: { array: { type: 'u8', len: 32 } },
32-
recipient: 'string',
33-
callbackUrl: { option: 'string' },
34-
},
35-
};
28+
export const Nep413MessageSchema = b.struct({
29+
message: b.string(),
30+
nonce: b.bytes(32),
31+
recipient: b.string(),
32+
callbackUrl: b.option(b.string()),
33+
});
3634

3735
/**
3836
* General signing interface, can be used for in memory signing, RPC singing, external wallet, HSM, etc.
@@ -70,8 +68,11 @@ export abstract class Signer {
7068

7169
// 2**31 + 413 == 2147484061
7270
const PREFIX = 2147484061;
73-
const serializedPrefix = serialize('u32', PREFIX);
74-
const serializedParams = serialize(Nep413MessageSchema, params);
71+
const serializedPrefix = b.u32().serialize(PREFIX);
72+
const serializedParams = Nep413MessageSchema.serialize({
73+
...params,
74+
callbackUrl: params.callbackUrl ?? null,
75+
});
7576

7677
const serializedPayload = new Uint8Array(
7778
serializedPrefix.length + serializedParams.length

0 commit comments

Comments
 (0)