Wrong Slatepack address fix (#773)

This commit is contained in:
ardocrat
2026-07-26 18:07:53 +02:00
committed by GitHub
parent d2fe2b90f6
commit cd6285f8c0
12 changed files with 338 additions and 110 deletions
+49 -34
View File
@@ -33,7 +33,6 @@ use crate::{controller, display};
use qr_code::QrCode;
use serde_json as json;
use std::convert::TryFrom;
use std::fs::File;
use std::io::{Read, Write};
use std::sync::atomic::Ordering;
@@ -315,12 +314,12 @@ pub struct SendArgs {
pub selection_strategy: String,
pub estimate_selection_strategies: bool,
pub late_lock: bool,
pub dest: String,
pub dest: Option<String>,
pub change_outputs: usize,
pub fluff: bool,
pub max_outputs: usize,
pub target_slate_version: Option<u16>,
pub payment_proof_address: Option<SlatepackAddress>,
pub payment_proof_address: Option<String>,
pub ttl_blocks: Option<u64>,
pub skip_tor: Option<bool>,
pub outfile: Option<String>,
@@ -341,6 +340,15 @@ where
C: NodeClient + 'static,
K: keychain::Keychain + 'static,
{
let dest = if args.estimate_selection_strategies {
None
} else {
args.dest
.as_deref()
.map(SlatepackAddress::try_from)
.transpose()?
};
let mut slate = Slate::blank(2, false);
let mut amount = args.amount;
if args.use_max_amount {
@@ -369,6 +377,11 @@ where
.collect::<Result<Vec<_>, grin_wallet_libwallet::Error>>()?;
display::estimate(amount, strategies, dark_scheme);
} else {
let payment_proof_recipient_address = args
.payment_proof_address
.as_deref()
.map(SlatepackAddress::try_from)
.transpose()?;
let init_args = InitTxArgs {
src_acct_name: None,
amount,
@@ -378,7 +391,7 @@ where
num_change_outputs: args.change_outputs as u32,
selection_strategy_is_use_all: args.selection_strategy == "all",
target_slate_version: args.target_slate_version,
payment_proof_recipient_address: args.payment_proof_address.clone(),
payment_proof_recipient_address,
ttl_blocks: args.ttl_blocks,
send_args: None,
late_lock: Some(args.late_lock),
@@ -390,7 +403,9 @@ where
info!(
"Tx created: {} grin to {} (strategy '{}')",
core::amount_to_hr_string(amount, false),
args.dest,
dest.as_ref()
.map(ToString::to_string)
.unwrap_or_else(|| "no destination".to_string()),
args.selection_strategy,
);
s
@@ -415,7 +430,7 @@ where
owner_api,
keychain_mask,
&slate,
args.dest.as_str(),
dest.clone(),
args.outfile,
true,
false,
@@ -424,11 +439,12 @@ where
};
let can_send = tor_config.send_tor(args.skip_tor);
if test_mode || !can_send {
if test_mode || !can_send || dest.is_none() {
return output_sp();
}
let res = try_slatepack_sync_workflow(&slate, &args.dest, Some(tor_config), None, false);
let dest = dest.as_ref().unwrap();
let res = try_slatepack_sync_workflow(&slate, dest, Some(tor_config), None, false);
match res {
Ok(s) => {
@@ -457,7 +473,7 @@ pub fn output_slatepack<L, C, K>(
owner_api: &mut Owner<L, C, K>,
keychain_mask: Option<&SecretKey>,
slate: &Slate,
dest: &str,
dest: Option<SlatepackAddress>,
out_file_override: Option<String>,
lock: bool,
finalizing: bool,
@@ -469,12 +485,8 @@ where
K: keychain::Keychain + 'static,
{
// Output the slatepack file to stdout and to a file
let address = match SlatepackAddress::try_from(dest) {
Ok(a) => Some(a),
Err(_) => None,
};
// encrypt for recipient by default
let recipients = match address.clone() {
let recipients = match dest.clone() {
Some(a) => vec![a],
None => vec![],
};
@@ -520,7 +532,7 @@ where
println!();
}
}
if address.is_some() {
if dest.is_some() {
println!("The slatepack data is encrypted for the recipient only");
} else {
println!("The slatepack data is NOT encrypted");
@@ -638,17 +650,12 @@ where
},
)?;
let dest = match ret_address {
Some(a) => String::try_from(&a)?,
None => String::from(""),
};
let output_sp = || -> Result<(), Error> {
Ok(output_slatepack(
owner_api,
keychain_mask,
&slate,
&dest,
ret_address.clone(),
args.outfile,
false,
false,
@@ -657,11 +664,12 @@ where
};
let can_send = tor_config.send_tor(args.skip_tor);
if test_mode || !can_send {
if test_mode || !can_send || ret_address.is_none() {
return output_sp();
}
let res = try_slatepack_sync_workflow(&slate, &dest, Some(tor_config), None, true);
let dest = ret_address.as_ref().unwrap();
let res = try_slatepack_sync_workflow(&slate, dest, Some(tor_config), None, true);
match res {
Ok(s) => {
@@ -829,7 +837,7 @@ where
owner_api,
keychain_mask,
&slate,
"",
None,
args.outfile,
false,
true,
@@ -842,7 +850,7 @@ where
/// Issue Invoice Args
pub struct IssueInvoiceArgs {
/// Slatepack address
pub dest: String,
pub dest: Option<String>,
/// issue invoice tx args
pub issue_args: IssueInvoiceTxArgs,
/// output file override
@@ -861,6 +869,11 @@ where
C: NodeClient + 'static,
K: keychain::Keychain + 'static,
{
let dest = args
.dest
.as_deref()
.map(SlatepackAddress::try_from)
.transpose()?;
let issue_args = args.issue_args.clone();
let slate = owner_api.issue_invoice_tx(keychain_mask, issue_args)?;
@@ -869,7 +882,7 @@ where
owner_api,
keychain_mask,
&slate,
args.dest.as_str(),
dest,
args.outfile,
false,
false,
@@ -882,7 +895,7 @@ where
pub struct ProcessInvoiceArgs {
pub minimum_confirmations: u64,
pub selection_strategy: String,
pub ret_address: Option<SlatepackAddress>,
pub ret_address: Option<String>,
pub max_outputs: usize,
pub slate: Slate,
pub estimate_selection_strategies: bool,
@@ -907,11 +920,12 @@ where
C: NodeClient + 'static,
K: keychain::Keychain + 'static,
{
let ret_address = args
.ret_address
.as_deref()
.map(SlatepackAddress::try_from)
.transpose()?;
let mut slate = args.slate.clone();
let dest = match args.ret_address.clone() {
Some(a) => String::try_from(&a)?,
None => String::from(""),
};
if args.estimate_selection_strategies {
let strategies = vec!["smallest", "all"]
@@ -970,7 +984,7 @@ where
owner_api,
keychain_mask,
&slate,
&dest,
ret_address.clone(),
args.outfile,
true,
false,
@@ -979,11 +993,12 @@ where
};
let can_send = tor_config.send_tor(args.skip_tor);
if test_mode || !can_send {
if test_mode || !can_send || ret_address.is_none() {
return output_sp();
}
let res = try_slatepack_sync_workflow(&slate, &dest, Some(tor_config), None, true);
let dest = ret_address.as_ref().unwrap();
let res = try_slatepack_sync_workflow(&slate, dest, Some(tor_config), None, true);
match res {
Ok(s) => {
+1 -1
View File
@@ -129,7 +129,7 @@ fn send_preflight() {
let args = InitTxArgs {
amount: 1,
send_args: Some(InitTxSendArgs {
dest: "unused".into(),
dest: "tgrin1xtxavwfgs48ckf3gk8wwgcndmn0nt4tvkl8a7ltyejjcy2mc6nfs9gm2lp".into(),
post_tx: false,
fluff: false,
skip_tor: Some(true),
+117 -2
View File
@@ -18,6 +18,7 @@ extern crate grin_wallet_controller as wallet;
extern crate grin_wallet_impls as impls;
use grin_core as core;
use grin_wallet_config::GlobalWalletConfig;
use grin_wallet_libwallet as libwallet;
use std::path::PathBuf;
@@ -28,8 +29,8 @@ use std::thread;
use std::time::Duration;
use grin_wallet_libwallet::{
InitTxArgs, IssueInvoiceTxArgs, Slate, Slatepack, SlatepackAddress, Slatepacker,
SlatepackerArgs,
InitTxArgs, InitTxSendArgs, IssueInvoiceTxArgs, Slate, Slatepack, SlatepackAddress,
Slatepacker, SlatepackerArgs,
};
use ed25519_dalek::SigningKey as edDalekSecretKey;
@@ -601,6 +602,110 @@ fn slatepack_api_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
Ok(())
}
/// Do not create transaction for invalid or wrong network Slatepack address.
fn slatepack_address_validation(test_dir: &'static str) -> Result<(), libwallet::Error> {
let mut wallet_proxy = create_wallet_proxy(test_dir);
let chain = wallet_proxy.chain.clone();
let stopper = wallet_proxy.running.clone();
let config_path = PathBuf::from(test_dir).join("grin-wallet.toml");
GlobalWalletConfig::for_chain(&core::global::ChainTypes::AutomatedTesting, &config_path)
.write_to_file(config_path.to_str().unwrap(), false, None, None)
.unwrap();
create_wallet_and_add!(
client1,
wallet1,
mask1_i,
test_dir,
"wallet1",
None,
&mut wallet_proxy,
false
);
let mask1 = (&mask1_i).as_ref();
create_wallet_and_add!(
client2,
wallet2,
mask2_i,
test_dir,
"wallet2",
None,
&mut wallet_proxy,
false
);
let mask2 = (&mask2_i).as_ref();
let proxy_thread = thread::spawn(move || {
if let Err(e) = wallet_proxy.run() {
error!("Wallet Proxy error: {}", e);
}
});
let reward = core::consensus::REWARD;
let _ = test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, 6, false);
let mut slate = Slate::blank(2, true);
wallet::controller::owner_single_use(wallet2.clone(), mask2, config_path.clone(), |api, m| {
slate = api.issue_invoice_tx(
m,
IssueInvoiceTxArgs {
amount: reward,
..Default::default()
},
)?;
Ok(())
})?;
wallet::controller::owner_single_use(wallet1.clone(), mask1, config_path, |api, m| {
let args = InitTxArgs {
src_acct_name: Some("mining".to_owned()),
amount: reward,
minimum_confirmations: 2,
max_outputs: 500,
num_change_outputs: 1,
selection_strategy_is_use_all: true,
..Default::default()
};
let mut wrong_net_args = args.clone();
wrong_net_args.send_args = Some(InitTxSendArgs {
dest: "grin1dvge9z4uqgqlpspmljrd7smh3grrw9xu2r9lkz3u67s3emj3ud2sd5gk9p".to_string(),
post_tx: false,
fluff: false,
skip_tor: Some(true),
});
assert!(api.init_send_tx(m, wrong_net_args.clone()).is_err());
assert!(api.process_invoice_tx(m, &slate, wrong_net_args).is_err());
let mut invalid_args = args.clone();
invalid_args.send_args = Some(InitTxSendArgs {
dest: "tgrinaddr10qlk22rxjap2ny8qltc2tl996kenxr3hhwuu6hrzs6tdq08yaqgqnlumr7"
.to_string(),
post_tx: false,
fluff: false,
skip_tor: Some(true),
});
assert!(api.init_send_tx(m, invalid_args.clone()).is_err());
assert!(api.process_invoice_tx(m, &slate, invalid_args).is_err());
let mut valid_args = args;
valid_args.send_args = Some(InitTxSendArgs {
dest: "tgrin1xtxavwfgs48ckf3gk8wwgcndmn0nt4tvkl8a7ltyejjcy2mc6nfs9gm2lp".to_string(),
post_tx: false,
fluff: false,
skip_tor: Some(true),
});
api.process_invoice_tx(m, &slate, valid_args.clone())?;
api.init_send_tx(m, valid_args)?;
Ok(())
})?;
stopper.store(false, Ordering::Relaxed);
proxy_thread.join().expect("wallet proxy thread panicked");
Ok(())
}
#[test]
fn slatepack_exchange_json() {
let test_dir = "test_output/slatepack_exchange_json";
@@ -677,3 +782,13 @@ fn slatepack_api() {
}
clean_output_dir(test_dir);
}
#[test]
fn slatepack_address() {
let test_dir = "test_output/slatepack_address";
setup(test_dir);
if let Err(e) = slatepack_address_validation(test_dir) {
panic!("Libwallet Error: {}", e);
}
clean_output_dir(test_dir);
}