From 7266486068f35619b1167e06fed0bc49862bb144 Mon Sep 17 00:00:00 2001 From: Jon Cinque Date: Wed, 27 Sep 2023 19:10:41 +0200 Subject: [PATCH] macro: Remove dependency on solana-program (#5375) * macro: Remove dependency on solana-program * Bump version and fix clippy --- Cargo.lock | 8 ++++---- libraries/discriminator/derive/Cargo.toml | 4 ++-- libraries/discriminator/syn/Cargo.toml | 6 +++--- libraries/discriminator/syn/src/lib.rs | 4 ++-- libraries/program-error/derive/Cargo.toml | 2 +- libraries/program-error/derive/src/macro_impl.rs | 6 ++++-- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13f014f60a0..947eeacf977 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6471,7 +6471,7 @@ dependencies = [ [[package]] name = "spl-discriminator-derive" -version = "0.1.0" +version = "0.1.1" dependencies = [ "quote 1.0.29", "spl-discriminator-syn", @@ -6480,11 +6480,11 @@ dependencies = [ [[package]] name = "spl-discriminator-syn" -version = "0.1.0" +version = "0.1.1" dependencies = [ "proc-macro2 1.0.63", "quote 1.0.29", - "solana-program", + "sha2 0.10.7", "syn 2.0.28", "thiserror", ] @@ -6799,7 +6799,7 @@ version = "0.3.0" dependencies = [ "proc-macro2 1.0.63", "quote 1.0.29", - "solana-program", + "sha2 0.10.7", "syn 2.0.28", ] diff --git a/libraries/discriminator/derive/Cargo.toml b/libraries/discriminator/derive/Cargo.toml index bfdc1680655..ef6c62d53f7 100644 --- a/libraries/discriminator/derive/Cargo.toml +++ b/libraries/discriminator/derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "spl-discriminator-derive" -version = "0.1.0" +version = "0.1.1" description = "Derive macro library for the `spl-discriminator` library" authors = ["Solana Labs Maintainers "] repository = "https://github.com/solana-labs/solana-program-library" @@ -16,4 +16,4 @@ syn = { version = "2.0", features = ["full"] } proc-macro = true [package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] \ No newline at end of file +targets = ["x86_64-unknown-linux-gnu"] diff --git a/libraries/discriminator/syn/Cargo.toml b/libraries/discriminator/syn/Cargo.toml index e1222ced2c0..f51f1c2139e 100644 --- a/libraries/discriminator/syn/Cargo.toml +++ b/libraries/discriminator/syn/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "spl-discriminator-syn" -version = "0.1.0" +version = "0.1.1" description = "Token parsing and generating library for the `spl-discriminator` library" authors = ["Solana Labs Maintainers "] repository = "https://github.com/solana-labs/solana-program-library" @@ -10,7 +10,7 @@ edition = "2021" [dependencies] proc-macro2 = "1.0" quote = "1.0" -solana-program = "1.16.13" +sha2 = "0.10" syn = { version = "2.0", features = ["full"] } thiserror = "1.0" @@ -18,4 +18,4 @@ thiserror = "1.0" crate-type = ["cdylib", "lib"] [package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] \ No newline at end of file +targets = ["x86_64-unknown-linux-gnu"] diff --git a/libraries/discriminator/syn/src/lib.rs b/libraries/discriminator/syn/src/lib.rs index e8810beb911..db555916573 100644 --- a/libraries/discriminator/syn/src/lib.rs +++ b/libraries/discriminator/syn/src/lib.rs @@ -10,7 +10,7 @@ use { crate::{error::SplDiscriminateError, parser::parse_hash_input}, proc_macro2::{Span, TokenStream}, quote::{quote, ToTokens}, - solana_program::hash, + sha2::{Digest, Sha256}, syn::{parse::Parse, Generics, Ident, Item, ItemEnum, ItemStruct, LitByteStr, WhereClause}, }; @@ -102,7 +102,7 @@ impl From<&SplDiscriminateBuilder> for TokenStream { /// Returns the bytes for the TLV hash_input discriminator fn get_discriminator_bytes(hash_input: &str) -> LitByteStr { LitByteStr::new( - &hash::hashv(&[hash_input.as_bytes()]).to_bytes()[..8], + &Sha256::digest(hash_input.as_bytes())[..8], Span::call_site(), ) } diff --git a/libraries/program-error/derive/Cargo.toml b/libraries/program-error/derive/Cargo.toml index d44e74874a4..9833aa1fa80 100644 --- a/libraries/program-error/derive/Cargo.toml +++ b/libraries/program-error/derive/Cargo.toml @@ -13,5 +13,5 @@ proc-macro = true [dependencies] proc-macro2 = "1.0" quote = "1.0" -solana-program = "1.16.13" +sha2 = "0.10" syn = { version = "2.0", features = ["full"] } diff --git a/libraries/program-error/derive/src/macro_impl.rs b/libraries/program-error/derive/src/macro_impl.rs index 114aa699af7..f323e2ba786 100644 --- a/libraries/program-error/derive/src/macro_impl.rs +++ b/libraries/program-error/derive/src/macro_impl.rs @@ -4,6 +4,7 @@ use { crate::parser::SplProgramErrorArgs, proc_macro2::Span, quote::quote, + sha2::{Digest, Sha256}, syn::{ punctuated::Punctuated, token::Comma, Expr, ExprLit, Ident, ItemEnum, Lit, LitInt, LitStr, Token, Variant, @@ -184,9 +185,10 @@ fn u32_from_hash(enum_ident: &Ident) -> u32 { // `SPL_ERROR_HASH_MIN_VALUE`! let mut nonce: u32 = 0; loop { - let hash = solana_program::hash::hashv(&[hash_input.as_bytes(), &nonce.to_le_bytes()]); + let mut hasher = Sha256::new_with_prefix(hash_input.as_bytes()); + hasher.update(nonce.to_le_bytes()); let d = u32::from_le_bytes( - hash.to_bytes()[13..17] + hasher.finalize()[13..17] .try_into() .expect("Unable to convert hash to u32"), );