From a6b6595a34ec448ad295b63403b99b531f8cd827 Mon Sep 17 00:00:00 2001 From: wiesche Date: Sat, 12 Sep 2026 19:40:11 +0200 Subject: [PATCH] share receiving contract check --- libwallet/src/api_impl/foreign.rs | 34 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/libwallet/src/api_impl/foreign.rs b/libwallet/src/api_impl/foreign.rs index 6012a1f..53ff439 100644 --- a/libwallet/src/api_impl/foreign.rs +++ b/libwallet/src/api_impl/foreign.rs @@ -246,6 +246,20 @@ where Ok(sl) } +// The foreign API only handles the receiving side +fn check_receiving_contract(net_change: Option, action: &str) -> Result<(), Error> { + let net_change = net_change.ok_or_else(|| { + Error::GenericError("Contract requires a net change (--send or --receive)".to_string()) + })?; + if net_change <= 0 { + return Err(Error::GenericError(format!( + "Can't {} a non-receiving contract from a foreign API.", + action + ))); + } + Ok(()) +} + /// Initialize a receive transaction contract pub fn contract_new( w: &mut WalletBackend, @@ -256,15 +270,7 @@ where C: NodeClient, K: Keychain, { - let net_change = args.setup_args.net_change.ok_or_else(|| { - Error::GenericError("Contract requires a net change (--send or --receive)".to_string()) - })?; - // The foreign API only handles the receiving side. - if net_change <= 0 { - return Err(Error::GenericError( - "Can't create a non-receiving contract from a foreign API.".to_string(), - )); - } + check_receiving_contract(args.setup_args.net_change, "create")?; owner_contract_new(w, keychain_mask, args) } @@ -279,15 +285,7 @@ where C: NodeClient, K: Keychain, { - let net_change = args.net_change.ok_or_else(|| { - Error::GenericError("Contract requires a net change (--send or --receive)".to_string()) - })?; - // The foreign API only handles the receiving side. - if net_change <= 0 { - return Err(Error::GenericError( - "Can't sign a non-receiving contract from a foreign API.".to_string(), - )); - } + check_receiving_contract(args.net_change, "sign")?; owner_contract_sign(w, keychain_mask, args, slate) }