Skip to content

Commit

Permalink
feat(account): accept async function in authData of AccountGeneralized
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Feb 8, 2023
1 parent 0bec5b5 commit c1066c5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/account/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import { Encoded } from '../utils/encoder';
import Node from '../Node';
import CompilerBase from '../contract/compiler/Base';

interface AuthData {
gasLimit?: number;
callData?: Encoded.ContractBytearray;
sourceCode?: string;
args?: any[];
}

/**
* Account is one of the three basic building blocks of an
* {@link AeSdk} and provides access to a signing key pair.
Expand All @@ -36,12 +43,7 @@ export default abstract class AccountBase {
options: {
innerTx?: boolean;
networkId?: string;
authData?: {
gasLimit?: number;
callData?: Encoded.ContractBytearray;
sourceCode?: string;
args?: any[];
};
authData?: AuthData | ((tx: Encoded.Transaction) => Promise<AuthData>);
onNode?: Node;
onCompiler?: CompilerBase;
aeppOrigin?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/account/Generalized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class AccountGeneralized extends AccountBase {
}
const {
callData, sourceCode, args, gasLimit,
} = authData;
} = typeof authData === 'function' ? await authData(tx) : authData;

const authCallData = callData ?? await (async () => {
if (this.#authFun == null) {
Expand Down
15 changes: 15 additions & 0 deletions test/integration/ga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from '../../src';
import { Encoded } from '../../src/utils/encoder';
import { ContractMethodsBase } from '../../src/contract/Contract';
import { ensureEqual } from '../utils';

const sourceCode = `contract BlindAuth =
record state = { txHash: option(hash) }
Expand Down Expand Up @@ -106,6 +107,20 @@ describe('Generalized Account', () => {
.eql((await authContract.getTxHash()).decodedResult);
});

it('accepts a function in authData', async () => {
let spendTx;
const { rawTx } = await aeSdk.spend(10000, publicKey, {
authData: async (tx) => {
spendTx = tx;
return { sourceCode, args: [genSalt()] };
},
});
const txParams = unpackTx(rawTx, Tag.SignedTx);
ensureEqual<Tag.GaMetaTx>(txParams.encodedTx.tag, Tag.GaMetaTx);
ensureEqual<Tag.SignedTx>(txParams.encodedTx.tx.tag, Tag.SignedTx);
expect(buildTx(txParams.encodedTx.tx.encodedTx)).to.be.equal(spendTx);
});

it('fails trying to send SignedTx using generalized account', async () => {
await expect(aeSdk.spend(1, gaAccountAddress, { onAccount: accountBeforeGa })).to.be
.rejectedWith('Generalized account can\'t be used to generate SignedTx with signatures');
Expand Down

0 comments on commit c1066c5

Please sign in to comment.