Skip to content

Commit

Permalink
Feat/improve pda derivation (#364)
Browse files Browse the repository at this point in the history
* chore: Release metaboss version 0.43.1

* allow parsing numbers as seeds

* fix clippy lints

* remove debug print statement
  • Loading branch information
samuelvanderwaal authored Feb 1, 2025
1 parent 92c7e7f commit 5a6a207
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/collections/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub async fn check_collection_items(
Ok(())
}

async fn get_mint_collection<'a>(
async fn get_mint_collection(
client: &AsyncRpcClient,
mint: String,
) -> AnyResult<(String, Option<MdCollection>)> {
Expand Down
20 changes: 18 additions & 2 deletions src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub fn get_token_account_pda(mint: String, owner: Option<String>, token_22: bool
pub fn get_generic_pda(str_seeds: String, program_id: String) {
let seeds: Vec<Vec<u8>> = str_seeds
.split(',')
.map(|s| s.into())
.map(pubkey_or_bytes)
.map(|s| s.trim())
.flat_map(parse_seed)
.collect();

let seeds: Vec<&[u8]> = seeds.iter().map(|seed| seed.as_slice()).collect();
Expand All @@ -43,6 +43,22 @@ pub fn get_generic_pda(str_seeds: String, program_id: String) {
println!("{}", derive_generic_pda(seeds, program_id));
}

fn parse_seed(s: &str) -> Vec<Vec<u8>> {
if s.starts_with('"') && s.ends_with('"') {
// Handle quoted strings
vec![s[1..s.len() - 1].as_bytes().to_vec()]
} else if s.chars().all(|c| c.is_ascii_digit()) {
// Handle numbers: each digit becomes a separate u8
s.chars()
.map(|c| c.to_digit(10).unwrap() as u8)
.map(|digit| vec![digit])
.collect()
} else {
// Default to pubkey_or_bytes for other cases
vec![pubkey_or_bytes(s.to_string())]
}
}

fn pubkey_or_bytes(seed: String) -> Vec<u8> {
let res = Pubkey::from_str(&seed);
let value: Vec<u8> = match res {
Expand Down
2 changes: 1 addition & 1 deletion src/snapshot/print_editions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct SnapshotPrintEditionsArgs {
pub output: String,
}

pub async fn snapshot_print_editions<'a>(args: SnapshotPrintEditionsArgs) -> Result<()> {
pub async fn snapshot_print_editions(args: SnapshotPrintEditionsArgs) -> Result<()> {
let master_mint_pubkey = Pubkey::from_str(&args.master_mint)?;
let master_edition_pubkey = derive_edition_pda(&master_mint_pubkey);

Expand Down

0 comments on commit 5a6a207

Please sign in to comment.