A class that represents the parameters required for a transaction.

Hierarchy

  • TransactionParameters

Implements

Constructors

  • Creates an instance of TransactionParameters.

    Throws

    TransactionParametersError: If no output UTXOs and input UTXOs are provided, no verifier IDL is provided, no Poseidon hasher is provided, no action is defined, message Merkle tree pubkey needs to be defined if a message is provided, message needs to be defined if a message Merkle tree is provided, etc.

    Parameters

    • options: {
          action: Action;
          ataCreationFee?: boolean;
          encryptedUtxos?: Uint8Array;
          inputUtxos?: Utxo[];
          lookUpTable?: PublicKey;
          message?: Buffer;
          messageMerkleTreePubkey?: PublicKey;
          outputUtxos?: Utxo[];
          poseidon: any;
          provider?: Provider;
          recipientSol?: PublicKey;
          recipientSpl?: PublicKey;
          relayer?: Relayer;
          senderSol?: PublicKey;
          senderSpl?: PublicKey;
          transactionMerkleTreePubkey: PublicKey;
          transactionNonce: number;
          validateUtxos?: boolean;
          verifierIdl: Idl;
      }

      An object containing the parameters for the transaction.

      • action: Action

        Action to perform in the transaction.

      • Optional ataCreationFee?: boolean

        Optional flag indicating whether to include the ATA creation fee in the transaction.

      • Optional encryptedUtxos?: Uint8Array

        Optional encrypted UTXOs for the transaction.

      • Optional inputUtxos?: Utxo[]

        Optional array of input UTXOs for the transaction.

      • Optional lookUpTable?: PublicKey

        Optional lookup table for the transaction.

      • Optional message?: Buffer

        Optional message for the transaction.

      • Optional messageMerkleTreePubkey?: PublicKey

        Optional public key of the message Merkle tree.

      • Optional outputUtxos?: Utxo[]

        Optional array of output UTXOs for the transaction.

      • poseidon: any

        Poseidon hasher for the transaction.

      • Optional provider?: Provider

        Optional provider for the transaction.

      • Optional recipientSol?: PublicKey

        Optional public key of the recipient for SOL transactions.

      • Optional recipientSpl?: PublicKey

        Optional public key of the recipient for SPL transactions.

      • Optional relayer?: Relayer

        Optional relayer for the transaction.

      • Optional senderSol?: PublicKey

        Optional public key of the sender for SOL transactions.

      • Optional senderSpl?: PublicKey

        Optional public key of the sender for SPL transactions.

      • transactionMerkleTreePubkey: PublicKey

        Public key of the transaction Merkle tree.

      • transactionNonce: number

        Nonce for the transaction.

      • Optional validateUtxos?: boolean

        Optional flag indicating whether to validate UTXOs in the transaction.

      • verifierIdl: Idl

        Interface description language for the transaction verifier.

    Returns TransactionParameters

Properties

accounts: lightAccounts
action: Action
assetPubkeys: PublicKey[]
assetPubkeysCircuit: string[]
ataCreationFee?: boolean
encryptedUtxos?: Uint8Array
inputUtxos: Utxo[]
message?: Buffer
outputUtxos: Utxo[]
poseidon: any
publicAmountSol: BN
publicAmountSpl: BN
relayer: Relayer
transactionNonce: number
txIntegrityHash?: BN
verifierConfig: VerifierConfig
verifierIdl: Idl
verifierProgramId: PublicKey

