diff --git a/lib/account.js b/lib/account.js index 06f0cc52f9..83d1829bd0 100644 --- a/lib/account.js +++ b/lib/account.js @@ -92,8 +92,6 @@ class Account { const result = await exponential_backoff_1.default(TX_STATUS_RETRY_WAIT, TX_NONCE_RETRY_NUMBER, TX_STATUS_RETRY_WAIT_BACKOFF, async () => { [txHash, signedTx] = await this.signTransaction(receiverId, actions); const publicKey = signedTx.transaction.publicKey; - console.warn('pubkey', publicKey); - throw new Error('publicKey ' + publicKey); try { const result = await exponential_backoff_1.default(TX_STATUS_RETRY_WAIT, TX_STATUS_RETRY_NUMBER, TX_STATUS_RETRY_WAIT_BACKOFF, async () => { try { diff --git a/lib/account_multisig.d.ts b/lib/account_multisig.d.ts index 529e0c8e31..69d33bfa8b 100644 --- a/lib/account_multisig.d.ts +++ b/lib/account_multisig.d.ts @@ -4,6 +4,7 @@ import { Connection } from './connection'; import { PublicKey } from './utils/key_pair'; import { Action } from './transaction'; import { FinalExecutionOutcome } from './providers'; +export declare const MULTISIG_STORAGE_KEY = "__multisigRequest"; export declare const MULTISIG_ALLOWANCE: BN; export declare const MULTISIG_GAS: BN; export declare const MULTISIG_DEPOSIT: BN; @@ -19,8 +20,8 @@ interface MultisigContract { } export declare class AccountMultisig extends Account { contract: MultisigContract; - pendingRequest: any; - constructor(connection: Connection, accountId: string); + storage: any; + constructor(connection: Connection, accountId: string, storage: any); addKey(publicKey: string | PublicKey, contractId?: string, methodName?: string, amount?: BN): Promise; signAndSendTransaction(receiverId: string, actions: Action[]): Promise; signAndSendTransactions(transactions: any): Promise; @@ -30,7 +31,7 @@ export declare class AccountMultisig extends Account { getRequestIds(): Promise; isDeleteAction(actions: any): Boolean; getRequest(): any; - setRequest(data: any): void; + setRequest(data: any): any; sendRequestCode(): Promise; verifyRequestCode(securityCode: string): Promise; getRecoveryMethods(): Promise<{ diff --git a/lib/account_multisig.js b/lib/account_multisig.js index b08a334ec5..18dfcdfb35 100644 --- a/lib/account_multisig.js +++ b/lib/account_multisig.js @@ -3,15 +3,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.AccountMultisig = exports.MULTISIG_CONFIRM_METHODS = exports.MULTISIG_VIEW_METHODS = exports.MULTISIG_CHANGE_METHODS = exports.MULTISIG_DEPOSIT = exports.MULTISIG_GAS = exports.MULTISIG_ALLOWANCE = void 0; +exports.AccountMultisig = exports.MULTISIG_CONFIRM_METHODS = exports.MULTISIG_VIEW_METHODS = exports.MULTISIG_CHANGE_METHODS = exports.MULTISIG_DEPOSIT = exports.MULTISIG_GAS = exports.MULTISIG_ALLOWANCE = exports.MULTISIG_STORAGE_KEY = void 0; const bn_js_1 = __importDefault(require("bn.js")); const account_1 = require("./account"); const contract_1 = require("./contract"); const format_1 = require("./utils/format"); const key_pair_1 = require("./utils/key_pair"); const transaction_1 = require("./transaction"); +const web_1 = require("./utils/web"); const NETWORK_ID = process.env.REACT_APP_NETWORK_ID || 'default'; const CONTRACT_HELPER_URL = process.env.CONTRACT_HELPER_URL || 'https://helper.testnet.near.org'; +exports.MULTISIG_STORAGE_KEY = '__multisigRequest'; exports.MULTISIG_ALLOWANCE = new bn_js_1.default(process.env.MULTISIG_ALLOWANCE || format_1.parseNearAmount('1')); exports.MULTISIG_GAS = new bn_js_1.default(process.env.MULTISIG_GAS || '100000000000000'); exports.MULTISIG_DEPOSIT = new bn_js_1.default('0'); @@ -19,9 +21,14 @@ exports.MULTISIG_CHANGE_METHODS = ['add_request', 'add_request_and_confirm', 'de exports.MULTISIG_VIEW_METHODS = ['get_request_nonce', 'list_request_ids']; exports.MULTISIG_CONFIRM_METHODS = ['confirm']; ; +// in memory request cache for node w/o localStorage +let storageFallback = { + [exports.MULTISIG_STORAGE_KEY]: null +}; class AccountMultisig extends account_1.Account { - constructor(connection, accountId) { + constructor(connection, accountId, storage) { super(connection, accountId); + this.storage = storage; this.contract = getContract(this); } async addKey(publicKey, contractId, methodName, amount) { @@ -81,7 +88,7 @@ class AccountMultisig extends account_1.Account { await contract.delete_request({ request_id }); } catch (e) { - console.warn(e); + console.warn("Attempt to delete an earlier request before 15 minutes failed. Will try again."); } } } @@ -96,10 +103,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 (this.storage) { + return JSON.parse(this.storage.getItem(exports.MULTISIG_STORAGE_KEY) || `{}`); + } + return storageFallback[exports.MULTISIG_STORAGE_KEY]; } setRequest(data) { - localStorage.setItem(`__multisigRequest`, JSON.stringify(data)); + if (this.storage) { + return this.storage.setItem(exports.MULTISIG_STORAGE_KEY, JSON.stringify(data)); + } + storageFallback[exports.MULTISIG_STORAGE_KEY] = data; } // default helpers for CH async sendRequestCode() { @@ -154,21 +167,10 @@ class AccountMultisig extends account_1.Account { return { blockNumber, blockNumberSignature }; } async postSignedJson(path, body) { - const response = await fetch(CONTRACT_HELPER_URL + path, { - method: 'POST', - body: JSON.stringify({ - ...body, - ...(await this.signatureFor()) - }), - headers: { 'Content-type': 'application/json; charset=utf-8' } - }); - if (!response.ok) { - throw new Error(response.status + ': ' + await response.text()); - } - if (response.status === 204) { - return null; - } - return await response.json(); + return await web_1.fetchJson(CONTRACT_HELPER_URL + path, JSON.stringify({ + ...body, + ...(await this.signatureFor()) + })); } } exports.AccountMultisig = AccountMultisig; @@ -192,7 +194,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) { diff --git a/package.json b/package.json index b242907523..8a2c79feda 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/account_multisig.ts b/src/account_multisig.ts index f0bd7dabb1..c675382401 100644 --- a/src/account_multisig.ts +++ b/src/account_multisig.ts @@ -8,10 +8,12 @@ import { parseNearAmount } from './utils/format'; import { PublicKey } from './utils/key_pair'; import { Action, addKey, deleteKey, deployContract, functionCall, functionCallAccessKey } from './transaction'; import { FinalExecutionOutcome } from './providers'; +import { fetchJson } from './utils/web'; const NETWORK_ID = process.env.REACT_APP_NETWORK_ID || 'default' const CONTRACT_HELPER_URL = process.env.CONTRACT_HELPER_URL || 'https://helper.testnet.near.org'; +export const MULTISIG_STORAGE_KEY = '__multisigRequest' export const MULTISIG_ALLOWANCE = new BN(process.env.MULTISIG_ALLOWANCE || parseNearAmount('1')); export const MULTISIG_GAS = new BN(process.env.MULTISIG_GAS || '100000000000000'); export const MULTISIG_DEPOSIT = new BN('0'); @@ -19,18 +21,25 @@ 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 storageFallback = { + [MULTISIG_STORAGE_KEY]: null +} + export class AccountMultisig extends Account { public contract: MultisigContract; - public pendingRequest: any; + public storage: any; - constructor(connection: Connection, accountId: string) { + constructor(connection: Connection, accountId: string, storage: any) { super(connection, accountId); + this.storage = storage; this.contract = getContract(this); } @@ -98,7 +107,7 @@ export class AccountMultisig extends Account { try { await contract.delete_request({ request_id }) } catch(e) { - console.warn(e) + console.warn("Attempt to delete an earlier request before 15 minutes failed. Will try again.") } } } @@ -118,11 +127,17 @@ export class AccountMultisig extends Account { } getRequest() { - return JSON.parse(localStorage.getItem(`__multisigRequest`) || `{}`) + if (this.storage) { + return JSON.parse(this.storage.getItem(MULTISIG_STORAGE_KEY) || `{}`) + } + return storageFallback[MULTISIG_STORAGE_KEY] } setRequest(data) { - localStorage.setItem(`__multisigRequest`, JSON.stringify(data)) + if (this.storage) { + return this.storage.setItem(MULTISIG_STORAGE_KEY, JSON.stringify(data)) + } + storageFallback[MULTISIG_STORAGE_KEY] = data } // default helpers for CH @@ -183,21 +198,10 @@ export class AccountMultisig extends Account { } async postSignedJson(path, body) { - const response = await fetch(CONTRACT_HELPER_URL + path, { - method: 'POST', - body: JSON.stringify({ - ...body, - ...(await this.signatureFor()) - }), - headers: { 'Content-type': 'application/json; charset=utf-8' } - }); - if (!response.ok) { - throw new Error(response.status + ': ' + await response.text()); - } - if (response.status === 204) { - return null; - } - return await response.json(); + return await fetchJson(CONTRACT_HELPER_URL + path, JSON.stringify({ + ...body, + ...(await this.signatureFor()) + })); } } @@ -223,7 +227,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) { diff --git a/yarn.lock b/yarn.lock index 3f98a02317..676e342c7d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3696,7 +3696,7 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== -node-fetch@^2.3.0: +node-fetch@^2.6.1: version "2.6.1" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==