Reading a receipt from another program

A Receipt is an ordinary account, so a program can read one in the same transaction that acts on it. A program that pays for an answer, releases something on an answer or records an answer takes the Receipt as an account and reads it.

What the Receipt's existence means#

A Receipt is written only by settle_call. For the account to exist at its address, in that one instruction the voynich program:

  • found the call open and inside its deadline
  • read the enclave's attestation record and found it unexpired, with a measurement on the model's list
  • verified the enclave's Ed25519 signature over the call address, the input hash and the output hash
  • paid the escrow to the owner, the operator and the fee recipient

A program that finds a Receipt owned by the voynich program at the address derived from a call has all four, without verifying a signature itself.

Finding it#

The Receipt of a call is at the PDA ["receipt", call] of the voynich program. The call is at ["call", caller, nonce], where nonce is 8 bytes little-endian. The program's address on a cluster is programId in GET /v1/config.

Reading it#

A Receipt is 401 bytes: the discriminator 279a496a50669199, then 16 fixed-size fields. The layout with every offset is in Accounts: Receipt.

With Anchor 0.31:

Rust
use anchor_lang::prelude::*;

pub const RECEIPT_DISCRIMINATOR: [u8; 8] = [0x27, 0x9a, 0x49, 0x6a, 0x50, 0x66, 0x91, 0x99];
pub const RECEIPT_BYTES: usize = 401;

#[derive(AnchorDeserialize)]
pub struct Receipt {
    pub call: Pubkey,
    pub model: Pubkey,
    pub enclave: Pubkey,
    pub input_hash: [u8; 32],
    pub output_hash: [u8; 32],
    pub signature: [u8; 64],
    pub slot: u64,
    pub price_mint: Pubkey,
    pub total: u64,
    pub owner: Pubkey,
    pub operator: Pubkey,
    pub fee_recipient: Pubkey,
    pub owner_amount: u64,
    pub operator_amount: u64,
    pub protocol_amount: u64,
    pub bump: u8,
}

#[error_code]
pub enum ReadError {
    #[msg("The account is not a Voynich receipt for this call.")]
    NotAReceipt,
}

/// Reads the Receipt of `call`, or fails if the account is anything else.
pub fn read_receipt(info: &AccountInfo, voynich: &Pubkey, call: &Pubkey) -> Result<Receipt> {
    require_keys_eq!(*info.owner, *voynich, ReadError::NotAReceipt);
    let (address, _) = Pubkey::find_program_address(&[b"receipt", call.as_ref()], voynich);
    require_keys_eq!(*info.key, address, ReadError::NotAReceipt);
    let data = info.try_borrow_data()?;
    require!(
        data.len() == RECEIPT_BYTES && data[..8] == RECEIPT_DISCRIMINATOR,
        ReadError::NotAReceipt
    );
    let receipt = Receipt::deserialize(&mut &data[8..])?;
    require_keys_eq!(receipt.call, *call, ReadError::NotAReceipt);
    Ok(receipt)
}

The owner check and the address check are what make the account a Receipt. Data alone can be copied into any account; an account owned by the voynich program at that PDA can only have been written by settle_call.

What to compare#

The Receipt says a call settled. Your program decides whether it is the call it was waiting for:

CompareWithSo that
modelthe Model your program expectsthe answer came from that model and not another
callthe call your program opened or was told aboutthe receipt answers that request
input_hashthe SHA-256 of the input envelope your flow committed tothe enclave answered that input
output_hashthe SHA-256 of the output envelope presented to your programthe output shown is the one the enclave signed
slotthe current slotthe answer is as recent as your program needs

The output itself is not on chain. A Receipt carries its hash, so a transaction that presents the output envelope to your program proves it by hashing to output_hash.

total, price_mint and the three amounts say what the call paid and to whom, for a program that accounts for what it spent.