diff --git a/Cargo.lock b/Cargo.lock index 8d4a985..5058877 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,7 +37,7 @@ version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" dependencies = [ - "getrandom 0.2.14", + "getrandom", "once_cell", "version_check", ] @@ -145,7 +145,7 @@ dependencies = [ "bech32", "hex", "libsecp256k1", - "rand 0.8.5", + "rand", "serde", "sha2 0.10.8", "thiserror", @@ -251,7 +251,7 @@ dependencies = [ "libsecp256k1", "once_cell", "pbkdf2 0.12.2", - "rand 0.8.5", + "rand", "ripemd", "rustc-hash", "sha2 0.10.8", @@ -269,7 +269,7 @@ dependencies = [ "base58", "base64 0.21.7", "p256", - "rand 0.8.5", + "rand", ] [[package]] @@ -288,7 +288,7 @@ dependencies = [ "anychain-core", "base58", "blake2b_simd 1.0.2", - "ed25519-dalek 2.1.1", + "ed25519-dalek", "libsecp256k1", "parity-scale-codec", "serde_json", @@ -304,16 +304,6 @@ dependencies = [ "libsecp256k1", ] -[[package]] -name = "anychain-solana" -version = "0.1.0" -dependencies = [ - "anychain-core", - "bs58 0.4.0", - "ed25519-dalek 1.0.1", - "serde", -] - [[package]] name = "anychain-tron" version = "0.2.3" @@ -463,7 +453,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" dependencies = [ "num-traits", - "rand 0.8.5", + "rand", ] [[package]] @@ -554,7 +544,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93f2635620bf0b9d4576eb7bb9a38a55df78bd1205d26fa994b25911a69f212f" dependencies = [ "bitcoin_hashes", - "rand 0.8.5", + "rand", "rand_core 0.6.4", "serde", "unicode-normalization", @@ -1198,19 +1188,10 @@ dependencies = [ "digest 0.10.7", "elliptic-curve", "rfc6979", - "signature 2.2.0", + "signature", "spki", ] -[[package]] -name = "ed25519" -version = "1.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91cff35c70bba8a626e3185d8cd48cc11b5437e1a5bcd15b9b5fa3c64b6dfee7" -dependencies = [ - "signature 1.6.4", -] - [[package]] name = "ed25519" version = "2.2.3" @@ -1218,21 +1199,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" dependencies = [ "pkcs8", - "signature 2.2.0", -] - -[[package]] -name = "ed25519-dalek" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" -dependencies = [ - "curve25519-dalek 3.2.0", - "ed25519 1.5.3", - "rand 0.7.3", - "serde", - "sha2 0.9.9", - "zeroize", + "signature", ] [[package]] @@ -1242,7 +1209,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" dependencies = [ "curve25519-dalek 4.1.2", - "ed25519 2.2.3", + "ed25519", "serde", "sha2 0.10.8", "subtle", @@ -1408,7 +1375,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfcf0ed7fe52a17a03854ec54a9f76d6d84508d1c0e66bc1793301c73fc8493c" dependencies = [ "byteorder", - "rand 0.8.5", + "rand", "rustc-hex", "static_assertions", ] @@ -1420,7 +1387,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" dependencies = [ "byteorder", - "rand 0.8.5", + "rand", "rustc-hex", "static_assertions", ] @@ -1661,17 +1628,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "getrandom" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" -dependencies = [ - "cfg-if", - "libc", - "wasi 0.9.0+wasi-snapshot-preview1", -] - [[package]] name = "getrandom" version = "0.2.14" @@ -1680,7 +1636,7 @@ checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" dependencies = [ "cfg-if", "libc", - "wasi 0.11.0+wasi-snapshot-preview1", + "wasi", ] [[package]] @@ -1689,7 +1645,7 @@ version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ea1015b5a70616b688dc230cfe50c8af89d972cb132d5a622814d29773b10b9" dependencies = [ - "rand 0.8.5", + "rand", "rand_core 0.6.4", ] @@ -2016,7 +1972,7 @@ dependencies = [ "libsecp256k1-core", "libsecp256k1-gen-ecmult", "libsecp256k1-gen-genmult", - "rand 0.8.5", + "rand", "serde", "sha2 0.9.9", "typenum", @@ -2654,19 +2610,6 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" -[[package]] -name = "rand" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" -dependencies = [ - "getrandom 0.1.16", - "libc", - "rand_chacha 0.2.2", - "rand_core 0.5.1", - "rand_hc", -] - [[package]] name = "rand" version = "0.8.5" @@ -2674,20 +2617,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", - "rand_chacha 0.3.1", + "rand_chacha", "rand_core 0.6.4", ] -[[package]] -name = "rand_chacha" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" -dependencies = [ - "ppv-lite86", - "rand_core 0.5.1", -] - [[package]] name = "rand_chacha" version = "0.3.1" @@ -2703,9 +2636,6 @@ name = "rand_core" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" -dependencies = [ - "getrandom 0.1.16", -] [[package]] name = "rand_core" @@ -2713,16 +2643,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ - "getrandom 0.2.14", -] - -[[package]] -name = "rand_hc" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core 0.5.1", + "getrandom", ] [[package]] @@ -3165,12 +3086,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "signature" -version = "1.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" - [[package]] name = "signature" version = "2.2.0" @@ -3222,7 +3137,7 @@ dependencies = [ "parking_lot", "paste", "primitive-types 0.12.2", - "rand 0.8.5", + "rand", "scale-info", "schnorrkel", "secp256k1", @@ -3642,7 +3557,7 @@ checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ "cfg-if", "digest 0.10.7", - "rand 0.8.5", + "rand", "static_assertions", ] @@ -3747,8 +3662,8 @@ dependencies = [ "arrayref", "constcat", "digest 0.10.7", - "rand 0.8.5", - "rand_chacha 0.3.1", + "rand", + "rand_chacha", "rand_core 0.6.4", "sha2 0.10.8", "sha3", @@ -3756,12 +3671,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -3944,7 +3853,7 @@ dependencies = [ "memfd", "memoffset", "paste", - "rand 0.8.5", + "rand", "rustix 0.36.17", "wasmtime-asm-macros", "wasmtime-environ", diff --git a/Cargo.toml b/Cargo.toml index 71bd473..13951f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ members = [ "anychain-polkadot", "anychain-ripple", "anychain-neo", - "anychain-solana", "anychain-kms", "examples/*", ] diff --git a/anychain-solana/Cargo.toml b/anychain-solana/Cargo.toml deleted file mode 100644 index ea8c33e..0000000 --- a/anychain-solana/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "anychain-solana" -description = "A Rust library for Solana-focused cryptocurrency wallets, enabling seamless transactions on the Solana blockchain" -version = "0.1.0" -keywords = ["solana", "blockchain", "cryptocurrency", "wallet", "transactions"] - -# Workspace inherited keys -authors.workspace = true -edition.workspace = true -homepage.workspace = true -license.workspace = true -repository.workspace = true - - -[dependencies] -anychain-core = { path = "../anychain-core", version = "0.1.5" } -ed25519-dalek = { version = "=1.0.1" } -serde = { workspace = true } -bs58 = { workspace = true } \ No newline at end of file diff --git a/anychain-solana/src/address.rs b/anychain-solana/src/address.rs deleted file mode 100644 index 98eb854..0000000 --- a/anychain-solana/src/address.rs +++ /dev/null @@ -1,114 +0,0 @@ -use { - crate::{format::SolanaFormat, public_key::SolanaPublicKey}, - anychain_core::{Address, AddressError, PublicKey, PublicKeyError}, - core::{ - fmt::{Display, Formatter, Result as FmtResult}, - str::FromStr, - }, - ed25519_dalek::PUBLIC_KEY_LENGTH, -}; - -/// Represents a Solana address -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct SolanaAddress(pub String); - -impl Address for SolanaAddress { - type SecretKey = ed25519_dalek::SecretKey; - type Format = SolanaFormat; - type PublicKey = SolanaPublicKey; - - fn from_secret_key( - secret_key: &Self::SecretKey, - format: &Self::Format, - ) -> Result { - Self::PublicKey::from_secret_key(secret_key).to_address(format) - } - - fn from_public_key( - public_key: &Self::PublicKey, - _: &Self::Format, - ) -> Result { - let address = bs58::encode(public_key.0.to_bytes()).into_string(); - Ok(Self(address)) - } - - fn is_valid(address: &str) -> bool { - Self::from_str(address).is_ok() - } -} - -impl FromStr for SolanaAddress { - type Err = AddressError; - - fn from_str(addr: &str) -> Result { - // Check if the address is valid - if addr.len() > crate::public_key::MAX_BASE58_LEN { - return Err(AddressError::InvalidCharacterLength(addr.len())); - } - let pubkey_vec = bs58::decode(addr) - .into_vec() - .map_err(|error| PublicKeyError::Crate("base58", format!("{:?}", error)))?; - if pubkey_vec.len() != PUBLIC_KEY_LENGTH { - return Err(AddressError::InvalidAddress(addr.to_string())); - } - let buffer: [u8; PUBLIC_KEY_LENGTH] = pubkey_vec.as_slice().try_into().unwrap(); - let _ = ed25519_dalek::PublicKey::from_bytes(&buffer) - .map_err(|error| AddressError::Message(error.to_string()))?; - - Ok(Self(addr.to_string())) - } -} - -impl Display for SolanaAddress { - fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - write!(f, "{}", self.0) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use ed25519_dalek::{SecretKey, KEYPAIR_LENGTH, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH}; - - #[test] - fn test_address_alice() { - let keypair_bytes: [u8; KEYPAIR_LENGTH] = [ - 41, 196, 252, 146, 80, 100, 13, 46, 69, 89, 172, 157, 224, 135, 23, 62, 54, 65, 52, 68, - 14, 50, 112, 112, 156, 210, 24, 236, 139, 169, 38, 63, 205, 66, 112, 255, 116, 177, 79, - 182, 192, 20, 240, 193, 219, 162, 23, 149, 26, 247, 181, 186, 145, 168, 26, 232, 228, - 76, 102, 109, 64, 189, 172, 44, - ]; - - let mut secret_bytes: [u8; PUBLIC_KEY_LENGTH] = [0u8; SECRET_KEY_LENGTH]; - secret_bytes.copy_from_slice(&keypair_bytes[0..SECRET_KEY_LENGTH]); - let secret_key: SecretKey = SecretKey::from_bytes(&secret_bytes).unwrap(); - - let address = - SolanaAddress::from_secret_key(&secret_key, &SolanaFormat::default()).unwrap(); - assert_eq!( - "EpFLfuH524fk9QP9i9uL9AHtX6smBaxaMHwek9T11nK5", - address.to_string() - ); - } - - #[test] - fn test_address_bob() { - let keypair_bytes: [u8; KEYPAIR_LENGTH] = [ - 47, 232, 53, 167, 54, 186, 162, 109, 156, 250, 166, 187, 29, 118, 132, 137, 28, 228, - 202, 245, 100, 119, 252, 44, 3, 55, 22, 129, 80, 11, 154, 149, 178, 218, 84, 101, 24, - 203, 245, 149, 168, 220, 195, 44, 240, 213, 89, 146, 82, 159, 117, 129, 133, 128, 7, - 99, 136, 179, 15, 161, 42, 132, 31, 41, - ]; - - let mut secret_bytes: [u8; PUBLIC_KEY_LENGTH] = [0u8; SECRET_KEY_LENGTH]; - secret_bytes.copy_from_slice(&keypair_bytes[0..SECRET_KEY_LENGTH]); - let secret_key: SecretKey = SecretKey::from_bytes(&secret_bytes).unwrap(); - - let address = - SolanaAddress::from_secret_key(&secret_key, &SolanaFormat::default()).unwrap(); - assert_eq!( - "D3AfQC64W8xCqwH1y94dQY4JLG6HQx6uLoHk9V6qqAKr", - address.to_string() - ); - } -} diff --git a/anychain-solana/src/amount.rs b/anychain-solana/src/amount.rs deleted file mode 100644 index d1b5e0a..0000000 --- a/anychain-solana/src/amount.rs +++ /dev/null @@ -1,167 +0,0 @@ -//! Definitions for the native SOL token and its fractional lamports. - -use { - anychain_core::{to_basic_unit_u64, Amount, AmountError}, - core::fmt, - serde::{Deserialize, Serialize}, - std::ops::{Add, Sub}, -}; - -/// Represents the amount of SOL in lamports -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -pub struct SolanaAmount(pub u64); - -pub enum Denomination { - LAMPORTS, - SOL, -} - -impl Denomination { - /// The number of decimal places more than one lamports. - /// There are 10^9 lamports in one SOL - fn precision(self) -> u64 { - match self { - Denomination::LAMPORTS => 0, - - Denomination::SOL => 9, - } - } -} - -impl fmt::Display for Denomination { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}", - match self { - Denomination::LAMPORTS => "lamports", - Denomination::SOL => "SOL", - } - ) - } -} - -impl Amount for SolanaAmount {} - -impl SolanaAmount { - pub fn from_u64(lamports: u64) -> Self { - Self(lamports) - } - - pub fn from_u64_str(value: &str) -> Result { - match value.parse::() { - Ok(lamports) => Ok(lamports), - Err(error) => Err(AmountError::Crate("uint", format!("{:?}", error))), - } - } - pub fn from_lamports(lamports_value: &str) -> Result { - let lamports = Self::from_u64_str(lamports_value)?; - Ok(Self::from_u64(lamports)) - } - - pub fn from_sol(sol_value: &str) -> Result { - let lamports_value = to_basic_unit_u64(sol_value, Denomination::SOL.precision()); - let lamports = Self::from_u64_str(&lamports_value)?; - Ok(Self::from_u64(lamports)) - } -} - -impl Add for SolanaAmount { - type Output = Self; - fn add(self, rhs: Self) -> Self { - Self(self.0 + rhs.0) - } -} - -impl Sub for SolanaAmount { - type Output = Self; - fn sub(self, rhs: Self) -> Self::Output { - Self(self.0 - rhs.0) - } -} - -impl fmt::Display for SolanaAmount { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -#[cfg(test)] -#[allow(dead_code)] -mod tests { - use super::*; - - fn test_from_lamports(lamports_value: &str, expected_amount: &str) { - let amount = SolanaAmount::from_lamports(lamports_value).unwrap(); - assert_eq!(expected_amount, amount.to_string()) - } - - fn test_from_sol(sol_value: &str, expected_amount: &str) { - let amount = SolanaAmount::from_sol(sol_value).unwrap(); - assert_eq!(expected_amount, amount.to_string()) - } - - pub struct AmountDenominationTestCase { - lamports: &'static str, - sol: &'static str, - } - - const TEST_AMOUNTS: [AmountDenominationTestCase; 2] = [ - AmountDenominationTestCase { - lamports: "0", - sol: "0", - }, - AmountDenominationTestCase { - lamports: "1000000000", - sol: "1", - }, - ]; - - #[test] - fn test_lamports_conversion() { - TEST_AMOUNTS - .iter() - .for_each(|amounts| test_from_lamports(amounts.lamports, amounts.lamports)); - } - - #[test] - fn test_sol_conversion() { - TEST_AMOUNTS - .iter() - .for_each(|amounts| test_from_sol(amounts.sol, amounts.lamports)); - } - - fn test_addition(a: &str, b: &str, result: &str) { - let a = SolanaAmount::from_lamports(a).unwrap(); - let b = SolanaAmount::from_lamports(b).unwrap(); - let result = SolanaAmount::from_lamports(result).unwrap(); - - assert_eq!(result, a.add(b)); - } - - fn test_subtraction(a: &str, b: &str, result: &str) { - let a = SolanaAmount::from_lamports(a).unwrap(); - let b = SolanaAmount::from_lamports(b).unwrap(); - let result = SolanaAmount::from_lamports(result).unwrap(); - - assert_eq!(result, a.sub(b)); - } - mod valid_arithmetic { - use super::*; - - const TEST_VALUES: [(&str, &str, &str); 5] = [ - ("0", "0", "0"), - ("1", "2", "3"), - ("100000", "0", "100000"), - ("123456789", "987654321", "1111111110"), - ("1000000000000000", "2000000000000000", "3000000000000000"), - ]; - - #[test] - fn test_valid_addition() { - TEST_VALUES - .iter() - .for_each(|(a, b, c)| test_addition(a, b, c)); - } - } -} diff --git a/anychain-solana/src/format.rs b/anychain-solana/src/format.rs deleted file mode 100644 index 4a535f3..0000000 --- a/anychain-solana/src/format.rs +++ /dev/null @@ -1,28 +0,0 @@ -use { - anychain_core::Format, - core::{default::Default, fmt}, -}; - -#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum SolanaFormat { - #[default] - Standard, -} - -impl Format for SolanaFormat {} - -impl fmt::Display for SolanaFormat { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Standard") - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_display() { - assert_eq!(SolanaFormat::Standard.to_string(), "Standard"); - } -} diff --git a/anychain-solana/src/lib.rs b/anychain-solana/src/lib.rs deleted file mode 100644 index 5b9e10e..0000000 --- a/anychain-solana/src/lib.rs +++ /dev/null @@ -1,11 +0,0 @@ -pub mod address; -pub mod amount; -pub mod format; -pub mod public_key; -// pub mod transaction; - -pub use self::address::*; -pub use self::amount::*; -pub use self::format::*; -pub use self::public_key::*; -// pub use self::transaction::*; diff --git a/anychain-solana/src/public_key.rs b/anychain-solana/src/public_key.rs deleted file mode 100644 index c3059c4..0000000 --- a/anychain-solana/src/public_key.rs +++ /dev/null @@ -1,89 +0,0 @@ -use { - crate::{address::SolanaAddress, format::SolanaFormat}, - anychain_core::{Address, AddressError, PublicKey, PublicKeyError}, - core::{convert::TryInto, fmt, str::FromStr}, - ed25519_dalek::PUBLIC_KEY_LENGTH, -}; - -/// Maximum string length of a base58 encoded pubkey -pub const MAX_BASE58_LEN: usize = 44; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct SolanaPublicKey(pub ed25519_dalek::PublicKey); - -impl PublicKey for SolanaPublicKey { - type SecretKey = ed25519_dalek::SecretKey; - type Address = SolanaAddress; - type Format = SolanaFormat; - - fn from_secret_key(secret_key: &Self::SecretKey) -> Self { - let signing_key = ed25519_dalek::SecretKey::from_bytes(secret_key.as_bytes()).unwrap(); - let verifying_key: ed25519_dalek::PublicKey = (&signing_key).into(); - SolanaPublicKey(verifying_key) - } - - fn to_address(&self, format: &Self::Format) -> Result { - Self::Address::from_public_key(self, format) - } -} - -impl FromStr for SolanaPublicKey { - type Err = PublicKeyError; - - fn from_str(s: &str) -> Result { - if s.len() > MAX_BASE58_LEN { - return Err(PublicKeyError::InvalidByteLength(s.len())); - } - let pubkey_vec = bs58::decode(s) - .into_vec() - .map_err(|error| PublicKeyError::Crate("base58", format!("{:?}", error)))?; - if pubkey_vec.len() != PUBLIC_KEY_LENGTH { - return Err(PublicKeyError::InvalidByteLength(pubkey_vec.len())); - } - let buffer: [u8; PUBLIC_KEY_LENGTH] = pubkey_vec.as_slice().try_into().unwrap(); - let verifying_key = ed25519_dalek::PublicKey::from_bytes(&buffer) - .map_err(|error| PublicKeyError::Crate("base58", format!("{:?}", error)))?; - Ok(SolanaPublicKey(verifying_key)) - } -} - -impl fmt::Display for SolanaPublicKey { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", bs58::encode(self.0.to_bytes()).into_string()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use anychain_core::PublicKey; - use ed25519_dalek::{SecretKey, KEYPAIR_LENGTH, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH}; - - #[test] - fn test_public_key_from_str() { - let pubkey_str = "EpFLfuH524fk9QP9i9uL9AHtX6smBaxaMHwek9T11nK5"; - let pubkey_res = SolanaPublicKey::from_str(pubkey_str); - assert!(pubkey_res.is_ok()); - let pubkey = pubkey_res.unwrap(); - assert_eq!(pubkey.to_string(), pubkey_str); - } - - #[test] - fn test_public_key_from_from_secret_key() { - // let public_bytes :[u8;PUBLIC_KEY_LENGTH] = [0xu8;PUBLIC_KEY_LENGTH]; - let pubkey_str = "EpFLfuH524fk9QP9i9uL9AHtX6smBaxaMHwek9T11nK5"; - - let mut secret_bytes: [u8; PUBLIC_KEY_LENGTH] = [0u8; SECRET_KEY_LENGTH]; - let keypair_bytes: [u8; KEYPAIR_LENGTH] = [ - 41, 196, 252, 146, 80, 100, 13, 46, 69, 89, 172, 157, 224, 135, 23, 62, 54, 65, 52, 68, - 14, 50, 112, 112, 156, 210, 24, 236, 139, 169, 38, 63, 205, 66, 112, 255, 116, 177, 79, - 182, 192, 20, 240, 193, 219, 162, 23, 149, 26, 247, 181, 186, 145, 168, 26, 232, 228, - 76, 102, 109, 64, 189, 172, 44, - ]; - secret_bytes.copy_from_slice(&keypair_bytes[0..SECRET_KEY_LENGTH]); - - let secret_key: SecretKey = SecretKey::from_bytes(&secret_bytes).unwrap(); - let public_key = SolanaPublicKey::from_secret_key(&secret_key); - assert_eq!(public_key.to_string(), pubkey_str); - } -}