Skip to content

Commit

Permalink
RustSDK in Solana
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz2891 committed Sep 30, 2024
1 parent a5010d5 commit 2ca03f3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
21 changes: 12 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ default = ["pure"]
pure = ["primitive-types"]

# An extension for Casper network
network_casper = ["casper-contract/wee_alloc", "casper-types"]
network_casper = []

# An extension for Radix network
network_radix = ["radix-common", "scrypto"]
network_radix = []

# An extension for debug-printing of messages in the Casper extension. Not supported by Casper Contracts deployed to the network.
casper_debug = ["print_debug", "casper-contract/test-support"]
casper_debug = ["print_debug"]

# An extension for debug-printing of messages.
print_debug = []
Expand All @@ -27,21 +27,24 @@ print_debug = []
crypto_secp256k1 = ["secp256k1/recovery", "secp256k1/lowmemory", "secp256k1/alloc"]

# A variant of decrypting the message-signers using k256 library. Cheaper during contract deployment.
crypto_k256 = ["k256/alloc", "k256/sha256", "k256/ecdsa"]
crypto_k256 = []

crypto_solana = ["anchor-lang"]

# A set of helpers for testing & offline usage.
helpers = ["hex/serde", "hex/alloc"]

[dependencies]
casper-contract = { version = "^4.0.0", default-features = false, features = [], optional = true }
casper-types = { version = "^4.0.1", default-features = false, features = [], optional = true }
radix-common = { version = "^1.2.0", default-features = false, features = [], optional = true }
scrypto = { version = "^1.2.0", optional = true }
#casper-contract = { version = "^4.0.0", default-features = false, features = [], optional = true }
#casper-types = { version = "^4.0.1", default-features = false, features = [], optional = true }
#radix-common = { version = "^1.2.0", default-features = false, features = [], optional = true }
#scrypto = { version = "^1.2.0", optional = true }
sha3 = { version = "^0.10.8", default-features = false, features = ["asm"] }
k256 = { version = "^0.13.3", default-features = false, features = [], optional = true }
#k256 = { version = "^0.13.3", default-features = false, features = [], optional = true }
secp256k1 = { version = "^0.29.0", default-features = false, features = [], optional = true }
hex = { version = "^0.4.3", default-features = false, features = [], optional = true }
primitive-types = { version = "^0.13.1", optional = true }
anchor-lang = { version = "^0.30.1", optional = true, default-features = false }

[dev-dependencies]
itertools = { version = "^0.13.0" }
9 changes: 7 additions & 2 deletions src/crypto/keccak256.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use sha3::{Digest, Keccak256};

#[cfg(not(feature = "crypto_solana"))]
pub fn keccak256(data: &[u8]) -> Box<[u8]> {
Keccak256::new_with_prefix(data)
sha3::Keccak256::new_with_prefix(data)
.finalize()
.as_slice()
.into()
}

#[cfg(feature = "crypto_solana")]
pub fn keccak256(data: &[u8]) -> Box<[u8]> {
anchor_lang::solana_program::keccak::hash(data).to_bytes().into()
}

#[cfg(feature = "helpers")]
#[cfg(test)]
mod tests {
Expand Down
18 changes: 17 additions & 1 deletion src/crypto/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,23 @@ pub(crate) mod crypto256 {
}
}

#[cfg(all(not(feature = "crypto_k256"), not(feature = "crypto_secp256k1")))]
#[cfg(feature = "crypto_solana")]
pub(crate) mod crypto256 {
use anchor_lang::solana_program::secp256k1_recover::secp256k1_recover;
// use crate::network::{assert::Unwrap, error::Error};

pub(crate) fn recover_public_key(
message_hash: Box<[u8]>,
signature_bytes: &[u8],
recovery_byte: u8,
) -> Box<[u8]> {
secp256k1_recover(&message_hash, recovery_byte, &signature_bytes).unwrap().
// unwrap_or_revert(|_| Error::CryptographicError(signature_bytes.len()))
to_bytes().into()
}
}

#[cfg(all(not(feature = "crypto_k256"), not(feature = "crypto_secp256k1"), not(feature = "crypto_solana")))]
pub(crate) mod crypto256 {
pub(crate) fn recover_public_key(
_message_hash: Box<[u8]>,
Expand Down

0 comments on commit 2ca03f3

Please sign in to comment.