Skip to content

Commit

Permalink
Static pubkey example.
Browse files Browse the repository at this point in the history
  • Loading branch information
cqfd committed Sep 27, 2021
1 parent 0de09e7 commit 96036e1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/tutorial/basic-0/programs/basic-0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ cpi = ["no-entrypoint"]

[dependencies]
anchor-lang = { path = "../../../../../lang" }
pubkey = { path = "./src/pubkey" }
3 changes: 3 additions & 0 deletions examples/tutorial/basic-0/programs/basic-0/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use anchor_lang::prelude::*;
use pubkey;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
mod basic_0 {

use super::*;
pub fn initialize(_ctx: Context<Initialize>) -> ProgramResult {
let hmm = pubkey::pubkey!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
Ok(())
}
}
Expand Down
18 changes: 18 additions & 0 deletions examples/tutorial/basic-0/programs/basic-0/src/pubkey/Cargo.toml
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 examples/tutorial/basic-0/programs/basic-0/src/pubkey/src/lib.rs
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()
}
3 changes: 2 additions & 1 deletion lang/attribute/account/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
extern crate proc_macro;

use proc_macro2::Span;
use quote::quote;
use syn::parse_macro_input;
use syn::{parse_macro_input, LitByte, LitStr};

mod id;

Expand Down

0 comments on commit 96036e1

Please sign in to comment.