From dbc4694adb59d26ff64e1c600464f2b9180a35df Mon Sep 17 00:00:00 2001 From: billythedummy Date: Tue, 12 Dec 2023 13:11:25 +0800 Subject: [PATCH] sike this is real release 0.5.0 --- CHANGELOG.md | 1 + Cargo.lock | 2 +- Cargo.toml | 1 + solores/Cargo.toml | 2 +- solores/src/write_src.rs | 35 ++++++++++++++++++++++++----------- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4118456..eede17a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - No longer generates `*IxArgs` struct if no instruction args. - No longer generates `*Accounts` `*Keys` structs and `*_verify_account_keys()` function if instruction has no account inputs. - All reference types for `*Keys` and `*Accounts` have been changed to pass by value since they impl `Copy` +- Replaced `solana-program` dependency with `bs58` ## [0.4.0] - 2023-08-07 diff --git a/Cargo.lock b/Cargo.lock index e711c0f..bad0f00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4304,6 +4304,7 @@ dependencies = [ name = "solores" version = "0.5.0" dependencies = [ + "bs58", "clap 4.0.29", "env_logger 0.10.0", "heck", @@ -4316,7 +4317,6 @@ dependencies = [ "serde", "serde_json", "sha2 0.10.6", - "solana-program", "syn 1.0.105", "test_utils", "toml 0.7.6", diff --git a/Cargo.toml b/Cargo.toml index 0006174..aee3d50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ members = [ [workspace.dependencies] borsh = "^0.10" +bs58 = ">=0.1" bytemuck = "^1" lazy_static = "^1" num-derive = "^0.3" diff --git a/solores/Cargo.toml b/solores/Cargo.toml index ebc3221..2113605 100644 --- a/solores/Cargo.toml +++ b/solores/Cargo.toml @@ -35,6 +35,7 @@ name = "solores" name = "solores" [dependencies] +bs58 = { workspace = true } clap = { version = "^4.0.29", features = ["derive"] } env_logger = "^0.10.0" heck = "^0.4.0" @@ -47,7 +48,6 @@ quote = "^1.0" serde = { version = "^1.0", features = ["derive"] } serde_json = "^1" sha2 = "^0.10" -solana-program = { workspace = true } syn = { version = "^1.0", features = ["full"] } toml = "^0.7.6" void = "^1.0" diff --git a/solores/src/write_src.rs b/solores/src/write_src.rs index 19a6903..c51f4e2 100644 --- a/solores/src/write_src.rs +++ b/solores/src/write_src.rs @@ -1,22 +1,35 @@ use proc_macro2::{Ident, Span, TokenStream}; use quote::quote; -use solana_program::pubkey::Pubkey; -use std::{io::Write, path::Path, str::FromStr}; +use std::{io::Write, path::Path}; use crate::{idl_format::IdlFormat, utils::open_file_create_overwrite, Args}; const DEFAULT_PROGRAM_ID_STR: &str = "TH1S1SNoTAVAL1DPUBKEYDoNoTUSE11111111111111"; +const MAX_BASE58_LEN: usize = 44; +const PUBKEY_BYTES_SIZE: usize = 32; + +/// Copied from solana_program::Pubkey::from_str() +/// so that we dont have to have solana_program as a dep +fn is_valid_pubkey(s: &str) -> bool { + if s.len() > MAX_BASE58_LEN { + return false; + } + let pubkey_vec = match bs58::decode(s).into_vec() { + Ok(v) => v, + Err(_) => return false, + }; + if pubkey_vec.len() != PUBKEY_BYTES_SIZE { + return false; + } + true +} + pub fn write_lib(args: &Args, idl: &dyn IdlFormat) -> std::io::Result<()> { - let user_provided_id_opt = args.program_id.as_ref().and_then(|s| { - Pubkey::from_str(s) - .map_err(|e| { - log::warn!("provided pubkey {s} invalid. Error: {e}"); - e - }) - .ok() - .map(|_| s) - }); + let user_provided_id_opt = + args.program_id + .as_ref() + .and_then(|s| if is_valid_pubkey(s) { Some(s) } else { None }); let id = user_provided_id_opt .map(|string| string.as_ref()) .unwrap_or_else(|| {