Skip to content

Commit

Permalink
Merge branch 'master' into multi-contract-keystore
Browse files Browse the repository at this point in the history
  • Loading branch information
amirsaran3 authored Nov 1, 2023
2 parents 1949122 + 61349ae commit 474b673
Show file tree
Hide file tree
Showing 32 changed files with 295 additions and 187 deletions.
8 changes: 8 additions & 0 deletions .changeset/funny-jars-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@near-js/accounts": patch
"near-api-js": patch
"@near-js/providers": patch
"@near-js/utils": patch
---

add check for global 'process' object
5 changes: 5 additions & 0 deletions .changeset/nice-worms-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@near-js/biometric-ed25519": minor
---

Handing null response on create key flow
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
6 changes: 6 additions & 0 deletions .changeset/wet-jars-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"near-api-js": patch
"@near-js/providers": patch
---

fixes override of global fetch property
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
5 changes: 3 additions & 2 deletions 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 Expand Up @@ -363,7 +364,7 @@ export class Account {
* @param beneficiaryId The NEAR account that will receive the remaining Ⓝ balance from the account being deleted
*/
async deleteAccount(beneficiaryId: string) {
if (!process.env['NEAR_NO_LOGS']) {
if (!(typeof process === 'object' && process.env['NEAR_NO_LOGS'])) {
console.log('Deleting an account does not automatically transfer NFTs and FTs to the beneficiary address. Ensure to transfer assets before deleting.');
}
return this.signAndSendTransaction({
Expand Down
5 changes: 2 additions & 3 deletions packages/accounts/test/providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ test('json rpc query view_state', withProvider(async (provider) => {
block_height: expect.any(Number),
block_hash: expect.any(String),
values: [
{ key: 'bmFtZQ==', value: 'aGVsbG8=', proof: [] }
],
proof: []
{ key: 'bmFtZQ==', value: 'aGVsbG8=' }
]
});
});
}));
Expand Down
5 changes: 3 additions & 2 deletions packages/biometric-ed25519/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@near-js/biometric-ed25519",
"description": "JavaScript library to handle webauthn and biometric keys",
"version": "0.3.0",
"version": "0.4.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
Expand All @@ -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
13 changes: 12 additions & 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 Expand Up @@ -34,6 +34,13 @@ function setBufferIfUndefined() {
}
}

export class PasskeyProcessCanceled extends Error {
constructor(message) {
super(message);
this.name = 'PasskeyProcessCanceled';
}
}

export const createKey = async (username: string): Promise<KeyPair> => {
const cleanUserName = validateUsername(username);
if (!f2l.f2l) {
Expand All @@ -51,6 +58,10 @@ export const createKey = async (username: string): Promise<KeyPair> => {
setBufferIfUndefined();
return navigator.credentials.create({ publicKey })
.then(async (res) => {
if (!res) {
throw new PasskeyProcessCanceled('Failed to retrieve response from navigator.credentials.create');
}

const result = await f2l.attestation({
clientAttestationResponse: res,
origin,
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
5 changes: 1 addition & 4 deletions packages/near-api-js/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@
import { readKeyFile } from './key_stores/unencrypted_file_system_keystore';
import { InMemoryKeyStore, MergeKeyStore } from './key_stores';
import { Near, NearConfig } from './near';
import fetch from './utils/setup-node-fetch';
import { logWarning } from './utils';

global.fetch = fetch;

export interface ConnectConfig extends NearConfig {
/**
* Initialize an {@link key_stores/in_memory_key_store!InMemoryKeyStore} by reading the file at keyPath.
Expand Down Expand Up @@ -57,7 +54,7 @@ export async function connect(config: ConnectConfig): Promise<Near> {
keyPathStore,
config.keyStore || config.deps?.keyStore
], { writeKeyStoreIndex: 1 });
if (!process.env['NEAR_NO_LOGS']) {
if (!(typeof process === 'object' && process.env['NEAR_NO_LOGS'])) {
console.log(`Loaded master account ${accountKeyFile[0]} key from ${config.keyPath} with public key = ${keyPair.getPublicKey()}`);
}
}
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
7 changes: 2 additions & 5 deletions packages/providers/src/fetch_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface ConnectionInfo {
headers?: { [key: string]: string | number };
}

const logWarning = (...args) => !process.env['NEAR_NO_LOGS'] && console.warn(...args);
const logWarning = (...args) => !(typeof process === 'object' && process.env['NEAR_NO_LOGS']) && console.warn(...args);

export async function fetchJson(connectionInfoOrUrl: string | ConnectionInfo, json?: string): Promise<any> {
let connectionInfo: ConnectionInfo = { url: null };
Expand All @@ -28,11 +28,8 @@ export async function fetchJson(connectionInfoOrUrl: string | ConnectionInfo, js

const response = await exponentialBackoff(START_WAIT_TIME_MS, RETRY_NUMBER, BACKOFF_MULTIPLIER, async () => {
try {
if (!global.fetch) {
global.fetch = (await import('./fetch')).default;
}

const response = await global.fetch(connectionInfo.url, {
const response = await (global.fetch ?? (await import('./fetch')).default)(connectionInfo.url, {
method: json ? 'POST' : 'GET',
body: json ? json : undefined,
headers: { ...connectionInfo.headers, 'Content-Type': 'application/json' }
Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -372,7 +372,7 @@ export class JsonRpcProvider extends Provider {
return response;
} catch (error) {
if (error.type === 'TimeoutError') {
if (!process.env['NEAR_NO_LOGS']) {
if (!(typeof process === 'object' && process.env['NEAR_NO_LOGS'])) {
console.warn(`Retrying request to ${method} as it has timed out`, params);
}
return null;
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 474b673

Please sign in to comment.