Skip to content

Commit

Permalink
Extract offchain-message crate (#3443)
Browse files Browse the repository at this point in the history
* extract offchain-message crate

* missing feature activation

* update lock file

* remove accidental change

* update deprecation

* remove another accidental change

* add missing metadata

Co-authored-by: Jon C <[email protected]>

* add doc_auto_cfg

Co-authored-by: Jon C <[email protected]>

---------

Co-authored-by: Jon C <[email protected]>
  • Loading branch information
kevinheavey and joncinque authored Dec 9, 2024
1 parent 1c2dc20 commit de77e17
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 19 deletions.
19 changes: 18 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ members = [
"sdk/msg",
"sdk/native-token",
"sdk/nonce",
"sdk/offchain-message",
"sdk/package-metadata",
"sdk/package-metadata-macro",
"sdk/packet",
Expand Down Expand Up @@ -509,6 +510,7 @@ solana-net-utils = { path = "net-utils", version = "=2.2.0" }
solana-nohash-hasher = "0.2.1"
solana-nonce = { path = "sdk/nonce", version = "=2.2.0" }
solana-notifier = { path = "notifier", version = "=2.2.0" }
solana-offchain-message = { path = "sdk/offchain-message", version = "=2.2.0" }
solana-package-metadata = { path = "sdk/package-metadata", version = "=2.2.0" }
solana-package-metadata-macro = { path = "sdk/package-metadata-macro", version = "=2.2.0" }
solana-packet = { path = "sdk/packet", version = "=2.2.0" }
Expand Down
16 changes: 15 additions & 1 deletion programs/sbf/Cargo.lock

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

3 changes: 2 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ full = [
"dep:solana-ed25519-program",
"dep:solana-compute-budget-interface",
"dep:solana-keypair",
"dep:solana-offchain-message",
"dep:solana-precompile-error",
"dep:solana-precompiles",
"dep:solana-presigner",
Expand Down Expand Up @@ -104,7 +105,6 @@ log = { workspace = true }
memmap2 = { workspace = true, optional = true }
num-derive = { workspace = true }
num-traits = { workspace = true }
num_enum = { workspace = true }
qualifier_attr = { workspace = true, optional = true }
rand = { workspace = true, optional = true }
rand0-7 = { workspace = true, optional = true }
Expand Down Expand Up @@ -146,6 +146,7 @@ solana-keypair = { workspace = true, optional = true, features = [
"seed-derivable",
] }
solana-native-token = { workspace = true }
solana-offchain-message = { workspace = true, optional = true, features = ["verify"] }
solana-packet = { workspace = true, features = ["bincode", "serde"] }
solana-poh-config = { workspace = true, features = ["serde"] }
solana-precompile-error = { workspace = true, optional = true }
Expand Down
34 changes: 34 additions & 0 deletions sdk/offchain-message/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "solana-offchain-message"
description = "Solana offchain message signing"
documentation = "https://docs.rs/solana-offchain-message"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
num_enum = { workspace = true }
solana-hash = { workspace = true }
solana-packet = { workspace = true }
solana-pubkey = { workspace = true, optional = true }
solana-sanitize = { workspace = true }
solana-sha256-hasher = { workspace = true }
solana-signature = { workspace = true }
solana-signer = { workspace = true }

[dev-dependencies]
solana-keypair = { workspace = true }
solana-offchain-message = { path = ".", features = ["dev-context-only-utils"] }
static_assertions = { workspace = true }

[features]
dev-context-only-utils = ["verify"]
verify = ["dep:solana-pubkey", "solana-signature/verify"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
all-features = true
rustdoc-args = ["--cfg=docsrs"]
28 changes: 14 additions & 14 deletions sdk/src/offchain_message.rs → sdk/offchain-message/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
//! Off-chain message container for storing non-transaction messages.
#![cfg(feature = "full")]

#![cfg_attr(docsrs, feature(doc_auto_cfg))]
use {
crate::{
hash::Hash,
pubkey::Pubkey,
signature::{Signature, Signer},
},
num_enum::{IntoPrimitive, TryFromPrimitive},
solana_hash::Hash,
solana_sanitize::SanitizeError,
solana_signature::Signature,
solana_signer::Signer,
};

#[cfg(test)]
Expand Down Expand Up @@ -46,11 +42,10 @@ pub enum MessageFormat {
pub mod v0 {
use {
super::{is_printable_ascii, is_utf8, MessageFormat, OffchainMessage as Base},
crate::{
hash::{Hash, Hasher},
packet::PACKET_DATA_SIZE,
},
solana_hash::Hash,
solana_packet::PACKET_DATA_SIZE,
solana_sanitize::SanitizeError,
solana_sha256_hasher::Hasher,
};

/// OffchainMessage Version 0.
Expand Down Expand Up @@ -239,15 +234,20 @@ impl OffchainMessage {
Ok(signer.sign_message(&self.serialize()?))
}

#[cfg(feature = "verify")]
/// Verify that the message signature is valid for the given public key
pub fn verify(&self, signer: &Pubkey, signature: &Signature) -> Result<bool, SanitizeError> {
pub fn verify(
&self,
signer: &solana_pubkey::Pubkey,
signature: &Signature,
) -> Result<bool, SanitizeError> {
Ok(signature.verify(signer.as_ref(), &self.serialize()?))
}
}

#[cfg(test)]
mod tests {
use {super::*, crate::signature::Keypair, std::str::FromStr};
use {super::*, solana_keypair::Keypair, std::str::FromStr};

#[test]
fn test_offchain_message_ascii() {
Expand Down
4 changes: 3 additions & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ pub mod log;
pub mod native_loader;
pub mod net;
pub mod nonce_account;
pub mod offchain_message;
pub mod precompiles;
pub mod program_utils;
pub mod pubkey;
Expand Down Expand Up @@ -130,6 +129,9 @@ pub use solana_feature_set as feature_set;
pub use solana_fee_structure as fee;
#[deprecated(since = "2.1.0", note = "Use `solana-inflation` crate instead")]
pub use solana_inflation as inflation;
#[cfg(feature = "full")]
#[deprecated(since = "2.2.0", note = "Use `solana-offchain-message` crate instead")]
pub use solana_offchain_message as offchain_message;
#[deprecated(since = "2.1.0", note = "Use `solana-packet` crate instead")]
pub use solana_packet as packet;
#[deprecated(since = "2.2.0", note = "Use `solana-poh-config` crate instead")]
Expand Down
16 changes: 15 additions & 1 deletion svm/examples/Cargo.lock

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

0 comments on commit de77e17

Please sign in to comment.