Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/switch-borsh-to-zorsh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"near-api-js": patch
---

Replace borsh with @zorsh/zorsh for Borsh serialization (maintains identical binary format)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@noble/curves": "1.8.1",
"@noble/hashes": "1.7.1",
"@scure/base": "1.2.4",
"borsh": "1.0.0",
"@zorsh/zorsh": "^0.4.0",
"depd": "2.0.0",
"exponential-backoff": "3.1.2",
"is-my-json-valid": "2.20.6",
Expand Down
11 changes: 8 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions src/signers/signer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sha256 } from '@noble/hashes/sha256';
import { type Schema, serialize } from 'borsh';
import { b } from '@zorsh/zorsh';
import { KeyType, type PublicKey } from '../crypto/index.js';
import {
type DelegateAction,
Expand All @@ -25,14 +25,12 @@ export interface SignedMessage {
state?: string; // Optional, applicable to browser wallets (e.g. MyNearWallet). The same state passed in SignMessageParams.
}

export const Nep413MessageSchema: Schema = {
struct: {
message: 'string',
nonce: { array: { type: 'u8', len: 32 } },
recipient: 'string',
callbackUrl: { option: 'string' },
},
};
export const Nep413MessageSchema = b.struct({
message: b.string(),
nonce: b.bytes(32),
recipient: b.string(),
callbackUrl: b.option(b.string()),
});

/**
* General signing interface, can be used for in memory signing, RPC singing, external wallet, HSM, etc.
Expand Down Expand Up @@ -66,8 +64,11 @@ export abstract class Signer {

// 2**31 + 413 == 2147484061
const PREFIX = 2147484061;
const serializedPrefix = serialize('u32', PREFIX);
const serializedParams = serialize(Nep413MessageSchema, params);
const serializedPrefix = b.u32().serialize(PREFIX);
const serializedParams = Nep413MessageSchema.serialize({
...params,
callbackUrl: params.callbackUrl ?? null,
});

const serializedPayload = new Uint8Array(serializedPrefix.length + serializedParams.length);
serializedPayload.set(serializedPrefix);
Expand Down
Loading
Loading