Skip to content

Commit

Permalink
Added attempt to get eosio.code permission adding working.
Browse files Browse the repository at this point in the history
  • Loading branch information
thekevinbrown committed Apr 23, 2019
1 parent 7828425 commit 7fb1077
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/accounts/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Permissions } from './permissions';
import * as ecc from 'eosjs-ecc';
import { Contract } from '../contracts';
import { AccountManager } from './accountManager';

export class Account {
public name: string;
Expand Down Expand Up @@ -41,4 +43,8 @@ export class Account {
},
];
}

public addCodePermission = async (contract: Contract) => {
await AccountManager.addCodePermission(this, contract);
};
}
44 changes: 44 additions & 0 deletions src/accounts/accountManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as ecc from 'eosjs-ecc';
import { Account } from './account';
import { accountNameFromPublicKey } from './utils';
import { EOSManager } from '../eosManager';
import { Contract } from '../contracts';

interface AccountCreationOptions {
creator?: Account;
Expand Down Expand Up @@ -119,6 +120,49 @@ export class AccountManager {
return await EOSManager.transact({ actions }, eos);
};

static addCodePermission = async (account: Account, contract: Contract) => {
// 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.permission === 'eosio.code'
);

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

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

const actions: any = [
{
account: 'eosio',
name: 'updateauth',
authorization: account.owner,
data: {
account: account.name,
permission: 'active',
parent: 'owner',
auth,
},
},
];

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

private static flattenOptions(options?: AccountCreationOptions) {
const creator = (options && options.creator) || EOSManager.adminAccount;
const eos = (options && options.eos) || EOSManager.api;
Expand Down

0 comments on commit 7fb1077

Please sign in to comment.