Skip to content

Commit

Permalink
feat: update borsh-js to v1.0.0 (#1168)
Browse files Browse the repository at this point in the history
We have been working on a new version of borsh-js, this commit
updates all the files that depend on borsh-js. This update is
a breaking change, since the objects schemas (which are exported)
are completely different from v0.7.0 (before) to v1.0.1 (now).

The main benefit of updating borsh-js is that the new version
is agnostic to the class being serialized, meaning that two
"deep-equal" instances of different classes can be serialized
using the same Schema. This means that near-api-js will be
able to (de)serialize objects created across different
library versions (which currently is not possible).

The new version of Borsh has been released without the `baseDecode`
and `baseEncode` methods. I have migrated these methods as they
were from `borsh` to the `near-api-js/utils/format` package.
  • Loading branch information
gagdiez authored Oct 31, 2023
1 parent 7cc6f7f commit 61349ae
Show file tree
Hide file tree
Showing 25 changed files with 2,957 additions and 2,533 deletions.
10 changes: 10 additions & 0 deletions .changeset/spicy-tomatoes-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"near-api-js": major
"@near-js/accounts": major
"@near-js/biometric-ed25519": major
"@near-js/crypto": major
"@near-js/transactions": major
"@near-js/wallet-account": major
---

feat: updated borsh-js to v1.0.1
2 changes: 1 addition & 1 deletion packages/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"ajv": "^8.11.2",
"ajv-formats": "^2.1.1",
"bn.js": "5.2.1",
"borsh": "^0.7.0",
"borsh": "1.0.0",
"depd": "^2.0.0",
"near-abi": "0.1.1"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/accounts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import {
BlockReference,
} from '@near-js/types';
import {
baseDecode,
baseEncode,
logWarning,
parseResultError,
DEFAULT_FUNCTION_CALL_GAS,
printTxOutcomeLogs,
printTxOutcomeLogsAndFailures,
} from '@near-js/utils';
import BN from 'bn.js';
import { baseDecode, baseEncode } from 'borsh';

import { Connection } from './connection';

Expand Down
3 changes: 2 additions & 1 deletion packages/biometric-ed25519/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
"@aws-crypto/sha256-js": "^4.0.0",
"@hexagon/base64": "^1.1.26",
"@near-js/crypto": "workspace:*",
"@near-js/utils": "workspace:*",
"asn1-parser": "^1.1.8",
"bn.js": "5.2.1",
"borsh": "^0.7.0",
"borsh": "1.0.0",
"buffer": "^6.0.3",
"elliptic": "^6.5.4",
"fido2-lib": "3.4.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/biometric-ed25519/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Sha256 } from '@aws-crypto/sha256-js';
import { Buffer } from 'buffer';
import asn1 from 'asn1-parser';
import { KeyPair } from '@near-js/crypto';
import { baseEncode } from 'borsh';
import { baseEncode } from '@near-js/utils';
import {
validateUsername,
preformatMakeCredReq,
Expand Down
3 changes: 2 additions & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"license": "ISC",
"dependencies": {
"@near-js/types": "workspace:*",
"@near-js/utils": "workspace:*",
"bn.js": "5.2.1",
"borsh": "^0.7.0",
"borsh": "1.0.0",
"tweetnacl": "^1.0.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/src/key_pair_ed25519.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { baseEncode, baseDecode } from 'borsh';
import { baseEncode, baseDecode } from '@near-js/utils';
import nacl from 'tweetnacl';

import { KeyType } from './constants';
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/src/public_key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Assignable } from '@near-js/types';
import { baseEncode, baseDecode } from 'borsh';
import { baseEncode, baseDecode } from '@near-js/utils';
import nacl from 'tweetnacl';

import { KeyType } from './constants';
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/test/key_pair.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { baseEncode } = require('borsh');
const { baseEncode } = require('@near-js/utils');
const { sha256 } = require('js-sha256');

const { KeyPair, KeyPairEd25519, PublicKey } = require('../lib');
Expand Down
2 changes: 1 addition & 1 deletion packages/near-api-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"ajv": "^8.11.2",
"ajv-formats": "^2.1.1",
"bn.js": "5.2.1",
"borsh": "^0.7.0",
"borsh": "1.0.0",
"depd": "^2.0.0",
"error-polyfill": "^0.1.3",
"http-errors": "^1.7.2",
Expand Down
12 changes: 6 additions & 6 deletions packages/near-api-js/src/utils/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export {
baseEncode as base_encode,
baseDecode as base_decode,
serialize,
deserialize,
Schema,
BorshError,
BinaryWriter,
BinaryReader,
} from 'borsh';
} from 'borsh';

export {
baseEncode as base_encode,
baseDecode as base_decode,
} from '@near-js/utils';
2 changes: 1 addition & 1 deletion packages/providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@near-js/types": "workspace:*",
"@near-js/utils": "workspace:*",
"bn.js": "5.2.1",
"borsh": "^0.7.0",
"borsh": "1.0.0",
"http-errors": "^1.7.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/providers/src/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @see {@link providers/provider | providers} for a list of request and response types
*/
import {
baseEncode,
getErrorTypeFromErrorMessage,
parseRpcError,
} from '@near-js/utils';
Expand Down Expand Up @@ -34,7 +35,6 @@ import {
encodeTransaction,
SignedTransaction,
} from '@near-js/transactions';
import { baseEncode } from 'borsh';

import { exponentialBackoff } from './exponential-backoff';
import { Provider } from './provider';
Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@near-js/types": "workspace:*",
"@near-js/utils": "workspace:*",
"bn.js": "5.2.1",
"borsh": "^0.7.0",
"borsh": "1.0.0",
"js-sha256": "^0.9.0"
},
"devDependencies": {
Expand Down
8 changes: 5 additions & 3 deletions packages/transactions/src/action_creators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Signature } from './signature';

