@typesol/cli 0.44.0 → 0.45.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 (47) hide show
  1. package/CHANGELOG.md +166 -0
  2. package/NOTICE +14 -1
  3. package/README.md +52 -10
  4. package/SECURITY.md +1 -1
  5. package/dist/cli.js +194 -48
  6. package/dist/compiler/emit/alias.d.ts +16 -2
  7. package/dist/compiler/emit/alias.js +62 -4
  8. package/dist/compiler/emit/calls.js +15 -7
  9. package/dist/compiler/emit/cpi-core.d.ts +6 -0
  10. package/dist/compiler/emit/cpi-core.js +18 -5
  11. package/dist/compiler/emit/create.js +19 -11
  12. package/dist/compiler/emit/function.js +5 -5
  13. package/dist/compiler/emit/metaplex.js +17 -4
  14. package/dist/compiler/emit/token.js +64 -39
  15. package/dist/compiler/index.d.ts +51 -1
  16. package/dist/compiler/index.js +17 -2
  17. package/dist/compiler/toolchain.d.ts +23 -0
  18. package/dist/compiler/toolchain.js +128 -8
  19. package/dist/tools/docker.d.ts +183 -0
  20. package/dist/tools/docker.js +442 -0
  21. package/dist/tools/doctor.d.ts +3 -0
  22. package/dist/tools/doctor.js +12 -0
  23. package/dist/workspace.d.ts +2 -1
  24. package/dist/workspace.js +33 -6
  25. package/docker/Dockerfile +163 -0
  26. package/docker/deps/0.43.0.lock.json +1206 -0
  27. package/docker/deps/0.44.0.lock.json +1206 -0
  28. package/docker/deps/0.44.1.lock.json +1206 -0
  29. package/docker/deps/0.45.0.lock.json +1206 -0
  30. package/docs/architecture.md +13 -5
  31. package/docs/audit.md +54 -7
  32. package/docs/cli.md +31 -4
  33. package/docs/errors.md +1 -1
  34. package/docs/language.md +6 -4
  35. package/docs/mainnet.md +86 -6
  36. package/docs/security-model.md +47 -12
  37. package/docs/stability.md +62 -3
  38. package/docs/testing.md +49 -2
  39. package/docs/troubleshooting.md +46 -0
  40. package/llms-full.txt +283 -34
  41. package/package.json +3 -1
  42. package/releases/0.43.0.json +669 -0
  43. package/releases/0.44.0.json +672 -0
  44. package/releases/0.44.1.json +672 -0
  45. package/runtime/LICENSE-compiler-builtins.txt +275 -0
  46. package/runtime/typesol_builtins.c +484 -0
  47. package/runtime/typesol_rt.h +8 -5
@@ -321,18 +321,20 @@ export class CreateEmitter extends CpiCoreEmitter {
321
321
  // Each initializer: its bytes, then a CPI with the mint as the only (writable) account.
322
322
  const lines = [];
323
323
  const cpi = (bytes) => {
324
- const total = bytes.reduce((n, [, k]) => n + k, 0);
324
+ const total = bytes.reduce((n, b) => n + (Array.isArray(b) ? b[1] : 33), 0);
325
325
  const buf = this.tmp("xd");
326
326
  lines.push(`{ uint8_t ${buf}[${total}]; uint64_t at = 0;`);
327
- for (const [src, n] of bytes)
328
- lines.push(` ts_memcpy(${buf} + at, ${src}, ${n}); at += ${n};`);
327
+ for (const b of bytes)
328
+ if (Array.isArray(b))
329
+ lines.push(` ts_memcpy(${buf} + at, ${b[0]}, ${b[1]}); at += ${b[1]};`);
330
+ else
331
+ lines.push(` ${buf}[at++] = ${b.option}.some ? 1 : 0;`, ` if (${b.option}.some) { ts_memcpy(${buf} + at, &${b.option}.v, 32); at += 32; }`);
329
332
  lines.push(` SolAccountMeta m[1] = {{&${acct}->key, true, false}};`, ` TS_TRY(ts_cpi(params, ${tp}, ${buf}, at, m, 1, NULL, 0)); }`);
330
333
  };
331
334
  const tag = (n) => `(const uint8_t[]){${n}}`;
332
- const coption = (t) => [
333
- [`(const uint8_t[]){${t}.some ? 1 : 0}`, 1],
334
- [`&${t}.v`, 32],
335
- ];
335
+ // Token-2022's COption<Pubkey> in instruction data (pack_pubkey_option): a tag byte, then the key only when Some.
336
+ // A None is one byte, and Token-2022 reads the next field right after it.
337
+ const coption = (t) => [{ option: t }];
336
338
  let space = 0;
337
339
  for (const name of Object.keys(KINDS)) {
338
340
  const v = held.get(name);
@@ -441,8 +443,11 @@ export class CreateEmitter extends CpiCoreEmitter {
441
443
  checks.push(`{ SolPubkey k, w = ${want}; if (!ts_t22_key(${acct}, ${c.type}, ${at}, &k)) ts_memzero(&k, 32); if (!ts_key_eq(&k, &w)) TS_FAIL(${c.code + 1 + i}, "TypeSOL: the mint's ${name} ${f === "v" ? "key" : f} differs"); }`);
442
444
  });
443
445
  }
