2026-04-10 20:23:25 +03:00
|
|
|
use authenticated_transfer_core::Instruction as AuthTransferInstruction;
|
2026-03-13 22:38:23 +03:00
|
|
|
use common::{HashType, transaction::NSSATransaction};
|
2025-10-03 15:59:27 -03:00
|
|
|
use nssa::{
|
2025-11-24 17:09:30 +03:00
|
|
|
AccountId, PublicTransaction,
|
2025-10-03 15:59:27 -03:00
|
|
|
program::Program,
|
|
|
|
|
public_transaction::{Message, WitnessSet},
|
|
|
|
|
};
|
2026-05-21 20:46:13 -04:00
|
|
|
use pyo3::exceptions::PyRuntimeError;
|
2026-03-13 22:38:23 +03:00
|
|
|
use sequencer_service_rpc::RpcClient as _;
|
2025-09-22 16:38:25 +03:00
|
|
|
|
2025-11-30 01:57:59 +03:00
|
|
|
use super::NativeTokenTransfer;
|
2026-05-18 13:44:03 +03:00
|
|
|
use crate::{
|
2026-05-21 20:46:13 -04:00
|
|
|
ExecutionFailureKind, cli::CliAccountMention, helperfunctions::read_pin, signing::SigningGroups,
|
2026-05-18 13:44:03 +03:00
|
|
|
};
|
2025-09-22 16:38:25 +03:00
|
|
|
|
2025-11-30 01:57:59 +03:00
|
|
|
impl NativeTokenTransfer<'_> {
|
|
|
|
|
pub async fn send_public_transfer(
|
2025-09-22 16:38:25 +03:00
|
|
|
&self,
|
2025-11-24 17:09:30 +03:00
|
|
|
from: AccountId,
|
|
|
|
|
to: AccountId,
|
2025-09-22 16:38:25 +03:00
|
|
|
balance_to_move: u128,
|
2026-05-21 20:46:13 -04:00
|
|
|
from_mention: &CliAccountMention,
|
|
|
|
|
to_mention: &CliAccountMention,
|
2026-03-13 22:38:23 +03:00
|
|
|
) -> Result<HashType, ExecutionFailureKind> {
|
2026-05-21 20:46:13 -04:00
|
|
|
let mut groups = SigningGroups::new();
|
|
|
|
|
groups
|
|
|
|
|
.add_sender(from_mention, from, self.0)
|
|
|
|
|
.and_then(|()| groups.add_recipient(to_mention, to, self.0))
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
ExecutionFailureKind::KeycardError(pyo3::PyErr::new::<PyRuntimeError, _>(
|
|
|
|
|
e.to_string(),
|
|
|
|
|
))
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let program_id = Program::authenticated_transfer_program().id();
|
|
|
|
|
let nonces = self
|
2026-03-04 18:42:33 +03:00
|
|
|
.0
|
2026-05-21 20:46:13 -04:00
|
|
|
.get_accounts_nonces(groups.signing_ids())
|
2026-05-18 13:44:03 +03:00
|
|
|
.await
|
2026-03-04 18:42:33 +03:00
|
|
|
.map_err(ExecutionFailureKind::SequencerError)?;
|
2025-09-22 16:38:25 +03:00
|
|
|
|
2026-05-21 20:46:13 -04:00
|
|
|
let message = Message::try_new(
|
|
|
|
|
program_id,
|
|
|
|
|
vec![from, to],
|
|
|
|
|
nonces,
|
|
|
|
|
AuthTransferInstruction::Transfer {
|
|
|
|
|
amount: balance_to_move,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.map_err(ExecutionFailureKind::TransactionBuildError)?;
|
|
|
|
|
|
|
|
|
|
let pin = if groups.needs_pin() {
|
|
|
|
|
read_pin()
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
ExecutionFailureKind::KeycardError(pyo3::PyErr::new::<PyRuntimeError, _>(
|
|
|
|
|
e.to_string(),
|
|
|
|
|
))
|
|
|
|
|
})?
|
|
|
|
|
.as_str()
|
|
|
|
|
.to_owned()
|
|
|
|
|
} else {
|
|
|
|
|
String::new()
|
|
|
|
|
};
|
2025-09-22 16:38:25 +03:00
|
|
|
|
2026-05-21 20:46:13 -04:00
|
|
|
let sigs = groups.sign_all(&message.hash(), &pin).map_err(|e| {
|
|
|
|
|
ExecutionFailureKind::KeycardError(pyo3::PyErr::new::<PyRuntimeError, _>(e.to_string()))
|
|
|
|
|
})?;
|
2025-09-22 16:38:25 +03:00
|
|
|
|
2026-05-21 20:46:13 -04:00
|
|
|
let tx = PublicTransaction::new(message, WitnessSet::from_raw_parts(sigs));
|
|
|
|
|
Ok(self
|
|
|
|
|
.0
|
|
|
|
|
.sequencer_client
|
|
|
|
|
.send_transaction(NSSATransaction::Public(tx))
|
|
|
|
|
.await?)
|
2025-09-22 16:38:25 +03:00
|
|
|
}
|
2025-10-10 17:47:23 -03:00
|
|
|
|
2025-11-30 01:57:59 +03:00
|
|
|
pub async fn register_account(
|
2025-10-10 17:47:23 -03:00
|
|
|
&self,
|
2025-11-24 17:09:30 +03:00
|
|
|
from: AccountId,
|
2026-05-21 20:46:13 -04:00
|
|
|
account_mention: &CliAccountMention,
|
2026-03-13 22:38:23 +03:00
|
|
|
) -> Result<HashType, ExecutionFailureKind> {
|
2026-03-04 18:42:33 +03:00
|
|
|
let nonces = self
|
|
|
|
|
.0
|
|
|
|
|
.get_accounts_nonces(vec![from])
|
|
|
|
|
.await
|
|
|
|
|
.map_err(ExecutionFailureKind::SequencerError)?;
|
2025-10-10 17:47:23 -03:00
|
|
|
|
2025-11-24 17:09:30 +03:00
|
|
|
let account_ids = vec![from];
|
2025-10-10 17:47:23 -03:00
|
|
|
let program_id = Program::authenticated_transfer_program().id();
|
2026-04-10 20:23:25 +03:00
|
|
|
let message = Message::try_new(
|
|
|
|
|
program_id,
|
|
|
|
|
account_ids,
|
|
|
|
|
nonces,
|
|
|
|
|
AuthTransferInstruction::Initialize,
|
|
|
|
|
)
|
2026-05-21 20:46:13 -04:00
|
|
|
.map_err(ExecutionFailureKind::TransactionBuildError)?;
|
|
|
|
|
|
|
|
|
|
let mut groups = SigningGroups::new();
|
|
|
|
|
groups
|
|
|
|
|
.add_sender(account_mention, from, self.0)
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
ExecutionFailureKind::KeycardError(pyo3::PyErr::new::<PyRuntimeError, _>(
|
|
|
|
|
e.to_string(),
|
|
|
|
|
))
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let pin = if groups.needs_pin() {
|
|
|
|
|
read_pin()
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
ExecutionFailureKind::KeycardError(pyo3::PyErr::new::<PyRuntimeError, _>(
|
|
|
|
|
e.to_string(),
|
|
|
|
|
))
|
|
|
|
|
})?
|
|
|
|
|
.as_str()
|
|
|
|
|
.to_owned()
|
|
|
|
|
} else {
|
|
|
|
|
String::new()
|
2025-10-10 17:47:23 -03:00
|
|
|
};
|
|
|
|
|
|
2026-05-21 20:46:13 -04:00
|
|
|
let sigs = groups.sign_all(&message.hash(), &pin).map_err(|e| {
|
|
|
|
|
ExecutionFailureKind::KeycardError(pyo3::PyErr::new::<PyRuntimeError, _>(e.to_string()))
|
|
|
|
|
})?;
|
2025-10-10 17:47:23 -03:00
|
|
|
|
2026-05-21 20:46:13 -04:00
|
|
|
let tx = PublicTransaction::new(message, WitnessSet::from_raw_parts(sigs));
|
2026-03-13 22:38:23 +03:00
|
|
|
Ok(self
|
|
|
|
|
.0
|
|
|
|
|
.sequencer_client
|
|
|
|
|
.send_transaction(NSSATransaction::Public(tx))
|
|
|
|
|
.await?)
|
2025-10-10 17:47:23 -03:00
|
|
|
}
|
2026-05-25 14:41:51 +03:00
|
|
|
}
|