Skip to content

Commit

Permalink
RustSDK in Solana
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz2891 committed Dec 2, 2024
1 parent 9fa7cd7 commit 180b54f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
24 changes: 14 additions & 10 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,24 +27,28 @@ 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 = []

# A variant of decrypting the message-signers using Solana library.
crypto_solana = ["anchor-lang"]

# A variant of decrypting the message-signers using Radix library.
crypto_radix = ["scrypto", "radix-common"]
crypto_radix = []

# 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.2", default-features = false, features = [], optional = true }
radix-common = { version = "^1.3.0", default-features = false, features = [], optional = true }
scrypto = { version = "^1.3.0", optional = true }
#casper-contract = { version = "^4.0.0", default-features = false, features = [], optional = true }
#casper-types = { version = "^4.0.2", default-features = false, features = [], optional = true }
#radix-common = { version = "^1.3.0", default-features = false, features = [], optional = true }
#scrypto = { version = "^1.3.0", optional = true }
sha3 = { version = "^0.10.8", default-features = false, features = ["asm"] }
k256 = { version = "^0.13.4", default-features = false, features = [], optional = true }
#k256 = { version = "^0.13.4", default-features = false, features = [], optional = true }
secp256k1 = { version = "^0.29.1", 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 }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "^0.2.15", default-features = false, features = ["js"] }
Expand Down
7 changes: 7 additions & 0 deletions src/crypto/keccak256.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::crypto::Keccak256Hash;
#[cfg(not(all(feature = "crypto_radix", target_arch = "wasm32")))]
#[cfg(not(feature = "crypto_solana"))]
use sha3::Digest;

#[cfg(not(all(feature = "crypto_radix", target_arch = "wasm32")))]
#[cfg(not(feature = "crypto_solana"))]
pub fn keccak256(data: &[u8]) -> Keccak256Hash {
sha3::Keccak256::new_with_prefix(data)
.finalize()
Expand All @@ -16,6 +18,11 @@ pub fn keccak256(data: &[u8]) -> Keccak256Hash {
scrypto::prelude::CryptoUtils::keccak256_hash(data).0
}

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

#[cfg(not(all(feature = "crypto_radix", target_arch = "wasm32")))]
#[cfg(feature = "helpers")]
#[cfg(test)]
Expand Down
21 changes: 21 additions & 0 deletions src/crypto/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,28 @@ pub(crate) mod crypto256 {
}
}

#[cfg(feature = "crypto_solana")]
pub(crate) mod crypto256 {
use super::{EcdsaUncompressedPublicKey, Keccak256Hash, Secp256SigRs};
use anchor_lang::solana_program::secp256k1_recover::secp256k1_recover;

pub(crate) fn recover_public_key(
message_hash: Keccak256Hash,
signature_bytes: Secp256SigRs,
recovery_byte: u8,
) -> EcdsaUncompressedPublicKey {
let key = secp256k1_recover(&message_hash, recovery_byte, &signature_bytes)
.unwrap().0;
let mut uncompressed_key = [0u8; 65];
uncompressed_key[0] = 0x04;
uncompressed_key[1..].copy_from_slice(&key);

uncompressed_key
}
}

#[cfg(all(
not(feature = "crypto_solana"),
not(feature = "crypto_k256"),
not(feature = "crypto_secp256k1"),
not(all(feature = "crypto_radix", target_arch = "wasm32"))
Expand Down

0 comments on commit 180b54f

Please sign in to comment.