444
- // Base account (165, the mint padded to an account's size), the account type byte, then each TLV entry.
445
- return { space: 165 + 1 + space, lines, checks };
446
+ // Base account (165, the mint padded to an account's size), the account type byte, then each TLV entry. A total of
447
+ // 355 is a Multisig's length, which Token-2022 never gives a mint: its own size calculation adds 2 bytes then
448
+ // (try_calculate_account_len), and InitializeMint2 refuses an account of any other size.
449
+ const total = 165 + 1 + space;
450
+ return { space: total === 355 ? total + 2 : total, lines, checks };
446
451
  }
447
452
  /**
448
453
  * `ata.create(payer)` / `ata.createIfNeeded(payer)` on `Ata<Init|InitIfNeeded<TokenAccount>, "owner", "mint">`
@@ -536,9 +541,12 @@ export class CreateEmitter extends CpiCoreEmitter {
536
541
  if (a.length === 3)
537
542
  this.line(`TS_TRY(ts_system_transfer(params, ${from.c}, ${to.c}, ${amt.c}));`);
538
543
  else {
539
- // A system-owned PDA of this program (a SOL vault) pays.
544
+ // A system-owned PDA of this program (a SOL vault) pays. JS reads the amount before the seed list: copied first
545
+ // when the list runs code that may change it.
546
+ const mark = this.lines.length;
540
547
  const s = this.signingSeeds(a[3]);
541
- this.line(`TS_TRY(ts_system_transfer_signed(params, ${from.c}, ${to.c}, ${amt.c}, ${s.name}, ${s.count}));`);
548
+ const lamports = this.runsCode(a[3], true) ? this.settle(amt, mark) : amt;
549
+ this.line(`TS_TRY(ts_system_transfer_signed(params, ${from.c}, ${to.c}, ${lamports.c}, ${s.name}, ${s.count}));`);
542
550
  }
543
551
  return undefined;
544
552
  }
@@ -152,14 +152,14 @@ export class FunctionEmitter extends StatementEmitter {
152
152
  head.push(...vecLenCheck(k.struct, v, `d_${a.name}`));
153
153
  }
154
154
  }
155
- // realloc(x, …) needs the size the account had when the instruction started (the 10 KiB growth limit).
155
+ // realloc(x, …) needs the size the account had when the instruction started (the 10 KiB growth limit), as does
156
+ // updateExtraAccountMetas(payer, x, …).
156
157
  const reallocated = (() => {
157
158
  let found = false;
158
159
  const visit = (nd) => {
159
- if (ts.isCallExpression(nd) &&
160
- ["realloc", "resizeAccount"].includes(this.exportName(nd.expression) ?? "") &&
161
- nd.arguments[0] &&
162
- this.accountAt(nd.arguments[0]) === a)
160
+ const fn = ts.isCallExpression(nd) ? (this.exportName(nd.expression) ?? "") : "";
161
+ const target = ts.isCallExpression(nd) ? nd.arguments[fn === "updateExtraAccountMetas" ? 1 : 0] : undefined;
162
+ if (["realloc", "resizeAccount", "updateExtraAccountMetas"].includes(fn) && target && this.accountAt(target) === a)
163
163
  found = true;
164
164
  ts.forEachChild(nd, visit);
165
165
  };
@@ -327,9 +327,12 @@ export class MetaplexEmitter extends TokenEmitter {
327
327
  for (const k of ["name", "symbol", "uri", "sellerFeeBasisPoints"])
328
328
  if (!props.has(k))
329
329
  this.fail(a[2], `data needs ${k} (it replaces the whole DataV2)`);
330
+ // A view of account text is read at the call: copied first when another argument runs code that may rewrite it.
331
+ const others = (n) => [...props.values(), ...a.slice(3)].some((o) => o !== n && this.runsCode(o, true));
330
332
  const text = (k) => {
331
333
  const t = this.tmp("md");
332
- this.line(`TS_Str ${t} = ${this.expect(this.expr(props.get(k), STRING), STRING, props.get(k)).c};`);
334
+ const v = this.expect(this.expr(props.get(k), STRING), STRING, props.get(k));
335
+ this.line(`TS_Str ${t} = ${(others(props.get(k)) ? this.ownedString(v) : v).c};`);
333
336
  return t;
334
337
  };
335
338
  [name, symbol, uri] = ["name", "symbol", "uri"].map(text);
@@ -353,9 +356,19 @@ export class MetaplexEmitter extends TokenEmitter {
353
356
  this.line(`SolPubkey ${t} = *(${key.c});`);
354
357
  next = `&${t}`;
355
358
  }
356
- const flag = (n) => (isNull(n) ? "-1" : `((${this.expect(this.expr(n, BOOL), BOOL, n).c}) ? 1 : 0)`);
357
- const primary = flag(a[4]);
358
- const mutable = flag(a[5]);
359
+ const flag = (i) => {
360
+ if (isNull(a[i]))
361
+ return "-1";
362
+ const f = `((${this.expect(this.expr(a[i], BOOL), BOOL, a[i]).c}) ? 1 : 0)`;
363
+ if (!a.slice(i + 1).some((n) => this.runsCode(n, true)))
364
+ return f;
365
+ // Read before a later argument that runs code, as JS reads it.
366
+ const t = this.tmp("flag");
367
+ this.line(`int ${t} = ${f};`);
368
+ return t;
369
+ };
370
+ const primary = flag(4);
371
+ const mutable = flag(5);
359
372
  const seeds = this.signerSeeds(a[6], ua, a[1]);
360
373
  this.line(`TS_TRY(ts_mpl_update_v2(params, a_${program.name}, a_${metadata.name}, a_${ua.name}, ${hasData}, ${name}, ${symbol}, ${uri}, ${fee}, ${creators}, ${ncreators}, ${collection}, ${next}, ${primary}, ${mutable}, ${seeds.name}, ${seeds.count}));`);
361
374
  return undefined;
@@ -24,15 +24,23 @@ export class TokenEmitter extends CreateEmitter {
24
24
  if (a.length !== fixed && a.length !== fixed + 1)
25
25
  this.fail(node, `${method} takes ${fixed} arguments plus optional signerSeeds`);
26
26
  };
27
- const amount = (n) => this.expect(this.expr(n, U64), U64, n).c;
27
+ /**
28
+ * The amount, then the signer seeds: JS reads the amount first, so it is copied before a seed list that runs code
29
+ * (`["vault", next(st), bump]`) when it reads storage that code may change.
30
+ */
31
+ const amountThenSeeds = (n, seedsNode, auth, at) => {
32
+ const v = this.expect(this.expr(n, U64), U64, n);
33
+ const mark = this.lines.length;
34
+ const s = this.signerSeeds(seedsNode, auth, at);
35
+ return [seedsNode && this.runsCode(seedsNode, true) ? this.settle(v, mark).c : v.c, s];
36
+ };
28
37
  switch (method) {
29
38
  case "transfer": {
30
39
  arity(4);
31
40
  const from = want(a[0], "token", true, "the source");
32
41
  const to = want(a[1], "token", true, "the destination");
33
42
  const auth = this.cpiAccount(a[2], "the authority");
34
- const amt = amount(a[3]);
35
- const s = this.signerSeeds(a[4], auth, a[2]);
43
+ const [amt, s] = amountThenSeeds(a[3], a[4], auth, a[2]);
36
44
  this.line(`TS_TRY(ts_token_transfer(params, ${p}, ${from.c}, ${to.c}, ${auth.c}, ${amt}, ${s.name}, ${s.count}));`);
37
45
  return undefined;
38
46
  }
@@ -42,8 +50,7 @@ export class TokenEmitter extends CreateEmitter {
42
50
  const mint = want(a[1], "mint", false, "the mint");
43
51
  const to = want(a[2], "token", true, "the destination");
44
52
  const auth = this.cpiAccount(a[3], "the authority");
45
- const amt = amount(a[4]);
46
- const s = this.signerSeeds(a[5], auth, a[3]);
53
+ const [amt, s] = amountThenSeeds(a[4], a[5], auth, a[3]);
47
54
  this.line(`TS_TRY(ts_token_transfer_checked(params, ${p}, ${from.c}, ${mint.c}, ${to.c}, ${auth.c}, ${amt}, ${s.name}, ${s.count}));`);
48
55
  return undefined;
49
56
  }
@@ -56,8 +63,7 @@ export class TokenEmitter extends CreateEmitter {
56
63
  this.fail(a[0], "the mint must be a Mut<Mint>, or a Mut<Signer> / Mut<AccountInfo> mint this instruction created");
57
64
  const to = want(a[1], "token", true, "the destination");
58
65
  const auth = this.cpiAccount(a[2], "the mint authority");
59
- const amt = amount(a[3]);
60
- const s = this.signerSeeds(a[4], auth, a[2]);
66
+ const [amt, s] = amountThenSeeds(a[3], a[4], auth, a[2]);
61
67
  this.line(`TS_TRY(ts_token_mint_to(params, ${p}, ${mint.c}, ${to.c}, ${auth.c}, ${amt}, ${s.name}, ${s.count}));`);
62
68
  return undefined;
63
69
  }
@@ -66,8 +72,7 @@ export class TokenEmitter extends CreateEmitter {
66
72
  const from = want(a[0], "token", true, "the burned account");
67
73
  const mint = want(a[1], "mint", true, "the mint");
68
74
  const auth = this.cpiAccount(a[2], "the authority");
69
- const amt = amount(a[3]);
70
- const s = this.signerSeeds(a[4], auth, a[2]);
75
+ const [amt, s] = amountThenSeeds(a[3], a[4], auth, a[2]);
71
76
  this.line(`TS_TRY(ts_token_burn(params, ${p}, ${from.c}, ${mint.c}, ${auth.c}, ${amt}, ${s.name}, ${s.count}));`);
72
77
  return undefined;
73
78
  }
@@ -82,7 +87,11 @@ export class TokenEmitter extends CreateEmitter {
82
87
  this.fail(a[0], "the mint must be writable (Mut<...>)");
83
88
  const ua = this.cpiAccount(a[1], "the update authority");
84
89
  const ma = this.cpiAccount(a[2], "the mint authority");
85
- const [nm, sym, uri] = [a[3], a[4], a[5]].map((n) => this.expect(this.expr(n, STRING), STRING, n).c);
90
+ // Read at the call: a view of account text is copied first when a later argument runs code that may rewrite it.
91
+ const [nm, sym, uri] = [3, 4, 5].map((i) => {
92
+ const v = this.expect(this.expr(a[i], STRING), STRING, a[i]);
93
+ return (a.slice(i + 1).some((n) => this.runsCode(n, true)) ? this.ownedString(v) : v).c;
94
+ });
86
95
  const s = this.signerSeeds(a[6], ma, a[2]);
87
96
  this.line(`TS_TRY(ts_t22_init_metadata(params, ${p}, ${mint.c}, ${ua.c}, ${ma.c}, ${nm}, ${sym}, ${uri}, ${s.name}, ${s.count}));`);
88
97
  return undefined;
@@ -110,7 +119,15 @@ export class TokenEmitter extends CreateEmitter {
110
119
  this.fail(n, `${what} must be writable (Mut<...>)`);
111
120
  return acct;
112
121
  };
113
- const simple = (tag, fields, metas, seeds) => {
122
+ /**
123
+ * Signer seeds given as a function are lowered after the fields when an argument runs code (JS evaluates the
124
+ * fields first: `approve(src, d, owner, next(st), ["vault", st.count, bump])` signs for the count next() left), and
125
+ * before them otherwise, which keeps those programs' code as it was.
126
+ */
127
+ const seedsFirst = (seeds, fields) => typeof seeds === "function" && !(fields.length && a.some((n) => this.runsCode(n, true))) ? seeds() : seeds;
128
+ const seedsNow = (seeds) => (typeof seeds === "function" ? seeds() : seeds);
129
+ const simple = (tag, fields, metas, seeds0) => {
130
+ const seeds = seedsFirst(seeds0, fields);
114
131
  const { buf, len, put, finish } = this.byteBuffer("tok", 128, "TS_ERR_INSTRUCTION_DATA", "token instruction");
115
132
  const t = this.tmp("tag");
116
133
  this.line(`uint8_t ${t} = ${tag};`);
@@ -118,7 +135,7 @@ export class TokenEmitter extends CreateEmitter {
118
135
  for (const f of fields)
119
136
  f(put);
120
137
  finish(node);
121
- this.cpi(p, metas.map(([acc, w, sg]) => [acc.c, w, sg]), buf, len, seeds);
138
+ this.cpi(p, metas.map(([acc, w, sg]) => [acc.c, w, sg]), buf, len, seedsNow(seeds));
122
139
  };
123
140
  const value = (n, t) => (put) => this.serialize(this.expect(this.expr(n, t), t, n), n, put);
124
141
  const optKey = { kind: "option", inner: PUBKEY };
@@ -151,7 +168,8 @@ export class TokenEmitter extends CreateEmitter {
151
168
  };
152
169
  const none = { name: "NULL", count: 0 };
153
170
  // The Token Group interface (spl-token-group-interface), which Token-2022 implements: 8-byte discriminators.
154
- const grouped = (disc, fields, metas, seeds) => {
171
+ const grouped = (disc, fields, metas, seeds0) => {
172
+ const seeds = seedsFirst(seeds0, fields);
155
173
  const { buf, len, put, finish } = this.byteBuffer("grp", 96, "TS_ERR_INSTRUCTION_DATA", "token group instruction");
156
174
  const d = this.tmp("disc");
157
175
  this.line(`static const uint8_t ${d}[8] = {${disc.join(", ")}};`);
@@ -159,7 +177,7 @@ export class TokenEmitter extends CreateEmitter {
159
177
  for (const f of fields)
160
178
  f(put);
161
179
  finish(node);
162
- this.cpi(p, metas.map(([acc, w, sg]) => [acc.c, w, sg]), buf, len, seeds);
180
+ this.cpi(p, metas.map(([acc, w, sg]) => [acc.c, w, sg]), buf, len, seedsNow(seeds));
163
181
  };
164
182
  switch (method) {
165
183
  case "initializeTokenGroup": {
@@ -173,7 +191,7 @@ export class TokenEmitter extends CreateEmitter {
173
191
  [group, true, false],
174
192
  [mint, false, false],
175
193
  [auth, false, true],
176
- ], this.signerSeeds(a[5], auth, a[2]));
194
+ ], () => this.signerSeeds(a[5], auth, a[2]));
177
195
  return undefined;
178
196
  }
179
197
  case "initializeGroupMember": {
@@ -203,7 +221,7 @@ export class TokenEmitter extends CreateEmitter {
203
221
  grouped([108, 37, 171, 143, 248, 30, 18, 110], [value(a[2], U64)], [
204
222
  [group, true, false],
205
223
  [auth, false, true],
206
- ], this.signerSeeds(a[3], auth, a[1]));
224
+ ], () => this.signerSeeds(a[3], auth, a[1]));
207
225
  return undefined;
208
226
  }
209
227
  case "updateGroupAuthority": {
@@ -215,7 +233,7 @@ export class TokenEmitter extends CreateEmitter {
215
233
  grouped([161, 105, 88, 1, 237, 221, 216, 203], [nonZeroKey(a[2])], [
216
234
  [group, true, false],
217
235
  [auth, false, true],
218
- ], this.signerSeeds(a[3], auth, a[1]));
236
+ ], () => this.signerSeeds(a[3], auth, a[1]));
219
237
  return undefined;
220
238
  }
221
239
  case "initializeTransferFeeConfig": {
@@ -294,7 +312,7 @@ export class TokenEmitter extends CreateEmitter {
294
312
  simple(28, [byte(1), (put) => put(`&${st}`, "1", 1)], [
295
313
  [mint, true, false],
296
314
  [auth, false, true],
297
- ], this.signerSeeds(a[3], auth, a[1]));
315
+ ], () => this.signerSeeds(a[3], auth, a[1]));
298
316
  return undefined;
299
317
  }
300
318
  case "enableRequiredMemoTransfers":
@@ -307,7 +325,7 @@ export class TokenEmitter extends CreateEmitter {
307
325
  simple(30, [byte(method === "enableRequiredMemoTransfers" ? 0 : 1)], [
308
326
  [acct, true, false],
309
327
  [owner, false, true],
310
- ], this.signerSeeds(a[2], owner, a[1]));
328
+ ], () => this.signerSeeds(a[2], owner, a[1]));
311
329
  return undefined;
312
330
  }
313
331
  case "updateTransferHook": {
@@ -319,7 +337,7 @@ export class TokenEmitter extends CreateEmitter {
319
337
  simple(36, [byte(1), nonZeroKey(a[2])], [
320
338
  [mint, true, false],
321
339
  [auth, false, true],
322
- ], this.signerSeeds(a[3], auth, a[1]));
340
+ ], () => this.signerSeeds(a[3], auth, a[1]));
323
341
  return undefined;
324
342
  }
325
343
  case "updateInterestRate": {
@@ -331,7 +349,7 @@ export class TokenEmitter extends CreateEmitter {
331
349
  simple(33, [byte(1), value(a[2], I16)], [
332
350
  [mint, true, false],
333
351
  [auth, false, true],
334
- ], this.signerSeeds(a[3], auth, a[1]));
352
+ ], () => this.signerSeeds(a[3], auth, a[1]));
335
353
  return undefined;
336
354
  }
337
355
  case "transferCheckedWithFee": {
@@ -353,7 +371,7 @@ export class TokenEmitter extends CreateEmitter {
353
371
  [mint, false, false],
354
372
  [to, true, false],
355
373
  [auth, false, true],
356
- ], this.signerSeeds(a[6], auth, a[3]));
374
+ ], () => this.signerSeeds(a[6], auth, a[3]));
357
375
  return undefined;
