// Solbound v3. Each successful grant or renewal pays the exact configured lamports. // Record layouts, PDA seeds and owner-only revocations follow v1. #include #include enum { REVOKE = 1, REVOKE_ALL = 2, PAID_GRANT = 6, UPDATE_CONFIG = 4, INIT_CONFIG = 5 }; enum { VAULT_TAG = 1, GRANT_TAG = 2, CONFIG_TAG = 4, STATE_SIZE = 41, GRANT_SIZE = 146, CONFIG_SIZE = 82, MAX_ACCOUNTS = 6 }; enum { OK = 0, BAD_EXPIRY = 101, BAD_SCOPE = 102, BAD_PDA = 103, INVALID_STATE = 104, ARITHMETIC_ERROR = 105, UNSUPPORTED_RENT = 106, BAD_TREASURY = 107, FEE_LIMIT = 111, BAD_CONFIG = 112, STALE_CONFIG = 113, PAUSED = 114, BAD_AUTHORITY = 115 }; static const SolPubkey SYSTEM_PROGRAM = {{0}}; static const SolPubkey UPGRADEABLE_LOADER = {{0x02, 0xa8, 0xf6, 0x91, 0x4e, 0x88, 0xa1, 0xb0, 0xe2, 0x10, 0x15, 0x3e, 0xf7, 0x63, 0xae, 0x2b, 0x00, 0xc2, 0xb9, 0x3d, 0x16, 0xc1, 0x24, 0xd2, 0xc0, 0x53, 0x7a, 0x10, 0x04, 0x80, 0x00, 0x00}}; // Initial recipient only. Subsequent changes require the config authority's signature. static const SolPubkey TREASURY = {{0xa2, 0x60, 0xe1, 0x25, 0x90, 0x41, 0x9d, 0x63, 0x84, 0x5d, 0x2a, 0x9b, 0xdc, 0x56, 0xba, 0x89, 0x71, 0xd2, 0x94, 0xf7, 0x39, 0x1b, 0x0a, 0xb7, 0xf3, 0xd1, 0x31, 0x61, 0x18, 0xac, 0x35, 0xf6}}; static const uint64_t MAX_FEE_LAMPORTS = 1000000000ULL; static const uint64_t INITIAL_FEE_LAMPORTS = 32000000ULL; static const uint64_t MAX_LIFETIME = 366UL * 24 * 60 * 60; // Runtime syscall ABI. Both structs match the aligned repr(C) sysvars. typedef struct { uint64_t slot; int64_t start; uint64_t epoch, leader_epoch; int64_t now; } Clock; typedef struct { uint64_t price, threshold_bits; uint8_t burn, padding[7]; } Rent; extern uint64_t sol_get_clock_sysvar(Clock *); extern uint64_t sol_get_rent_sysvar(Rent *); extern void sol_memset_(void *, uint8_t, uint64_t); // LLVM may lower a constant-size zeroing operation to the C ABI. __attribute__((visibility("hidden"))) void *memset(void *dst, int value, size_t size) { sol_memset_(dst, (uint8_t)value, size); return dst; } static uint64_t read_u64(const uint8_t *p) { uint64_t n; __builtin_memcpy(&n, p, sizeof(n)); return n; } static void write_u64(uint8_t *p, uint64_t n) { __builtin_memcpy(p, &n, sizeof(n)); } static bool same(const uint8_t *a, const uint8_t *b) { for (int i = 0; i < 32; i++) if (a[i] != b[i]) return false; return true; } static uint64_t check_pda(const SolParameters *p, const SolAccountInfo *account, SolSignerSeed *seeds, int count, uint8_t *bump) { SolPubkey derived; uint64_t err = sol_try_find_program_address(seeds, count, p->program_id, &derived, bump); if (err) return err; if (!SolPubkey_same(&derived, account->key)) return BAD_PDA; seeds[count] = (SolSignerSeed){bump, 1}; return OK; } // Each grant belongs to precisely one (vault, delegate, audience, scope, asset). static void grant_seeds(SolSignerSeed seeds[7], const SolPubkey *vault, const uint8_t *delegate, const uint8_t *audience, const uint8_t *scope, const uint8_t *asset) { seeds[0] = (SolSignerSeed){(const uint8_t *)"grant", 5}; seeds[1] = (SolSignerSeed){vault->x, 32}; seeds[2] = (SolSignerSeed){delegate, 32}; seeds[3] = (SolSignerSeed){audience, 32}; seeds[4] = (SolSignerSeed){scope, 1}; seeds[5] = (SolSignerSeed){asset, 32}; } static uint64_t system_call(const SolParameters *p, SolAccountMeta *metas, uint64_t count, uint8_t *data, uint64_t length, const SolSignerSeeds *signer) { SolPubkey system = SYSTEM_PROGRAM; SolInstruction ix = {&system, metas, count, data, length}; return sol_invoke_signed(&ix, p->ka, p->ka_num, signer, signer ? 1 : 0); } // Allocate/assign works even if a third party previously sent lamports to the PDA. // The vault pays record deposits only to the canonical registry PDAs. static uint64_t ensure_record(const SolParameters *p, SolAccountInfo *record, uint64_t size, SolSignerSeed *seeds, int count, bool *created) { *created = false; if (!record->is_writable || record->executable) return ERROR_INVALID_ACCOUNT_DATA; if (SolPubkey_same(record->owner, p->program_id)) return record->data_len == size ? OK : INVALID_STATE; if (!SolPubkey_same(record->owner, &SYSTEM_PROGRAM) || record->data_len != 0) return ERROR_INCORRECT_PROGRAM_ID; Rent rent = {0}; uint64_t err = sol_get_rent_sysvar(&rent); if (err) return err; // The current and legacy rent sysvars use exact 1.0 and 2.0 thresholds. uint64_t years; if (rent.threshold_bits == 0x3ff0000000000000UL) years = 1; else if (rent.threshold_bits == 0x4000000000000000UL) years = 2; else return UNSUPPORTED_RENT; uint64_t units = (128 + size) * years; // Maximum allocation in this program is GRANT_SIZE bytes, at most two years. if (rent.price > UINT64_MAX / ((128 + GRANT_SIZE) * 2)) return ARITHMETIC_ERROR; uint64_t minimum = rent.price * units; SolAccountMeta payer = {p->ka[0].key, true, true}; SolAccountMeta target = {record->key, true, false}; if (*record->lamports < minimum) { uint8_t transfer[12] = {2}; write_u64(transfer + 4, minimum - *record->lamports); SolAccountMeta metas[] = {payer, target}; err = system_call(p, metas, 2, transfer, sizeof(transfer), NULL); if (err) return err; } SolSignerSeeds signer = {seeds, count}; target.is_signer = true; uint8_t allocate[12] = {8}; write_u64(allocate + 4, size); err = system_call(p, &target, 1, allocate, sizeof(allocate), &signer); if (err) return err; uint8_t assign[36] = {1}; __builtin_memcpy(assign + 4, p->program_id->x, 32); err = system_call(p, &target, 1, assign, sizeof(assign), &signer); if (err) return err; *created = true; return OK; } static bool valid_state(const SolParameters *p, const SolAccountInfo *state) { return SolPubkey_same(state->owner, p->program_id) && state->data_len == STATE_SIZE && state->data[0] == VAULT_TAG && same(p->ka[0].key->x, state->data + 1); } static uint64_t check_config(const SolParameters *p, const SolAccountInfo *config, SolSignerSeed seeds[2], uint8_t *bump) { seeds[0] = (SolSignerSeed){(const uint8_t *)"config", 6}; return check_pda(p, config, seeds, 1, bump); } static bool valid_config(const SolParameters *p, const SolAccountInfo *config) { return SolPubkey_same(config->owner, p->program_id) && !config->executable && config->data_len == CONFIG_SIZE && config->data[0] == CONFIG_TAG; } static uint64_t init_config(const SolParameters *p) { if (p->ka_num != 4 || p->data_len != 33) return ERROR_INVALID_INSTRUCTION_DATA; if (!p->ka[0].is_writable || !SolPubkey_same(p->ka[2].key, &SYSTEM_PROGRAM) || same(p->data + 1, SYSTEM_PROGRAM.x)) return BAD_CONFIG; // Loader-v3 ProgramData: u32 tag 3, u64 slot, Option (1 + 32 bytes). const SolAccountInfo *code = &p->ka[3]; SolSignerSeed program_seed = {p->program_id->x, 32}; SolPubkey canonical; uint8_t bump; uint64_t err = sol_try_find_program_address(&program_seed, 1, &UPGRADEABLE_LOADER, &canonical, &bump); if (err) return err; if (!SolPubkey_same(code->key, &canonical) || !SolPubkey_same(code->owner, &UPGRADEABLE_LOADER) || code->executable || code->data_len <= 45 || code->data[0] != 3 || code->data[1] || code->data[2] || code->data[3] || code->data[12] != 1 || !same(code->data + 13, p->ka[0].key->x)) return BAD_AUTHORITY; SolAccountInfo *config = &p->ka[1]; SolSignerSeed seeds[2]; err = check_config(p, config, seeds, &bump); if (err) return err; bool created; err = ensure_record(p, config, CONFIG_SIZE, seeds, 2, &created); if (err) return err; if (!created) return BAD_CONFIG; config->data[0] = CONFIG_TAG; __builtin_memcpy(config->data + 1, p->data + 1, 32); __builtin_memcpy(config->data + 33, TREASURY.x, 32); write_u64(config->data + 65, INITIAL_FEE_LAMPORTS); config->data[73] = 0; write_u64(config->data + 74, 1); return OK; } static uint64_t update_config(const SolParameters *p) { if (p->ka_num != 2 || p->data_len != 50) return ERROR_INVALID_INSTRUCTION_DATA; SolAccountInfo *config = &p->ka[1]; SolSignerSeed seeds[2]; uint8_t bump; uint64_t err = check_config(p, config, seeds, &bump); if (err) return err; if (!config->is_writable || !valid_config(p, config)) return BAD_CONFIG; if (!same(config->data + 1, p->ka[0].key->x)) return BAD_AUTHORITY; uint64_t version = read_u64(config->data + 74), lamports = read_u64(p->data + 9); if (version != read_u64(p->data + 1) || version == UINT64_MAX) return STALE_CONFIG; if (lamports > MAX_FEE_LAMPORTS || p->data[49] > 1 || same(p->data + 17, SYSTEM_PROGRAM.x)) return BAD_CONFIG; __builtin_memcpy(config->data + 33, p->data + 17, 32); write_u64(config->data + 65, lamports); config->data[73] = p->data[49]; write_u64(config->data + 74, version + 1); return OK; } static uint64_t collect_service_fee(const SolParameters *p) { const SolAccountInfo *treasury = &p->ka[4], *config = &p->ka[5]; SolSignerSeed seeds[2]; uint8_t bump; uint64_t err = check_config(p, config, seeds, &bump); if (err) return err; if (!valid_config(p, config)) return BAD_CONFIG; if (read_u64(config->data + 74) != read_u64(p->data + 114)) return STALE_CONFIG; if (config->data[73]) return PAUSED; // The recipient must remain a plain System account, distinct from registry records. // This also excludes prefunded state/grant PDAs before their allocation below. if (!same(treasury->key->x, config->data + 33) || !treasury->is_writable || treasury->executable || !SolPubkey_same(treasury->owner, &SYSTEM_PROGRAM) || treasury->data_len || SolPubkey_same(treasury->key, &SYSTEM_PROGRAM) || SolPubkey_same(treasury->key, p->ka[1].key) || SolPubkey_same(treasury->key, p->ka[2].key) || SolPubkey_same(treasury->key, config->key)) return BAD_TREASURY; uint64_t lamports = read_u64(config->data + 65), approved = read_u64(p->data + 106); if (lamports > MAX_FEE_LAMPORTS) return BAD_CONFIG; if (approved != lamports || approved > MAX_FEE_LAMPORTS) return FEE_LIMIT; if (!lamports) return OK; // A transfer from the treasury to itself would have no cost. Refuse that paid grant. if (SolPubkey_same(treasury->key, p->ka[0].key)) return BAD_TREASURY; uint8_t transfer[12] = {2}; write_u64(transfer + 4, lamports); SolAccountMeta metas[] = {{p->ka[0].key, true, true}, {treasury->key, true, false}}; return system_call(p, metas, 2, transfer, sizeof(transfer), NULL); } static uint64_t grant(const SolParameters *p) { if (p->ka_num != 6 || p->data_len != 122) return ERROR_INVALID_INSTRUCTION_DATA; SolAccountInfo *vault = &p->ka[0], *state = &p->ka[1], *grant = &p->ka[2]; if (!vault->is_writable || !SolPubkey_same(p->ka[3].key, &SYSTEM_PROGRAM)) return ERROR_INVALID_ARGUMENT; const uint8_t *scope = p->data + 1, *delegate = p->data + 2, *audience = p->data + 34, *asset = p->data + 66; if (same(delegate, SYSTEM_PROGRAM.x) || same(delegate, vault->key->x) || same(audience, SYSTEM_PROGRAM.x)) return ERROR_INVALID_ARGUMENT; if (*scope > 1 || ((*scope == 0) != same(asset, SYSTEM_PROGRAM.x))) return BAD_SCOPE; Clock clock; uint64_t err = sol_get_clock_sysvar(&clock); if (err) return err; int64_t expiry = (int64_t)read_u64(p->data + 98); if (clock.now < 0 || expiry <= clock.now || (uint64_t)(expiry - clock.now) > MAX_LIFETIME) return BAD_EXPIRY; SolSignerSeed state_seeds[3] = {{(const uint8_t *)"vault", 5}, {vault->key->x, 32}}; SolSignerSeed seeds[7]; grant_seeds(seeds, vault->key, delegate, audience, scope, asset); uint8_t state_bump, grant_bump; err = check_pda(p, state, state_seeds, 2, &state_bump); if (err) return err; err = check_pda(p, grant, seeds, 6, &grant_bump); if (err) return err; // Any later error, including record allocation, rolls the transfer back with the instruction. err = collect_service_fee(p); if (err) return err; bool created; err = ensure_record(p, state, STATE_SIZE, state_seeds, 3, &created); if (err) return err; if (created) { state->data[0] = VAULT_TAG; __builtin_memcpy(state->data + 1, vault->key->x, 32); write_u64(state->data + 33, 0); } else if (!valid_state(p, state)) return INVALID_STATE; err = ensure_record(p, grant, GRANT_SIZE, seeds, 7, &created); if (err) return err; if (!created && (grant->data[0] != GRANT_TAG || !same(grant->data + 1, vault->key->x) || !same(grant->data + 33, delegate) || !same(grant->data + 65, audience) || grant->data[97] != *scope || !same(grant->data + 98, asset))) return INVALID_STATE; grant->data[0] = GRANT_TAG; __builtin_memcpy(grant->data + 1, vault->key->x, 32); __builtin_memcpy(grant->data + 33, delegate, 32); __builtin_memcpy(grant->data + 65, audience, 32); grant->data[97] = *scope; __builtin_memcpy(grant->data + 98, asset, 32); write_u64(grant->data + 130, read_u64(state->data + 33)); write_u64(grant->data + 138, expiry); return OK; } static uint64_t revoke_one(const SolParameters *p) { if (p->ka_num != 2 || p->data_len != 1) return ERROR_INVALID_INSTRUCTION_DATA; SolAccountInfo *vault = &p->ka[0], *grant = &p->ka[1]; if (!vault->is_writable || !grant->is_writable || !SolPubkey_same(grant->owner, p->program_id) || grant->data_len != GRANT_SIZE || grant->data[0] != GRANT_TAG || !same(grant->data + 1, vault->key->x)) return INVALID_STATE; SolSignerSeed seeds[7]; uint8_t bump; grant_seeds(seeds, vault->key, grant->data + 33, grant->data + 65, grant->data + 97, grant->data + 98); uint64_t err = check_pda(p, grant, seeds, 6, &bump); if (err) return err; if (*vault->lamports > UINT64_MAX - *grant->lamports) return ARITHMETIC_ERROR; *vault->lamports += *grant->lamports; *grant->lamports = 0; // Clear the serialized length and contents before returning ownership to System. // A same-transaction deposit can no longer revive a closed grant. __builtin_memset(grant->data, 0, GRANT_SIZE); write_u64(grant->data - sizeof(uint64_t), 0); grant->data_len = 0; __builtin_memset(grant->owner->x, 0, 32); return OK; } static uint64_t revoke_all(const SolParameters *p) { if (p->ka_num != 2 || p->data_len != 1) return ERROR_INVALID_INSTRUCTION_DATA; SolAccountInfo *state = &p->ka[1]; if (!state->is_writable || !valid_state(p, state)) return INVALID_STATE; SolSignerSeed seeds[3] = {{(const uint8_t *)"vault", 5}, {p->ka[0].key->x, 32}}; uint8_t bump; uint64_t err = check_pda(p, state, seeds, 2, &bump); if (err) return err; uint64_t epoch = read_u64(state->data + 33); if (epoch == UINT64_MAX) return ARITHMETIC_ERROR; write_u64(state->data + 33, epoch + 1); return OK; } uint64_t entrypoint(const uint8_t *input) { SolAccountInfo accounts[MAX_ACCOUNTS]; SolParameters p = {.ka = accounts}; if (!sol_deserialize(input, &p, MAX_ACCOUNTS) || !p.ka_num || p.ka_num > MAX_ACCOUNTS || !p.data_len) return ERROR_INVALID_INSTRUCTION_DATA; if (!p.ka[0].is_signer) return ERROR_MISSING_REQUIRED_SIGNATURES; if (!SolPubkey_same(p.ka[0].owner, &SYSTEM_PROGRAM) || p.ka[0].data_len || p.ka[0].executable) return ERROR_INVALID_ARGUMENT; switch (p.data[0]) { case PAID_GRANT: return grant(&p); case UPDATE_CONFIG: return update_config(&p); case INIT_CONFIG: return init_config(&p); case REVOKE: return revoke_one(&p); case REVOKE_ALL: return revoke_all(&p); default: return ERROR_INVALID_INSTRUCTION_DATA; } }