From 7fb1077f4065a2cf3d292fa8e1e66f8e06aba0ee Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Tue, 23 Apr 2019 19:37:41 +1000 Subject: [PATCH] Added attempt to get `eosio.code` permission adding working. --- src/accounts/account.ts | 6 +++++ src/accounts/accountManager.ts | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/accounts/account.ts b/src/accounts/account.ts index e2bbef7..c252b74 100644 --- a/src/accounts/account.ts +++ b/src/accounts/account.ts @@ -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; @@ -41,4 +43,8 @@ export class Account { }, ]; } + + public addCodePermission = async (contract: Contract) => { + await AccountManager.addCodePermission(this, contract); + }; } diff --git a/src/accounts/accountManager.ts b/src/accounts/accountManager.ts index 95ea772..35b7bdf 100644 --- a/src/accounts/accountManager.ts +++ b/src/accounts/accountManager.ts @@ -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; @@ -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;