@typesol/cli 0.44.1 → 0.46.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.
- package/CHANGELOG.md +343 -0
- package/NOTICE +14 -1
- package/README.md +115 -26
- package/SECURITY.md +10 -8
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +9 -0
- package/dist/cli.js +226 -62
- package/dist/compiler/analyze.js +57 -6
- package/dist/compiler/diagnostics.d.ts +4 -3
- package/dist/compiler/diagnostics.js +10 -6
- package/dist/compiler/emit/base.d.ts +1 -1
- package/dist/compiler/emit/calls.js +18 -5
- package/dist/compiler/emit/cpi-core.d.ts +3 -1
- package/dist/compiler/emit/cpi-core.js +8 -4
- package/dist/compiler/emit/expressions.d.ts +14 -0
- package/dist/compiler/emit/expressions.js +52 -4
- package/dist/compiler/emit/shared.d.ts +8 -1
- package/dist/compiler/emit/shared.js +8 -1
- package/dist/compiler/emit/statements.d.ts +8 -3
- package/dist/compiler/emit/statements.js +68 -3
- package/dist/compiler/emit/storage.js +5 -1
- package/dist/compiler/hints.d.ts +7 -0
- package/dist/compiler/hints.js +58 -9
- package/dist/compiler/index.d.ts +56 -1
- package/dist/compiler/index.js +27 -3
- package/dist/compiler/model.js +3 -2
- package/dist/compiler/toolchain.d.ts +73 -2
- package/dist/compiler/toolchain.js +222 -17
- package/dist/node-version.d.ts +4 -0
- package/dist/node-version.js +12 -0
- package/dist/tools/docker.d.ts +183 -0
- package/dist/tools/docker.js +442 -0
- package/dist/tools/doctor.d.ts +44 -0
- package/dist/tools/doctor.js +108 -14
- package/dist/workspace.d.ts +11 -3
- package/dist/workspace.js +46 -9
- package/docker/Dockerfile +163 -0
- package/docker/deps/0.43.0.lock.json +1206 -0
- package/docker/deps/0.44.0.lock.json +1206 -0
- package/docker/deps/0.44.1.lock.json +1206 -0
- package/docker/deps/0.45.0.lock.json +1206 -0
- package/docker/deps/0.46.0.lock.json +1206 -0
- package/docs/ai.md +2 -1
- package/docs/architecture.md +18 -6
- package/docs/audit.md +15 -7
- package/docs/case-study-phoenix.md +1 -1
- package/docs/case-study-raydium-amm.md +1 -1
- package/docs/case-study-spl-stake-pool.md +1 -1
- package/docs/cli.md +71 -6
- package/docs/errors.md +22 -8
- package/docs/glossary.md +210 -0
- package/docs/language-accounts.md +499 -0
- package/docs/language-basics.md +389 -0
- package/docs/language-builtins.md +411 -0
- package/docs/language-chain.md +198 -0
- package/docs/language-cpi.md +470 -0
- package/docs/language-errors-events.md +184 -0
- package/docs/language-native.md +493 -0
- package/docs/language-pdas.md +198 -0
- package/docs/language-statements.md +620 -0
- package/docs/language-types.md +601 -0
- package/docs/language.md +190 -467
- package/docs/mainnet.md +103 -8
- package/docs/migrate-from-anchor.md +7 -6
- package/docs/security-model.md +4 -4
- package/docs/stability.md +64 -3
- package/docs/test-your-program.md +52 -15
- package/docs/testing.md +49 -2
- package/docs/troubleshooting.md +210 -42
- package/lib/typesol.d.ts +67 -22
- package/llms-full.txt +5084 -591
- package/llms.txt +12 -1
- package/package.json +4 -2
- package/releases/0.43.0.json +669 -0
- package/releases/0.44.0.json +672 -0
- package/releases/0.44.1.json +672 -0
- package/releases/0.45.0.json +732 -0
- package/runtime/LICENSE-compiler-builtins.txt +275 -0
- package/runtime/typesol_builtins.c +484 -0
- package/ts-plugin/index.js +16 -5
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# PDAs
|
|
2
|
+
|
|
3
|
+
A program derived address (PDA) is an address derived from seeds and a program id, with no private key: only the
|
|
4
|
+
program can sign for it. This page covers seeds, bumps, creating accounts at PDAs, checking them, signing for them in
|
|
5
|
+
calls to other programs, and how the IDL records them. Part of the [language guide](language.md).
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { type Init, type Mut, type Pubkey, type Seeds, type Signer, type SystemAccount, type SystemProgram, type u16, type u64, type u8, findProgramAddressBump, programId, requirePda, signer } from "typesol";
|
|
9
|
+
|
|
10
|
+
export interface Vault {
|
|
11
|
+
owner: Pubkey;
|
|
12
|
+
bump: u8;
|
|
13
|
+
solBump: u8;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function open(owner: Mut<Signer>, vault: Init<Vault>, systemProgram: SystemProgram, index: u16): void {
|
|
17
|
+
vault.bump = vault.create(owner, ["vault", owner.key, index]);
|
|
18
|
+
vault.owner = owner.key;
|
|
19
|
+
vault.solBump = findProgramAddressBump(["sol", vault.key], programId());
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function pay(
|
|
23
|
+
owner: Signer,
|
|
24
|
+
vault: Seeds<Mut<Vault>, ["vault", "@owner", "@index"], "bump">,
|
|
25
|
+
sol: Mut<SystemAccount>,
|
|
26
|
+
to: Mut<SystemAccount>,
|
|
27
|
+
systemProgram: SystemProgram,
|
|
28
|
+
index: u16,
|
|
29
|
+
amount: u64,
|
|
30
|
+
): void {
|
|
31
|
+
requirePda(sol, ["sol", vault.key], vault.solBump);
|
|
32
|
+
const solSigner = signer(["sol", vault.key, vault.solBump]);
|
|
33
|
+
systemProgram.transfer(sol, to, amount, solSigner);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`open` creates the vault at `["vault", owner, index]` and stores both bumps, the second derived, not taken from the
|
|
38
|
+
arguments. `pay` moves lamports only for the vault's owner: the `Seeds` constraint derives the vault's address from
|
|
39
|
+
the signing `owner`, so another signer names another address and fails with 2006.
|
|
40
|
+
|
|
41
|
+
## Seeds
|
|
42
|
+
|
|
43
|
+
A seed list is an array literal written where the seeds go. Each seed contributes its bytes:
|
|
44
|
+
|
|
45
|
+
| Seed | Bytes |
|
|
46
|
+
| --- | --- |
|
|
47
|
+
| a string literal or string constant (`"vault"`) | its UTF-8 bytes |
|
|
48
|
+
| a `string` argument or `FixedString` field | its UTF-8 bytes |
|
|
49
|
+
| a `Pubkey` (`owner.key`, a field, `pubkey("…")`) | its 32 bytes |
|
|
50
|
+
| a `Hash` (`sha256(…)`) | its 32 bytes, as Marginfi seeds an order with the hash of its bank keys |
|
|
51
|
+
| an integer (an argument, a field, a local, a typed literal) | little-endian at its declared width: a `u16` gives 2 bytes; a bare `5n` is a u64, 8 bytes |
|
|
52
|
+
| a unit enum value (`Side.Ask`, an argument or a field) | 1 byte, its variant index |
|
|
53
|
+
| `bigEndian(x)` | an integer's bytes most significant first (`["tick_array", pool.key, bigEndian(start)]`), Rust's `to_be_bytes()` |
|
|
54
|
+
| the bump (a `u8`) | 1 byte, last |
|
|
55
|
+
|
|
56
|
+
A seed is at most 32 bytes (`a seed can be at most 32 bytes`, when its length is known at compile time). A `string`
|
|
57
|
+
argument longer than that is only known at run time: `create`, `createIfNeeded`, `requirePda` and `Seeds<…>` then fail
|
|
58
|
+
with 2006, as for any other address that is not the PDA; check `byteLength(s) <= 32n` first to fail with an error of
|
|
59
|
+
your own. Solana takes at
|
|
60
|
+
most 16 seeds per address, the bump included. A list the derivation adds the bump to (`create`, `requirePda`,
|
|
61
|
+
`findProgramAddress`) holds at most 15 entries (`at most 15 seeds: the bump the derivation adds makes 16, Solana's
|
|
62
|
+
limit`); a list that ends with its bump (signer seeds, `createProgramAddress`) holds 16: 15 seeds and the bump (`at
|
|
63
|
+
most 16 seeds, the bump included (Solana's limit)`).
|
|
64
|
+
|
|
65
|
+
## Creating an account at a PDA
|
|
66
|
+
|
|
67
|
+
`acct.create(payer, seeds)` on an `Init<T>` derives the PDA of this program for the seeds, finds the canonical bump,
|
|
68
|
+
checks that the account passed is that address (2006), creates it and returns the bump ([creating
|
|
69
|
+
accounts](language-accounts.md#creating-accounts)). Store the bump in the account (`vault.bump = …`) to check and sign
|
|
70
|
+
cheaply later.
|
|
71
|
+
|
|
72
|
+
- `acct.createIfNeeded(payer, seeds)` on an `InitIfNeeded<T>` does the same once, and afterwards re-derives the address
|
|
73
|
+
and checks it.
|
|
74
|
+
- A seed may be an Option integer argument with a default, `opt ?? 0n`; the IDL records `opt.unwrap_or(0)`, as Anchor's
|
|
75
|
+
does.
|
|
76
|
+
- `create(payer, null)` makes a keypair account instead of a PDA: the account signs the transaction.
|
|
77
|
+
|
|
78
|
+
### createPda
|
|
79
|
+
|
|
80
|
+
`systemProgram.createPda(payer, account, space, owner, seeds)` is Anchor's `init` with `seeds`, `space` and
|
|
81
|
+
`owner = …` on an `UncheckedAccount`: `account` is a `Mut<AccountInfo>` that must be this program's PDA for `seeds`
|
|
82
|
+
(2006). It is created owned by `owner`, or topped up, allocated and assigned when someone funded the address first. It
|
|
83
|
+
returns the bump, records the PDA in the IDL, and among the creates a body starts with runs before the constraints.
|
|
84
|
+
|
|
85
|
+
## Seeds constraints
|
|
86
|
+
|
|
87
|
+
`Seeds<A, ["vault", "@owner"], "bump">` checks before the body that the account is the PDA for those seeds (2006), as
|
|
88
|
+
Anchor's `seeds = […], bump`:
|
|
89
|
+
|
|
90
|
+
- `"@name"` is the key of the account parameter `name`, or the bytes of the argument `name`.
|
|
91
|
+
- `"@name:be"` is an integer argument's bytes most significant first (Rust's `to_be_bytes()`). The IDL records the
|
|
92
|
+
argument as Anchor's does, so clients that derive the address from the IDL get it wrong: pass such accounts
|
|
93
|
+
explicitly.
|
|
94
|
+
- `"@acct.field"` is a `Pubkey`, integer or unit enum field of a struct account parameter, also the account's own:
|
|
95
|
+
`["offer", "@maker", "@offer.id"]`.
|
|
96
|
+
- Other strings are literals.
|
|
97
|
+
- The third argument names the bump: a field of the account itself (`"bump"`, Anchor's `bump = vault.bump`), a field of
|
|
98
|
+
another struct account (`"@acct.field"`, Anchor's `bump = multisig.nonce`), or a field in a struct field of it
|
|
99
|
+
(`"@acct.field.inner"`, Anchor's `bump = config.bumps.vault`). Without it (`never`, or left out) the canonical bump
|
|
100
|
+
is searched for, which costs more.
|
|
101
|
+
- `acct.pdaBump` is the bump (Anchor's `ctx.bumps.acct`): the stored field when one is named, else the canonical bump
|
|
102
|
+
the check found. Use it to sign as the PDA.
|
|
103
|
+
- `Seeds<A, ["metadata", "@mint"], never, "@metadataProgram">`: the fourth argument derives the PDA under another
|
|
104
|
+
program (a program parameter or a base58 address; `pda.program` in the IDL).
|
|
105
|
+
|
|
106
|
+
Inside an [account group](language-accounts.md#account-groups), `"@name"` names the group's own fields first.
|
|
107
|
+
|
|
108
|
+
## Checking a PDA in the body
|
|
109
|
+
|
|
110
|
+
`requirePda(account, seeds, bump)` checks that `account` is the PDA of this program for `seeds` and `bump` (2006). Any
|
|
111
|
+
bump that gives a valid PDA passes, as with Anchor's `bump = expr`. Use a bump stored when the account was created (or
|
|
112
|
+
`Seeds<…>`): one taken from the instruction's arguments lets the caller pick among up to 256 addresses for the same
|
|
113
|
+
seeds.
|
|
114
|
+
|
|
115
|
+
## Deriving addresses in the body
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { type Mut, type Pubkey, type u64, type u8, bigEndian, findProgramAddress, findProgramAddressBump, findProgramAddressWithBump, programId } from "typesol";
|
|
119
|
+
|
|
120
|
+
export interface Book {
|
|
121
|
+
tick: Pubkey;
|
|
122
|
+
vault: Pubkey;
|
|
123
|
+
bump: u8;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function locate(book: Mut<Book>, owner: Pubkey, start: u64): void {
|
|
127
|
+
book.tick = findProgramAddress(["tick", owner, bigEndian(start)], programId());
|
|
128
|
+
const [vault, bump] = findProgramAddressWithBump(["vault", owner], programId());
|
|
129
|
+
book.vault = vault;
|
|
130
|
+
book.bump = bump;
|
|
131
|
+
book.bump = findProgramAddressBump(["vault", owner], programId());
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
- `findProgramAddress(seeds, program)` is the canonical PDA of any program (Rust's
|
|
136
|
+
`Pubkey::find_program_address(..).0`, e.g. a Bubblegum asset id), and `findProgramAddressBump(seeds, program)` its
|
|
137
|
+
bump. `const [address, bump] = findProgramAddressWithBump(seeds, program)` gives both from one search (Rust's
|
|
138
|
+
`let (address, bump) = …`). Each search costs about 450 CU per bump tried.
|
|
139
|
+
- `createProgramAddress(seeds, program)` is `Pubkey::create_program_address`: the address for seeds that include the
|
|
140
|
+
bump, or `null` when they give none.
|
|
141
|
+
- `programId()` is this program's address.
|
|
142
|
+
|
|
143
|
+
## Signing as a PDA
|
|
144
|
+
|
|
145
|
+
A PDA of this program signs a call to another program when the call is given its seeds, with the bump:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { type Account, type HasOne, type Mut, type Pubkey, type Seeds, type Signer, type SystemAccount, type SystemProgram, type u64, type u8, signer } from "typesol";
|
|
149
|
+
|
|
150
|
+
export interface Config {
|
|
151
|
+
admin: Pubkey;
|
|
152
|
+
bump: u8;
|
|
153
|
+
treasuryBump: u8;
|
|
154
|
+
feesBump: u8;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function payOut(admin: Signer, config: HasOne<Seeds<Account<Config>, ["config"], "bump">, "admin">, treasury: Mut<SystemAccount>, fees: Mut<SystemAccount>, to: Mut<SystemAccount>, systemProgram: SystemProgram, amount: u64): void {
|
|
158
|
+
systemProgram.transfer(treasury, to, amount, ["treasury", admin.key, config.treasuryBump]);
|
|
159
|
+
const feeSigner = signer(["fees", config.feesBump]);
|
|
160
|
+
systemProgram.transfer(fees, to, 1n, feeSigner);
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Signer seeds are authority: whoever reaches the call spends what the PDA holds. Here `HasOne` lets only the config's
|
|
165
|
+
`admin` call `payOut` (2001 for anyone else), and the bumps come from the config, where they were stored when the
|
|
166
|
+
PDAs were created. The config is itself a PDA, `["config"]`, so the program has exactly one: a `HasOne` on any
|
|
167
|
+
`Account<Config>` would accept a config anyone created naming themselves admin, and drain `fees`. Whoever creates that
|
|
168
|
+
one config is the admin, so gate its creation, for example on the upgrade authority
|
|
169
|
+
([ProgramData](language-accounts.md#programdata)). Seeds that include the caller, as `["treasury", admin.key, …]`
|
|
170
|
+
does, also limit a call to the caller's own PDA: another signer's seeds give another address, which then does not
|
|
171
|
+
sign.
|
|
172
|
+
|
|
173
|
+
- Every CPI takes optional signer seeds as its last argument: a seed list written there with the bump
|
|
174
|
+
(`["treasury", admin.key, bump]`), or a named one. When several PDAs sign one `invoke`, pass a list of seed lists,
|
|
175
|
+
`[["vault", owner.key, vaultBump], ["fees", feesBump]]` (at most 16).
|
|
176
|
+
- A seed list used more than once can be named: `const vault = signer(["vault", owner.key, bump])`, then pass `vault`
|
|
177
|
+
wherever signer seeds go. One of two lists can be picked at run time:
|
|
178
|
+
`const pool = indexed ? signer([…, index, bump]) : signer([…, bump])`. `signer([…])` is written in a declaration or
|
|
179
|
+
as a helper's argument; inside a CPI's own argument list it is refused (`seeds must be an array literal`): write the
|
|
180
|
+
array there instead.
|
|
181
|
+
- **When seeds are read.** A named list, and one given to a helper, holds its seeds as they were when it was written
|
|
182
|
+
(JavaScript evaluates the array then): a field or local changed afterwards does not change what it signs for. A list
|
|
183
|
+
written in a call (a helper's `signer([…])` argument, seeds written in a CPI, `create(payer, [...], space)`) holds
|
|
184
|
+
them as they were when the list was read, even when a later argument changes a seed.
|
|
185
|
+
- Helpers take signer seeds as a `SignerSeeds` parameter, called with `signer([…])` (or `signer([])` when nothing
|
|
186
|
+
signs).
|
|
187
|
+
- The compiler refuses a signer flag in `invoke` on an account that neither signed nor is covered by signer seeds
|
|
188
|
+
([calling other programs](language-cpi.md#accounts)).
|
|
189
|
+
- A typed call from an IDL takes `signer([...seeds, bump])` when a PDA of your program signs, or `signer([])`.
|
|
190
|
+
|
|
191
|
+
## PDAs in the IDL
|
|
192
|
+
|
|
193
|
+
When an instruction calls `x.create(payer, seeds)` or `requirePda(x, seeds, bump)`, or `x` has a `Seeds<…>`
|
|
194
|
+
constraint, and every seed is a string literal, an integer literal, a constant, an argument, `acct.key` or a scalar
|
|
195
|
+
field of an account, the IDL entry for `x` gets Anchor's `pda.seeds` (`const` / `arg` / `account`), so Anchor and
|
|
196
|
+
Codama clients derive the address themselves. An associated token account (`Ata<…>`) gets the ATA program's `pda`.
|
|
197
|
+
|
|
198
|
+
**Next:** [Errors, logs, events and return values](language-errors-events.md).
|