-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Automatic client-side derivation of PDAs #1004
Comments
Sounds like we need to have a way to declaratively specify PDA seeds that are a function of either constants, or other accounts in a given context. This has come up a several times so there's definitely demand for this feature. Any suggestions for what this API would look like? |
Forgive me for getting in, but I also really want this feature and am ready to help with the implementation 😅
let data = my_program::instruction::Method {
...
};
let acc = my_program:accounts::MethodAccountsBuilder::default()
.instruction(&data)
.account_one(pubkey)
.build()?;
// Or
let acc = my_program:accounts::MethodAccounts {
from_instruction: &data,
account_one: pubkey
};
// And we do final transformation at the `to_account_metas` level.
// The `Builder` option is a little cleaner, but the completeness
// check happens at runtime, which is always frustrating.
{
"name": "pda_account",
"isMut": true,
"isSigner": false,
"seeds": [
{
"type": "const",
"name": "MYSEED",
"_comment": "The only thing that needs to be clarified is that there are bytes and string constants that are converted to these bytes."
},
{
"type": "string",
"name": "STRING_FOR_UTF8_ENCODE"
},
{
"type": "account",
"name": "source_of_seed",
"_comment": "Here we take the key from this account"
},
{
"type": "arg",
"name": "source_of_seed",
"_comment": "And here we take the argument directly, but limit the possible types for this feature"
}
]
} |
looks good, probably adding a few more types will help:
|
|
a) on-chain it's probably ok and all easy as the account anyways was loaded by the run-time
|
The first version of this feature is gated by a CLI feature flag in the Anchor.toml. [features]
seeds = true When this is enabled, seeds will be parsed into the IDL and automatically used to generate PDAs in the new typescript builder API. |
Right now the IDL does not allow to communicate PDA derivation paths. Hence an anchor user needs to replicate the derivation logic both on the client as well as on the program side. It's a common source of errors for sure. Here's an example of such a derivation helper in one of the anchor programs I recently wrote.
https://github.com/mschneider/assembly/blob/2a820887f7efd9033a88fe19117d41895bdfe660/lib/index.ts#L290
The text was updated successfully, but these errors were encountered: