diff --git a/Cargo.lock b/Cargo.lock index d2752508a..128e0505d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,12 +178,12 @@ dependencies = [ "lazycell", "peeking_take_while", "prettyplease", - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "regex", "rustc-hash", "shlex", - "syn 2.0.18", + "syn 2.0.41", ] [[package]] @@ -212,7 +212,7 @@ dependencies = [ [[package]] name = "bitcoin" -version = "6.13.0" +version = "6.14.0" dependencies = [ "bitcoin 0.29.0", "bitcoin 0.29.2", @@ -231,6 +231,7 @@ dependencies = [ "serde", "serde_json", "simple_logger", + "thiserror", "tiny-keccak 2.0.2", ] @@ -330,7 +331,7 @@ version = "1.0.3" dependencies = [ "bitcoin 0.29.0", "bitcoin 0.29.2", - "bitcoin 6.13.0", + "bitcoin 6.14.0", "chain_ids", "common", "database_utils", @@ -358,7 +359,7 @@ version = "1.0.2" dependencies = [ "bitcoin 0.29.0", "bitcoin 0.29.2", - "bitcoin 6.13.0", + "bitcoin 6.14.0", "chain_ids", "common", "database_utils", @@ -387,7 +388,7 @@ version = "1.1.0" dependencies = [ "bitcoin 0.29.0", "bitcoin 0.29.2", - "bitcoin 6.13.0", + "bitcoin 6.14.0", "chain_ids", "common", "database_utils", @@ -667,8 +668,8 @@ checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610" dependencies = [ "fnv", "ident_case", - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "strsim", "syn 1.0.109", ] @@ -680,7 +681,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835" dependencies = [ "darling_core", - "quote 1.0.28", + "quote 1.0.33", "syn 1.0.109", ] @@ -726,8 +727,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" dependencies = [ "convert_case", - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "rustc_version", "syn 1.0.109", ] @@ -964,8 +965,8 @@ name = "eosio-core-derive" version = "0.2.1" source = "git+https://github.com/bifrost-finance/rust-eos?rev=8e57843#8e57843f3ae5fd4b289f4c7841761ed3b6ce5c5e" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "syn 1.0.109", ] @@ -1241,8 +1242,8 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "syn 1.0.109", "synstructure", ] @@ -1259,7 +1260,7 @@ version = "6.10.3" dependencies = [ "bitcoin 0.29.0", "bitcoin 0.29.2", - "bitcoin 6.13.0", + "bitcoin 6.14.0", "common", "derive_more", "hex", @@ -1429,9 +1430,9 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", - "syn 2.0.18", + "proc-macro2 1.0.70", + "quote 1.0.33", + "syn 2.0.41", ] [[package]] @@ -1820,8 +1821,8 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11d7a9f6330b71fea57921c9b61c47ee6e84f72d394754eff6163ae67e7395eb" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "syn 1.0.109", ] @@ -2290,8 +2291,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1557010476e0595c9b568d16dcfb81b93cdeb157612726f5170d31aa707bed27" dependencies = [ "proc-macro-crate", - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "syn 1.0.109", ] @@ -2501,9 +2502,9 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39407670928234ebc5e6e580247dd567ad73a3578460c5990f9503df207e8f07" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", - "syn 2.0.18", + "proc-macro2 1.0.70", + "quote 1.0.33", + "syn 2.0.41", ] [[package]] @@ -2587,8 +2588,8 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b69d39aab54d069e7f2fe8cb970493e7834601ca2d8c65fd7bbd183578080d1" dependencies = [ - "proc-macro2 1.0.60", - "syn 2.0.18", + "proc-macro2 1.0.70", + "syn 2.0.41", ] [[package]] @@ -2634,8 +2635,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "syn 1.0.109", "version_check", ] @@ -2646,8 +2647,8 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "version_check", ] @@ -2662,9 +2663,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.60" +version = "1.0.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" dependencies = [ "unicode-ident", ] @@ -2686,11 +2687,11 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.28" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ - "proc-macro2 1.0.60", + "proc-macro2 1.0.70", ] [[package]] @@ -3233,9 +3234,9 @@ version = "1.0.164" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", - "syn 2.0.18", + "proc-macro2 1.0.70", + "quote 1.0.33", + "syn 2.0.41", ] [[package]] @@ -3278,8 +3279,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e182d6ec6f05393cc0e5ed1bf81ad6db3a8feedf8ee515ecdd369809bcce8082" dependencies = [ "darling", - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "syn 1.0.109", ] @@ -3301,8 +3302,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2881bccd7d60fb32dfa3d7b3136385312f8ad75e2674aab2852867a09790cae8" dependencies = [ "proc-macro-error", - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "rustversion", "syn 1.0.109", ] @@ -3468,8 +3469,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "339f799d8b549e3744c7ac7feb216383e4005d94bdb22561b3ab8f3b808ae9fb" dependencies = [ "heck", - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "syn 1.0.109", ] @@ -3502,19 +3503,19 @@ version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "unicode-ident", ] [[package]] name = "syn" -version = "2.0.18" +version = "2.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" +checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "unicode-ident", ] @@ -3524,8 +3525,8 @@ version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", + "proc-macro2 1.0.70", + "quote 1.0.33", "syn 1.0.109", "unicode-xid 0.2.4", ] @@ -3547,22 +3548,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.40" +version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" +checksum = "f11c217e1416d6f036b870f14e0413d480dbf28edbee1f877abaf0206af43bb7" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.40" +version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" +checksum = "01742297787513b79cf8e29d1056ede1313e2420b7b3b15d0a768b4921f549df" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", - "syn 2.0.18", + "proc-macro2 1.0.70", + "quote 1.0.33", + "syn 2.0.41", ] [[package]] @@ -3941,9 +3942,9 @@ dependencies = [ "bumpalo", "log", "once_cell", - "proc-macro2 1.0.60", - "quote 1.0.28", - "syn 2.0.18", + "proc-macro2 1.0.70", + "quote 1.0.33", + "syn 2.0.41", "wasm-bindgen-shared", ] @@ -3965,7 +3966,7 @@ version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" dependencies = [ - "quote 1.0.28", + "quote 1.0.33", "wasm-bindgen-macro-support", ] @@ -3975,9 +3976,9 @@ version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", - "syn 2.0.18", + "proc-macro2 1.0.70", + "quote 1.0.33", + "syn 2.0.41", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -4191,9 +4192,9 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ - "proc-macro2 1.0.60", - "quote 1.0.28", - "syn 2.0.18", + "proc-macro2 1.0.70", + "quote 1.0.33", + "syn 2.0.41", ] [[package]] diff --git a/common/bitcoin/Cargo.toml b/common/bitcoin/Cargo.toml index 451360af0..f91614e93 100644 --- a/common/bitcoin/Cargo.toml +++ b/common/bitcoin/Cargo.toml @@ -3,7 +3,7 @@ license = "MIT" publish = false name = "bitcoin" edition = "2021" -version = "6.13.0" +version = "6.14.0" readme = "README.md" rust-version = "1.56" keywords = ["provable", "defi", "crypto"] @@ -15,6 +15,7 @@ ltc = ["litecoin"] default = ["bitcoin"] [dependencies] +thiserror = "1.0.51" hex = { workspace = true } log = { workspace = true } paste = { workspace = true } diff --git a/common/bitcoin/src/btc_constants.rs b/common/bitcoin/src/btc_constants.rs index 4bc814ab7..518047b0f 100644 --- a/common/bitcoin/src/btc_constants.rs +++ b/common/bitcoin/src/btc_constants.rs @@ -24,6 +24,7 @@ pub const BTC_TX_VERSION: i32 = 1; pub const MAX_NUM_OUTPUTS: usize = 2; pub const BTC_NUM_DECIMALS: usize = 8; pub const BTC_PUB_KEY_SLICE_LENGTH: usize = 33; +pub(crate) const BTC_FEE_HARDCAP: u64 = 500_000; // NOTE: 0.005 btc pub const BTC_TX_LOCK_TIME: PackedLockTime = PackedLockTime::ZERO; pub const DEFAULT_BTC_SEQUENCE: Sequence = Sequence(4_294_967_295); // NOTE: 0xFFFFFFFF pub const BTC_CORE_IS_INITIALIZED_JSON: &str = "{btc_enclave_initialized:true}"; diff --git a/common/bitcoin/src/btc_transaction.rs b/common/bitcoin/src/btc_transaction.rs index 238dd8333..dc9533bc6 100644 --- a/common/bitcoin/src/btc_transaction.rs +++ b/common/bitcoin/src/btc_transaction.rs @@ -5,7 +5,7 @@ use crate::{ blockdata::transaction::{Transaction as BtcTransaction, TxIn as BtcUtxo}, Sighash, }, - btc_constants::{BTC_TX_LOCK_TIME, BTC_TX_VERSION, DUST_AMOUNT}, + btc_constants::{BTC_FEE_HARDCAP, BTC_TX_LOCK_TIME, BTC_TX_VERSION, DUST_AMOUNT}, btc_recipients_and_amounts::BtcRecipientsAndAmounts, btc_utils::{ create_new_pay_to_pub_key_hash_output, @@ -15,6 +15,7 @@ use crate::{ }, deposit_address_info::DepositAddressInfo, utxo_manager::BtcUtxosAndValues, + BitcoinError, BtcPrivateKey, }; @@ -44,6 +45,11 @@ pub fn create_signed_raw_btc_tx_for_n_input_n_outputs( lock_time: BTC_TX_LOCK_TIME, }; let fee = zero_change_tx.size() as u64 * sats_per_byte; + + if fee > BTC_FEE_HARDCAP { + return Err(BitcoinError::FeeHardCapExceeded(BTC_FEE_HARDCAP).to_string().into()); + }; + let utxo_total = utxos_and_values.sum(); info!("✔ UTXO(s) total: {}", utxo_total); info!("✔ Outgoing total: {}", total_to_spend); @@ -141,6 +147,8 @@ pub fn create_signed_raw_btc_tx_for_n_input_n_outputs( #[cfg(all(test, not(feature = "ltc")))] mod tests { + use common::AppError; + use super::*; use crate::{ btc_recipients_and_amounts::BtcRecipientAndAmount, @@ -235,4 +243,31 @@ mod tests { assert_eq!(result_hex, expected_result); assert_eq!(tx_id, expected_tx_id); } + + #[test] + fn should_error_if_fee_hard_cap_is_exceeded() { + let utxos_and_values = BtcUtxosAndValues::new(vec![ + get_sample_p2pkh_utxo_and_value_n(3).unwrap(), + get_sample_p2pkh_utxo_and_value_n(4).unwrap(), + ]); + let sats_per_byte = 3000; + let btc_private_key = get_sample_btc_private_key(); + let remainder_btc_address = SAMPLE_TARGET_BTC_ADDRESS; + let recipient_addresses_and_amounts = BtcRecipientsAndAmounts::new(vec![ + BtcRecipientAndAmount::new("mudzxCq9aCQ4Una9MmayvJVCF1Tj9fypiM", 666).unwrap(), + BtcRecipientAndAmount::new("mu1FFNnoiMytR5tKGXp6M1XhUZFQd3Mc8n", 1337).unwrap(), + ]); + let expected_error = format!("btc fee hard cap of {BTC_FEE_HARDCAP} exceeded"); + match create_signed_raw_btc_tx_for_n_input_n_outputs( + sats_per_byte, + recipient_addresses_and_amounts, + remainder_btc_address, + &btc_private_key, + utxos_and_values, + ) { + Ok(_) => panic!("should not have succeeded!"), + Err(AppError::Custom(e)) => assert_eq!(e, expected_error), + Err(e) => panic!("wrong error received: {e}"), + } + } } diff --git a/common/bitcoin/src/error.rs b/common/bitcoin/src/error.rs new file mode 100644 index 000000000..22f45754e --- /dev/null +++ b/common/bitcoin/src/error.rs @@ -0,0 +1,7 @@ +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum BitcoinError { + #[error("btc fee hard cap of {0} exceeded")] + FeeHardCapExceeded(u64), +} diff --git a/common/bitcoin/src/lib.rs b/common/bitcoin/src/lib.rs index 073eb042e..aab55a738 100644 --- a/common/bitcoin/src/lib.rs +++ b/common/bitcoin/src/lib.rs @@ -21,6 +21,7 @@ mod btc_utils; mod check_btc_parent_exists; mod core_initialization; mod deposit_address_info; +mod error; mod extract_utxos_from_p2pkh_txs; mod extract_utxos_from_p2sh_txs; mod extract_utxos_from_txs; @@ -94,6 +95,7 @@ pub use self::{ DepositAddressInfoVersion, DepositInfoHashMap, }, + error::BitcoinError, extract_utxos_from_p2pkh_txs::{ extract_utxos_from_p2pkh_tx, extract_utxos_from_p2pkh_txs,