|
| 1 | +import { NO_FROM } from '@aztec/aztec.js/account'; |
| 2 | +import type { Aliased } from '@aztec/aztec.js/wallet'; |
| 3 | +import { Fr } from '@aztec/foundation/curves/bn254'; |
| 4 | +import type { PXE } from '@aztec/pxe/server'; |
| 5 | +import { FunctionCall, FunctionSelector, FunctionType } from '@aztec/stdlib/abi'; |
| 6 | +import { AztecAddress } from '@aztec/stdlib/aztec-address'; |
| 7 | +import { Gas, GasFees } from '@aztec/stdlib/gas'; |
| 8 | +import type { AztecNode } from '@aztec/stdlib/interfaces/client'; |
| 9 | +import type { PrivateKernelTailCircuitPublicInputs } from '@aztec/stdlib/kernel'; |
| 10 | +import { |
| 11 | + ExecutionPayload, |
| 12 | + PrivateCallExecutionResult, |
| 13 | + PrivateExecutionResult, |
| 14 | + TxSimulationResult, |
| 15 | +} from '@aztec/stdlib/tx'; |
| 16 | + |
| 17 | +import { beforeEach, describe, expect, it, jest } from '@jest/globals'; |
| 18 | + |
| 19 | +import type { AccountContractsProvider } from './account-contract-providers/types.js'; |
| 20 | +import { EmbeddedWallet } from './embedded_wallet.js'; |
| 21 | +import type { WalletDB } from './wallet_db.js'; |
| 22 | + |
| 23 | +describe('EmbeddedWallet', () => { |
| 24 | + let pxe: PXE; |
| 25 | + let node: AztecNode; |
| 26 | + let wallet: TestWallet; |
| 27 | + let simulateTx: jest.MockedFunction<PXE['simulateTx']>; |
| 28 | + let proveTx: jest.MockedFunction<PXE['proveTx']>; |
| 29 | + let getPredictedMinFees: jest.MockedFunction<AztecNode['getPredictedMinFees']>; |
| 30 | + let getNodeInfo: jest.MockedFunction<AztecNode['getNodeInfo']>; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + simulateTx = jest.fn(); |
| 34 | + proveTx = jest.fn(); |
| 35 | + getPredictedMinFees = jest.fn(); |
| 36 | + getNodeInfo = jest.fn(); |
| 37 | + pxe = { simulateTx, proveTx } as unknown as PXE; |
| 38 | + node = { getPredictedMinFees, getNodeInfo } as unknown as AztecNode; |
| 39 | + wallet = new TestWallet(pxe, node, {} as WalletDB, {} as AccountContractsProvider); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('sendTx', () => { |
| 43 | + it('passes sendMessagesAs as senderForTags to PXE simulation', async () => { |
| 44 | + getPredictedMinFees.mockResolvedValue([new GasFees(2, 2)]); |
| 45 | + getNodeInfo.mockResolvedValue({ l1ChainId: 1, rollupVersion: 1 } as any); |
| 46 | + simulateTx.mockResolvedValue(makeMinimalSimResult()); |
| 47 | + proveTx.mockRejectedValue(new Error('stop-at-prove')); |
| 48 | + |
| 49 | + const sendMessagesAs = await AztecAddress.random(); |
| 50 | + const call = FunctionCall.from({ |
| 51 | + name: 'test', |
| 52 | + to: await AztecAddress.random(), |
| 53 | + selector: FunctionSelector.random(), |
| 54 | + type: FunctionType.PRIVATE, |
| 55 | + hideMsgSender: false, |
| 56 | + isStatic: false, |
| 57 | + args: [], |
| 58 | + returnTypes: [], |
| 59 | + }); |
| 60 | + const payload = new ExecutionPayload([call], [], []); |
| 61 | + |
| 62 | + await expect(wallet.sendTx(payload, { from: NO_FROM, sendMessagesAs })).rejects.toThrow('stop-at-prove'); |
| 63 | + |
| 64 | + expect(simulateTx).toHaveBeenCalledWith( |
| 65 | + expect.anything(), |
| 66 | + expect.objectContaining({ senderForTags: sendMessagesAs }), |
| 67 | + ); |
| 68 | + }); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +class TestWallet extends EmbeddedWallet { |
| 73 | + override getAccounts(): Promise<Aliased<AztecAddress>[]> { |
| 74 | + return Promise.resolve([]); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function makeMinimalSimResult(): TxSimulationResult { |
| 79 | + const entrypoint = { offchainEffects: [], nestedExecutionResults: [] } as unknown as PrivateCallExecutionResult; |
| 80 | + const privateResult = new PrivateExecutionResult(entrypoint, Fr.zero(), []); |
| 81 | + const publicInputs = { gasUsed: Gas.empty() } as unknown as PrivateKernelTailCircuitPublicInputs; |
| 82 | + return new TxSimulationResult(privateResult, publicInputs); |
| 83 | +} |
0 commit comments