function fullAccessKey(): AccessKey {
return new AccessKey({
nonce: 0,
permission: new AccessKeyPermission({
fullAccess: new FullAccessPermission({}),
})
Expand All @@ -30,6 +31,7 @@ function fullAccessKey(): AccessKey {

function functionCallAccessKey(receiverId: string, methodNames: string[], allowance?: BN): AccessKey {
return new AccessKey({
nonce: 0,
permission: new AccessKeyPermission({
functionCall: new FunctionCallPermission({ receiverId, allowance, methodNames }),
})
Expand Down Expand Up @@ -60,7 +62,7 @@ export function stringifyJsonOrBytes(args: any): Buffer {
* @param stringify Convert input arguments into bytes array.
* @param jsContract Is contract from JS SDK, skips stringification of arguments.
*/
function functionCall(methodName: string, args: Uint8Array | object, gas: BN, deposit: BN, stringify = stringifyJsonOrBytes, jsContract = false): Action {
function functionCall(methodName: string, args: Uint8Array | object, gas: BN = new BN(0), deposit: BN = new BN(0), stringify = stringifyJsonOrBytes, jsContract = false): Action {
if(jsContract){
return new Action({ functionCall: new FunctionCall({ methodName, args, gas, deposit }) });
}
Expand All @@ -75,11 +77,11 @@ function functionCall(methodName: string, args: Uint8Array | object, gas: BN, de
});
}

function transfer(deposit: BN): Action {
function transfer(deposit: BN = new BN(0)): Action {
return new Action({ transfer: new Transfer({ deposit }) });
}

function stake(stake: BN, publicKey: PublicKey): Action {
function stake(stake: BN = new BN(0), publicKey: PublicKey): Action {
return new Action({ stake: new Stake({ stake, publicKey }) });
}

Expand Down
1 change: 1 addition & 0 deletions packages/transactions/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class AccessKeyPermission extends Enum {
}

export class AccessKey extends Assignable {
nonce: BN;
permission: AccessKeyPermission;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/src/create_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import { Action } from './actions';
import { Transaction } from './schema';

export function createTransaction(signerId: string, publicKey: PublicKey, receiverId: string, nonce: BN | string | number, actions: Action[], blockHash: Uint8Array): Transaction {
return new Transaction({ signerId, publicKey, nonce, receiverId, actions, blockHash });
return new Transaction({ signerId, publicKey, nonce: new BN(nonce), receiverId, actions, blockHash });
}
Loading

0 comments on commit 61349ae

Please sign in to comment.