Merge branch 'marvin/refactor-wallet-pub-acc' into marvin/keycard-commands

This commit is contained in:
jonesmarvin8 2026-04-28 14:17:15 -04:00
commit dd78314ca0
3 changed files with 42 additions and 61 deletions

View File

@ -56,9 +56,9 @@ impl WalletChainStore {
.expect("Malformed persistent account data, must have private root");
let mut public_tree = KeyTreePublic::new_from_root(match public_root {
PersistentAccountData::Public(data) => {
data.data.expect("Expect valid public account keys")
}
PersistentAccountData::Public(data) => data
.data
.expect("public tree in persistent_accounts failed to return a valid KeyTree."),
_ => unreachable!(),
});
let mut private_tree = KeyTreePrivate::new_from_root(match private_root {

View File

@ -548,14 +548,14 @@ impl WalletCore {
}
pub fn sign_public_message(
wallet: &Self,
&self,
message: &nssa::public_transaction::Message,
account_ids: &[AccountId],
) -> Result<nssa::public_transaction::WitnessSet, ExecutionFailureKind> {
let mut private_keys = Vec::new();
for &account_id in account_ids {
let key = wallet
let key = self
.storage
.user_data
.get_pub_account_signing_key(account_id)
@ -581,4 +581,13 @@ impl WalletCore {
&acc_manager.public_account_auth(),
)
}
#[must_use]
pub fn filter_owned_accounts(&self, account_ids: &[nssa::AccountId]) -> Vec<nssa::AccountId> {
account_ids
.iter()
.filter(|&&account_id| self.get_account_public_signing_key(account_id).is_some())
.copied()
.collect()
}
}

View File

@ -25,63 +25,35 @@ impl NativeTokenTransfer<'_> {
.await
.map_err(ExecutionFailureKind::SequencerError)?;
if balance >= balance_to_move {
let account_ids = vec![from, to];
let program_id = Program::authenticated_transfer_program().id();
let mut sign_ids = Vec::new();
sign_ids.push(from);
let mut nonces = self
.0
.get_accounts_nonces(vec![from])
.await
.map_err(ExecutionFailureKind::SequencerError)?;
let to_signing_key = self.0.storage.user_data.get_pub_account_signing_key(to);
if let Some(_to_signing_key) = to_signing_key {
sign_ids.push(to);
let to_nonces = self
.0
.get_accounts_nonces(vec![to])
.await
.map_err(ExecutionFailureKind::SequencerError)?;
nonces.extend(to_nonces);
} else {
println!(
"Receiver's account ({to}) private key not found in wallet. Proceeding with only sender's key."
);
}
let message =
Message::try_new(program_id, account_ids, nonces, balance_to_move).unwrap();
let witness_set = if pin.is_none() {
WalletCore::sign_public_message(self.0, &message, &sign_ids)
.expect("Expect a valid signature")
} else {
let pub_key = KeycardWallet::get_public_key_for_path_with_connect(
pin.as_ref().expect("Expect a pin as a String."),
key_path.as_ref().expect("Expect a key path String."),
);
let signature = KeycardWallet::sign_message_for_path_with_connect(
pin.as_ref().expect("Expect a pin as a String."),
key_path.as_ref().expect("Expect a key path String."),
&message.hash_message(),
)
.expect("Expect valid signature");
WitnessSet::from_list(&[signature], &[pub_key])
};
let tx = PublicTransaction::new(message, witness_set);
Ok(self
.0
.sequencer_client
.send_transaction(NSSATransaction::Public(tx))
.await?)
} else {
Err(ExecutionFailureKind::InsufficientFundsError)
if balance < balance_to_move {
return Err(ExecutionFailureKind::InsufficientFundsError);
}
let account_ids = vec![from, to];
let program_id = Program::authenticated_transfer_program().id();
let sign_ids = self.0.filter_owned_accounts(&[from, to]);
// Fetch nonces for both accounts unconditionally
let nonces = self
.0
.get_accounts_nonces(account_ids.clone())
.await
.map_err(ExecutionFailureKind::SequencerError)?;
let message = Message::try_new(program_id, account_ids, nonces, balance_to_move).unwrap();
// Assumes this now silently skips accounts without signing keys
let witness_set = WalletCore::sign_public_message(self.0, &message, &sign_ids)
.expect("`WalletCore::sign_public_message() failed to produce a signature for a NativeTokenTransfer.");
let tx = PublicTransaction::new(message, witness_set);
Ok(self
.0
.sequencer_client
.send_transaction(NSSATransaction::Public(tx))
.await?)
}
pub async fn register_account(