Skip to content

Commit

Permalink
cleanup account_multisig, remove enable and add check to deployMultisig
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlockyer committed Oct 31, 2020
1 parent 9c7a62f commit a521ff5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/account_multisig.d.ts

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

54 changes: 50 additions & 4 deletions lib/account_multisig.js

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

62 changes: 57 additions & 5 deletions src/account_multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class AccountMultisig extends Account {
functionCall('add_request_and_confirm', args, MULTISIG_GAS, MULTISIG_DEPOSIT)
]);

await this.sendCode();
await this.sendCode()

const result = await this.promptAndVerify();
if (this.onResult) {
Expand All @@ -140,25 +140,71 @@ export class AccountMultisig extends Account {

async deployMultisig(contractBytes: Uint8Array) {
const { accountId } = this

console.log(await this.state(), (await this.state()).code_hash)

const initialize = (await this.state()).code_hash === "11111111111111111111111111111111"
// replace account keys & recovery keys with limited access keys; DO NOT replace seed phrase keys
const accountKeys = (await this.getAccessKeys()).map((ak) => ak.public_key)
const seedPhraseKeys = (await this.getRecoveryMethods()).data
.filter(({ kind, publicKey }) => kind === 'phrase' && publicKey !== null && accountKeys.includes(publicKey))
const seedOrLedgerKeys = (await this.getRecoveryMethods()).data
.filter(({ kind, publicKey }) => (kind === 'phrase' || kind === 'ledger') && publicKey !== null && accountKeys.includes(publicKey))
.map((rm) => rm.publicKey)
const fak2lak = accountKeys.filter((k) => !seedPhraseKeys.includes(k)).map(toPK)
const fak2lak = accountKeys.filter((k) => !seedOrLedgerKeys.includes(k)).map(toPK)
const confirmOnlyKey = toPK((await this.postSignedJson('/2fa/getAccessKey', { accountId })).publicKey)
const newArgs = new Uint8Array(new TextEncoder().encode(JSON.stringify({ 'num_confirmations': 2 })));
const actions = [
...fak2lak.map((pk) => deleteKey(pk)),
...fak2lak.map((pk) => addKey(pk, functionCallAccessKey(accountId, MULTISIG_CHANGE_METHODS, null))),
addKey(confirmOnlyKey, functionCallAccessKey(accountId, MULTISIG_CONFIRM_METHODS, null)),
deployContract(contractBytes),
functionCall('new', newArgs, MULTISIG_GAS, MULTISIG_DEPOSIT),
]
if (initialize) {
actions.push(functionCall('new', newArgs, MULTISIG_GAS, MULTISIG_DEPOSIT),)
}
console.log('deploying multisig contract for', accountId)
return await super.signAndSendTransaction(accountId, actions);
}

async disable(contractBytes: Uint8Array) {
const { accountId } = this
const accessKeys = await this.getAccessKeys()
const lak2fak = accessKeys.filter(({ access_key }) =>
access_key && access_key.permission && access_key.permission.FunctionCall &&
access_key.permission.FunctionCall.receiver_id === accountId &&
access_key.permission.FunctionCall.method_names &&
access_key.permission.FunctionCall.method_names.length === 4 &&
access_key.permission.FunctionCall.method_names.includes('add_request_and_confirm')
)
const confirmOnlyKey = PublicKey.from((await this.postSignedJson('/2fa/getAccessKey', { accountId })).publicKey)
const actions = [
deleteKey(confirmOnlyKey),
...lak2fak.map(({ public_key }) => deleteKey(public_key)),
...lak2fak.map(({ public_key }) => addKey(public_key, null)),
deployContract(contractBytes),
]
console.log('disabling 2fa for', accountId)
return await this.signAndSendTransaction(accountId, actions)
}

// async enable(contractBytes: Uint8Array) {
// const { accountId } = this
// const accountKeys = (await this.getAccessKeys()).map((ak) => ak.public_key)
// const seedPhraseKeys = (await this.getRecoveryMethods()).data
// .filter(({ kind, publicKey }) => kind === 'phrase' && publicKey !== null && accountKeys.includes(publicKey))
// .map((rm) => rm.publicKey)
// const confirmOnlyKey = (await this.postSignedJson('/2fa/getAccessKey', { accountId })).publicKey
// const fak2lak = accountKeys.filter((k) => !seedPhraseKeys.includes(k) && !confirmOnlyKey.includes(k))
// .map((k) => PublicKey.from(k))
// const actions = [
// addKey(PublicKey.from(confirmOnlyKey), functionCallAccessKey(accountId, MULTISIG_CONFIRM_METHODS, null)),
// ...fak2lak.map((k) => deleteKey(k)),
// ...fak2lak.map((k) => addKey(k, functionCallAccessKey(accountId, MULTISIG_CHANGE_METHODS, null))),
// deployContract(contractBytes),
// ]
// console.log('enabling 2fa for', accountId, actions)
// return await super.signAndSendTransaction(accountId, actions);
// }

async deleteUnconfirmedRequests () {
const { contract } = this
const request_ids = await this.getRequestIds()
Expand Down Expand Up @@ -208,6 +254,11 @@ export class AccountMultisig extends Account {
return
}
const method = await this.get2faMethod();
console.log('this.postSignedJson', {
accountId,
method,
requestId,
})
await this.postSignedJson('/2fa/send', {
accountId,
method,
Expand All @@ -225,6 +276,7 @@ export class AccountMultisig extends Account {
const securityCode = await this.getCode(method)
try {
const { success, res: result } = await this.verifyCode(securityCode);
console.log(success, result)
if (!success || result === false) {
throw new Error('Request failed with error: ' + JSON.stringify(result));
}
Expand Down
20 changes: 10 additions & 10 deletions test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const HELLO_WASM_BALANCE = new BN('10000000000000000000000000');
const HELLO_WASM_METHODS = {
viewMethods: ['getValue', 'getLastResult'],
changeMethods: ['setValue', 'callPromise']
}
};
const MULTISIG_WASM_PATH = process.env.MULTISIG_WASM_PATH || './test/wasm/multisig.wasm';

async function setUpTestConnection() {
Expand Down Expand Up @@ -47,25 +47,25 @@ async function createAccountMultisig(near, options) {
// add a confirm key for multisig (contract helper sim)

try {
const confirmKeyPair = nearApi.utils.KeyPair.fromRandom('ed25519')
const { publicKey } = confirmKeyPair
const confirmKeyPair = nearApi.utils.KeyPair.fromRandom('ed25519');
const { publicKey } = confirmKeyPair;
// const account = new nearApi.Account(near.connection, newAccountName);
// await account.addKey(publicKey, account.accountId, nearApi.multisig.MULTISIG_CONFIRM_METHODS, '0')
// create multisig account instance and deploy contract
const accountMultisig = new nearApi.multisig.AccountMultisig(near.connection, newAccountName, options);
accountMultisig.useConfirmKey = async () => {
await near.connection.signer.setKey(networkId, config.masterAccount, confirmKeyPair)
}
accountMultisig.getRecoveryMethods = () => ({ data: [] })
await near.connection.signer.setKey(networkId, config.masterAccount, confirmKeyPair);
};
accountMultisig.getRecoveryMethods = () => ({ data: [] });
accountMultisig.postSignedJson = async (path) => {
switch (path) {
case '/2fa/getAccessKey': return { publicKey }
case '/2fa/getAccessKey': return { publicKey };
}
}
await accountMultisig.deployMultisig(new Uint8Array([...(await fs.readFile(MULTISIG_WASM_PATH))]))
};
await accountMultisig.deployMultisig(new Uint8Array([...(await fs.readFile(MULTISIG_WASM_PATH))]));
return accountMultisig;
} catch(e) {
console.log(e)
console.log(e);
}
}

Expand Down

0 comments on commit a521ff5

Please sign in to comment.