Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AccountMultisig - update for near-cli support #414

Merged
merged 4 commits into from
Oct 2, 2020
Merged
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
2 changes: 0 additions & 2 deletions lib/account.js

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

7 changes: 4 additions & 3 deletions lib/account_multisig.d.ts

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

44 changes: 23 additions & 21 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
46 changes: 25 additions & 21 deletions src/account_multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,38 @@ 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');
export const MULTISIG_CHANGE_METHODS = ['add_request', 'add_request_and_confirm', 'delete_request', '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 = <MultisigContract>getContract(this);
}

Expand Down Expand Up @@ -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.")
}
}
}
Expand All @@ -118,11 +127,17 @@ export class AccountMultisig extends Account {
}

getRequest() {
return JSON.parse(localStorage.getItem(`__multisigRequest`) || `{}`)
mattlockyer marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -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())
}));
}
}

Expand All @@ -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) {
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.