Skip to content

Commit

Permalink
refactor: remove reimplimented code
Browse files Browse the repository at this point in the history
  • Loading branch information
esaminu committed Mar 25, 2022
1 parent 7071651 commit 037dde4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
37 changes: 19 additions & 18 deletions lib/account_multisig.js

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

47 changes: 24 additions & 23 deletions src/account_multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import { Account, SignAndSendTransactionOptions } from './account';
import { Connection } from './connection';
import { parseNearAmount } from './utils/format';
import { PublicKey } from './utils/key_pair';
import { Action, addKey, deleteKey, deployContract, fullAccessKey, functionCall, functionCallAccessKey, signTransaction } from './transaction';
import { Action, addKey, deleteKey, deployContract, fullAccessKey, functionCall, functionCallAccessKey } from './transaction';
import { FinalExecutionOutcome, TypedError } from './providers';
import { fetchJson } from './utils/web';
import { AccessKeyView, FunctionCallPermissionView } from './providers/provider';
import { baseDecode } from 'borsh';
import { FunctionCallPermissionView } from './providers/provider';

export const MULTISIG_STORAGE_KEY = '__multisigRequest';
export const MULTISIG_ALLOWANCE = new BN(parseNearAmount('1'));
Expand Down Expand Up @@ -316,30 +315,32 @@ export class Account2FA extends AccountMultisig {
}

async disableWithFAK({ contractBytes, cleanupContractBytes }: { contractBytes: Uint8Array, cleanupContractBytes?: Uint8Array }) {
const publicKey = await this.connection.signer.getPublicKey(this.accountId, this.connection.networkId);
const accessKey = await this.connection.provider.query<AccessKeyView>({
request_type: 'view_access_key',
account_id: this.accountId,
public_key: publicKey.toString(),
finality: 'optimistic'
});
const block = await this.connection.provider.block({ finality: 'final' });
const blockHash = block.header.hash;
let cleanupActions = [];
const keyConversionActions = await this.get2faDisableKeyConversionActions();
if(cleanupContractBytes) {
let deleteAllRequestsError;
await this.deleteAllRequests().catch(e => deleteAllRequestsError = e);
cleanupActions = await this.get2faDisableCleanupActions(cleanupContractBytes).catch(e => {
if(e.type === 'ContractHasExistingState') {
throw deleteAllRequestsError || e;
}
throw e;
});
}

const actions = [
...cleanupActions,
...keyConversionActions,
deployContract(contractBytes)
];

const accessKeyInfo = await this.findAccessKey(this.accountId, actions);

if(accessKey.permission !== 'FullAccess') {
if(accessKeyInfo?.accessKey?.permission !== 'FullAccess') {
throw new TypedError(`No full access key found in keystore. Unable to bypass multisig`, 'NoFAKFound');
}

const nonce = ++accessKey.nonce;
const actions = [
...(cleanupContractBytes ? await this.get2faDisableCleanupActions(cleanupContractBytes) : []),
...(await this.get2faDisableKeyConversionActions()),
deployContract(contractBytes),
]
const [, signedTx] = await signTransaction(
this.accountId, nonce, actions, baseDecode(blockHash), this.connection.signer, this.accountId, this.connection.networkId
);
return this.connection.provider.sendTransaction(signedTx)
return this.signAndSendTransactionWithAccount(this.accountId, actions);
}

async get2faDisableCleanupActions(cleanupContractBytes: Uint8Array) {
Expand Down

0 comments on commit 037dde4

Please sign in to comment.