Skip to content

Commit

Permalink
feat(account): introduce execute method
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Feb 9, 2022
1 parent ca4ad9d commit 0be78c6
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 38 deletions.
17 changes: 10 additions & 7 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('deploy and test Wallet', () => {
expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(1000).toString());
});
test('execute by wallet owner', async () => {
const { code, transaction_hash } = await account.invokeFunction({
const { code, transaction_hash } = await account.execute({
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [toBN(erc20Address).toString(), '10'],
Expand All @@ -105,12 +105,15 @@ describe('deploy and test Wallet', () => {
entrypoint: 'get_nonce',
});
const nonce = toBN(result[0]).toNumber();
const { code, transaction_hash } = await account.invokeFunction({
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [toBN(erc20Address).toString(), '10'],
nonce,
});
const { code, transaction_hash } = await account.execute(
{
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [toBN(erc20Address).toString(), '10'],
},
undefined,
{ nonce }
);

expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTx(transaction_hash);
Expand Down
48 changes: 36 additions & 12 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import assert from 'minimalistic-assert';
import { compileCalldata } from '../contract';
import { Provider } from '../provider';
import { Signer, SignerInterface } from '../signer';
import { Abi, AddTransactionResponse, Invocation, KeyPair, Signature } from '../types';
import {
Abi,
AddTransactionResponse,
Invocation,
InvocationsDetails,
KeyPair,
Signature,
} from '../types';
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
import { getSelectorFromName } from '../utils/stark';
import { TypedData, getMessageHash } from '../utils/typedData';
Expand Down Expand Up @@ -36,10 +43,23 @@ export class Account extends Provider implements AccountInterface {
* @param transaction - transaction to be invoked
* @returns a confirmation of invoking a function on the starknet contract
*/
public override async invokeFunction(
{ contractAddress, calldata = [], nonce, entrypoint, ...invocation }: Invocation,
_abi?: Abi
public async execute(
transactions: Invocation | Invocation[],
abis: Abi[] = [],
transactionsDetail: InvocationsDetails = {}
): Promise<AddTransactionResponse> {
if (Array.isArray(transactions) && transactions.length !== 1) {
throw new Error('Only one transaction at a time is currently supported');
}

const {
contractAddress,
calldata = [],
entrypoint,
...invocation
} = Array.isArray(transactions) ? transactions[0] : transactions;
const { nonce } = transactionsDetail;

assert(
!invocation.signature,
"Adding signatures to an account transaction currently isn't supported"
Expand All @@ -48,14 +68,18 @@ export class Account extends Provider implements AccountInterface {
const nonceBn = toBN(nonce ?? (await this.getNonce()));
const calldataDecimal = bigNumberishArrayToDecimalStringArray(calldata);

const signature = await this.signer.signTransaction({
...invocation,
contractAddress,
walletAddress: this.address,
nonce: nonceBn,
calldata: calldataDecimal,
entrypoint,
});
const signature = await this.signer.signTransaction(
[
{
...invocation,
contractAddress,
calldata: calldataDecimal,
entrypoint,
},
],
{ walletAddress: this.address, nonce: nonceBn },
abis
);

const entrypointSelector = getSelectorFromName(entrypoint);

Expand Down
10 changes: 6 additions & 4 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AddTransactionResponse,
DeployContractPayload,
Invocation,
InvocationsDetails,
Signature,
} from '../types';
import { BigNumberish } from '../utils/number';
Expand All @@ -30,7 +31,7 @@ export abstract class AccountInterface extends ProviderInterface {
/**
* Invokes a function on starknet
*
* @param invocation the invocation object containing:
* @param transactions the invocation object or an array of them, containing:
* - contractAddress - the address of the contract
* - entrypoint - the entrypoint of the contract
* - calldata - (defaults to []) the calldata
Expand All @@ -39,9 +40,10 @@ export abstract class AccountInterface extends ProviderInterface {
*
* @returns response from addTransaction
*/
public abstract override invokeFunction(
invocation: Invocation,
abi?: Abi
public abstract execute(
transactions: Invocation | Invocation[],
abis?: Abi[],
transactionsDetail?: InvocationsDetails
): Promise<AddTransactionResponse>;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function compileCalldata(args: Args): RawCalldata {
export class Contract {
connectedTo: string | null = null;

abi: Abi[];
abi: Abi;

structs: { [name: string]: StructAbi };

Expand All @@ -54,7 +54,7 @@ export class Contract {
* @param abi - Abi of the contract object
* @param address (optional) - address to connect to
*/
constructor(abi: Abi[], address: string | null = null, provider: Provider = defaultProvider) {
constructor(abi: Abi, address: string | null = null, provider: Provider = defaultProvider) {
this.connectedTo = address;
this.provider = provider;
this.abi = abi;
Expand Down
18 changes: 15 additions & 3 deletions src/signer/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Abi, KeyPair, Signature, TransactionDetails } from '../types';
import { Abi, Invocation, InvocationsSignerDetails, KeyPair, Signature } from '../types';
import { getStarkKey, sign } from '../utils/ellipticCurve';
import { addHexPrefix } from '../utils/encode';
import { hashMessage } from '../utils/hash';
Expand All @@ -19,9 +19,21 @@ export class Signer implements SignerInterface {
}

public async signTransaction(
{ contractAddress, entrypoint, calldata = [], nonce, walletAddress }: TransactionDetails,
_abi?: Abi
transactions: Invocation[],
transactionsDetail: InvocationsSignerDetails,
abis: Abi[] = []
): Promise<Signature> {
if (transactions.length !== 1) {
throw new Error('Only one transaction at a time is currently supported by this signer');
}
if (abis?.length !== 0 && abis.length !== transactions.length) {
throw new Error('ABI must be provided for each transaction or no transaction');
}
// now use abi to display decoded data somewhere, but as this signer is headless, we can't do that

const { contractAddress, entrypoint, calldata = [] } = transactions[0];
const { nonce, walletAddress } = transactionsDetail;

const nonceBn = toBN(nonce);
const entrypointSelector = getSelectorFromName(entrypoint);
const calldataDecimal = bigNumberishArrayToDecimalStringArray(calldata);
Expand Down
8 changes: 6 additions & 2 deletions src/signer/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Abi, Signature, TransactionDetails } from '../types';
import { Abi, Invocation, InvocationsSignerDetails, Signature } from '../types';
import { TypedData } from '../utils/typedData';

export abstract class SignerInterface {
Expand Down Expand Up @@ -31,5 +31,9 @@ export abstract class SignerInterface {
*
* @returns signature
*/
public abstract signTransaction(transaction: TransactionDetails, abi?: Abi): Promise<Signature>;
public abstract signTransaction(
transactions: Invocation[],
transactionsDetail: InvocationsSignerDetails,
abis?: Abi[]
): Promise<Signature>;
}
2 changes: 1 addition & 1 deletion src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export type GetBlockResponse = {

export type GetCodeResponse = {
bytecode: string[];
abi: Abi[];
abi: Abi;
};

export type GetTransactionStatusResponse = {
Expand Down
9 changes: 6 additions & 3 deletions src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ export type Invocation = {
contractAddress: string;
entrypoint: string;
calldata?: RawCalldata;
nonce?: BigNumberish;
signature?: Signature;
};

export type InvocationsDetails = {
nonce?: BigNumberish;
};

export type Call = Omit<Invocation, 'signature' | 'nonce'>;

export type Status =
Expand Down Expand Up @@ -51,14 +54,14 @@ export type StructAbi = {
type: 'struct';
};

export type Abi = FunctionAbi | StructAbi;
export type Abi = Array<FunctionAbi | StructAbi>;

export type EntryPointsByType = object;
export type Program = Record<any, any>;
export type BlockNumber = 'pending' | null | number;

export type CompiledContract = {
abi: Abi[];
abi: Abi;
entry_points_by_type: EntryPointsByType;
program: Program;
};
Expand Down
6 changes: 2 additions & 4 deletions src/types/signer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { BigNumberish } from '../utils/number';
import { Invocation } from './lib';
import { InvocationsDetails } from './lib';

export interface TransactionDetails extends Omit<Invocation, 'nonce' | 'signature'> {
export interface InvocationsSignerDetails extends Required<InvocationsDetails> {
walletAddress: string;
nonce: BigNumberish;
}

0 comments on commit 0be78c6

Please sign in to comment.