diff --git a/README.md b/README.md index 3b6366c6..75ef4625 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@
-### Our development resources, aiming to onboarding engineers to Solana development. From geeks to geeks. +### Our development resources aim to onboard engineers to Solana development. From geeks to geeks.
diff --git a/chapters/04_program_derived_addresses.md b/chapters/04_program_derived_addresses.md index 91a837e9..e887a2ec 100644 --- a/chapters/04_program_derived_addresses.md +++ b/chapters/04_program_derived_addresses.md @@ -2,3 +2,107 @@
+ +### tl; dr + +
+ + +* PDAs are addresses with special properties. They are not public keys (so they don't have an associated public key). + +* PDAs provide a mechanism to build hashmap-like structures on-chain, allowing programs to sign instructions. + +* PDAs simplify the programming model and make programs more secure. + + +
+ +---- + +### PDA Creation + +
+ +* PDA are created by hashing a number of seeds the user can choose with the `program_id`. + +* Seeds can be anything: pubkey, strings, an array of numbers, etc. + +* There is a 50% chance that this hash can result in a public key, so a bump has to be searched: + +
+ + +```rust +// pseudo code +fn find_pda(seeds, program_id) { + for bump in 0..256 { + let potential_pda = hash(seeds, bump, program_id); + if is_pubkey(potential_pda) { + continue; + } + return (potential_pda, bump); + } + panic!("Could not find pda after 256 tries."); +} +``` + +
+ +* The first bump that results in a PDA is called a "canonical bump," and they are the recommended one for usage. + +
+ +--- + +### Hashmap-like Structures with PDAs + +
+ +* PDAs are hashed from a bump, a program_id, and several seeds. These seeds can be used to build hashmap-like structures on-chain. + +* With PDA, you can create structs that encode the information about a relationship between the user and some data account, so that PDA serves as the address: + +
+ +```rust +pub struct UserStats { + level: u16, + name: String, + bump: u8 +} +``` + +
+ + +---- + +### CPI Signing with PDAs + +
+ + +* In some cases, it's possible to reduce the number of accounts needed by making a PDA storing state also sign a CPI instead of defining a separate PDA for that. + +* This means that programs can be given control over assets, which they then manage according to the rules defined in the code. + +
+ +--- + +### Demos + +
+ +* Learn how PDA works on Anchor through [demo 3](https://github.com/urani-labs/solana-dev-onboarding-rs/tree/main/demos/03_anchor_pda). + +
+ +--- + +### References + +
+ +* [Anchor Docs on PDA](https://www.anchor-lang.com/docs/pdas) +