|
| 1 | +/** |
| 2 | + * @brief C-based CPI BPF program |
| 3 | + */ |
| 4 | +#include <solana_sdk.h> |
| 5 | + |
| 6 | +/// Amount of bytes of account data to allocate |
| 7 | +#define SIZE 42 |
| 8 | + |
| 9 | +extern uint64_t entrypoint(const uint8_t *input) |
| 10 | +{ |
| 11 | + SolAccountInfo accounts[2]; |
| 12 | + SolParameters params = (SolParameters){.ka = accounts}; |
| 13 | + |
| 14 | + if (!sol_deserialize(input, ¶ms, SOL_ARRAY_SIZE(accounts))) |
| 15 | + { |
| 16 | + return ERROR_INVALID_ARGUMENT; |
| 17 | + } |
| 18 | + |
| 19 | + SolAccountInfo allocated_info = params.ka[0]; |
| 20 | + SolAccountInfo system_program_info = params.ka[1]; |
| 21 | + |
| 22 | + uint8_t seed[] = {'Y', 'o', 'u', ' ', 'p', 'a', 's', 's', |
| 23 | + ' ', 'b', 'u', 't', 't', 'e', 'r'}; |
| 24 | + const SolSignerSeed seeds[] = {{seed, SOL_ARRAY_SIZE(seed)}, |
| 25 | + {¶ms.data[0], 1}}; |
| 26 | + |
| 27 | + const SolSignerSeeds signers_seeds[] = {{seeds, SOL_ARRAY_SIZE(seeds)}}; |
| 28 | + |
| 29 | + SolPubkey expected_allocated_key; |
| 30 | + if (SUCCESS != sol_create_program_address(seeds, SOL_ARRAY_SIZE(seeds), |
| 31 | + params.program_id, |
| 32 | + &expected_allocated_key)) |
| 33 | + { |
| 34 | + return ERROR_INVALID_INSTRUCTION_DATA; |
| 35 | + } |
| 36 | + |
| 37 | + // allocated key does not match the derived address |
| 38 | + if (!SolPubkey_same(&expected_allocated_key, allocated_info.key)) |
| 39 | + { |
| 40 | + return ERROR_INVALID_ARGUMENT; |
| 41 | + } |
| 42 | + |
| 43 | + SolAccountMeta arguments[] = {{allocated_info.key, true, true}}; |
| 44 | + uint8_t data[4 + 8]; // Enough room for the Allocate instruction |
| 45 | + *(uint16_t *)data = 8; // Allocate instruction enum value |
| 46 | + *(uint64_t *)(data + 4) = SIZE; // Size to allocate |
| 47 | + const SolInstruction instruction = {system_program_info.key, arguments, |
| 48 | + SOL_ARRAY_SIZE(arguments), data, |
| 49 | + SOL_ARRAY_SIZE(data)}; |
| 50 | + |
| 51 | + return sol_invoke_signed(&instruction, params.ka, params.ka_num, |
| 52 | + signers_seeds, SOL_ARRAY_SIZE(signers_seeds)); |
| 53 | +} |
0 commit comments