|
| 1 | +# Basic Operations Examples |
| 2 | + |
| 3 | +Example programs showing all basic operations for compressed accounts. |
| 4 | + |
| 5 | +## Summary |
| 6 | + |
| 7 | +- Demonstrates create, update, close, reinit, and burn operations |
| 8 | +- Anchor uses Poseidon hashing (`light_sdk::account::LightAccount`); Native uses SHA-256 (`light_sdk::account::sha::LightAccount`) |
| 9 | +- All programs derive addresses from seeds `[b"message", signer.key.as_ref()]` + `ADDRESS_TREE_V2` + program ID |
| 10 | +- Close preserves address for reinitialization; burn permanently deletes address |
| 11 | + |
| 12 | +## [Anchor README](anchor/README.md) | [Native README](native/README.md) |
| 13 | + |
| 14 | +## Source structure |
| 15 | + |
| 16 | +``` |
| 17 | +basic-operations/ |
| 18 | +├── anchor/ # Anchor framework (Poseidon hashing) |
| 19 | +│ ├── create/programs/create/src/lib.rs |
| 20 | +│ ├── update/programs/update/src/lib.rs |
| 21 | +│ ├── close/programs/close/src/lib.rs |
| 22 | +│ ├── reinit/programs/reinit/src/lib.rs |
| 23 | +│ └── burn/programs/burn/src/lib.rs |
| 24 | +└── native/ # Native Solana (SHA-256 hashing) |
| 25 | + └── programs/ |
| 26 | + ├── create/src/lib.rs |
| 27 | + ├── update/src/lib.rs |
| 28 | + ├── close/src/lib.rs |
| 29 | + ├── reinit/src/lib.rs |
| 30 | + └── burn/src/lib.rs |
| 31 | +``` |
| 32 | + |
| 33 | +## Accounts |
| 34 | + |
| 35 | +### MyCompressedAccount |
| 36 | + |
| 37 | +Shared account structure across all examples. Derives `LightDiscriminator` for compressed account type identification. |
| 38 | + |
| 39 | +```rust |
| 40 | +#[derive(LightDiscriminator)] |
| 41 | +pub struct MyCompressedAccount { |
| 42 | + pub owner: Pubkey, // Account owner |
| 43 | + pub message: String, // User-defined message |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +## Instructions |
| 48 | + |
| 49 | +Native programs use `InstructionType` enum discriminators (first byte of instruction data). |
| 50 | + |
| 51 | +### create_account (discriminator: 0) |
| 52 | + |
| 53 | +| Parameter | Type | Description | |
| 54 | +|-----------|------|-------------| |
| 55 | +| `proof` | `ValidityProof` | ZK proof for address non-existence | |
| 56 | +| `address_tree_info` | `PackedAddressTreeInfo` | Address tree metadata | |
| 57 | +| `output_state_tree_index` | `u8` | Target state tree index | |
| 58 | +| `message` | `String` | Initial message content | |
| 59 | + |
| 60 | +Calls `LightAccount::new_init()` with derived address, invokes `LightSystemProgramCpi` with `with_new_addresses()`. |
| 61 | + |
| 62 | +### update_account (discriminator: 1) |
| 63 | + |
| 64 | +| Parameter | Type | Description | |
| 65 | +|-----------|------|-------------| |
| 66 | +| `proof` | `ValidityProof` | ZK proof for account existence | |
| 67 | +| `account_meta` | `CompressedAccountMeta` | Current account metadata | |
| 68 | +| `current_account` / `current_message` | varies | Current account state for verification | |
| 69 | +| `new_message` | `String` | Updated message content | |
| 70 | + |
| 71 | +Calls `LightAccount::new_mut()` with current state, modifies message, invokes CPI. |
| 72 | + |
| 73 | +### close_account (discriminator: 1) |
| 74 | + |
| 75 | +| Parameter | Type | Description | |
| 76 | +|-----------|------|-------------| |
| 77 | +| `proof` | `ValidityProof` | ZK proof for account existence | |
| 78 | +| `account_meta` | `CompressedAccountMeta` | Current account metadata | |
| 79 | +| `current_message` | `String` | Current message for verification | |
| 80 | + |
| 81 | +Calls `LightAccount::new_close()` - clears data but preserves address for reinitialization. |
| 82 | + |
| 83 | +### reinit_account (discriminator: 2) |
| 84 | + |
| 85 | +| Parameter | Type | Description | |
| 86 | +|-----------|------|-------------| |
| 87 | +| `proof` | `ValidityProof` | ZK proof for empty account at address | |
| 88 | +| `account_meta` | `CompressedAccountMeta` | Account metadata | |
| 89 | + |
| 90 | +Calls `LightAccount::new_empty()` to reinitialize previously closed account. |
| 91 | + |
| 92 | +### burn_account (discriminator: 1) |
| 93 | + |
| 94 | +| Parameter | Type | Description | |
| 95 | +|-----------|------|-------------| |
| 96 | +| `proof` | `ValidityProof` | ZK proof for account existence | |
| 97 | +| `account_meta` | `CompressedAccountMetaBurn` | Account metadata (burn-specific) | |
| 98 | +| `current_message` / `current_account` | varies | Current state for verification | |
| 99 | + |
| 100 | +Calls `LightAccount::new_burn()` - permanently deletes account. Address cannot be reused. |
| 101 | + |
| 102 | +## Security |
| 103 | + |
| 104 | +- Address tree validation: Checks `address_tree_pubkey.to_bytes() == ADDRESS_TREE_V2` |
| 105 | +- Program ID verification (native only): `program_id != &ID` returns `IncorrectProgramId` |
| 106 | +- Signer required: First account must be mutable signer |
| 107 | +- State verification: Close/update/burn require current state to match on-chain data |
| 108 | + |
| 109 | +## Errors |
| 110 | + |
| 111 | +| Error | Source | Condition | |
| 112 | +|-------|--------|-----------| |
| 113 | +| `ProgramError::IncorrectProgramId` | Native entrypoint | Program ID mismatch | |
| 114 | +| `ProgramError::InvalidInstructionData` | Entrypoint | Empty or malformed instruction data | |
| 115 | +| `ProgramError::NotEnoughAccountKeys` | All | Missing required accounts | |
| 116 | +| `ProgramError::InvalidAccountData` | All | Invalid address tree | |
| 117 | +| `LightSdkError::Borsh` | Native | Instruction data deserialization failure | |
0 commit comments