Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

4.0 KB
Raw Blame
//! Address conformance between the `knock_escrow` program and its JS client.
//!
//! The client in `api/_lib/knock/escrow.js` re-derives these PDAs to read a
//! knock back off the chain. If either side changes a seed, an account name, or
//! the program id, it reads the wrong account: the API would report "no
//! escrowed knock exists" for money that was really paid, or decode a stranger's
//! account as a knock. Nothing about that failure is visible to a test that only
//! talks to its own side, so both sides pin the SAME fixtures, and this file is
//! the authoritative one because it derives from the program's real constants.
//!
//! The JS half is `tests/knock-escrow-pda.test.js`. Change a seed and both fail.

use program_tests::*;
use solana_pubkey::Pubkey;
use std::str::FromStr;

const PROGRAM: &str = "uVX46U6sGUs6PD3339ZXbTpMyhZwkQhBLPxnvRX9ps7";
/// Fixed, secret-free inputs. Any valid pubkey works; these are well-known ones
/// so the fixture can be regenerated by anybody without a keypair.
const OWNER: &str = "11111111111111111111111111111111";
const SENDER: &str = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
const HANDLE: &[u8] = b"nirholas";
const NONCE: u64 = 7;

const EXPECT_DOOR_ID: &str = "b3d92abfa95658f0d35e6b20752e8ca9ba4fad9e9bbc62352c999c75853fb53d";
const EXPECT_CONFIG: &str = "7jAKSyTgRC8aGAjdYkMjQMPv7uh3uJX869GRXkBTUPkZ";
const EXPECT_DOOR: &str = "DMDKj3mSoM5rjsftGAhSbLbrAWMwmFxEkHDLsx997vk8";
const EXPECT_KNOCK: &str = "8WvZ64SL6JNss232BC78aapB2M6MjE95f9LtWfYYRn5t";
const EXPECT_VAULT: &str = "8LLgeTjcL4ZiU1MwoFWR4DkTkYnXruBsorMG7MKWbSwv";
const EXPECT_DISC_DOOR: &str = "541e65686ab2068c";
const EXPECT_DISC_KNOCK: &str = "f9c621b06c558191";

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

#[test]
fn pda_derivation_is_stable_and_matches_the_js_client() {
    let program = Pubkey::from_str(PROGRAM).unwrap();
    let owner = Pubkey::from_str(OWNER).unwrap();
    let sender = Pubkey::from_str(SENDER).unwrap();

    let door_id = sha256(HANDLE);
    assert_eq!(hex(&door_id), EXPECT_DOOR_ID, "door id is sha256 of the handle");

    let config = Pubkey::find_program_address(&[b"config"], &program).0;
    let door =
        Pubkey::find_program_address(&[b"door", owner.as_ref(), door_id.as_ref()], &program).0;
    let knock = Pubkey::find_program_address(
        &[b"knock", door.as_ref(), sender.as_ref(), &NONCE.to_le_bytes()],
        &program,
    )
    .0;
    let vault = Pubkey::find_program_address(&[b"vault", knock.as_ref()], &program).0;

    assert_eq!(config.to_string(), EXPECT_CONFIG);
    assert_eq!(door.to_string(), EXPECT_DOOR);
    assert_eq!(knock.to_string(), EXPECT_KNOCK);
    assert_eq!(vault.to_string(), EXPECT_VAULT);
}

#[test]
fn account_discriminators_match_the_js_decoder() {
    // Anchor's account discriminator is sha256("account:<Name>")[..8]. The JS
    // decoder checks these before it reads a single field.
    assert_eq!(hex(&sha256(b"account:Door")[..8]), EXPECT_DISC_DOOR);
    assert_eq!(hex(&sha256(b"account:KnockRecord")[..8]), EXPECT_DISC_KNOCK);
}

#[test]
fn instruction_discriminators_match_the_browser_client() {
    // Anchor's instruction discriminator is sha256("global:<snake_case_name>")[..8].
    // The browser builds these by hand in src/knock/escrow-program.js, because
    // it signs for the sender and the owner directly rather than going through
    // a server that could move their money. A mismatch here does not fail
    // loudly on-chain; it dispatches to a DIFFERENT instruction or none at all,
    // so both sides pin the same eight bytes.
    for (name, expect) in [
        ("initialize", "afaf6d1f0d989bed"),
        ("set_config", "6c9e9aafd4623442"),
        ("open_door", "0b8c29ba454e37f7"),
        ("set_door", "e3022b8b592a417a"),
        ("knock", "fbd9cc537e9b5da3"),
        ("answer", "48d62c2035de09f4"),
        ("refuse", "c449f3c611fac816"),
        ("reclaim", "2cb1ecf9916da3ba"),
    ] {
        let disc = &ix_data(name, &[])[..8];
        assert_eq!(hex(disc), expect, "discriminator for {name}");
    }
}