Skip to content

Commit

Permalink
Fixed eosio.code permission granting for accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchPierias committed Apr 24, 2019
1 parent 095f680 commit e351836
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/accounts/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Contract } from '../contracts';
import { AccountManager } from './accountManager';

export class Account {

public name: string;
public publicKey: string;
public privateKey: string;
Expand Down Expand Up @@ -45,6 +46,6 @@ export class Account {
}

public addCodePermission = async (contract: Contract) => {
await AccountManager.addCodePermission(this, contract);
await AccountManager.addCodePermission(this);
};
}
22 changes: 16 additions & 6 deletions src/accounts/accountManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,38 @@ export class AccountManager {

return await EOSManager.transact({ actions }, eos);
};

static addCodePermission = async (account: Account, contract: Contract) => {

/**
* Grants `eosio.code` permission to the specified account's `active` key
* @note Should be moved to the `contracts/contract.ts` I think?
* @note Actually it is `account` based and not specific to contracts...
* @author Kevin Brown
* @author Mitch Pierias
* @param account Account without `eosio.code` permissions
*/
static addCodePermission = async (account: Account) => {
// We need to get their existing permissions, then add in a new eosio.code permission for this contract.
const { permissions } = await EOSManager.rpc.get_account(account.name);
const active = permissions.find((permission: any) => permission.perm_name == 'active');

const auth = active.required_auth;
const existingPermission = auth.accounts.find(
(account: any) =>
account.permission.actor === contract.account.name &&
account.permission.actor === account.name &&
account.permission.permission === 'eosio.code'
);

if (existingPermission) {
throw new Error(
`Code permission is already present on account ${account.name} for contract ${
contract.account.name
account.name
}`
);
}

// Add it in.
auth.accounts.push({
permission: { actor: contract.account.name, permission: 'eosio.code' },
permission: { actor: account.name, permission: 'eosio.code' },
weight: 1,
});

Expand All @@ -160,6 +168,8 @@ export class AccountManager {
},
];

//console.log(JSON.stringify(actions, null, '\t'))

await EOSManager.transact({ actions });
};

Expand Down

0 comments on commit e351836

Please sign in to comment.