diff --git a/Cargo.lock b/Cargo.lock index 681184de65..431959e5c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1026,6 +1026,7 @@ dependencies = [ "bytes 0.4.12", "cfg-if 1.0.0", "chain", + "chrono", "common", "cosmrs", "crossbeam 0.8.2", @@ -1077,6 +1078,7 @@ dependencies = [ "mm2_state_machine", "mm2_test_helpers", "mocktopus", + "nom", "num-traits", "parking_lot 0.12.0", "primitives", @@ -1102,10 +1104,12 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "serde_with", "serialization", "serialization_derive", "sha2 0.10.7", "sha3 0.9.1", + "sia-rust", "solana-client", "solana-sdk", "solana-transaction-status", @@ -2019,6 +2023,7 @@ version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e9c280362032ea4203659fc489832d0204ef09f247a0506f170dafcac08c369" dependencies = [ + "serde", "signature 1.4.0", ] @@ -2055,6 +2060,7 @@ dependencies = [ "ed25519 1.5.2", "rand 0.7.3", "serde", + "serde_bytes", "sha2 0.9.9", "zeroize", ] @@ -4617,6 +4623,7 @@ dependencies = [ "serde_json", "serialization", "serialization_derive", + "sia-rust", "sp-runtime-interface", "sp-trie", "spv_validation", @@ -4624,6 +4631,7 @@ dependencies = [ "tokio", "trie-db", "trie-root 0.16.0", + "url", "uuid 1.2.2", "wasm-bindgen", "wasm-bindgen-futures", @@ -7011,6 +7019,26 @@ dependencies = [ "log", ] +[[package]] +name = "sia-rust" +version = "0.1.0" +source = "git+https://github.com/KomodoPlatform/sia-rust?rev=9f188b80b3213bcb604e7619275251ce08fae808#9f188b80b3213bcb604e7619275251ce08fae808" +dependencies = [ + "base64 0.21.7", + "blake2b_simd", + "chrono", + "derive_more", + "ed25519-dalek", + "hex", + "nom", + "reqwest", + "rustc-hex", + "serde", + "serde_json", + "serde_with", + "url", +] + [[package]] name = "signal-hook-registry" version = "1.4.0" diff --git a/mm2src/coins/Cargo.toml b/mm2src/coins/Cargo.toml index 3f37c3d74e..c5eeb570ea 100644 --- a/mm2src/coins/Cargo.toml +++ b/mm2src/coins/Cargo.toml @@ -18,7 +18,8 @@ enable-solana = [ ] enable-sia = [ "dep:reqwest", - "blake2b_simd" + "dep:blake2b_simd", + "dep:sia-rust" ] default = [] run-docker-tests = [] @@ -41,13 +42,14 @@ byteorder = "1.3" bytes = "0.4" cfg-if = "1.0" chain = { path = "../mm2_bitcoin/chain" } +chrono = { version = "0.4.23", "features" = ["serde"] } common = { path = "../common" } cosmrs = { version = "0.14.0", default-features = false } crossbeam = "0.8" crypto = { path = "../crypto" } db_common = { path = "../db_common" } derive_more = "0.99" -ed25519-dalek = "1.0.1" +ed25519-dalek = { version = "1.0.1", features = ["serde"] } enum_derives = { path = "../derives/enum_derives" } ethabi = { version = "17.0.0" } ethcore-transaction = { git = "https://github.com/KomodoPlatform/mm2-parity-ethereum.git", rev = "mm2-v2.1.1" } @@ -69,6 +71,7 @@ jsonrpc-core = "18.0.0" keys = { path = "../mm2_bitcoin/keys" } lazy_static = "1.4" libc = "0.2" +nom = "6.1.2" mm2_core = { path = "../mm2_core" } mm2_err_handle = { path = "../mm2_err_handle" } mm2_event_stream = { path = "../mm2_event_stream" } @@ -101,8 +104,10 @@ ser_error_derive = { path = "../derives/ser_error_derive" } serde = "1.0" serde_derive = "1.0" serde_json = { version = "1", features = ["preserve_order", "raw_value"] } +serde_with = "1.14.0" serialization = { path = "../mm2_bitcoin/serialization" } serialization_derive = { path = "../mm2_bitcoin/serialization_derive" } +sia-rust = { git = "https://github.com/KomodoPlatform/sia-rust", rev = "9f188b80b3213bcb604e7619275251ce08fae808", optional = true } spv_validation = { path = "../mm2_bitcoin/spv_validation" } sha2 = "0.10" sha3 = "0.9" diff --git a/mm2src/coins/hd_wallet/pubkey.rs b/mm2src/coins/hd_wallet/pubkey.rs index 7732819295..7babb12bd5 100644 --- a/mm2src/coins/hd_wallet/pubkey.rs +++ b/mm2src/coins/hd_wallet/pubkey.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "enable-sia")] +use crate::siacoin::sia_hd_wallet::Ed25519ExtendedPublicKey; use crate::CoinProtocol; use super::*; @@ -30,6 +32,13 @@ impl ExtendedPublicKeyOps for Secp256k1ExtendedPublicKey { fn to_string(&self, prefix: Prefix) -> String { self.to_string(prefix) } } +#[cfg(feature = "enable-sia")] +impl ExtendedPublicKeyOps for Ed25519ExtendedPublicKey { + fn derive_child(&self, child_number: ChildNumber) -> Result { self.derive_child(child_number) } + + fn to_string(&self, prefix: Prefix) -> String { self.to_string(prefix) } +} + /// This trait should be implemented for coins /// to support extracting extended public keys from any depth. /// The extraction can be from either an internal or external wallet. diff --git a/mm2src/coins/lp_coins.rs b/mm2src/coins/lp_coins.rs index 8ca714a34f..1af6027e30 100644 --- a/mm2src/coins/lp_coins.rs +++ b/mm2src/coins/lp_coins.rs @@ -240,7 +240,7 @@ use coin_errors::{MyAddressError, ValidatePaymentError, ValidatePaymentFut, Vali pub mod coins_tests; pub mod eth; -use eth::eth_swap_v2::{PaymentStatusErr, ValidatePaymentV2Err}; +use eth::eth_swap_v2::{PaymentStatusErr, PrepareTxDataError, ValidatePaymentV2Err}; use eth::GetValidEthWithdrawAddError; use eth::{eth_coin_from_conf_and_request, get_eth_address, EthCoin, EthGasDetailsErr, EthTxFeeDetails, GetEthAddressError, SignedEthTx}; @@ -277,6 +277,9 @@ pub use test_coin::TestCoin; pub mod tx_history_storage; +#[cfg(feature = "enable-sia")] pub mod siacoin; +#[cfg(feature = "enable-sia")] use siacoin::SiaCoin; + #[doc(hidden)] #[allow(unused_variables)] #[cfg(all( @@ -319,9 +322,6 @@ use script::Script; pub mod z_coin; use crate::coin_balance::{BalanceObjectOps, HDWalletBalanceObject}; use z_coin::{ZCoin, ZcoinProtocolInfo}; -#[cfg(feature = "enable-sia")] pub mod sia; -use crate::eth::eth_swap_v2::PrepareTxDataError; -#[cfg(feature = "enable-sia")] use sia::SiaCoin; pub type TransactionFut = Box + Send>; pub type TransactionResult = Result; diff --git a/mm2src/coins/sia/address.rs b/mm2src/coins/sia/address.rs deleted file mode 100644 index 5218a08bc8..0000000000 --- a/mm2src/coins/sia/address.rs +++ /dev/null @@ -1,167 +0,0 @@ -use crate::sia::blake2b_internal::standard_unlock_hash; -use blake2b_simd::Params; -use ed25519_dalek::PublicKey; -use hex::FromHexError; -use rpc::v1::types::H256; -use serde::{Deserialize, Serialize}; -use std::convert::TryInto; -use std::fmt; -use std::str::FromStr; - -// TODO this could probably include the checksum within the data type -// generating the checksum on the fly is how Sia Go does this however -#[derive(Debug, Clone, PartialEq)] -pub struct Address(pub H256); - -impl Address { - pub fn str_without_prefix(&self) -> String { - let bytes = self.0 .0.as_ref(); - let checksum = blake2b_checksum(bytes); - format!("{}{}", hex::encode(bytes), hex::encode(checksum)) - } -} - -impl fmt::Display for Address { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "addr:{}", self.str_without_prefix()) } -} - -impl fmt::Display for ParseAddressError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Failed to parse Address: {:?}", self) } -} - -#[derive(Debug, Deserialize, Serialize)] -pub enum ParseAddressError { - #[serde(rename = "Address must begin with addr: prefix")] - MissingPrefix, - InvalidHexEncoding(String), - InvalidChecksum, - InvalidLength, - // Add other error kinds as needed -} - -impl From for ParseAddressError { - fn from(e: FromHexError) -> Self { ParseAddressError::InvalidHexEncoding(e.to_string()) } -} - -impl FromStr for Address { - type Err = ParseAddressError; - - fn from_str(s: &str) -> Result { - if !s.starts_with("addr:") { - return Err(ParseAddressError::MissingPrefix); - } - - let without_prefix = &s[5..]; - if without_prefix.len() != (32 + 6) * 2 { - return Err(ParseAddressError::InvalidLength); - } - - let (address_hex, checksum_hex) = without_prefix.split_at(32 * 2); - - let address_bytes: [u8; 32] = hex::decode(address_hex) - .map_err(ParseAddressError::from)? - .try_into() - .expect("length is 32 bytes"); - - let checksum = hex::decode(checksum_hex).map_err(ParseAddressError::from)?; - let checksum_bytes: [u8; 6] = checksum.try_into().expect("length is 6 bytes"); - - if checksum_bytes != blake2b_checksum(&address_bytes) { - return Err(ParseAddressError::InvalidChecksum); - } - - Ok(Address(H256::from(address_bytes))) - } -} - -// Sia uses the first 6 bytes of blake2b(preimage) appended -// to address as checksum -fn blake2b_checksum(preimage: &[u8]) -> [u8; 6] { - let hash = Params::new().hash_length(32).to_state().update(preimage).finalize(); - hash.as_array()[0..6].try_into().expect("array is 64 bytes long") -} - -pub fn v1_standard_address_from_pubkey(pubkey: &PublicKey) -> Address { - let hash = standard_unlock_hash(pubkey); - Address(hash) -} - -#[test] -fn test_v1_standard_address_from_pubkey() { - let pubkey = PublicKey::from_bytes( - &hex::decode("8a88e3dd7409f195fd52db2d3cba5d72ca6709bf1d94121bf3748801b40f6f5c").unwrap(), - ) - .unwrap(); - let address = v1_standard_address_from_pubkey(&pubkey); - assert_eq!( - format!("{}", address), - "addr:c959f9b423b662c36ee58057b8157acedb4095cfeb7926e4ba44cd9ee1f49a5b7803c7501a7b" - ) -} - -#[test] -fn test_blake2b_checksum() { - let checksum = - blake2b_checksum(&hex::decode("591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a884").unwrap()); - let expected: [u8; 6] = hex::decode("0be0653e411f").unwrap().try_into().unwrap(); - assert_eq!(checksum, expected); -} - -#[test] -fn test_address_display() { - let address = Address("591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a884".into()); - let address_str = "addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f"; - assert_eq!(format!("{}", address), address_str); -} - -#[test] -fn test_address_fromstr() { - let address1 = Address("591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a884".into()); - - let address2 = - Address::from_str("addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f").unwrap(); - assert_eq!(address1, address2); -} - -#[test] -fn test_address_fromstr_bad_length() { - let address = Address::from_str("addr:dead"); - assert!(matches!(address, Err(ParseAddressError::InvalidLength))); -} - -#[test] -fn test_address_fromstr_odd_length() { - let address = Address::from_str("addr:f00"); - assert!(matches!(address, Err(ParseAddressError::InvalidLength))); -} - -#[test] -fn test_address_fromstr_invalid_hex() { - let address = - Address::from_str("addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e41gg"); - assert!(matches!(address, Err(ParseAddressError::InvalidHexEncoding(_)))); -} - -#[test] -fn test_address_fromstr_missing_prefix() { - let address = Address::from_str("591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e41gg"); - assert!(matches!(address, Err(ParseAddressError::MissingPrefix))); -} - -#[test] -fn test_address_fromstr_invalid_checksum() { - let address = - Address::from_str("addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a884ffffffffffff"); - assert!(matches!(address, Err(ParseAddressError::InvalidChecksum))); -} - -#[test] -fn test_address_str_without_prefix() { - let address = - Address::from_str("addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f").unwrap(); - - assert_eq!( - address.str_without_prefix(), - "591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f" - ); -} diff --git a/mm2src/coins/sia/blake2b_internal.rs b/mm2src/coins/sia/blake2b_internal.rs deleted file mode 100644 index 39c4f7c82b..0000000000 --- a/mm2src/coins/sia/blake2b_internal.rs +++ /dev/null @@ -1,326 +0,0 @@ -use blake2b_simd::Params; -use ed25519_dalek::PublicKey; -use rpc::v1::types::H256; -use std::default::Default; - -#[cfg(test)] use hex; -#[cfg(test)] use std::convert::TryInto; - -const LEAF_HASH_PREFIX: [u8; 1] = [0u8]; -const NODE_HASH_PREFIX: [u8; 1] = [1u8]; - -pub const ED25519_IDENTIFIER: [u8; 16] = [ - 0x65, 0x64, 0x32, 0x35, 0x35, 0x31, 0x39, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, -]; - -// Precomputed hash values used for all standard v1 addresses -// a standard address has 1 ed25519 public key, requires 1 signature and has a timelock of 0 -// https://github.com/SiaFoundation/core/blob/b5b08cde6b7d0f1b3a6f09b8aa9d0b817e769efb/types/hash.go#L94 -const STANDARD_TIMELOCK_BLAKE2B_HASH: [u8; 32] = [ - 0x51, 0x87, 0xb7, 0xa8, 0x02, 0x1b, 0xf4, 0xf2, 0xc0, 0x04, 0xea, 0x3a, 0x54, 0xcf, 0xec, 0xe1, 0x75, 0x4f, 0x11, - 0xc7, 0x62, 0x4d, 0x23, 0x63, 0xc7, 0xf4, 0xcf, 0x4f, 0xdd, 0xd1, 0x44, 0x1e, -]; - -const STANDARD_SIGS_REQUIRED_BLAKE2B_HASH: [u8; 32] = [ - 0xb3, 0x60, 0x10, 0xeb, 0x28, 0x5c, 0x15, 0x4a, 0x8c, 0xd6, 0x30, 0x84, 0xac, 0xbe, 0x7e, 0xac, 0x0c, 0x4d, 0x62, - 0x5a, 0xb4, 0xe1, 0xa7, 0x6e, 0x62, 0x4a, 0x87, 0x98, 0xcb, 0x63, 0x49, 0x7b, -]; - -#[derive(Debug, PartialEq)] -pub struct Accumulator { - trees: [H256; 64], - num_leaves: u64, -} - -impl Default for Accumulator { - fn default() -> Self { - Accumulator { - trees: [H256::default(); 64], // Initialize all bytes to zero - num_leaves: 0, - } - } -} - -impl Accumulator { - // Check if there is a tree at the given height - fn has_tree_at_height(&self, height: u64) -> bool { self.num_leaves & (1 << height) != 0 } - - // Add a leaf to the accumulator - pub fn add_leaf(&mut self, h: H256) { - let mut i = 0; - let mut new_hash = h; - while self.has_tree_at_height(i) { - new_hash = hash_blake2b_pair(&NODE_HASH_PREFIX, &self.trees[i as usize].0, &new_hash.0); - i += 1; - } - self.trees[i as usize] = new_hash; - self.num_leaves += 1; - } - - // Calulate the root hash of the Merkle tree - pub fn root(&self) -> H256 { - // trailing_zeros determines the height Merkle tree accumulator where the current lowest single leaf is located - let i = self.num_leaves.trailing_zeros() as u64; - if i == 64 { - return H256::default(); // Return all zeros if no leaves - } - let mut root = self.trees[i as usize]; - for j in i + 1..64 { - if self.has_tree_at_height(j) { - root = hash_blake2b_pair(&NODE_HASH_PREFIX, &self.trees[j as usize].0, &root.0); - } - } - root - } -} - -pub fn sigs_required_leaf(sigs_required: u64) -> H256 { - let sigs_required_array: [u8; 8] = sigs_required.to_le_bytes(); - let mut combined = Vec::new(); - combined.extend_from_slice(&LEAF_HASH_PREFIX); - combined.extend_from_slice(&sigs_required_array); - - hash_blake2b_single(&combined) -} - -// public key leaf is -// blake2b(leafHashPrefix + 16_byte_ascii_algorithm_identifier + public_key_length_u64 + public_key) -pub fn public_key_leaf(pubkey: &PublicKey) -> H256 { - let mut combined = Vec::new(); - combined.extend_from_slice(&LEAF_HASH_PREFIX); - combined.extend_from_slice(&ED25519_IDENTIFIER); - combined.extend_from_slice(&32u64.to_le_bytes()); - combined.extend_from_slice(pubkey.as_bytes()); - hash_blake2b_single(&combined) -} - -pub fn timelock_leaf(timelock: u64) -> H256 { - let timelock: [u8; 8] = timelock.to_le_bytes(); - let mut combined = Vec::new(); - combined.extend_from_slice(&LEAF_HASH_PREFIX); - combined.extend_from_slice(&timelock); - - hash_blake2b_single(&combined) -} - -// https://github.com/SiaFoundation/core/blob/b5b08cde6b7d0f1b3a6f09b8aa9d0b817e769efb/types/hash.go#L96 -// An UnlockHash is the Merkle root of UnlockConditions. Since the standard -// UnlockConditions use a single public key, the Merkle tree is: -// -// ┌─────────┴──────────┐ -// ┌─────┴─────┐ │ -// timelock pubkey sigsrequired -pub fn standard_unlock_hash(pubkey: &PublicKey) -> H256 { - let pubkey_leaf = public_key_leaf(pubkey); - let timelock_pubkey_node = hash_blake2b_pair(&NODE_HASH_PREFIX, &STANDARD_TIMELOCK_BLAKE2B_HASH, &pubkey_leaf.0); - hash_blake2b_pair( - &NODE_HASH_PREFIX, - &timelock_pubkey_node.0, - &STANDARD_SIGS_REQUIRED_BLAKE2B_HASH, - ) -} - -pub fn hash_blake2b_single(preimage: &[u8]) -> H256 { - let hash = Params::new().hash_length(32).to_state().update(preimage).finalize(); - let ret_array = hash.as_array(); - ret_array[0..32].into() -} - -fn hash_blake2b_pair(prefix: &[u8], leaf1: &[u8], leaf2: &[u8]) -> H256 { - let hash = Params::new() - .hash_length(32) - .to_state() - .update(prefix) - .update(leaf1) - .update(leaf2) - .finalize(); - let ret_array = hash.as_array(); - ret_array[0..32].into() -} - -#[test] -fn test_accumulator_new() { - let default_accumulator = Accumulator::default(); - - let expected = Accumulator { - trees: [H256::from("0000000000000000000000000000000000000000000000000000000000000000"); 64], - num_leaves: 0, - }; - assert_eq!(default_accumulator, expected) -} - -#[test] -fn test_accumulator_root_default() { assert_eq!(Accumulator::default().root(), H256::default()) } - -#[test] -fn test_accumulator_root() { - let mut accumulator = Accumulator::default(); - - let timelock_leaf = timelock_leaf(0u64); - accumulator.add_leaf(timelock_leaf); - - let pubkey = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - let pubkey_leaf = public_key_leaf(&pubkey); - accumulator.add_leaf(pubkey_leaf); - - let sigs_required_leaf = sigs_required_leaf(1u64); - accumulator.add_leaf(sigs_required_leaf); - - let expected = H256::from("72b0762b382d4c251af5ae25b6777d908726d75962e5224f98d7f619bb39515d"); - assert_eq!(accumulator.root(), expected); -} - -#[test] -fn test_accumulator_add_leaf_standard_unlock_hash() { - let mut accumulator = Accumulator::default(); - - let pubkey = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - - let pubkey_leaf = public_key_leaf(&pubkey); - let timelock_leaf = timelock_leaf(0u64); - let sigs_required_leaf = sigs_required_leaf(1u64); - - accumulator.add_leaf(timelock_leaf); - accumulator.add_leaf(pubkey_leaf); - accumulator.add_leaf(sigs_required_leaf); - - let root = accumulator.root(); - let expected = H256::from("72b0762b382d4c251af5ae25b6777d908726d75962e5224f98d7f619bb39515d"); - assert_eq!(root, expected) -} - -#[test] -fn test_accumulator_add_leaf_2of2_multisig_unlock_hash() { - let mut accumulator = Accumulator::default(); - - let pubkey1 = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - let pubkey2 = PublicKey::from_bytes( - &hex::decode("0101010000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - - let pubkey1_leaf = public_key_leaf(&pubkey1); - let pubkey2_leaf = public_key_leaf(&pubkey2); - - let timelock_leaf = timelock_leaf(0u64); - let sigs_required_leaf = sigs_required_leaf(2u64); - - accumulator.add_leaf(timelock_leaf); - accumulator.add_leaf(pubkey1_leaf); - accumulator.add_leaf(pubkey2_leaf); - accumulator.add_leaf(sigs_required_leaf); - - let root = accumulator.root(); - let expected = H256::from("1e94357817d236167e54970a8c08bbd41b37bfceeeb52f6c1ce6dd01d50ea1e7"); - assert_eq!(root, expected) -} - -#[test] -fn test_accumulator_add_leaf_1of2_multisig_unlock_hash() { - let mut accumulator = Accumulator::default(); - - let pubkey1 = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - let pubkey2 = PublicKey::from_bytes( - &hex::decode("0101010000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - - let pubkey1_leaf = public_key_leaf(&pubkey1); - let pubkey2_leaf = public_key_leaf(&pubkey2); - - let timelock_leaf = timelock_leaf(0u64); - let sigs_required_leaf = sigs_required_leaf(1u64); - - accumulator.add_leaf(timelock_leaf); - accumulator.add_leaf(pubkey1_leaf); - accumulator.add_leaf(pubkey2_leaf); - accumulator.add_leaf(sigs_required_leaf); - - let root = accumulator.root(); - let expected = H256::from("d7f84e3423da09d111a17f64290c8d05e1cbe4cab2b6bed49e3a4d2f659f0585"); - assert_eq!(root, expected) -} - -#[test] -fn test_standard_unlock_hash() { - let pubkey = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - - let hash = standard_unlock_hash(&pubkey); - let expected = H256::from("72b0762b382d4c251af5ae25b6777d908726d75962e5224f98d7f619bb39515d"); - assert_eq!(hash, expected) -} - -#[test] -fn test_hash_blake2b_pair() { - let left: [u8; 32] = hex::decode("cdcce3978a58ceb6c8480d218646db4eae85eb9ea9c2f5138fbacb4ce2c701e3") - .unwrap() - .try_into() - .unwrap(); - let right: [u8; 32] = hex::decode("b36010eb285c154a8cd63084acbe7eac0c4d625ab4e1a76e624a8798cb63497b") - .unwrap() - .try_into() - .unwrap(); - - let hash = hash_blake2b_pair(&NODE_HASH_PREFIX, &left, &right); - let expected = H256::from("72b0762b382d4c251af5ae25b6777d908726d75962e5224f98d7f619bb39515d"); - assert_eq!(hash, expected) -} - -#[test] -fn test_create_ed25519_identifier() { - let mut ed25519_identifier: [u8; 16] = [0; 16]; - - let bytes = "ed25519".as_bytes(); - for (i, &byte) in bytes.iter().enumerate() { - ed25519_identifier[i] = byte; - } - assert_eq!(ed25519_identifier, ED25519_IDENTIFIER); -} - -#[test] -fn test_timelock_leaf() { - let hash = timelock_leaf(0); - let expected = H256::from(STANDARD_TIMELOCK_BLAKE2B_HASH); - assert_eq!(hash, expected) -} - -#[test] -fn test_sigs_required_leaf() { - let hash = sigs_required_leaf(1u64); - let expected = H256::from(STANDARD_SIGS_REQUIRED_BLAKE2B_HASH); - assert_eq!(hash, expected) -} - -#[test] -fn test_hash_blake2b_single() { - let hash = hash_blake2b_single(&hex::decode("006564323535313900000000000000000020000000000000000102030000000000000000000000000000000000000000000000000000000000").unwrap()); - let expected = H256::from("21ce940603a2ee3a283685f6bfb4b122254894fd1ed3eb59434aadbf00c75d5b"); - assert_eq!(hash, expected) -} - -#[test] -fn test_public_key_leaf() { - let pubkey = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - - let hash = public_key_leaf(&pubkey); - let expected = H256::from("21ce940603a2ee3a283685f6bfb4b122254894fd1ed3eb59434aadbf00c75d5b"); - assert_eq!(hash, expected) -} diff --git a/mm2src/coins/sia/encoding.rs b/mm2src/coins/sia/encoding.rs deleted file mode 100644 index 5b6516101a..0000000000 --- a/mm2src/coins/sia/encoding.rs +++ /dev/null @@ -1,119 +0,0 @@ -use crate::sia::blake2b_internal::hash_blake2b_single; -use rpc::v1::types::H256; - -// https://github.com/SiaFoundation/core/blob/092850cc52d3d981b19c66cd327b5d945b3c18d3/types/encoding.go#L16 -// TODO go implementation limits this to 1024 bytes, should we? -#[derive(Default)] -pub struct Encoder { - pub buffer: Vec, -} - -impl Encoder { - pub fn reset(&mut self) { self.buffer.clear(); } - - /// writes a length-prefixed []byte to the underlying stream. - pub fn write_len_prefixed_bytes(&mut self, data: &[u8]) { - self.buffer.extend_from_slice(&data.len().to_le_bytes()); - self.buffer.extend_from_slice(data); - } - - pub fn write_slice(&mut self, data: &[u8]) { self.buffer.extend_from_slice(data); } - - pub fn write_u8(&mut self, u: u8) { self.buffer.extend_from_slice(&[u]) } - - pub fn write_u64(&mut self, u: u64) { self.buffer.extend_from_slice(&u.to_le_bytes()); } - - pub fn write_distinguisher(&mut self, p: &str) { self.buffer.extend_from_slice(format!("sia/{}|", p).as_bytes()); } - - pub fn write_bool(&mut self, b: bool) { self.buffer.push(b as u8) } - - pub fn hash(&self) -> H256 { hash_blake2b_single(&self.buffer) } -} - -#[test] -fn test_encoder_default_hash() { - assert_eq!( - Encoder::default().hash(), - H256::from("0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8") - ) -} - -#[test] -fn test_encoder_write_bytes() { - let mut encoder = Encoder::default(); - encoder.write_len_prefixed_bytes(&[1, 2, 3, 4]); - assert_eq!( - encoder.hash(), - H256::from("d4a72b52e2e1f40e20ee40ea6d5080a1b1f76164786defbb7691a4427f3388f5") - ); -} - -#[test] -fn test_encoder_write_u8() { - let mut encoder = Encoder::default(); - encoder.write_u8(1); - assert_eq!( - encoder.hash(), - H256::from("ee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25") - ); -} - -#[test] -fn test_encoder_write_u64() { - let mut encoder = Encoder::default(); - encoder.write_u64(1); - assert_eq!( - encoder.hash(), - H256::from("1dbd7d0b561a41d23c2a469ad42fbd70d5438bae826f6fd607413190c37c363b") - ); -} - -#[test] -fn test_encoder_write_distiguisher() { - let mut encoder = Encoder::default(); - encoder.write_distinguisher("test"); - assert_eq!( - encoder.hash(), - H256::from("25fb524721bf98a9a1233a53c40e7e198971b003bf23c24f59d547a1bb837f9c") - ); -} - -#[test] -fn test_encoder_write_bool() { - let mut encoder = Encoder::default(); - encoder.write_bool(true); - assert_eq!( - encoder.hash(), - H256::from("ee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25") - ); -} - -#[test] -fn test_encoder_reset() { - let mut encoder = Encoder::default(); - encoder.write_bool(true); - assert_eq!( - encoder.hash(), - H256::from("ee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25") - ); - - encoder.reset(); - encoder.write_bool(false); - assert_eq!( - encoder.hash(), - H256::from("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314") - ); -} - -#[test] -fn test_encoder_complex() { - let mut encoder = Encoder::default(); - encoder.write_distinguisher("test"); - encoder.write_bool(true); - encoder.write_u8(1); - encoder.write_len_prefixed_bytes(&[1, 2, 3, 4]); - assert_eq!( - encoder.hash(), - H256::from("b66d7a9bef9fb303fe0e41f6b5c5af410303e428c4ff9231f6eb381248693221") - ); -} diff --git a/mm2src/coins/sia/http_client.rs b/mm2src/coins/sia/http_client.rs deleted file mode 100644 index 774dcd08e1..0000000000 --- a/mm2src/coins/sia/http_client.rs +++ /dev/null @@ -1,208 +0,0 @@ -use crate::sia::address::Address; -use crate::sia::SiaHttpConf; -use base64::engine::general_purpose::STANDARD as BASE64; -use base64::Engine as _; // required for .encode() method -use core::fmt::Display; -use core::time::Duration; -use mm2_number::MmNumber; -use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION}; -use reqwest::{Client, Error, Url}; -use serde::de::DeserializeOwned; -use std::ops::Deref; -use std::sync::Arc; - -#[cfg(test)] use std::str::FromStr; - -const ENDPOINT_CONSENSUS_TIP: &str = "api/consensus/tip"; - -/// HTTP(s) client for Sia-protocol coins -#[derive(Debug)] -pub struct SiaHttpClientImpl { - /// Name of coin the http client is intended to work with - pub coin_ticker: String, - /// The uri to send requests to - pub uri: String, - /// Value of Authorization header password, e.g. "Basic base64(:password)" - pub auth: String, -} - -#[derive(Clone, Debug)] -pub struct SiaApiClient(pub Arc); - -impl Deref for SiaApiClient { - type Target = SiaApiClientImpl; - fn deref(&self) -> &SiaApiClientImpl { &self.0 } -} - -impl SiaApiClient { - pub fn new(_coin_ticker: &str, http_conf: SiaHttpConf) -> Result { - let new_arc = SiaApiClientImpl::new(http_conf.url, &http_conf.auth)?; - Ok(SiaApiClient(Arc::new(new_arc))) - } -} - -#[derive(Debug)] -pub struct SiaApiClientImpl { - client: Client, - base_url: Url, -} - -// this is neccesary to show the URL in error messages returned to the user -// this can be removed in favor of using ".with_url()" once reqwest is updated to v0.11.23 -#[derive(Debug)] -pub struct ReqwestErrorWithUrl { - error: Error, - url: Url, -} - -impl Display for ReqwestErrorWithUrl { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "Error: {}, URL: {}", self.error, self.url) - } -} - -#[derive(Debug, Display)] -pub enum SiaApiClientError { - Timeout(String), - BuildError(String), - ApiUnreachable(String), - ReqwestError(ReqwestErrorWithUrl), - UrlParse(url::ParseError), -} - -impl From for String { - fn from(e: SiaApiClientError) -> Self { format!("{:?}", e) } -} - -async fn fetch_and_parse(client: &Client, url: Url) -> Result { - client - .get(url.clone()) - .send() - .await - .map_err(|e| { - SiaApiClientError::ReqwestError(ReqwestErrorWithUrl { - error: e, - url: url.clone(), - }) - })? - .json::() - .await - .map_err(|e| SiaApiClientError::ReqwestError(ReqwestErrorWithUrl { error: e, url })) -} - -// https://github.com/SiaFoundation/core/blob/4e46803f702891e7a83a415b7fcd7543b13e715e/types/types.go#L181 -#[derive(Deserialize, Serialize, Debug)] -pub struct GetConsensusTipResponse { - pub height: u64, - pub id: String, // TODO this can match "BlockID" type -} - -// https://github.com/SiaFoundation/walletd/blob/9574e69ff0bf84de1235b68e78db2a41d5e27516/api/api.go#L36 -// https://github.com/SiaFoundation/walletd/blob/9574e69ff0bf84de1235b68e78db2a41d5e27516/wallet/wallet.go#L25 -#[derive(Deserialize, Serialize, Debug)] -pub struct GetAddressesBalanceResponse { - pub siacoins: MmNumber, - #[serde(rename = "immatureSiacoins")] - pub immature_siacoins: MmNumber, - pub siafunds: u64, -} - -impl SiaApiClientImpl { - fn new(base_url: Url, password: &str) -> Result { - let mut headers = HeaderMap::new(); - let auth_value = format!("Basic {}", BASE64.encode(format!(":{}", password))); - headers.insert( - AUTHORIZATION, - HeaderValue::from_str(&auth_value).map_err(|e| SiaApiClientError::BuildError(e.to_string()))?, - ); - - let client = Client::builder() - .default_headers(headers) - .timeout(Duration::from_secs(10)) // TODO make this configurable - .build() - .map_err(|e| { - SiaApiClientError::ReqwestError(ReqwestErrorWithUrl { - error: e, - url: base_url.clone(), - }) - })?; - Ok(SiaApiClientImpl { client, base_url }) - } - - pub async fn get_consensus_tip(&self) -> Result { - let base_url = self.base_url.clone(); - let endpoint_url = base_url - .join(ENDPOINT_CONSENSUS_TIP) - .map_err(SiaApiClientError::UrlParse)?; - - fetch_and_parse::(&self.client, endpoint_url).await - } - - pub async fn get_addresses_balance( - &self, - address: &Address, - ) -> Result { - self.get_addresses_balance_str(&address.str_without_prefix()).await - } - - // use get_addresses_balance whenever possible to rely on Address deserialization - pub async fn get_addresses_balance_str( - &self, - address: &str, - ) -> Result { - let base_url = self.base_url.clone(); - - let endpoint_path = format!("api/addresses/{}/balance", address); - let endpoint_url = base_url.join(&endpoint_path).map_err(SiaApiClientError::UrlParse)?; - - fetch_and_parse::(&self.client, endpoint_url).await - } - - pub async fn get_height(&self) -> Result { - let resp = self.get_consensus_tip().await?; - Ok(resp.height) - } -} - -#[tokio::test] -#[ignore] -async fn test_api_client_timeout() { - let api_client = SiaApiClientImpl::new(Url::parse("http://foo").unwrap(), "password").unwrap(); - let result = api_client.get_consensus_tip().await; - assert!(matches!(result, Err(SiaApiClientError::Timeout(_)))); -} - -// TODO all of the following must be adapted to use Docker Sia node -#[tokio::test] -#[ignore] -async fn test_api_client_invalid_auth() { - let api_client = SiaApiClientImpl::new(Url::parse("http://127.0.0.1:9980").unwrap(), "password").unwrap(); - let result = api_client.get_consensus_tip().await; - assert!(matches!(result, Err(SiaApiClientError::BuildError(_)))); -} - -// TODO must be adapted to use Docker Sia node -#[tokio::test] -#[ignore] -async fn test_api_client() { - let api_client = SiaApiClientImpl::new(Url::parse("http://127.0.0.1:9980").unwrap(), "password").unwrap(); - let _result = api_client.get_consensus_tip().await.unwrap(); -} - -#[tokio::test] -#[ignore] -async fn test_api_get_addresses_balance() { - let api_client = SiaApiClientImpl::new(Url::parse("http://127.0.0.1:9980").unwrap(), "password").unwrap(); - let address = - Address::from_str("addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f").unwrap(); - let result = api_client.get_addresses_balance(&address).await.unwrap(); - println!("ret {:?}", result); -} - -#[tokio::test] -#[ignore] -async fn test_api_get_addresses_balance_invalid() { - let api_client = SiaApiClientImpl::new(Url::parse("http://127.0.0.1:9980").unwrap(), "password").unwrap(); - let result = api_client.get_addresses_balance_str("what").await.unwrap(); - println!("ret {:?}", result); -} diff --git a/mm2src/coins/sia/spend_policy.rs b/mm2src/coins/sia/spend_policy.rs deleted file mode 100644 index 6c28f7250a..0000000000 --- a/mm2src/coins/sia/spend_policy.rs +++ /dev/null @@ -1,322 +0,0 @@ -use crate::sia::address::Address; -use crate::sia::blake2b_internal::{public_key_leaf, sigs_required_leaf, standard_unlock_hash, timelock_leaf, - Accumulator, ED25519_IDENTIFIER}; -use crate::sia::encoding::Encoder; -use ed25519_dalek::PublicKey; -use rpc::v1::types::H256; - -#[cfg(test)] use std::str::FromStr; - -const POLICY_VERSION: u8 = 1u8; - -#[derive(Debug, Clone)] -pub enum SpendPolicy { - Above(u64), - After(u64), - PublicKey(PublicKey), - Hash(H256), - Threshold(PolicyTypeThreshold), - Opaque(Address), - UnlockConditions(PolicyTypeUnlockConditions), // For v1 compatibility -} - -impl SpendPolicy { - pub fn to_u8(&self) -> u8 { - match self { - SpendPolicy::Above(_) => 1, - SpendPolicy::After(_) => 2, - SpendPolicy::PublicKey(_) => 3, - SpendPolicy::Hash(_) => 4, - SpendPolicy::Threshold(_) => 5, - SpendPolicy::Opaque(_) => 6, - SpendPolicy::UnlockConditions(_) => 7, - } - } - - pub fn encode(&self) -> Encoder { - let mut encoder = Encoder::default(); - encoder.write_u8(POLICY_VERSION); - encoder.write_slice(&self.encode_wo_prefix().buffer); - encoder - } - - pub fn encode_wo_prefix(&self) -> Encoder { - let mut encoder = Encoder::default(); - let opcode = self.to_u8(); - match self { - SpendPolicy::Above(height) => { - encoder.write_u8(opcode); - encoder.write_u64(*height); - }, - SpendPolicy::After(time) => { - encoder.write_u8(opcode); - encoder.write_u64(*time); - }, - SpendPolicy::PublicKey(pubkey) => { - encoder.write_u8(opcode); - encoder.write_slice(&pubkey.to_bytes()); - }, - SpendPolicy::Hash(hash) => { - encoder.write_u8(opcode); - encoder.write_slice(&hash.0); - }, - SpendPolicy::Threshold(PolicyTypeThreshold { n, of }) => { - encoder.write_u8(opcode); - encoder.write_u8(*n); - encoder.write_u8(of.len() as u8); - for policy in of { - encoder.write_slice(&policy.encode_wo_prefix().buffer); - } - }, - SpendPolicy::Opaque(p) => { - encoder.write_u8(opcode); - encoder.write_slice(&p.0 .0); - }, - SpendPolicy::UnlockConditions(PolicyTypeUnlockConditions(unlock_condition)) => { - encoder.write_u8(opcode); - encoder.write_u64(unlock_condition.timelock); - encoder.write_u64(unlock_condition.pubkeys.len() as u64); - for pubkey in &unlock_condition.pubkeys { - encoder.write_slice(&ED25519_IDENTIFIER); - encoder.write_slice(&pubkey.to_bytes()); - } - encoder.write_u64(unlock_condition.sigs_required); - }, - } - encoder - } - - fn address(&self) -> Address { - if let SpendPolicy::UnlockConditions(PolicyTypeUnlockConditions(unlock_condition)) = self { - return unlock_condition.address(); - } - let mut encoder = Encoder::default(); - encoder.write_distinguisher("address"); - - // if self is a threshold policy, we need to convert all of its subpolicies to opaque - let mut new_policy = self.clone(); - if let SpendPolicy::Threshold(ref mut p) = new_policy { - p.of = p.of.iter().map(SpendPolicy::opaque).collect(); - } - - let encoded_policy = new_policy.encode(); - encoder.write_slice(&encoded_policy.buffer); - Address(encoder.hash()) - } - - pub fn above(height: u64) -> Self { SpendPolicy::Above(height) } - - pub fn after(time: u64) -> Self { SpendPolicy::After(time) } - - pub fn public_key(pk: PublicKey) -> Self { SpendPolicy::PublicKey(pk) } - - pub fn hash(h: H256) -> Self { SpendPolicy::Hash(h) } - - pub fn threshold(n: u8, of: Vec) -> Self { SpendPolicy::Threshold(PolicyTypeThreshold { n, of }) } - - pub fn opaque(p: &SpendPolicy) -> Self { SpendPolicy::Opaque(p.address()) } - - pub fn anyone_can_spend() -> Self { SpendPolicy::threshold(0, vec![]) } -} - -#[derive(Debug, Clone)] -pub struct PolicyTypeThreshold { - pub n: u8, - pub of: Vec, -} - -// Compatibility with Sia's "UnlockConditions" -#[derive(Debug, Clone)] -pub struct PolicyTypeUnlockConditions(UnlockCondition); - -#[derive(Debug, Clone)] -pub struct UnlockCondition { - pubkeys: Vec, - timelock: u64, - sigs_required: u64, -} - -impl UnlockCondition { - pub fn new(pubkeys: Vec, timelock: u64, sigs_required: u64) -> Self { - // TODO check go implementation to see if there should be limitations or checks imposed here - UnlockCondition { - pubkeys, - timelock, - sigs_required, - } - } - - pub fn unlock_hash(&self) -> H256 { - // almost all UnlockConditions are standard, so optimize for that case - if self.timelock == 0 && self.pubkeys.len() == 1 && self.sigs_required == 1 { - return standard_unlock_hash(&self.pubkeys[0]); - } - - let mut accumulator = Accumulator::default(); - - accumulator.add_leaf(timelock_leaf(self.timelock)); - - for pubkey in &self.pubkeys { - accumulator.add_leaf(public_key_leaf(pubkey)); - } - - accumulator.add_leaf(sigs_required_leaf(self.sigs_required)); - accumulator.root() - } - - pub fn address(&self) -> Address { Address(self.unlock_hash()) } -} - -#[test] -fn test_unlock_condition_unlock_hash_standard() { - let pubkey = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - let unlock_condition = UnlockCondition::new(vec![pubkey], 0, 1); - - let hash = unlock_condition.unlock_hash(); - let expected = H256::from("72b0762b382d4c251af5ae25b6777d908726d75962e5224f98d7f619bb39515d"); - assert_eq!(hash, expected); - - let hash = standard_unlock_hash(&pubkey); - assert_eq!(hash, expected); -} - -#[test] -fn test_unlock_condition_unlock_hash_2of2_multisig() { - let pubkey = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - let pubkey2 = PublicKey::from_bytes( - &hex::decode("0101010000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - let unlock_condition = UnlockCondition::new(vec![pubkey, pubkey2], 0, 2); - - let hash = unlock_condition.unlock_hash(); - let expected = H256::from("1e94357817d236167e54970a8c08bbd41b37bfceeeb52f6c1ce6dd01d50ea1e7"); - assert_eq!(hash, expected); -} - -#[test] -fn test_unlock_condition_unlock_hash_1of2_multisig() { - let pubkey = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - let pubkey2 = PublicKey::from_bytes( - &hex::decode("0101010000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - let unlock_condition = UnlockCondition::new(vec![pubkey, pubkey2], 0, 1); - - let hash = unlock_condition.unlock_hash(); - let expected = H256::from("d7f84e3423da09d111a17f64290c8d05e1cbe4cab2b6bed49e3a4d2f659f0585"); - assert_eq!(hash, expected); -} - -#[test] -fn test_spend_policy_encode_above() { - let policy = SpendPolicy::above(1); - - let hash = policy.encode().hash(); - let expected = H256::from("bebf6cbdfb440a92e3e5d832ac30fe5d226ff6b352ed3a9398b7d35f086a8ab6"); - assert_eq!(hash, expected); - - let address = policy.address(); - let expected = - Address::from_str("addr:188b997bb99dee13e95f92c3ea150bd76b3ec72e5ba57b0d57439a1a6e2865e9b25ea5d1825e").unwrap(); - assert_eq!(address, expected); -} - -#[test] -fn test_spend_policy_encode_after() { - let policy = SpendPolicy::after(1); - - let hash = policy.encode().hash(); - let expected = H256::from("07b0f28eafd87a082ad11dc4724e1c491821260821a30bec68254444f97d9311"); - assert_eq!(hash, expected); - - let address = policy.address(); - let expected = - Address::from_str("addr:60c74e0ce5cede0f13f83b0132cb195c995bc7688c9fac34bbf2b14e14394b8bbe2991bc017f").unwrap(); - assert_eq!(address, expected); -} - -#[test] -fn test_spend_policy_encode_pubkey() { - let pubkey = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - let policy = SpendPolicy::PublicKey(pubkey); - - let hash = policy.encode().hash(); - let expected = H256::from("4355c8f80f6e5a98b70c9c2f9a22f17747989b4744783c90439b2b034f698bfe"); - assert_eq!(hash, expected); - - let address = policy.address(); - let expected = - Address::from_str("addr:55a7793237722c6df8222fd512063cb74228085ef1805c5184713648c159b919ac792fbad0e1").unwrap(); - assert_eq!(address, expected); -} - -#[test] -fn test_spend_policy_encode_hash() { - let hash = H256::from("0102030000000000000000000000000000000000000000000000000000000000"); - let policy = SpendPolicy::Hash(hash); - - let encoded = policy.encode(); - let hash = encoded.hash(); - let expected = H256::from("9938967aefa6cbecc1f1620d2df5170d6811d4b2f47a879b621c1099a3b0628a"); - assert_eq!(hash, expected); - - let address = policy.address(); - let expected = - Address::from_str("addr:a4d5a06d8d3c2e45aa26627858ce8e881505ae3c9d122a1d282c7824163751936cffb347e435").unwrap(); - assert_eq!(address, expected); -} - -#[test] -fn test_spend_policy_encode_threshold() { - let policy = SpendPolicy::Threshold(PolicyTypeThreshold { - n: 1, - of: vec![SpendPolicy::above(1), SpendPolicy::after(1)], - }); - - let encoded = policy.encode(); - let hash = encoded.hash(); - let expected = H256::from("7d792df6cd0b5e0f795287b3bf4087bbcc4c1bd0c52880a552cdda3e5e33d802"); - assert_eq!(hash, expected); - - let address = policy.address(); - let expected = - Address::from_str("addr:4179b53aba165e46e4c85b3c8766bb758fb6f0bfa5721550b81981a3ec38efc460557dc1ded4").unwrap(); - assert_eq!(address, expected); -} - -#[test] -fn test_spend_policy_encode_unlock_condition() { - let pubkey = PublicKey::from_bytes( - &hex::decode("0102030000000000000000000000000000000000000000000000000000000000").unwrap(), - ) - .unwrap(); - let unlock_condition = UnlockCondition::new(vec![pubkey], 0, 1); - - let sub_policy = SpendPolicy::UnlockConditions(PolicyTypeUnlockConditions(unlock_condition)); - let base_address = sub_policy.address(); - let expected = - Address::from_str("addr:72b0762b382d4c251af5ae25b6777d908726d75962e5224f98d7f619bb39515dd64b9a56043a").unwrap(); - assert_eq!(base_address, expected); - - let policy = SpendPolicy::Threshold(PolicyTypeThreshold { - n: 1, - of: vec![sub_policy], - }); - let address = policy.address(); - let expected = - Address::from_str("addr:1498a58c843ce66740e52421632d67a0f6991ea96db1fc97c29e46f89ae56e3534078876331d").unwrap(); - assert_eq!(address, expected); -} diff --git a/mm2src/coins/sia.rs b/mm2src/coins/siacoin.rs similarity index 84% rename from mm2src/coins/sia.rs rename to mm2src/coins/siacoin.rs index 446a507070..4e77a74731 100644 --- a/mm2src/coins/sia.rs +++ b/mm2src/coins/siacoin.rs @@ -1,5 +1,5 @@ -use super::{CoinBalance, HistorySyncState, MarketCoinOps, MmCoin, RawTransactionFut, RawTransactionRequest, SwapOps, - TradeFee, TransactionEnum, TransactionFut}; +use super::{BalanceError, CoinBalance, HistorySyncState, MarketCoinOps, MmCoin, RawTransactionFut, + RawTransactionRequest, SwapOps, TradeFee, TransactionEnum, TransactionFut}; use crate::{coin_errors::MyAddressError, BalanceFut, CanRefundHtlc, CheckIfMyPaymentSentArgs, CoinFutSpawner, ConfirmPaymentInput, DexFee, FeeApproxStage, FoundSwapTxSpend, MakerSwapTakerCoin, MmCoinEnum, NegotiateSwapContractAddrErr, PaymentInstructionArgs, PaymentInstructions, PaymentInstructionsErr, @@ -14,25 +14,22 @@ use crate::{coin_errors::MyAddressError, BalanceFut, CanRefundHtlc, CheckIfMyPay WithdrawRequest}; use async_trait::async_trait; use common::executor::AbortedError; +pub use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature}; use futures::{FutureExt, TryFutureExt}; use futures01::Future; use keys::KeyPair; use mm2_core::mm_ctx::MmArc; use mm2_err_handle::prelude::*; -use mm2_number::{BigDecimal, MmNumber}; +use mm2_number::{BigDecimal, BigInt, MmNumber}; use rpc::v1::types::Bytes as BytesJson; use serde_json::Value as Json; use std::ops::Deref; use std::sync::Arc; -use url::Url; -pub mod address; -use address::v1_standard_address_from_pubkey; -pub mod blake2b_internal; -pub mod encoding; -pub mod http_client; -use http_client::{SiaApiClient, SiaApiClientError}; -pub mod spend_policy; +use sia_rust::http_client::{SiaApiClient, SiaApiClientError, SiaHttpConf}; +use sia_rust::spend_policy::SpendPolicy; + +pub mod sia_hd_wallet; #[derive(Clone)] pub struct SiaCoin(SiaArc); @@ -54,12 +51,6 @@ pub struct SiaCoinConf { pub foo: u32, } -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct SiaHttpConf { - pub url: Url, - pub auth: String, -} - // TODO see https://github.com/KomodoPlatform/komodo-defi-framework/pull/2086#discussion_r1521660384 // for additional fields needed #[derive(Clone, Debug, Deserialize, Serialize)] @@ -93,7 +84,7 @@ impl<'a> SiaConfBuilder<'a> { pub struct SiaCoinFields { /// SIA coin config pub conf: SiaCoinConf, - pub priv_key_policy: PrivKeyPolicy, + pub priv_key_policy: PrivKeyPolicy, /// HTTP(s) client pub http_client: SiaApiClient, } @@ -118,7 +109,7 @@ pub struct SiaCoinBuilder<'a> { ctx: &'a MmArc, ticker: &'a str, conf: &'a Json, - key_pair: ed25519_dalek::Keypair, + key_pair: Keypair, params: &'a SiaCoinActivationParams, } @@ -127,7 +118,7 @@ impl<'a> SiaCoinBuilder<'a> { ctx: &'a MmArc, ticker: &'a str, conf: &'a Json, - key_pair: ed25519_dalek::Keypair, + key_pair: Keypair, params: &'a SiaCoinActivationParams, ) -> Self { SiaCoinBuilder { @@ -140,15 +131,22 @@ impl<'a> SiaCoinBuilder<'a> { } } -fn generate_keypair_from_slice(priv_key: &[u8]) -> Result { - let secret_key = ed25519_dalek::SecretKey::from_bytes(priv_key).map_err(SiaCoinBuildError::EllipticCurveError)?; - let public_key = ed25519_dalek::PublicKey::from(&secret_key); - Ok(ed25519_dalek::Keypair { +fn generate_keypair_from_slice(priv_key: &[u8]) -> Result { + let secret_key = SecretKey::from_bytes(priv_key).map_err(SiaCoinBuildError::EllipticCurveError)?; + let public_key = PublicKey::from(&secret_key); + Ok(Keypair { secret: secret_key, public: public_key, }) } +/// Convert hastings amount to siacoin amount +fn siacoin_from_hastings(hastings: u128) -> BigDecimal { + let hastings = BigInt::from(hastings); + let decimals = BigInt::from(10u128.pow(24)); + BigDecimal::from(hastings) / BigDecimal::from(decimals) +} + impl From for SiaCoinBuildError { fn from(e: SiaConfError) -> Self { SiaCoinBuildError::ConfError(e) } } @@ -174,8 +172,9 @@ impl<'a> SiaCoinBuilder<'a> { let conf = SiaConfBuilder::new(self.conf, self.ticker()).build()?; let sia_fields = SiaCoinFields { conf, - http_client: SiaApiClient::new(self.ticker(), self.params.http_conf.clone()) - .map_err(SiaCoinBuildError::ClientError)?, + http_client: SiaApiClient::new(self.params.http_conf.clone()) + .map_err(SiaCoinBuildError::ClientError) + .await?, priv_key_policy: PrivKeyPolicy::Iguana(self.key_pair), }; let sia_arc = SiaArc::new(sia_fields); @@ -306,9 +305,15 @@ impl MarketCoinOps for SiaCoin { ) .into()); }, + #[cfg(target_arch = "wasm32")] + PrivKeyPolicy::Metamask(_) => { + return Err(MyAddressError::UnexpectedDerivationMethod( + "Metamask not supported. Must use iguana seed.".to_string(), + ) + .into()); + }, }; - - let address = v1_standard_address_from_pubkey(&key_pair.public); + let address = SpendPolicy::PublicKey(key_pair.public).address(); Ok(address.to_string()) } @@ -323,14 +328,30 @@ impl MarketCoinOps for SiaCoin { } fn my_balance(&self) -> BalanceFut { + let coin = self.clone(); let fut = async move { + let my_address = match &coin.0.priv_key_policy { + PrivKeyPolicy::Iguana(key_pair) => SpendPolicy::PublicKey(key_pair.public).address(), + _ => { + return MmError::err(BalanceError::UnexpectedDerivationMethod( + UnexpectedDerivationMethod::ExpectedSingleAddress, + )) + }, + }; + let balance = coin + .0 + .http_client + .address_balance(my_address) + .await + .map_to_mm(|e| BalanceError::Transport(e.to_string()))?; Ok(CoinBalance { - spendable: BigDecimal::default(), - unspendable: BigDecimal::default(), + spendable: siacoin_from_hastings(balance.siacoins.to_u128()), + unspendable: siacoin_from_hastings(balance.immature_siacoins.to_u128()), }) }; Box::new(fut.boxed().compat()) } + fn base_coin_balance(&self) -> BalanceFut { unimplemented!() } fn platform_ticker(&self) -> &str { "FOO" } // TODO Alright @@ -360,7 +381,7 @@ impl MarketCoinOps for SiaCoin { fn current_block(&self) -> Box + Send> { let http_client = self.0.http_client.clone(); // Clone the client - let height_fut = async move { http_client.get_height().await.map_err(|e| e.to_string()) } + let height_fut = async move { http_client.current_height().await.map_err(|e| e.to_string()) } .boxed() // Make the future 'static by boxing .compat(); // Convert to a futures 0.1-compatible future @@ -596,3 +617,29 @@ impl WatcherOps for SiaCoin { unimplemented!() } } + +#[cfg(test)] +mod tests { + use super::*; + use mm2_number::BigDecimal; + use std::str::FromStr; + + #[test] + fn test_siacoin_from_hastings() { + let hastings = u128::MAX; + let siacoin = siacoin_from_hastings(hastings); + assert_eq!( + siacoin, + BigDecimal::from_str("340282366920938.463463374607431768211455").unwrap() + ); + + let hastings = 0; + let siacoin = siacoin_from_hastings(hastings); + assert_eq!(siacoin, BigDecimal::from_str("0").unwrap()); + + // Total supply of Siacoin + let hastings = 57769875000000000000000000000000000; + let siacoin = siacoin_from_hastings(hastings); + assert_eq!(siacoin, BigDecimal::from_str("57769875000").unwrap()); + } +} diff --git a/mm2src/coins/siacoin/sia_hd_wallet.rs b/mm2src/coins/siacoin/sia_hd_wallet.rs new file mode 100644 index 0000000000..4c6a288ef5 --- /dev/null +++ b/mm2src/coins/siacoin/sia_hd_wallet.rs @@ -0,0 +1,44 @@ +use crate::hd_wallet::{HDAccount, HDAddress, HDWallet}; +use bip32::{ExtendedPublicKey, PrivateKeyBytes, PublicKey as bip32PublicKey, PublicKeyBytes, Result as bip32Result}; +use sia_rust::types::Address; +use sia_rust::PublicKey; + +pub struct SiaPublicKey(pub PublicKey); + +pub type SiaHDAddress = HDAddress; +pub type SiaHDAccount = HDAccount; +pub type SiaHDWallet = HDWallet; +pub type Ed25519ExtendedPublicKey = ExtendedPublicKey; + +impl bip32PublicKey for SiaPublicKey { + fn from_bytes(_bytes: PublicKeyBytes) -> bip32Result { + todo!() + //Ok(secp256k1_ffi::PublicKey::from_slice(&bytes)?) + } + + fn to_bytes(&self) -> PublicKeyBytes { + todo!() + // self.serialize() + } + + fn derive_child(&self, _other: PrivateKeyBytes) -> bip32Result { + todo!() + // use secp256k1_ffi::{Secp256k1, VerifyOnly}; + // let engine = Secp256k1::::verification_only(); + + // let mut child_key = *self; + // child_key + // .add_exp_assign(&engine, &other) + // .map_err(|_| Error::Crypto)?; + + // Ok(child_key) + } +} + +// coin type 1991 +// path component 0x800007c7 + +#[test] +fn test_something() { + println!("This is a test"); +} diff --git a/mm2src/coins_activation/src/prelude.rs b/mm2src/coins_activation/src/prelude.rs index 236d19b1c1..d000170fa3 100644 --- a/mm2src/coins_activation/src/prelude.rs +++ b/mm2src/coins_activation/src/prelude.rs @@ -1,5 +1,5 @@ #[cfg(feature = "enable-sia")] -use coins::sia::SiaCoinActivationParams; +use coins::siacoin::SiaCoinActivationParams; use coins::utxo::UtxoActivationParams; use coins::z_coin::ZcoinActivationParams; use coins::{coin_conf, CoinBalance, CoinProtocol, DerivationMethodResponse, MmCoinEnum}; diff --git a/mm2src/coins_activation/src/sia_coin_activation.rs b/mm2src/coins_activation/src/sia_coin_activation.rs index 7dd7539f66..11c72955ab 100644 --- a/mm2src/coins_activation/src/sia_coin_activation.rs +++ b/mm2src/coins_activation/src/sia_coin_activation.rs @@ -7,8 +7,8 @@ use async_trait::async_trait; use coins::coin_balance::{CoinBalanceReport, IguanaWalletBalance}; use coins::coin_errors::MyAddressError; use coins::my_tx_history_v2::TxHistoryStorage; -use coins::sia::{sia_coin_from_conf_and_params, SiaCoin, SiaCoinActivationParams, SiaCoinBuildError, - SiaCoinProtocolInfo}; +use coins::siacoin::{sia_coin_from_conf_and_params, SiaCoin, SiaCoinActivationParams, SiaCoinBuildError, + SiaCoinProtocolInfo}; use coins::tx_history_storage::CreateTxHistoryStorageError; use coins::{BalanceError, CoinBalance, CoinProtocol, MarketCoinOps, PrivKeyBuildPolicy, RegisterCoinError}; use crypto::hw_rpc_task::{HwRpcTaskAwaitingStatus, HwRpcTaskUserAction}; diff --git a/mm2src/mm2_main/Cargo.toml b/mm2src/mm2_main/Cargo.toml index f3232ac91e..60c3e9aa62 100644 --- a/mm2src/mm2_main/Cargo.toml +++ b/mm2src/mm2_main/Cargo.toml @@ -19,11 +19,11 @@ track-ctx-pointer = ["common/track-ctx-pointer"] zhtlc-native-tests = ["coins/zhtlc-native-tests"] run-docker-tests = ["coins/run-docker-tests"] # TODO -enable-solana = [] +enable-solana = ["coins/enable-solana", "coins_activation/enable-solana"] default = [] trezor-udp = ["crypto/trezor-udp"] # use for tests to connect to trezor emulator over udp run-device-tests = [] -enable-sia = [] +enable-sia = ["coins/enable-sia", "coins_activation/enable-sia"] [dependencies] async-std = { version = "1.5", features = ["unstable"] } @@ -132,6 +132,8 @@ ethabi = { version = "17.0.0" } rlp = { version = "0.5" } ethcore-transaction = { git = "https://github.com/KomodoPlatform/mm2-parity-ethereum.git", rev = "mm2-v2.1.1" } rustc-hex = "2" +sia-rust = { git = "https://github.com/KomodoPlatform/sia-rust", rev = "9f188b80b3213bcb604e7619275251ce08fae808" } +url = { version = "2.2.2", features = ["serde"] } [build-dependencies] chrono = "0.4" diff --git a/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs b/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs index 5403392240..b9066bf540 100644 --- a/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs +++ b/mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs @@ -28,7 +28,7 @@ use coins::rpc_command::{account_balance::account_balance, init_scan_for_new_addresses::{cancel_scan_for_new_addresses, init_scan_for_new_addresses, init_scan_for_new_addresses_status}, init_withdraw::{cancel_withdraw, init_withdraw, withdraw_status, withdraw_user_action}}; -#[cfg(feature = "enable-sia")] use coins::sia::SiaCoin; +#[cfg(feature = "enable-sia")] use coins::siacoin::SiaCoin; use coins::tendermint::{TendermintCoin, TendermintToken}; use coins::utxo::bch::BchCoin; use coins::utxo::qtum::QtumCoin; diff --git a/mm2src/mm2_main/tests/docker_tests/docker_tests_common.rs b/mm2src/mm2_main/tests/docker_tests/docker_tests_common.rs index 4e294e6031..fe5d7f96d4 100644 --- a/mm2src/mm2_main/tests/docker_tests/docker_tests_common.rs +++ b/mm2src/mm2_main/tests/docker_tests/docker_tests_common.rs @@ -105,6 +105,10 @@ pub const UTXO_ASSET_DOCKER_IMAGE: &str = "docker.io/artempikulin/testblockchain pub const UTXO_ASSET_DOCKER_IMAGE_WITH_TAG: &str = "docker.io/artempikulin/testblockchain:multiarch"; pub const GETH_DOCKER_IMAGE: &str = "docker.io/ethereum/client-go"; pub const GETH_DOCKER_IMAGE_WITH_TAG: &str = "docker.io/ethereum/client-go:stable"; +#[allow(dead_code)] +pub const SIA_DOCKER_IMAGE: &str = "docker.io/alrighttt/walletd-komodo"; +#[allow(dead_code)] +pub const SIA_DOCKER_IMAGE_WITH_TAG: &str = "docker.io/alrighttt/walletd-komodo:latest"; pub const NUCLEUS_IMAGE: &str = "docker.io/komodoofficial/nucleusd"; pub const ATOM_IMAGE: &str = "docker.io/komodoofficial/gaiad"; @@ -371,6 +375,22 @@ pub fn geth_docker_node<'a>(docker: &'a Cli, ticker: &'static str, port: u16) -> } } +#[allow(dead_code)] +pub fn sia_docker_node<'a>(docker: &'a Cli, ticker: &'static str, port: u16) -> DockerNode<'a> { + let image = + GenericImage::new(SIA_DOCKER_IMAGE, "latest").with_env_var("WALLETD_API_PASSWORD", "password".to_string()); + let args = vec![]; + let image = RunnableImage::from((image, args)) + .with_mapped_port((port, port)) + .with_container_name("sia-docker"); + let container = docker.run(image); + DockerNode { + container, + ticker: ticker.into(), + port, + } +} + pub fn nucleus_node(docker: &'_ Cli, runtime_dir: PathBuf) -> DockerNode<'_> { let nucleus_node_runtime_dir = runtime_dir.join("nucleus-testnet-data"); assert!(nucleus_node_runtime_dir.exists()); diff --git a/mm2src/mm2_main/tests/docker_tests/mod.rs b/mm2src/mm2_main/tests/docker_tests/mod.rs index 2adfe3b2c9..685f1bcad8 100644 --- a/mm2src/mm2_main/tests/docker_tests/mod.rs +++ b/mm2src/mm2_main/tests/docker_tests/mod.rs @@ -4,6 +4,7 @@ mod docker_ordermatch_tests; mod docker_tests_inner; mod eth_docker_tests; pub mod qrc20_tests; +#[cfg(feature = "enable-sia")] mod sia_docker_tests; mod slp_tests; #[cfg(feature = "enable-solana")] mod solana_tests; mod swap_proto_v2_tests; diff --git a/mm2src/mm2_main/tests/docker_tests/sia_docker_tests.rs b/mm2src/mm2_main/tests/docker_tests/sia_docker_tests.rs new file mode 100644 index 0000000000..b8546aa218 --- /dev/null +++ b/mm2src/mm2_main/tests/docker_tests/sia_docker_tests.rs @@ -0,0 +1,118 @@ +use common::block_on; +use sia_rust::http_client::{SiaApiClient, SiaApiClientError, SiaHttpConf}; +use sia_rust::http_endpoints::{AddressBalanceRequest, AddressUtxosRequest, ConsensusTipRequest, TxpoolBroadcastRequest}; +use sia_rust::spend_policy::SpendPolicy; +use sia_rust::transaction::{SiacoinOutput, V2TransactionBuilder}; +use sia_rust::types::{Address, Currency}; +use sia_rust::{Keypair, PublicKey, SecretKey}; +use std::process::Command; +use std::str::FromStr; +use url::Url; + +#[cfg(test)] +fn mine_blocks(n: u64, addr: &Address) { + Command::new("docker") + .arg("exec") + .arg("sia-docker") + .arg("walletd") + .arg("mine") + .arg(format!("-addr={}", addr)) + .arg(format!("-n={}", n)) + .status() + .expect("Failed to execute docker command"); +} + +#[test] +fn test_sia_new_client() { + let conf = SiaHttpConf { + url: Url::parse("http://localhost:9980/").unwrap(), + password: "password".to_string(), + }; + let _api_client = block_on(SiaApiClient::new(conf)).unwrap(); +} + +#[test] +fn test_sia_client_bad_auth() { + let conf = SiaHttpConf { + url: Url::parse("http://localhost:9980/").unwrap(), + password: "foo".to_string(), + }; + let result = block_on(SiaApiClient::new(conf)); + assert!(matches!(result, Err(SiaApiClientError::UnexpectedHttpStatus(401)))); +} + +#[test] +fn test_sia_client_consensus_tip() { + let conf = SiaHttpConf { + url: Url::parse("http://localhost:9980/").unwrap(), + password: "password".to_string(), + }; + let api_client = block_on(SiaApiClient::new(conf)).unwrap(); + let _response = block_on(api_client.dispatcher(ConsensusTipRequest)).unwrap(); +} + +// This test likely needs to be removed because mine_blocks has possibility of interfering with other async tests +// related to block height +#[test] +fn test_sia_client_address_balance() { + let conf = SiaHttpConf { + url: Url::parse("http://localhost:9980/").unwrap(), + password: "password".to_string(), + }; + let api_client = block_on(SiaApiClient::new(conf)).unwrap(); + + let address = + Address::from_str("addr:591fcf237f8854b5653d1ac84ae4c107b37f148c3c7b413f292d48db0c25a8840be0653e411f").unwrap(); + mine_blocks(10, &address); + + let request = AddressBalanceRequest { address }; + let response = block_on(api_client.dispatcher(request)).unwrap(); + + let expected = Currency::new(12919594847110692864, 54210108624275221); + assert_eq!(response.siacoins, expected); + assert_eq!(expected.to_u128(), 1000000000000000000000000000000000000); +} + +#[test] +fn test_sia_client_build_tx() { + let conf = SiaHttpConf { + url: Url::parse("http://localhost:9980/").unwrap(), + password: "password".to_string(), + }; + let api_client = block_on(SiaApiClient::new(conf)).unwrap(); + let sk: SecretKey = SecretKey::from_bytes( + &hex::decode("0100000000000000000000000000000000000000000000000000000000000000").unwrap(), + ) + .unwrap(); + let pk: PublicKey = (&sk).into(); + let keypair = Keypair { public: pk, secret: sk }; + let spend_policy = SpendPolicy::PublicKey(pk); + + let address = spend_policy.address(); + + mine_blocks(201, &address); + + let utxos = block_on(api_client.dispatcher(AddressUtxosRequest { + address: address.clone(), + })) + .unwrap(); + let spend_this = utxos[0].clone(); + let vin = spend_this.clone(); + println!("utxo[0]: {:?}", spend_this); + let vout = SiacoinOutput { + value: spend_this.siacoin_output.value, + address, + }; + let tx = V2TransactionBuilder::new(0u64.into()) + .add_siacoin_input(vin, spend_policy) + .add_siacoin_output(vout) + .sign_simple(vec![&keypair]) + .unwrap() + .build(); + + let req = TxpoolBroadcastRequest { + transactions: vec![], + v2transactions: vec![tx], + }; + let _response = block_on(api_client.dispatcher(req)).unwrap(); +} diff --git a/mm2src/mm2_main/tests/docker_tests_main.rs b/mm2src/mm2_main/tests/docker_tests_main.rs index 5225f1b029..5f0045ea33 100644 --- a/mm2src/mm2_main/tests/docker_tests_main.rs +++ b/mm2src/mm2_main/tests/docker_tests_main.rs @@ -173,6 +173,7 @@ fn remove_docker_containers(name: &str) { .expect("Failed to execute docker command"); } } + fn prepare_runtime_dir() -> std::io::Result { let project_root = { let mut current_dir = std::env::current_dir().unwrap(); diff --git a/mm2src/mm2_main/tests/docker_tests_sia_unique.rs b/mm2src/mm2_main/tests/docker_tests_sia_unique.rs new file mode 100644 index 0000000000..521da60e01 --- /dev/null +++ b/mm2src/mm2_main/tests/docker_tests_sia_unique.rs @@ -0,0 +1,106 @@ +#![allow(unused_imports, dead_code)] +#![cfg(feature = "enable-sia")] +#![feature(async_closure)] +#![feature(custom_test_frameworks)] +#![feature(test)] +#![test_runner(docker_tests_runner)] +#![feature(drain_filter)] +#![feature(hash_raw_entry)] +#![cfg(not(target_arch = "wasm32"))] + +#[cfg(test)] +#[macro_use] +extern crate common; +#[cfg(test)] +#[macro_use] +extern crate gstuff; +#[cfg(test)] +#[macro_use] +extern crate lazy_static; +#[cfg(test)] +#[macro_use] +extern crate serde_json; +#[cfg(test)] extern crate ser_error_derive; +#[cfg(test)] extern crate test; + +use std::env; +use std::io::{BufRead, BufReader}; +use std::path::PathBuf; +use std::process::Command; +use test::{test_main, StaticBenchFn, StaticTestFn, TestDescAndFn}; +use testcontainers::clients::Cli; + +mod docker_tests; +use docker_tests::docker_tests_common::*; + +#[allow(dead_code)] mod integration_tests_common; + +/// Custom test runner intended to initialize the SIA coin daemon in a Docker container. +pub fn docker_tests_runner(tests: &[&TestDescAndFn]) { + let docker = Cli::default(); + let mut containers = vec![]; + + let skip_docker_tests_runner = std::env::var("SKIP_DOCKER_TESTS_RUNNER") + .map(|v| v == "1") + .unwrap_or(false); + + if !skip_docker_tests_runner { + const IMAGES: &[&str] = &[SIA_DOCKER_IMAGE_WITH_TAG]; + + for image in IMAGES { + pull_docker_image(image); + remove_docker_containers(image); + } + + let sia_node = sia_docker_node(&docker, "SIA", 9980); + println!("ran container?"); + containers.push(sia_node); + } + // detect if docker is installed + // skip the tests that use docker if not installed + let owned_tests: Vec<_> = tests + .iter() + .map(|t| match t.testfn { + StaticTestFn(f) => TestDescAndFn { + testfn: StaticTestFn(f), + desc: t.desc.clone(), + }, + StaticBenchFn(f) => TestDescAndFn { + testfn: StaticBenchFn(f), + desc: t.desc.clone(), + }, + _ => panic!("non-static tests passed to lp_coins test runner"), + }) + .collect(); + let args: Vec = env::args().collect(); + test_main(&args, owned_tests, None); +} + +fn pull_docker_image(name: &str) { + Command::new("docker") + .arg("pull") + .arg(name) + .status() + .expect("Failed to execute docker command"); +} + +fn remove_docker_containers(name: &str) { + let stdout = Command::new("docker") + .arg("ps") + .arg("-f") + .arg(format!("ancestor={}", name)) + .arg("-q") + .output() + .expect("Failed to execute docker command"); + + let reader = BufReader::new(stdout.stdout.as_slice()); + let ids: Vec<_> = reader.lines().map(|line| line.unwrap()).collect(); + if !ids.is_empty() { + Command::new("docker") + .arg("rm") + .arg("-f") + .args(ids) + .status() + .expect("Failed to execute docker command"); + } +}