Skip to content

Commit

Permalink
more node aware code and added node-fetch for CH methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlockyer committed Sep 29, 2020
1 parent c524091 commit 9325ac1
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 30 deletions.
44 changes: 25 additions & 19 deletions dist/near-api-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,25 @@ exports.MULTISIG_DEPOSIT = new bn_js_1.default('0');
exports.MULTISIG_CHANGE_METHODS = ['add_request', 'add_request_and_confirm', 'delete_request', 'confirm'];
exports.MULTISIG_VIEW_METHODS = ['get_request_nonce', 'list_request_ids'];
exports.MULTISIG_CONFIRM_METHODS = ['confirm'];
const IS_BROWSER = typeof window !== 'undefined';
;
// in memory request cache for node w/o localStorage
let __multisigRequest = null;
class AccountMultisig extends account_1.Account {
constructor(connection, accountId) {
super(connection, accountId);
this.contract = getContract(this);
}
// overrides
// async findAccessKey(receiverId, actions) {
// const accessKeys = await this.getAccessKeys()
// const accessKey = accessKeys.find((k) =>
// k.access_key.permission &&
// k.access_key.permission.FunctionCall &&
// k.access_key.permission.FunctionCall.method_names.some((mn) => MULTISIG_CHANGE_METHODS.includes(mn))
// );
// return { publicKey: accessKey.public_key, accessKey: accessKey.access_key}
// }
async addKey(publicKey, contractId, methodName, amount) {
if (contractId) {
return super.addKey(publicKey, contractId, exports.MULTISIG_CHANGE_METHODS.join(), exports.MULTISIG_ALLOWANCE);
Expand Down Expand Up @@ -492,10 +505,16 @@ class AccountMultisig extends account_1.Account {
return actions && actions[0] && actions[0].functionCall && actions[0].functionCall.methodName === 'delete_request';
}
getRequest() {
return JSON.parse(localStorage.getItem(`__multisigRequest`) || `{}`);
if (IS_BROWSER) {
return JSON.parse(localStorage.getItem(`__multisigRequest`) || `{}`);
}
return __multisigRequest;
}
setRequest(data) {
localStorage.setItem(`__multisigRequest`, JSON.stringify(data));
if (IS_BROWSER) {
return localStorage.setItem(`__multisigRequest`, JSON.stringify(data));
}
__multisigRequest = data;
}
// default helpers for CH
async sendRequestCode() {
Expand Down Expand Up @@ -588,7 +607,7 @@ const convertActions = (actions, accountId, receiverId) => actions.map((a) => {
args: (args && Buffer.from(args).toString('base64')) || undefined,
code: (code && Buffer.from(code).toString('base64')) || undefined,
amount: (deposit && deposit.toString()) || undefined,
deposit: (deposit && deposit.toString()) || undefined,
deposit: (deposit && deposit.toString()) || '0',
permission: undefined,
};
if (accessKey) {
Expand Down Expand Up @@ -2370,7 +2389,7 @@ class JsonRpcProvider extends provider_1.Provider {
*/
async sendTransaction(signedTransaction) {
const bytes = signedTransaction.encode();
return this.sendJsonRpc('broadcast_tx_commit', [Buffer.from(bytes).toString('base64')]).then(provider_1.adaptTransactionResult);
return this.sendJsonRpc('broadcast_tx_commit', [Buffer.from(bytes).toString('base64')]);
}
/**
* Gets a transaction's status from the RPC
Expand All @@ -2380,7 +2399,7 @@ class JsonRpcProvider extends provider_1.Provider {
* @returns {Promise<FinalExecutionOutcome>}
*/
async txStatus(txHash, accountId) {
return this.sendJsonRpc('tx', [serialize_1.base_encode(txHash), accountId]).then(provider_1.adaptTransactionResult);
return this.sendJsonRpc('tx', [serialize_1.base_encode(txHash), accountId]);
}
/**
* Query the RPC as [shown in the docs](https://docs.nearprotocol.com/docs/interaction/rpc#query)
Expand Down Expand Up @@ -2494,7 +2513,7 @@ exports.JsonRpcProvider = JsonRpcProvider;
(function (Buffer){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.adaptTransactionResult = exports.getTransactionLastResult = exports.Provider = exports.IdType = exports.FinalExecutionStatusBasic = exports.ExecutionStatusBasic = void 0;
exports.getTransactionLastResult = exports.Provider = exports.IdType = exports.FinalExecutionStatusBasic = exports.ExecutionStatusBasic = void 0;
var ExecutionStatusBasic;
(function (ExecutionStatusBasic) {
ExecutionStatusBasic["Unknown"] = "Unknown";
Expand Down Expand Up @@ -2528,19 +2547,6 @@ function getTransactionLastResult(txResult) {
return null;
}
exports.getTransactionLastResult = getTransactionLastResult;
function adaptTransactionResult(txResult) {
if ('receipts' in txResult) {
txResult = {
status: txResult.status,
// not available
transaction: null,
transaction_outcome: txResult.transaction,
receipts_outcome: txResult.receipts
};
}
return txResult;
}
exports.adaptTransactionResult = adaptTransactionResult;

}).call(this,require("buffer").Buffer)
},{"buffer":43}],22:[function(require,module,exports){
Expand Down
6 changes: 3 additions & 3 deletions dist/near-api-js.min.js

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions lib/account_multisig.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"http-errors": "^1.7.2",
"js-sha256": "^0.9.0",
"mustache": "^4.0.0",
"node-fetch": "^2.3.0",
"node-fetch": "^2.6.1",
"text-encoding-utf-8": "^1.0.2",
"tweetnacl": "^1.0.1"
},
Expand Down
1 change: 1 addition & 0 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class Account {
const status = await this.connection.provider.status();

const nonce = ++accessKey.nonce;

[txHash, signedTx] = await signTransaction(
receiverId, nonce, actions, base_decode(status.sync_info.latest_block_hash), this.connection.signer, this.accountId, this.connection.networkId
);
Expand Down
23 changes: 20 additions & 3 deletions src/account_multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { PublicKey } from './utils/key_pair';
import { Action, addKey, deleteKey, deployContract, functionCall, functionCallAccessKey } from './transaction';
import { FinalExecutionOutcome } from './providers';

const IS_BROWSER = typeof window !== 'undefined'

let fetch = IS_BROWSER && window.fetch
if (!IS_BROWSER) {
fetch = require('node-fetch');
}

const NETWORK_ID = process.env.REACT_APP_NETWORK_ID || 'default'
const CONTRACT_HELPER_URL = process.env.CONTRACT_HELPER_URL || 'https://helper.testnet.near.org';

Expand All @@ -19,12 +26,16 @@ export const MULTISIG_CHANGE_METHODS = ['add_request', 'add_request_and_confirm'
export const MULTISIG_VIEW_METHODS = ['get_request_nonce', 'list_request_ids'];
export const MULTISIG_CONFIRM_METHODS = ['confirm'];


interface MultisigContract {
get_request_nonce(): any,
list_request_ids(): any,
delete_request({ request_id: Number }): any,
};

// in memory request cache for node w/o localStorage
let __multisigRequest = null

export class AccountMultisig extends Account {
public contract: MultisigContract;
public pendingRequest: any;
Expand Down Expand Up @@ -118,11 +129,17 @@ export class AccountMultisig extends Account {
}

getRequest() {
return JSON.parse(localStorage.getItem(`__multisigRequest`) || `{}`)
if (IS_BROWSER) {
return JSON.parse(localStorage.getItem(`__multisigRequest`) || `{}`)
}
return __multisigRequest
}

setRequest(data) {
localStorage.setItem(`__multisigRequest`, JSON.stringify(data))
if (IS_BROWSER) {
return localStorage.setItem(`__multisigRequest`, JSON.stringify(data))
}
__multisigRequest = data
}

// default helpers for CH
Expand Down Expand Up @@ -223,7 +240,7 @@ const convertActions = (actions, accountId, receiverId) => actions.map((a) => {
args: (args && Buffer.from(args).toString('base64')) || undefined,
code: (code && Buffer.from(code).toString('base64')) || undefined,
amount: (deposit && deposit.toString()) || undefined,
deposit: (deposit && deposit.toString()) || undefined,
deposit: (deposit && deposit.toString()) || '0',
permission: undefined,
};
if (accessKey) {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock

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

0 comments on commit 9325ac1

Please sign in to comment.