358
376
  }
359
377
  case "harvestWithheldTokensToMint": {
@@ -383,7 +401,7 @@ export class TokenEmitter extends CreateEmitter {
383
401
  [mint, true, false],
384
402
  [dest, true, false],
385
403
  [auth, false, true],
386
- ], this.signerSeeds(a[3], auth, a[2]));
404
+ ], () => this.signerSeeds(a[3], auth, a[2]));
387
405
  return undefined;
388
406
  }
389
407
  case "setTransferFee": {
@@ -400,7 +418,7 @@ export class TokenEmitter extends CreateEmitter {
400
418
  simple(26, [byte(5), bps, value(a[3], U64)], [
401
419
  [mint, true, false],
402
420
  [auth, false, true],
403
- ], this.signerSeeds(a[4], auth, a[1]));
421
+ ], () => this.signerSeeds(a[4], auth, a[1]));
404
422
  return undefined;
405
423
  }
406
424
  case "updateMetadataField":
@@ -445,7 +463,9 @@ export class TokenEmitter extends CreateEmitter {
445
463
  this.serialize(this.expect(this.expr(a[3], STRING), STRING, a[3]), a[3], put);
446
464
  }
447
465
  else if (method === "removeMetadataKey") {
448
- const key = this.expect(this.expr(a[2], STRING), STRING, a[2]);
466
+ // Written after `idempotent` is read: a view of account text is copied first when that runs code.
467
+ const key0 = this.expect(this.expr(a[2], STRING), STRING, a[2]);
468
+ const key = this.runsCode(a[3], true) ? this.ownedString(key0) : key0;
449
469
  const idem = this.expect(this.expr(a[3], BOOL), BOOL, a[3]);
450
470
  const t = this.tmp("idem");
451
471
  this.line(`uint8_t ${t} = (${idem.c}) ? 1 : 0;`);
@@ -475,7 +495,7 @@ export class TokenEmitter extends CreateEmitter {
475
495
  const src = want(a[0], "token", true, "the source");
476
496
  const delegate = this.cpiAccount(a[1], "the delegate");
477
497
  const owner = this.cpiAccount(a[2], "the owner");
478
- const s = this.signerSeeds(a[4], owner, a[2]);
498
+ const s = () => this.signerSeeds(a[4], owner, a[2]);
479
499
  const amt = value(a[3], U64);
480
500
  simple(4, [amt], [
481
501
  [src, true, false],
@@ -491,7 +511,7 @@ export class TokenEmitter extends CreateEmitter {
491
511
  const mint = want(a[1], "mint", false, "the mint");
492
512
  const delegate = this.cpiAccount(a[2], "the delegate");
493
513
  const owner = this.cpiAccount(a[3], "the owner");
494
- const s = this.signerSeeds(a[5], owner, a[3]);
514
+ const s = () => this.signerSeeds(a[5], owner, a[3]);
495
515
  simple(13, [value(a[4], U64), decimals(mint)], [
496
516
  [src, true, false],
497
517
  [mint, false, false],
@@ -505,7 +525,7 @@ export class TokenEmitter extends CreateEmitter {
505
525
  const mint = want(a[0], "mint", true, "the mint");
506
526
  const to = want(a[1], "token", true, "the destination");
507
527
  const auth = this.cpiAccount(a[2], "the mint authority");
508
- const s = this.signerSeeds(a[4], auth, a[2]);
528
+ const s = () => this.signerSeeds(a[4], auth, a[2]);
509
529
  simple(14, [value(a[3], U64), decimals(mint)], [
510
530
  [mint, true, false],
511
531
  [to, true, false],
@@ -518,7 +538,7 @@ export class TokenEmitter extends CreateEmitter {
518
538
  const from = want(a[0], "token", true, "the burned account");
519
539
  const mint = want(a[1], "mint", true, "the mint");
520
540
  const auth = this.cpiAccount(a[2], "the authority");
521
- const s = this.signerSeeds(a[4], auth, a[2]);
541
+ const s = () => this.signerSeeds(a[4], auth, a[2]);
522
542
  simple(15, [value(a[3], U64), decimals(mint)], [
523
543
  [from, true, false],
524
544
  [mint, true, false],
@@ -533,7 +553,7 @@ export class TokenEmitter extends CreateEmitter {
533
553
  simple(5, [], [
534
554
  [src, true, false],
535
555
  [owner, false, true],
536
- ], this.signerSeeds(a[2], owner, a[1]));
556
+ ], () => this.signerSeeds(a[2], owner, a[1]));
537
557
  return undefined;
538
558
  }
539
559
  case "setAuthority": {
@@ -541,7 +561,7 @@ export class TokenEmitter extends CreateEmitter {
541
561
  const target = anyAcct(a[0], "the mint or token account", true);
542
562
  const current = this.cpiAccount(a[1], "the current authority");
543
563
  this.model.options.set("pubkey", PUBKEY);
544
- const s = this.signerSeeds(a[4], current, a[1]);
564
+ const s = () => this.signerSeeds(a[4], current, a[1]);
545
565
  simple(6, [value(a[2], U8), value(a[3], optKey)], [
546
566
  [target, true, false],
547
567
  [current, false, true],
@@ -558,7 +578,7 @@ export class TokenEmitter extends CreateEmitter {
558
578
  [acct, true, false],
559
579
  [mint, false, false],
560
580
  [auth, false, true],
561
- ], this.signerSeeds(a[3], auth, a[2]));
581
+ ], () => this.signerSeeds(a[3], auth, a[2]));
562
582
  return undefined;
563
583
  }
564
584
  case "syncNative": {
@@ -668,7 +688,10 @@ export class TokenEmitter extends CreateEmitter {
668
688
  this.fail(n, `pass ${what} as an account, or named(account, address) to name its address with the slot's account`);
669
689
  return acct(i, what);
670
690
  };
671
- const send = (tag, fields, metas, seeds) => {
691
+ // Seeds given as a function: lowered after the fields when an argument runs code, as JS evaluates them (see tokenOp).
692
+ const runs = (n) => this.runsCode(n, true);
693
+ const send = (tag, fields, metas, seeds0) => {
694
+ let seeds = typeof seeds0 === "function" && !(fields.length && a.some(runs)) ? seeds0() : seeds0;
672
695
  const { buf, len, put, finish } = this.byteBuffer("stk", 116, "TS_ERR_INSTRUCTION_DATA", "stake instruction");
673
696
  const t = this.tmp("tag");
674
697
  this.line(`uint32_t ${t} = ${tag};`);
@@ -676,6 +699,8 @@ export class TokenEmitter extends CreateEmitter {
676
699
  for (const f of fields)
677
700
  f(put);
678
701
  finish(node);
702
+ if (typeof seeds === "function")
703
+ seeds = seeds();
679
704
  if (metas.some(([x]) => x.named)) {
680
705
  // Addresses named with the slot's account: the account infos are the CPI's accounts, as passed (ts_cpi_named).
681
706
  const m = this.tmp("metas");
@@ -743,7 +768,7 @@ export class TokenEmitter extends CreateEmitter {
743
768
  [sysvar(1, "the Clock sysvar"), false, false],
744
769
  [auth, false, true],
745
770
  ...(opt.custodian ? [custodian(opt.custodian, opt.seeds)] : []),
746
- ], this.signerSeeds(opt.seeds, auth, a[2]));
771
+ ], () => this.signerSeeds(opt.seeds, auth, a[2]));
747
772
  return undefined;
748
773
  }
749
774
  case "delegate": {
@@ -756,7 +781,7 @@ export class TokenEmitter extends CreateEmitter {
756
781
  [sysvar(3, "the StakeHistory sysvar"), false, false],
757
782
  [sysvar(4, "the stake config account"), false, false],
758
783
  [staker, false, true],
759
- ], this.signerSeeds(a[6], staker, a[5]));
784
+ ], () => this.signerSeeds(a[6], staker, a[5]));
760
785
  return undefined;
761
786
  }
762
787
  case "split": {
@@ -766,7 +791,7 @@ export class TokenEmitter extends CreateEmitter {
766
791
  [acct(0, "the stake account", true), true, false],
767
792
  [acct(1, "the split stake account", true), true, false],
768
793
  [auth, false, true],
769
- ], this.signerSeeds(a[4], auth, a[2]));
794
+ ], () => this.signerSeeds(a[4], auth, a[2]));
770
795
  return undefined;
771
796
  }
772
797
  case "deactivate": {
@@ -776,7 +801,7 @@ export class TokenEmitter extends CreateEmitter {
776
801
  [acct(0, "the stake account", true), true, false],
777
802
  [sysvar(1, "the Clock sysvar"), false, false],
778
803
  [staker, false, true],
779
- ], this.signerSeeds(a[3], staker, a[2]));
804
+ ], () => this.signerSeeds(a[3], staker, a[2]));
780
805
  return undefined;
781
806
  }
782
807
  case "withdraw": {
@@ -794,7 +819,7 @@ export class TokenEmitter extends CreateEmitter {
794
819
  [sysvar(3, "the StakeHistory sysvar"), false, false],
795
820
  [withdrawer, false, true],
796
821
  ...(opt.custodian ? [custodian(opt.custodian, opt.seeds)] : []),
797
- ], this.signerSeeds(opt.seeds, withdrawer, a[4]));
822
+ ], () => this.signerSeeds(opt.seeds, withdrawer, a[4]));
798
823
  return undefined;
799
824
  }
