Skip to content

Commit

Permalink
macro: Remove dependency on solana-program (#5375)
Browse files Browse the repository at this point in the history
* macro: Remove dependency on solana-program

* Bump version and fix clippy
  • Loading branch information
joncinque authored Sep 27, 2023
1 parent 51a6fdf commit 7266486
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions libraries/discriminator/derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
repository = "https://github.com/solana-labs/solana-program-library"
Expand All @@ -16,4 +16,4 @@ syn = { version = "2.0", features = ["full"] }
proc-macro = true

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
targets = ["x86_64-unknown-linux-gnu"]
6 changes: 3 additions & 3 deletions libraries/discriminator/syn/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
repository = "https://github.com/solana-labs/solana-program-library"
Expand All @@ -10,12 +10,12 @@ 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"

[lib]
crate-type = ["cdylib", "lib"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
targets = ["x86_64-unknown-linux-gnu"]
4 changes: 2 additions & 2 deletions libraries/discriminator/syn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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(),
)
}
2 changes: 1 addition & 1 deletion libraries/program-error/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
6 changes: 4 additions & 2 deletions libraries/program-error/derive/src/macro_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
);
Expand Down

0 comments on commit 7266486

Please sign in to comment.