Contracts: use checked conversions for amount -> i64

A large u64 amount could wrap when cast `as i64`. Convert with i64::try_from in
the CLI net-change derivation and in contract view, returning an error instead.
get_net_change / to_api_args now return Result (the two arg panics become
errors too), and the to_api_args call moves out of the owner-session closure.

Addresses PR #754 review (command.rs, view.rs).
This commit is contained in:
jwinterm
2026-06-07 16:37:54 -04:00
parent 5cc1683a1b
commit b3f75ee2b5
2 changed files with 38 additions and 32 deletions
+7 -3
View File
@@ -37,10 +37,14 @@ where
// Otherwise, you can't really predict who the party doing the next step should be.
// TODO: Do we need to do any slate verification here?
// Checked conversion so an out-of-range amount can't wrap into a bogus net change.
let suggested_net_change: Option<i64> = match slate.state {
// TODO: Check bounds against overflow/underflow
SlateState::Invoice1 => Some(slate.amount as i64),
SlateState::Standard1 => Some(-(slate.amount as i64)),
SlateState::Invoice1 => Some(i64::try_from(slate.amount).map_err(|_| {
Error::GenericError(format!("Slate amount {} exceeds i64", slate.amount))
})?),
SlateState::Standard1 => Some(-i64::try_from(slate.amount).map_err(|_| {
Error::GenericError(format!("Slate amount {} exceeds i64", slate.amount))
})?),
_ => None,
};
let is_executed = false;