800
825
  case "merge": {
@@ -806,7 +831,7 @@ export class TokenEmitter extends CreateEmitter {
806
831
  [sysvar(2, "the Clock sysvar"), false, false],
807
832
  [sysvar(3, "the StakeHistory sysvar"), false, false],
808
833
  [staker, false, true],
809
- ], this.signerSeeds(a[5], staker, a[4]));
834
+ ], () => this.signerSeeds(a[5], staker, a[4]));
810
835
  return undefined;
811
836
  }
812
837
  case "getMinimumDelegation": {
@@ -37,6 +37,54 @@ export interface BuildManifest {
37
37
  sources: Record<string, string>;
38
38
  /** sha256 of the .so. */
39
39
  binary: string;
40
+ /** Set when the build ran in the pinned container (`typesol build --docker`, after 0.44.0): what that environment is. */
41
+ container?: ContainerIdentity;
42
+ }
43
+ /**
44
+ * The build container of `--docker` (docker/Dockerfile): everything that decides what is inside it, so that another
45
+ * machine can build the same environment and compare. `image` is this machine's local image id; it can differ between
46
+ * machines that built the same Dockerfile (layer dates), so it identifies the image used, not the environment.
47
+ */
48
+ export interface ContainerIdentity {
49
+ /** The container platform: `linux/amd64` (before 0.45.0 some programs' binaries depended on the platform-tools host package). */
50
+ platform: string;
51
+ /** sha256 of docker/Dockerfile. */
52
+ dockerfile: string;
53
+ /** The local image id (`sha256:…`). */
54
+ image: string;
55
+ /** The base images, each pinned by digest (`node:24.22.2-bookworm-slim@sha256:…`). */
56
+ base: string[];
57
+ /** The Agave and platform-tools release files, with the sha256 the Dockerfile requires of them. */
58
+ downloads: {
59
+ url: string;
60
+ sha256: string;
61
+ }[];
62
+ /**
63
+ * The TypeSOL package installed in the image, as its release record (releases/<version>.json) describes it, and where
64
+ * that record came from (this package, a --release-record file, or the repository's main branch) with its sha256
65
+ * (recorded from 0.45.0 on).
66
+ */
67
+ typesol: {
68
+ version: string;
69
+ shasum: string;
70
+ integrity: string;
71
+ commit: string;
72
+ record?: {
73
+ kind: "package" | "file" | "repository";
74
+ location: string;
75
+ sha256: string;
76
+ };
77
+ };
78
+ /**
79
+ * The TypeSOL package's npm dependencies, installed by `npm ci` from this lock (every package checked against the
80
+ * integrity it records): `docker/deps/<version>.lock.json`, its sha256, and whether it came from the running
81
+ * package or from the version's own (checked) tarball (recorded from 0.45.0 on).
82
+ */
83
+ deps?: {
84
+ path: string;
85
+ sha256: string;
86
+ source: "package" | "tarball";
87
+ };
40
88
  }
41
89
  export interface BuildResult extends Generated {
42
90
  manifest: BuildManifest;
@@ -54,9 +102,11 @@ export declare function build(entry: string, outDir: string, programAddress: str
54
102
  /** The package version (one source: package.json), recorded in build manifests and shown by the CLI. */
55
103
  export declare const COMPILER_VERSION: string;
56
104
  /** A build's toolchain in one line: `clang 20.1.7-rust-dev (…), platform-tools v1.52, darwin-arm64, TypeSOL 0.44.0`. */
57
- export declare function describeToolchain(m: Pick<BuildManifest, "clang" | "platformTools" | "host" | "compiler">): string;
105
+ export declare function describeToolchain(m: Pick<BuildManifest, "clang" | "platformTools" | "host" | "compiler" | "container">): string;
58
106
  /** What differs between two builds' toolchains and compilers (the fields both record), e.g. ["clang", "TypeSOL version"]. */
59
107
  export declare function toolchainDifferences(a: Partial<BuildManifest>, b: Partial<BuildManifest>): string[];
108
+ /** The hosts (`<os>-<arch>`) on which programs were measured to build to the same bytes (docs/stability.md). */
109
+ export declare const MEASURED_HOSTS: readonly string[];
60
110
  /** The ELF's own length (it ends with the section header table): a program account keeps it zero-padded. */
61
111
  export declare function elfLength(elf: Buffer): number;
62
112
  /**
@@ -8,7 +8,7 @@ import { hintForTypeScript } from "./hints.js";
8
8
  import { emitProgram } from "./emit/index.js";
9
9
  import { buildIdl } from "./idl.js";
10
10
  import { buildSharedObject, checkToolchain, shortClang, TESTED_TOOLCHAIN } from "./toolchain.js";
11
- import { RUNTIME_DIR } from "./toolchain.js";
11
+ import { BUILTINS_SOURCE, RUNTIME_DIR } from "./toolchain.js";
12
12
  export { CompileError, formatDiagnostic } from "./diagnostics.js";
13
13
  export { SBPF_VERSIONS, TESTED_TOOLCHAIN } from "./toolchain.js";
14
14
  /** The hint the CLI adds under a TypeScript error (what to write instead in a TypeSOL program), for the editor plugin. */
@@ -43,6 +43,7 @@ export function build(entry, outDir, programAddress, opts = {}) {
43
43
  for (const f of gen.files)
44
44
  sources[path.relative(root, f)] = sha256(readFileSync(f));
45
45
  sources["<runtime>/typesol_rt.h"] = sha256(readFileSync(path.join(RUNTIME_DIR, "typesol_rt.h")));
46
+ sources["<runtime>/typesol_builtins.c"] = sha256(readFileSync(BUILTINS_SOURCE));
46
47
  const manifest = {
47
48
  program: gen.model.name,
48
49
  compiler: COMPILER_VERSION,
@@ -69,6 +70,7 @@ export function describeToolchain(m) {
69
70
  m.host ? `${m.host.os}-${m.host.arch}` : "host unknown",
70
71
  `TypeSOL ${m.compiler}`,
71
72
  ...(m.clang === TESTED_TOOLCHAIN.clang ? ["the tested toolchain"] : []),
73
+ ...(m.container ? [`in the build container (Dockerfile ${m.container.dockerfile.slice(0, 12)}…)`] : []),
72
74
  ].join(", ");
73
75
  }
74
76
  /** What differs between two builds' toolchains and compilers (the fields both record), e.g. ["clang", "TypeSOL version"]. */
@@ -84,9 +86,22 @@ export function toolchainDifferences(a, b) {
84
86
  out.push("TypeScript");
85
87
  if (a.sbpf !== b.sbpf)
86
88
  out.push("SBPF version");
87
- // The host is recorded, not compared: one clang version is meant to give the same bytes on every host.
89
+ // The same clang gives the same object code on each host measured. Before 0.45.0 programs linked platform-tools'
90
+ // compiler-builtins, which each host package splits into archive members differently, so one with 128-bit or signed
91
+ // division linked to another layout on another host; a build that links the runtime's builtins (its manifest lists
92
+ // them among the sources) gives the same bytes on each host measured (docs/stability.md). Between two such builds
93
+ // the host is not a difference when both hosts were measured, and "host (not measured)" when one was not.
94
+ const linksRuntimeBuiltins = (m) => !!m.sources?.["<runtime>/typesol_builtins.c"];
95
+ if (a.host && b.host && (a.host.os !== b.host.os || a.host.arch !== b.host.arch)) {
96
+ if (!(linksRuntimeBuiltins(a) && linksRuntimeBuiltins(b)))
97
+ out.push("host");
98
+ else if (!MEASURED_HOSTS.includes(`${a.host.os}-${a.host.arch}`) || !MEASURED_HOSTS.includes(`${b.host.os}-${b.host.arch}`))
99
+ out.push("host (not measured)");
100
+ }
88
101
  return out;
89
102
  }
103
+ /** The hosts (`<os>-<arch>`) on which programs were measured to build to the same bytes (docs/stability.md). */
104
+ export const MEASURED_HOSTS = ["darwin-arm64", "linux-arm64", "linux-x64"];
90
105
  /** The ELF's own length (it ends with the section header table): a program account keeps it zero-padded. */
91
106
  export function elfLength(elf) {
92
107
  if (elf.length < 0x40 || elf.readUInt32BE(0) !== 0x7f454c46)
@@ -9,14 +9,37 @@ export interface Toolchain {
9
9
  linkerScript: string;
10
10
  includes: string[];
11
11
  libDir: string;
12
+ /**
13
+ * platform-tools' compiler-builtins Rust library for this SBPF version. Not linked (programs link the runtime's own
14
+ * builtins, runtime/typesol_builtins.c, which build the same on each host measured); its presence tells that the
15
+ * platform-tools have this SBPF version's libraries, and the builtins tests compare against it.
16
+ */
12
17
  compilerBuiltins: string;
13
18
  }
19
+ /** The runtime's compiler builtins, compiled with each program and linked in place of compiler-builtins. */
20
+ export declare const BUILTINS_SOURCE: string;
14
21
  /** Locate the Solana C SDK that ships with the Agave CLI's platform-tools. */
15
22
  export declare function findToolchain(sbpf?: Sbpf): Toolchain;
16
23
  /** Compile generated C to an SBF shared object (same flags as the SDK's sbf.mk for the chosen SBPF version). */
17
24
  /** The SBF stack frame of one function. */
18
25
  export declare const STACK_FRAME = 4096;
26
+ /** clang's flags for SBF objects (the SDK's sbf.mk for the chosen SBPF version, plus TypeSOL's warnings). */
27
+ export declare function sbfCflags(sbpf?: Sbpf): string[];
28
+ /** The flags runtime/typesol_builtins.c is compiled with: the program's, and the SBPF version (its assembly differs). */
29
+ export declare function builtinsCflags(sbpf?: Sbpf): string[];
19
30
  export declare function buildSharedObject(cFile: string, outSo: string, sbpf?: Sbpf, tc?: Toolchain): void;
31
+ /** Link SBF objects into a program: libc from platform-tools, nothing from its Rust libraries. */
32
+ export declare function linkSharedObject(objects: string[], outSo: string, tc: Toolchain, extra?: string[]): void;
33
+ /**
34
+ * The syscalls a TypeSOL program may leave to the SBF loader: every one the SDK's headers declare (`@SYSCALL` in
35
+ * platform-tools-sdk/sbf/c/inc/sol/inc/*.inc) and every one runtime/typesol_rt.h declares itself, with `abort`.
36
+ * test/cli/toolchain.test.ts checks that this is exactly that set.
37
+ */
38
+ export declare const SYSCALLS: readonly string[];
39
+ /** Whether the SBF loader resolves `name` as a syscall for a TypeSOL program (one of SYSCALLS). */
40
+ export declare function isSyscall(name: string): boolean;
41
+ /** The undefined symbols of an SBF shared object's dynamic symbol table (what the loader resolves as syscalls). */
42
+ export declare function unresolvedSymbols(elf: Buffer): string[];
20
43
  /**
21
44
  * The toolchain this release is tested with: CI installs exactly this Agave CLI and platform-tools, and every
22
45
  * execution, differential and fuzz test runs on binaries this clang built. The C TypeSOL emits is only checked