Methods

  • Adds empty UTXOs to the given array until the array reaches a specified length.

    The zero-knowledge proof circuit requires all inputs to be defined, hence the need to populate the array with empty UTXOs when necessary. This function ensures that the number of UTXOs in the array matches the expected number as defined by the zk-SNARKs protocol.

    Parameters

    • utxos: Utxo[] = []

      The array of UTXOs to which empty UTXOs will be added. Default is an empty array.

    • len: number

      The desired number of UTXOs in the array after the function is executed.

    Returns Utxo[]

    An array of UTXOs of the desired length, populated with empty UTXOs as needed.

  • This method assigns sender and recipient accounts for Solana and SPL tokens to the transaction parameters based on the action (either 'unshield', 'transfer', or 'shield').

    For 'unshield' and 'transfer' actions, it assigns the sender accounts for both SPL and Solana tokens and checks if the recipient accounts are defined. If not, it throws an error.

    For the 'shield' action, it assigns the recipient accounts and checks if the sender accounts are defined. If not, it throws an error.

    Throws

    TransactionParametersError:

    • If the action is 'unshield' or 'transfer' and the recipient accounts for SPL or Solana tokens are undefined.

    • If the action is 'shield' and the sender accounts for SPL or Solana tokens are undefined.

    • If the assetPubkeys are undefined.

    • If the action is not 'deposit' but should be, based on the provided sender and recipient accounts and relayer.

    Returns void

  • Async

    This method is used to encrypt the output UTXOs.

    It first checks if there are encrypted UTXOs provided. If so, it uses those as the encrypted outputs. If not, it goes through the output UTXOs for this transaction. If the UTXO has application data and this is to be included, it throws an error as this is currently not implemented. Otherwise, it encrypts the UTXO and adds it to the list of encrypted outputs.

    Depending on the verifier configuration, it either combines two encrypted outputs into a single 256 byte output or adds padding to the encrypted outputs to ensure their length is correct.

    Throws

    • TransactionError: If automatic encryption for UTXOs with application data is attempted, as this is currently not implemented.

    Parameters

    • poseidon: any

      The poseidon hash function.

    • Optional encryptedUtxos: Uint8Array

      An optional parameter for previously encrypted UTXOs.

    Returns Promise<undefined | Uint8Array>

    • A Uint8Array of the encrypted output UTXOs.
  • Computes the integrity Poseidon hash over transaction inputs that are not part of the proof, but are included to prevent the relayer from changing any input of the transaction.

    The hash is computed over the following inputs in the given order:

    1. Recipient SPL Account
    2. Recipient Solana Account
    3. Relayer Public Key
    4. Relayer Fee
    5. Encrypted UTXOs (limited to 512 bytes)

    Throws

    Throws an error if the relayer, recipient SPL or Solana accounts, relayer fee, or encrypted UTXOs are undefined, or if the encryption of UTXOs fails.

    Example

    const integrityHash = await getTxIntegrityHash(poseidonInstance);
    

    Parameters

    • poseidon: any

      Poseidon hash function instance.

    Returns Promise<BN>

    A promise that resolves to the computed transaction integrity hash.

  • Asynchronously converts transaction parameters to bytes using the BorshAccountsCoder.

    This method performs the following operations:

    1. Initializes a new BorshAccountsCoder with the IDL_VERIFIER_PROGRAM_ZERO.
    2. Converts each utxo in the inputUtxos array to bytes and stores them in inputUtxosBytes.
    3. Converts each utxo in the outputUtxos array to bytes and stores them in outputUtxosBytes.
    4. Prepares an object containing the outputUtxosBytes, inputUtxosBytes, relayerPubkey, relayerFee, current object's properties, accounts' properties, and transactionNonce (converted to a BN instance).
    5. Encodes the prepared object under the "transactionParameters" key using the BorshAccountsCoder.

    Throws

    Throws an error if the encoding fails.

    Returns Promise<Buffer>

    A promise that resolves to a Buffer containing the encoded transaction parameters.

  • A static method to find the index of a specific IDL object in an array of IDL objects based on a given program ID.

    Throws

    If an IDL object in the provided array does not have any constants.

    Example

    let index = TransactionParameters.findIdlIndex("someProgramId", idlArray);
    if(index !== -1) {
    console.log("Program ID found at index: " + index);
    }

    Parameters

    • programId: string

      The ID of the program for which to find the IDL object.

    • idlObjects: Idl[]

      An array of IDL objects among which to search.

    Returns number

    The index of the IDL object that contains the provided program ID. Returns -1 if the program ID is not found.

  • A static method to create a new TransactionParameters instance from a given set of bytes.

    This method decodes the provided bytes using a BorshAccountsCoder and checks the validity of the resulting data. It retrieves the input and output UTXOs from the decoded data, ensuring that they match the provided IDLs. It also checks that the relayer's public key matches the one in the decoded data.

    If the decoded recipient is not the AUTHORITY, the action is set to UNSHIELD, otherwise, it's set to TRANSFER. The method then creates a new TransactionParameters instance with the retrieved data and returns it.

    Static

    Throws

    Throws a TransactionParametersError if the UTXO IDLs are not provided when needed, or if the relayer's public key does not match the one in the decoded data.

    Parameters

    • params: {
          assetLookupTable: string[];
          bytes: Buffer;
          poseidon: any;
          relayer: Relayer;
          utxoIdls?: Idl[];
          verifierIdl: Idl;
          verifierProgramLookupTable: string[];
      }

      The parameters for the method.

      • assetLookupTable: string[]
      • bytes: Buffer

        The bytes to decode into a TransactionParameters instance.

      • poseidon: any

        The Poseidon hash function instance.

      • relayer: Relayer

        The relayer for the transaction.

      • Optional utxoIdls?: Idl[]

        An optional array of IDLs for the UTXOs.

      • verifierIdl: Idl

        The IDL of the verifier program.

      • verifierProgramLookupTable: string[]

    Returns Promise<TransactionParameters>

    A promise that resolves to a new TransactionParameters instance.

  • This method collects and returns public keys of assets involved in a transaction, from both input and output UTXOs. It also checks whether the number of different assets involved in the transaction exceeds the maximum limit. If there are less assets than the maximum allowed, it fills up the remaining space with the System Program's public key.

    Throws

    • TransactionError: If no UTXOs are provided or if the number of different assets exceeds the maximum allowed.

    Parameters

    • Optional inputUtxos: Utxo[]

      The input UTXOs for the transaction.

    • Optional outputUtxos: Utxo[]

      The output UTXOs for the transaction.

    Returns {
        assetPubkeys: PublicKey[];
        assetPubkeysCircuit: string[];
    }

    • An object containing arrays of circuit and regular public keys of assets.
    • assetPubkeys: PublicKey[]
    • assetPubkeysCircuit: string[]
  • This method generates a Program Derived Address (PDA) with the seed "escrow" for the verifier program. PDAs in Solana are addresses that are based off of the public key of a deployed program and are unique to each specific program and seed. This method is used to get the PDA that is used as the escrow account.

    Parameters

    • verifierProgramId: PublicKey

      The public key of the verifier program.

    Returns PublicKey

    • The public key of the escrow Program Derived Address.
  • This method calculates the external amount for a specified asset. It achieves this by adding all output UTXOs of the same asset and subtracting all input UTXOs of the same asset. The result is then added to the field size and the modulus of the field size is returned.

    Parameters

    • assetIndex: number

      The index of the asset for which the external amount should be computed.

    • inputUtxos: Utxo[]

      The input UTXOs for the transaction.

    • outputUtxos: Utxo[]

      The output UTXOs for the transaction.

    • assetPubkeysCircuit: string[]

      An array of circuit public keys of assets.

    Returns BN

    • The public amount of the asset.
  • Static async method to generate transaction parameters.

    Static

    Async

    Throws

    • A TransactionParametersError if action is TRANSFER and no outUtxos and mergeUtxos is not set, or if the action is not SHIELD and relayer fee is undefined, or if the account is undefined.

    Throws

    • A CreateUtxoErrorCode if the account is undefined.

    Remarks

    The method constructs a TransactionParameters object which includes all the necessary parameters for a transaction. It selects the necessary input UTXOs, creates the output UTXOs, and validates the relayer and action of the transaction. It also handles various transaction actions like shield, unshield and transfer.

    Parameters

    • params: {
          account: Account;
          action: Action;
          addInUtxos?: boolean;
          addOutUtxos?: boolean;
          appUtxo?: AppUtxoConfig;
          assetLookupTable: string[];
          ataCreationFee?: boolean;
          inUtxos?: Utxo[];
          mergeUtxos?: boolean;
          message?: Buffer;
          outUtxos?: Utxo[];
          provider: Provider;
          publicAmountSol?: BN;
          publicAmountSpl?: BN;
          recipientSol?: PublicKey;
          recipientSplAddress?: PublicKey;
          relayer?: Relayer;
          tokenCtx: TokenData;
          transactionNonce: number;
          userSplAccount?: PublicKey;
          utxos?: Utxo[];
          verifierIdl: Idl;
          verifierProgramLookupTable: string[];
      }

      Parameters for generating transaction parameters.

      • account: Account

        The account involved in the transaction.

      • action: Action

        The action being performed (shield, unshield, transfer).

      • Optional addInUtxos?: boolean

        Whether to add input UTXOs to the transaction.

      • Optional addOutUtxos?: boolean

        Whether to add output UTXOs to the transaction.

      • Optional appUtxo?: AppUtxoConfig

        The configuration for the application UTXO.

      • assetLookupTable: string[]
      • Optional ataCreationFee?: boolean

        Whether to include the ATA (Associated Token Account) creation fee.

      • Optional inUtxos?: Utxo[]

        Array of input UTXOs for the transaction.

      • Optional mergeUtxos?: boolean

        Whether to merge UTXOs in the transaction.

      • Optional message?: Buffer

        The message data for the transaction.

      • Optional outUtxos?: Utxo[]

        Array of output UTXOs for the transaction.

      • provider: Provider

        The provider for the transaction.

      • Optional publicAmountSol?: BN

        The amount of SOL tokens involved in the transaction.

      • Optional publicAmountSpl?: BN

        The amount of SPL tokens involved in the transaction.

      • Optional recipientSol?: PublicKey

        The Solana address of the recipient.

      • Optional recipientSplAddress?: PublicKey

        The SPL address of the recipient.

      • Optional relayer?: Relayer

        The relayer for the transaction.

      • tokenCtx: TokenData

        The context of the token involved in the transaction.

      • transactionNonce: number

        The nonce for the transaction.

      • Optional userSplAccount?: PublicKey

        The SPL account of the user.

      • Optional utxos?: Utxo[]

        Array of UTXO (Unspent Transaction Outputs) objects.

      • verifierIdl: Idl

        The IDL (Interface Description Language) for the verifier program.

      • verifierProgramLookupTable: string[]

    Returns Promise<TransactionParameters>

    • A Promise that resolves with the generated TransactionParameters object.
  • A static method to fetch the verifier configuration from a given IDL object.

    This method parses the IDL object to identify an account with a name that starts with "zK" and ends with "ProofInputs". It then examines the fields of this account to identify the inputNullifier and outputCommitment fields, checking that they are of the correct array type, and retrieves their lengths. The lengths of these fields are then returned as a VerifierConfig object.

    Static

    Throws

    Throws an error if no matching account is found in the IDL, or if the inputNullifier or outputCommitment fields are not found or are of incorrect type.

    Parameters

    • verifierIdl: Idl

      The IDL object of the verifier program.

    Returns VerifierConfig

    A VerifierConfig object with the lengths of the inputNullifier and outputCommitment fields.

  • A static method to instantiate a new verifier program from a given IDL object.

    Static

    Remarks

    The programID is expected to be appended as a constant to the program that can be read directly from the IDL.

    Parameters

    • verifierIdl: Idl

      The IDL object of the verifier program.

    Returns Program<Idl>

    A new Anchor Program object for the verifier program.

  • A static method to retrieve the verifier program ID from a given IDL object.

    Static

    Example

    let verifierProgramId = TransactionParameters.getVerifierProgramId(verifierIdl);
    console.log("Verifier program ID: " + verifierProgramId);

    Remarks

    The programID is expected to be appended as a constant to the program that can be read directly from the IDL.

    Parameters

    • verifierIdl: Idl

      The IDL object containing the verifier program ID.

    Returns PublicKey

    The verifier program ID as a PublicKey object.

Generated using TypeDoc