import { AztecAddress, Fr, FunctionData, FunctionSelector, TxContext, TxRequest, Vector } from '@aztec/circuits.js';
import { schemas } from '@aztec/foundation/schemas';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { bufferToHex, hexToBuffer } from '@aztec/foundation/string';
import { inspect } from 'util';
import { z } from 'zod';
import { AuthWitness } from './auth_witness.js';
import { PackedValues } from './packed_values.js';
/**
 * Request to execute a transaction. Similar to TxRequest, but has the full args.
 */
export class TxExecutionRequest {
    constructor(
    /**
     * Sender.
     */
    origin, 
    /**
     * Selector of the function to call.
     */
    functionSelector, 
    /**
     * The hash of arguments of first call to be executed (usually account entrypoint).
     * @dev This hash is a pointer to `argsOfCalls` unordered array.
     */
    firstCallArgsHash, 
    /**
     * Transaction context.
     */
    txContext, 
    /**
     * An unordered array of packed arguments for each call in the transaction.
     * @dev These arguments are accessed in Noir via oracle and constrained against the args hash. The length of
     * the array is equal to the number of function calls in the transaction (1 args per 1 call).
     */
    argsOfCalls, 
    /**
     * Transient authorization witnesses for authorizing the execution of one or more actions during this tx.
     * These witnesses are not expected to be stored in the local witnesses database of the PXE.
     */
    authWitnesses) {
        this.origin = origin;
        this.functionSelector = functionSelector;
        this.firstCallArgsHash = firstCallArgsHash;
        this.txContext = txContext;
        this.argsOfCalls = argsOfCalls;
        this.authWitnesses = authWitnesses;
    }
    static get schema() {
        return z
            .object({
            origin: schemas.AztecAddress,
            functionSelector: schemas.FunctionSelector,
            firstCallArgsHash: schemas.Fr,
            txContext: TxContext.schema,
            argsOfCalls: z.array(PackedValues.schema),
            authWitnesses: z.array(AuthWitness.schema),
        })
            .transform(TxExecutionRequest.from);
    }
    toTxRequest() {
        return new TxRequest(this.origin, 
        // Entrypoints must be private as as defined by the protocol.
        new FunctionData(this.functionSelector, true /* isPrivate */), this.firstCallArgsHash, this.txContext);
    }
    static getFields(fields) {
        return [
            fields.origin,
            fields.functionSelector,
            fields.firstCallArgsHash,
            fields.txContext,
            fields.argsOfCalls,
            fields.authWitnesses,
        ];
    }
    static from(fields) {
        return new TxExecutionRequest(...TxExecutionRequest.getFields(fields));
    }
    /**
     * Serialize as a buffer.
     * @returns The buffer.
     */
    toBuffer() {
        return serializeToBuffer(this.origin, this.functionSelector, this.firstCallArgsHash, this.txContext, new Vector(this.argsOfCalls), new Vector(this.authWitnesses));
    }
    /**
     * Serialize as a string.
     * @returns The string.
     */
    toString() {
        return bufferToHex(this.toBuffer());
    }
    /**
     * Deserializes from a buffer or reader, corresponding to a write in cpp.
     * @param buffer - Buffer to read from.
     * @returns The deserialized TxRequest object.
     */
    static fromBuffer(buffer) {
        const reader = BufferReader.asReader(buffer);
        return new TxExecutionRequest(reader.readObject(AztecAddress), reader.readObject(FunctionSelector), Fr.fromBuffer(reader), reader.readObject(TxContext), reader.readVector(PackedValues), reader.readVector(AuthWitness));
    }
    /**
     * Deserializes from a string, corresponding to a write in cpp.
     * @param str - String to read from.
     * @returns The deserialized TxRequest object.
     */
    static fromString(str) {
        return TxExecutionRequest.fromBuffer(hexToBuffer(str));
    }
    static random() {
        return new TxExecutionRequest(AztecAddress.random(), FunctionSelector.random(), Fr.random(), TxContext.empty(), [PackedValues.random()], [AuthWitness.random()]);
    }
    [inspect.custom]() {
        return `TxExecutionRequest(${this.origin} called ${this.functionSelector})`;
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHhfZXhlY3V0aW9uX3JlcXVlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvdHhfZXhlY3V0aW9uX3JlcXVlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFlBQVksRUFBRSxFQUFFLEVBQUUsWUFBWSxFQUFFLGdCQUFnQixFQUFFLFNBQVMsRUFBRSxTQUFTLEVBQUUsTUFBTSxFQUFFLE1BQU0sb0JBQW9CLENBQUM7QUFDcEgsT0FBTyxFQUFFLE9BQU8sRUFBRSxNQUF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