-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
examples/tutorial/basic-0/programs/basic-0/src/pubkey/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "pubkey" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0" | ||
quote = "1.0" | ||
syn = { version = "1.0.60", features = ["full"] } | ||
anchor-syn = { path = "../../../../../../../lang/syn", version = "0.16.1", features = [ | ||
"hash", | ||
] } | ||
bs58 = "0.4.0" |
28 changes: 28 additions & 0 deletions
28
examples/tutorial/basic-0/programs/basic-0/src/pubkey/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use proc_macro2::Span; | ||
use quote::quote; | ||
use syn::{parse_macro_input, LitByte, LitStr}; | ||
|
||
#[proc_macro] | ||
pub fn pubkey(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
use std::convert::TryFrom; | ||
let id_literal = parse_macro_input!(input as LitStr); | ||
let id_vec = bs58::decode(id_literal.value()) | ||
.into_vec() | ||
.map_err(|_| syn::Error::new_spanned(&id_literal, "failed to decode base58 string")) | ||
.unwrap(); | ||
let id_array = <[u8; 32]>::try_from(<&[u8]>::clone(&&id_vec[..])) | ||
.map_err(|_| { | ||
syn::Error::new_spanned( | ||
&id_literal, | ||
format!("pubkey array is not 32 bytes long: len={}", id_vec.len()), | ||
) | ||
}) | ||
.unwrap(); | ||
let bytes = id_array.iter().map(|b| LitByte::new(*b, Span::call_site())); | ||
let output = quote! { | ||
::anchor_lang::solana_program::pubkey::Pubkey::new_from_array( | ||
[#(#bytes,)*] | ||
) | ||
}; | ||
output.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters