@typesol/cli 0.47.0 → 0.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/CHANGELOG.md +299 -0
  2. package/NOTICE +17 -0
  3. package/README.md +106 -37
  4. package/SECURITY.md +5 -0
  5. package/dist/compiler/emit/base.d.ts +12 -4
  6. package/dist/compiler/emit/base.js +19 -5
  7. package/dist/compiler/emit/calls.js +59 -1
  8. package/dist/compiler/emit/cpi.js +3 -1
  9. package/dist/compiler/emit/expressions.js +3 -1
  10. package/dist/compiler/emit/function.js +7 -2
  11. package/dist/compiler/emit/program.js +9 -4
  12. package/dist/compiler/emit/statements.d.ts +29 -2
  13. package/dist/compiler/emit/statements.js +149 -6
  14. package/dist/compiler/emit/storage.js +8 -3
  15. package/dist/compiler/emit/token.js +18 -1
  16. package/dist/compiler/hints.js +3 -0
  17. package/dist/compiler/toolchain.js +1 -0
  18. package/dist/idl-coder.d.ts +21 -2
  19. package/dist/idl-coder.js +42 -4
  20. package/dist/testing.d.ts +14 -3
  21. package/dist/testing.js +87 -17
  22. package/docker/deps/0.48.0.lock.json +1206 -0
  23. package/docker/deps/0.49.0.lock.json +1206 -0
  24. package/docs/architecture.md +2 -2
  25. package/docs/audit.md +31 -15
  26. package/docs/case-study-escrow.md +7 -5
  27. package/docs/case-study-marginfi.md +15 -2
  28. package/docs/case-study-multisig.md +2 -1
  29. package/docs/case-study-raydium-amm.md +2 -1
  30. package/docs/case-study-spl-stake-pool.md +4 -3
  31. package/docs/language-accounts.md +5 -6
  32. package/docs/language-basics.md +2 -1
  33. package/docs/language-builtins.md +5 -1
  34. package/docs/language-chain.md +13 -1
  35. package/docs/language-cpi.md +26 -0
  36. package/docs/language-errors-events.md +16 -1
  37. package/docs/language-native.md +23 -3
  38. package/docs/language-statements.md +4 -2
  39. package/docs/language.md +2 -2
  40. package/docs/mainnet.md +1 -1
  41. package/docs/runtime.md +7 -0
  42. package/docs/security-model.md +11 -4
  43. package/docs/stability.md +9 -4
  44. package/docs/test-your-program.md +43 -4
  45. package/docs/testing.md +16 -4
  46. package/docs/troubleshooting.md +1 -1
  47. package/lib/typesol.d.ts +46 -1
  48. package/llms-full.txt +164 -34
  49. package/llms.txt +2 -1
  50. package/package.json +3 -3
  51. package/releases/0.47.0.json +837 -0
  52. package/releases/0.48.0.json +847 -0
  53. package/runtime/typesol_rt.h +276 -1
@@ -77,7 +77,8 @@ static __attribute__((noinline)) uint64_t ts_fail(uint64_t code, const char *msg
77
77
  #ifdef TS_PANIC_ON_OVERFLOW
78
78
  /* PANIC_ON_OVERFLOW: arithmetic fails as a Rust `checked_*().unwrap()` does, with a panic (ProgramFailedToComplete). */
79
79
  #ifdef TS_PANIC_ABORT
80
- /* PANIC_STYLE "solana-1.14": a panic ends in the abort syscall ("SBF program panicked"), as that panic handler's. */
80
+ /* PANIC_STYLE "solana-1.14" or "solana-1.18" (with PANIC_ON_OVERFLOW): a panic ends in the abort syscall ("SBF
81
+ * program panicked"), as those solana_program panic handlers' do. */
81
82
  if (code == TS_ERR_OVERFLOW || code == TS_ERR_DIV_ZERO) abort();
82
83
  #else
83
84
  if (code == TS_ERR_OVERFLOW || code == TS_ERR_DIV_ZERO) sol_panic_("", 0, 0, 0);
@@ -2802,6 +2803,280 @@ static inline uint64_t ts_t22_init_metadata(TsParams *params, const TsAccount *p
2802
2803
  return ts_invoke(params, &ix, &signer, n > 0 ? 1 : 0);
2803
2804
  }
2804
2805
 
2806
+ /*
2807
+ * ---- Processed sibling instructions ----------------------------------------------------------------------------------
2808
+ * sol_get_processed_sibling_instruction, as solana-instruction's get_processed_sibling_instruction calls it: the
2809
+ * instructions the program that invoked this one has already invoked at this stack height (index 0 the latest), or the
2810
+ * transaction's earlier top-level instructions when this program runs at the top. A first call with zero lengths
2811
+ * reads the sizes, a second one the program id, data and accounts (Rust's AccountMeta: key, is_signer, is_writable),
2812
+ * which are copied to the heap in the Instructions sysvar's account layout (flags, then key) for TsIx.
2813
+ */
2814
+ typedef struct {
2815
+ uint64_t data_len;
2816
+ uint64_t accounts_len;
2817
+ } TsSiblingMeta;
2818
+ extern uint64_t sol_get_processed_sibling_instruction(uint64_t index, TsSiblingMeta *meta, SolPubkey *program_id, uint8_t *data, uint8_t *accounts);
2819
+
2820
+ /* How many processed sibling instructions there are: indexes 0, 1, … until the syscall finds none. */
2821
+ static __attribute__((unused)) uint64_t ts_sibling_count(void) {
2822
+ uint64_t n = 0;
2823
+ for (;;) {
2824
+ TsSiblingMeta meta = {0, 0};
2825
+ SolPubkey program;
2826
+ uint8_t data = 0;
2827
+ uint8_t account[34];
2828
+ if (sol_get_processed_sibling_instruction(n, &meta, &program, &data, account) != 1) return n;
2829
+ n++;
2830
+ }
2831
+ }
2832
+
2833
+ static __attribute__((unused)) uint64_t ts_sibling_load(uint64_t index, TsIx *out) {
2834
+ TsSiblingMeta meta = {0, 0};
2835
+ SolPubkey *program;
2836
+ TS_TRY(ts_alloc(sizeof(SolPubkey), (uint8_t **)&program));
2837
+ uint8_t byte = 0;
2838
+ uint8_t account[34];
2839
+ if (sol_get_processed_sibling_instruction(index, &meta, program, &byte, account) != 1)
2840
+ TS_FAIL(TS_ERR_INDEX, "TypeSOL: no processed sibling instruction at that index");
2841
+ if (meta.data_len > 0xffff || meta.accounts_len > 0xffff) TS_FAIL(TS_ERR_INDEX, "TypeSOL: the sibling instruction is too large to read");
2842
+ uint8_t *data, *raw, *accounts;
2843
+ TS_TRY(ts_alloc(meta.data_len + 1, &data));
2844
+ TS_TRY(ts_alloc(meta.accounts_len * 34 + 1, &raw));
2845
+ TS_TRY(ts_alloc(meta.accounts_len * 33 + 1, &accounts));
2846
+ sol_get_processed_sibling_instruction(index, &meta, program, data, raw);
2847
+ for (uint64_t i = 0; i < meta.accounts_len; i++) {
2848
+ accounts[i * 33] = (uint8_t)((raw[i * 34 + 32] ? 1 : 0) | (raw[i * 34 + 33] ? 2 : 0));
2849
+ ts_memcpy(accounts + i * 33 + 1, raw + i * 34, 32);
2850
+ }
2851
+ out->accounts = accounts;
2852
+ out->program_id = program;
2853
+ out->data = data;
2854
+ out->num_accounts = (uint16_t)meta.accounts_len;
2855
+ out->data_len = (uint16_t)meta.data_len;
2856
+ return SUCCESS;
2857
+ }
2858
+
2859
+ /*
2860
+ * ---- TransferChecked with a transfer hook's extra accounts ----------------------------------------------------------
2861
+ * Rust's `spl_token_2022::instruction::transfer_checked` followed, when the mint's TransferHook extension names a
2862
+ * program, by spl-transfer-hook-interface 2.1's `onchain::add_extra_accounts_for_execute_cpi` (with
2863
+ * spl-tlv-account-resolution 0.11's ExtraAccountMetaList::add_to_cpi_instruction), then `invoke_signed`. The extra
2864
+ * accounts are resolved from the hook's validation account (["extra-account-metas", mint] of the hook program) among
2865
+ * `ka[lo..hi]`, with the same refusals and codes as those crates: TransferHookError::IncorrectAccount when the hook
2866
+ * program is not among them, AccountResolutionError (2724315840 + n), TlvError::TypeNotFound, PodSliceError
2867
+ * BufferTooSmall (Custom(1)), InvalidAccountData and InvalidArgument. PDA seeds go to sol_try_find_program_address as
2868
+ * Rust's find_program_address sends them, and a failed search panics as it does.
2869
+ */
2870
+ #define TS_HOOK_INCORRECT_ACCOUNT 2110272652u
2871
+ #define TS_ACCT_RES(n) ((uint64_t)(2724315840u + (n)))
2872
+ #define TS_ACCT_RES_INCORRECT_ACCOUNT TS_ACCT_RES(0)
2873
+ #define TS_ACCT_RES_INVALID_BYTES_FOR_SEED TS_ACCT_RES(9)
2874
+ #define TS_ACCT_RES_INSTRUCTION_DATA_TOO_SMALL TS_ACCT_RES(11)
2875
+ #define TS_ACCT_RES_ACCOUNT_NOT_FOUND TS_ACCT_RES(12)
2876
+ #define TS_ACCT_RES_ACCOUNT_DATA_TOO_SMALL TS_ACCT_RES(15)
2877
+ #define TS_TLV_TYPE_NOT_FOUND 1202666432u
2878
+
2879
+ /* The program id of a Token-2022 mint's TransferHook extension (type 14: authority, program id), NULL for none. */
2880
+ static __attribute__((unused)) const SolPubkey *ts_mint_hook_program(const TsAccount *mint) {
2881
+ if (!ts_key_eq(&mint->owner, &TS_TOKEN_2022_PROGRAM_ID)) return NULL;
2882
+ uint64_t off = 166;
2883
+ while (off + 4 <= mint->data_len) {
2884
+ uint64_t t = mint->data[off] | ((uint64_t)mint->data[off + 1] << 8);
2885
+ uint64_t len = mint->data[off + 2] | ((uint64_t)mint->data[off + 3] << 8);
2886
+ if (t == 0) return NULL;
2887
+ if (t == 14) {
2888
+ if (off + 4 + 64 > mint->data_len) return NULL;
2889
+ const SolPubkey *program = (const SolPubkey *)(mint->data + off + 4 + 32);
2890
+ for (int i = 0; i < 32; i++)
2891
+ if (program->x[i]) return program;
2892
+ return NULL; /* OptionalNonZeroPubkey: all zeros is None */
2893
+ }
2894
+ off += 4 + len;
2895
+ }
2896
+ return NULL;
2897
+ }
2898
+
2899
+ static __attribute__((unused)) const TsAccount *ts_hook_find(const TsParams *params, uint64_t lo, uint64_t hi, const SolPubkey *key) {
2900
+ for (uint64_t i = lo; i < hi; i++)
2901
+ if (ts_key_eq(&params->ka[i]->key, key)) return params->ka[i];
2902
+ return NULL;
2903
+ }
2904
+
2905
+ static __attribute__((unused)) uint64_t ts_token_transfer_checked_hook(TsParams *params, const TsAccount *program, TsAccount *from, TsAccount *mint,
2906
+ TsAccount *to, TsAccount *authority, uint64_t amount, uint64_t lo, uint64_t hi,
2907
+ const SolSignerSeed *seeds, int n) {
2908
+ uint8_t data[10] = {12}; /* TransferChecked */
2909
+ ts_memcpy(data + 1, &amount, 8);
2910
+ data[9] = mint->data[44];
2911
+ const SolPubkey *hook = ts_mint_hook_program(mint);
2912
+ if (!hook) {
2913
+ SolAccountMeta metas[4] = {{&from->key, true, false}, {&mint->key, false, false}, {&to->key, true, false}, {&authority->key, false, true}};
2914
+ return ts_token_cpi(params, program, data, sizeof(data), metas, 4, seeds, n);
2915
+ }
2916
+ const TsAccount *hook_program = ts_hook_find(params, lo, hi, hook);
2917
+ if (!hook_program) return TS_HOOK_INCORRECT_ACCOUNT;
2918
+ SolSignerSeed vseeds[2] = {{(const uint8_t *)"extra-account-metas", 19}, {mint->key.x, 32}};
2919
+ SolPubkey vaddr;
2920
+ uint8_t vbump;
2921
+ if (sol_try_find_program_address(vseeds, 2, hook, &vaddr, &vbump) != SUCCESS) sol_panic_("", 0, 0, 0);
2922
+ const TsAccount *validation = ts_hook_find(params, lo, hi, &vaddr);
2923
+ uint64_t count = 0;
2924
+ const uint8_t *list = NULL;
2925
+ if (validation) {
2926
+ const uint8_t *d = validation->data;
2927
+ uint64_t len = validation->data_len;
2928
+ /* TlvStateBorrowed::unpack: every entry must fit (zeros may follow the last) */
2929
+ uint64_t i = 0;
2930
+ while (i < len) {
2931
+ if (len < i + 8) {
2932
+ for (uint64_t k = i; k < len; k++)
2933
+ if (d[k]) return ERROR_INVALID_ACCOUNT_DATA;
2934
+ break;
2935
+ }
2936
+ bool zero = true;
2937
+ for (int k = 0; k < 8; k++)
2938
+ if (d[i + k]) zero = false;
2939
+ if (zero) break;
2940
+ if (len < i + 12) return ERROR_INVALID_ACCOUNT_DATA;
2941
+ uint64_t end = i + 12 + *(const ts_u32_unaligned *)(d + i + 8);
2942
+ if (end > len) return ERROR_INVALID_ACCOUNT_DATA;
2943
+ i = end;
2944
+ }
2945
+ /* get_first_bytes::<ExecuteInstruction>() */
2946
+ static const uint8_t exec_disc[8] = {105, 37, 101, 197, 75, 251, 102, 26};
2947
+ uint64_t at = 0;
2948
+ bool found = false;
2949
+ while (at < len) {
2950
+ if (len < at + 12) return ERROR_INVALID_ACCOUNT_DATA;
2951
+ bool zero = true, match = true;
2952
+ for (int k = 0; k < 8; k++) {
2953
+ if (d[at + k]) zero = false;
2954
+ if (d[at + k] != exec_disc[k]) match = false;
2955
+ }
2956
+ if (match) {
2957
+ found = true;
2958
+ break;
2959
+ }
2960
+ if (zero) return TS_TLV_TYPE_NOT_FOUND;
2961
+ at = at + 12 + *(const ts_u32_unaligned *)(d + at + 8);
2962
+ }
2963
+ if (!found) return ERROR_INVALID_ACCOUNT_DATA;
2964
+ uint64_t vlen = *(const ts_u32_unaligned *)(d + at + 8);
2965
+ if (len < at + 12 + vlen) return ERROR_INVALID_ACCOUNT_DATA;
2966
+ /* ListView<ExtraAccountMeta, PodU32>::unpack */
2967
+ if (vlen < 4) return 1; /* PodSliceError::BufferTooSmall */
2968
+ if ((vlen - 4) % 35 != 0) return ERROR_INVALID_ARGUMENT;
2969
+ count = *(const ts_u32_unaligned *)(d + at + 12);
2970
+ if (count > (vlen - 4) / 35) return 1;
2971
+ list = d + at + 16;
2972
+ }
2973
+ /* The execute instruction's accounts as resolution reads them: source, mint, destination, authority, the validation
2974
+ account (all read-only), then each extra account as it is resolved. */
2975
+ const TsAccount **ex = NULL;
2976
+ bool *exw = NULL;
2977
+ SolAccountMeta *metas = NULL;
2978
+ TS_TRY(ts_alloc((5 + count) * sizeof(TsAccount *) + 1, (uint8_t **)&ex));
2979
+ TS_TRY(ts_alloc(5 + count + 1, (uint8_t **)&exw));
2980
+ TS_TRY(ts_alloc((6 + count) * sizeof(SolAccountMeta) + 1, (uint8_t **)&metas));
2981
+ uint64_t exn = 5;
2982
+ ex[0] = from, ex[1] = mint, ex[2] = to, ex[3] = authority, ex[4] = validation;
2983
+ for (int k = 0; k < 5; k++) exw[k] = false;
2984
+ uint8_t ixdata[16];
2985
+ static const uint8_t exec_disc2[8] = {105, 37, 101, 197, 75, 251, 102, 26};
2986
+ ts_memcpy(ixdata, exec_disc2, 8);
2987
+ ts_memcpy(ixdata + 8, &amount, 8);
2988
+ for (uint64_t j = 0; j < count; j++) {
2989
+ const uint8_t *e = list + 35 * j;
2990
+ const uint8_t *cfg = e + 1;
2991
+ SolPubkey key;
2992
+ bool writable = e[34] != 0;
2993
+ if (e[0] == 0) {
2994
+ ts_memcpy(key.x, cfg, 32);
2995
+ } else if (e[0] == 1 || e[0] >= 128) {
2996
+ const SolPubkey *owner = hook;
2997
+ if (e[0] >= 128) {
2998
+ uint64_t idx = e[0] - 128;
2999
+ if (idx >= exn) return TS_ACCT_RES_ACCOUNT_NOT_FOUND;
3000
+ owner = &ex[idx]->key;
3001
+ }
3002
+ /* Seed::unpack_address_config, then resolve_pda */
3003
+ uint8_t kind[16], a0[16], a1[16], a2[16];
3004
+ int ns = 0;
3005
+ uint64_t i = 0;
3006
+ while (i < 32) {
3007
+ uint8_t dsc = cfg[i];
3008
+ uint64_t rest = 31 - i;
3009
+ if (dsc == 0) break;
3010
+ if (dsc == 1) {
3011
+ if (rest < 1 || rest - 1 < cfg[i + 1]) return TS_ACCT_RES_INVALID_BYTES_FOR_SEED;
3012
+ a0[ns] = (uint8_t)(i + 2), a1[ns] = cfg[i + 1];
3013
+ i += 2 + cfg[i + 1];
3014
+ } else if (dsc == 2) {
3015
+ if (rest < 2) return TS_ACCT_RES_INVALID_BYTES_FOR_SEED;
3016
+ a0[ns] = cfg[i + 1], a1[ns] = cfg[i + 2];
3017
+ i += 3;
3018
+ } else if (dsc == 3) {
3019
+ if (rest < 1) return TS_ACCT_RES_INVALID_BYTES_FOR_SEED;
3020
+ a0[ns] = cfg[i + 1];
3021
+ i += 2;
3022
+ } else if (dsc == 4) {
3023
+ if (rest < 3) return TS_ACCT_RES_INVALID_BYTES_FOR_SEED;
3024
+ a0[ns] = cfg[i + 1], a1[ns] = cfg[i + 2], a2[ns] = cfg[i + 3];
3025
+ i += 4;
3026
+ } else
3027
+ return ERROR_INVALID_ACCOUNT_DATA;
3028
+ kind[ns++] = dsc;
3029
+ }
3030
+ SolSignerSeed pseeds[16];
3031
+ for (int k = 0; k < ns; k++) {
3032
+ if (kind[k] == 1) pseeds[k] = (SolSignerSeed){cfg + a0[k], a1[k]};
3033
+ else if (kind[k] == 2) {
3034
+ if ((uint64_t)a0[k] + a1[k] > 16) return TS_ACCT_RES_INSTRUCTION_DATA_TOO_SMALL;
3035
+ pseeds[k] = (SolSignerSeed){ixdata + a0[k], a1[k]};
3036
+ } else if (kind[k] == 3) {
3037
+ if (a0[k] >= exn) return TS_ACCT_RES_ACCOUNT_NOT_FOUND;
3038
+ pseeds[k] = (SolSignerSeed){ex[a0[k]]->key.x, 32};
3039
+ } else {
3040
+ if (a0[k] >= exn) return TS_ACCT_RES_ACCOUNT_NOT_FOUND;
3041
+ if (ex[a0[k]]->data_len < (uint64_t)a1[k] + a2[k]) return TS_ACCT_RES_ACCOUNT_DATA_TOO_SMALL;
3042
+ pseeds[k] = (SolSignerSeed){ex[a0[k]]->data + a1[k], a2[k]};
3043
+ }
3044
+ }
3045
+ uint8_t bump;
3046
+ if (sol_try_find_program_address(pseeds, ns, owner, &key, &bump) != SUCCESS) sol_panic_("", 0, 0, 0);
3047
+ } else if (e[0] == 2) {
3048
+ /* PubkeyData: from the instruction data (16 bytes, too short for a key) or from an account's data */
3049
+ if (cfg[0] == 1) return TS_ACCT_RES_INSTRUCTION_DATA_TOO_SMALL;
3050
+ if (cfg[0] != 2) return ERROR_INVALID_ACCOUNT_DATA;
3051
+ if (cfg[1] >= exn) return TS_ACCT_RES_ACCOUNT_NOT_FOUND;
3052
+ if (ex[cfg[1]]->data_len < (uint64_t)cfg[2] + 32) return TS_ACCT_RES_ACCOUNT_DATA_TOO_SMALL;
3053
+ ts_memcpy(key.x, ex[cfg[1]]->data + cfg[2], 32);
3054
+ } else
3055
+ return ERROR_INVALID_ACCOUNT_DATA;
3056
+ /* de_escalate_account_meta: read-only when every earlier mention of the key is; never a signer */
3057
+ bool seen = false, any_writable = false;
3058
+ for (uint64_t k = 0; k < exn; k++)
3059
+ if (ts_key_eq(&ex[k]->key, &key)) seen = true, any_writable = any_writable || exw[k];
3060
+ if (seen && !any_writable) writable = false;
3061
+ const TsAccount *acct = ts_hook_find(params, lo, hi, &key);
3062
+ if (!acct) return TS_ACCT_RES_INCORRECT_ACCOUNT;
3063
+ ex[exn] = acct;
3064
+ exw[exn++] = writable;
3065
+ }
3066
+ uint64_t m = 0;
3067
+ metas[m++] = (SolAccountMeta){&from->key, true, false};
3068
+ metas[m++] = (SolAccountMeta){&mint->key, false, false};
3069
+ metas[m++] = (SolAccountMeta){&to->key, true, false};
3070
+ metas[m++] = (SolAccountMeta){&authority->key, false, true};
3071
+ if (validation) {
3072
+ for (uint64_t k = 5; k < exn; k++) metas[m++] = (SolAccountMeta){(SolPubkey *)&ex[k]->key, exw[k], false};
3073
+ metas[m++] = (SolAccountMeta){(SolPubkey *)&validation->key, false, false};
3074
+ }
3075
+ metas[m++] = (SolAccountMeta){(SolPubkey *)&hook_program->key, false, false};
3076
+ if (m > TS_MAX_ACCOUNTS) TS_FAIL(TS_ERR_TOO_MANY_ACCOUNTS, "TypeSOL: the transfer lists more accounts than the program's MAX_ACCOUNTS");
3077
+ return ts_token_cpi(params, program, data, sizeof(data), metas, m, seeds, n);
3078
+ }
3079
+
2805
3080
  /* Metaplex metadata PDA: ["metadata", program, mint] under Token Metadata. */
2806
3081
  static inline uint64_t ts_metadata_address(const SolPubkey *mint, SolPubkey *out) {
2807
3082
  SolSignerSeed seeds[3] = {{(const uint8_t *)"metadata", 8}, {(const uint8_t *)&TS_MPL_METADATA_ID, 32}, {(const uint8_t *)mint, 32}};