Skip to content

Commit

Permalink
Merge pull request #99 from dallasjohnson/feature/Action-options-for-…
Browse files Browse the repository at this point in the history
…more-granular-permissions

Adds `auths` option to the options parameter when calling a contract action
  • Loading branch information
MitchPierias authored Oct 8, 2019
2 parents 100f771 + d77e1fe commit 358d803
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
21 changes: 15 additions & 6 deletions src/contracts/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export interface GetTableRowsOptions {
showPayer?: boolean;
}

export interface ActorPermission {
actor: string;
permission: string;
}

/**
* Adds additional functionality to the EOSJS `Contract` class
*/
Expand Down Expand Up @@ -108,23 +113,27 @@ export class Contract implements EOSJSContract {

// Who are we acting as?
// We default to sending transactions from the contract account.
let authorization = account;
let authorization: Array<ActorPermission> = account.active;
const options = arguments[action.fields.length];

if (options && options.from && options.from instanceof Account) {
authorization = options.from;
}
if (options) {
if (options.from && options.from instanceof Account) {
authorization = options.from.active;

// Ensure we have the key to sign with.
EOSManager.addSigningAccountIfMissing(authorization);
EOSManager.addSigningAccountIfMissing(options.from);
} else if (options.auths && options.auths instanceof Array) {
authorization = options.auths;
}
}

return EOSManager.transact(
{
actions: [
{
account: account.name,
name: action.name,
authorization: authorization.active,
authorization: authorization,
data,
},
],
Expand Down
8 changes: 6 additions & 2 deletions src/contracts/typeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ export const generateTypes = async (contractIdentifier: string) => {
'',
];
// Define imports
const imports = ['Account', 'Contract', 'GetTableRowsOptions'];
const imports = [
'Account',
'Contract',
'GetTableRowsOptions',
];
if (contractTables.length > 0) imports.push('TableRowsResult');
// Generate import definitions
result.push(`import { ${imports.join(', ')} } from 'lamington';`);
Expand All @@ -99,7 +103,7 @@ export const generateTypes = async (contractIdentifier: string) => {
(parameter: any) => `${parameter.name}: ${mapParameterType(parameter.type)}`
);
// Optional parameter at the end on every contract method.
parameters.push('options?: { from?: Account }');
parameters.push('options?: { from?: Account, auths?: ActorPermission[] }');

return `${action.name}(${parameters.join(', ')}): Promise<any>;`;
});
Expand Down

0 comments on commit 358d803

Please sign in to comment.