Skip to content

Commit 751cce1

Browse files
committed
feat: add asset freeze and key registration to utils FFI
Add AssetFreezeParams and key registration parameter structs (Online, Offline, NonParticipation) with corresponding Composer methods. Includes TryFrom implementations, Python bindings, and tests.
1 parent f29f72d commit 751cce1

File tree

7 files changed

+474
-2
lines changed

7 files changed

+474
-2
lines changed

crates/algokit_utils_ffi/src/transactions/composer.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ impl Composer {
6161
})
6262
}
6363

64-
pub fn add_asset_freeze(&self, params: super::asset_freeze::AssetFreezeParams) -> Result<(), UtilsError> {
64+
pub fn add_asset_freeze(
65+
&self,
66+
params: super::asset_freeze::AssetFreezeParams,
67+
) -> Result<(), UtilsError> {
6568
let mut composer = self.inner_composer.blocking_lock();
6669
composer
6770
.add_asset_freeze(params.try_into()?)
@@ -70,6 +73,42 @@ impl Composer {
7073
})
7174
}
7275

76+
pub fn add_online_key_registration(
77+
&self,
78+
params: super::key_registration::OnlineKeyRegistrationParams,
79+
) -> Result<(), UtilsError> {
80+
let mut composer = self.inner_composer.blocking_lock();
81+
composer
82+
.add_online_key_registration(params.try_into()?)
83+
.map_err(|e| UtilsError::UtilsError {
84+
message: e.to_string(),
85+
})
86+
}
87+
88+
pub fn add_offline_key_registration(
89+
&self,
90+
params: super::key_registration::OfflineKeyRegistrationParams,
91+
) -> Result<(), UtilsError> {
92+
let mut composer = self.inner_composer.blocking_lock();
93+
composer
94+
.add_offline_key_registration(params.try_into()?)
95+
.map_err(|e| UtilsError::UtilsError {
96+
message: e.to_string(),
97+
})
98+
}
99+
100+
pub fn add_non_participation_key_registration(
101+
&self,
102+
params: super::key_registration::NonParticipationKeyRegistrationParams,
103+
) -> Result<(), UtilsError> {
104+
let mut composer = self.inner_composer.blocking_lock();
105+
composer
106+
.add_non_participation_key_registration(params.try_into()?)
107+
.map_err(|e| UtilsError::UtilsError {
108+
message: e.to_string(),
109+
})
110+
}
111+
73112
pub async fn send(&self) -> Result<Vec<String>, UtilsError> {
74113
let mut composer = self.inner_composer.blocking_lock();
75114
let result = composer
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use crate::transactions::common::UtilsError;
2+
3+
use super::common::CommonParams;
4+
use algokit_utils::transactions::{
5+
NonParticipationKeyRegistrationParams as RustNonParticipationKeyRegistrationParams,
6+
OfflineKeyRegistrationParams as RustOfflineKeyRegistrationParams,
7+
OnlineKeyRegistrationParams as RustOnlineKeyRegistrationParams,
8+
};
9+
10+
#[derive(uniffi::Record)]
11+
pub struct OnlineKeyRegistrationParams {
12+
/// Common transaction parameters.
13+
pub common_params: CommonParams,
14+
15+
/// The root participation public key.
16+
pub vote_key: Vec<u8>,
17+
18+
/// The VRF public key.
19+
pub selection_key: Vec<u8>,
20+
21+
/// The first round that the participation key is valid.
22+
pub vote_first: u64,
23+
24+
/// The last round that the participation key is valid.
25+
pub vote_last: u64,
26+
27+
/// This is the dilution for the 2-level participation key.
28+
pub vote_key_dilution: u64,
29+
30+
/// The 64 byte state proof public key commitment.
31+
pub state_proof_key: Option<Vec<u8>>,
32+
}
33+
34+
#[derive(uniffi::Record)]
35+
pub struct OfflineKeyRegistrationParams {
36+
/// Common transaction parameters.
37+
pub common_params: CommonParams,
38+
39+
/// Mark account as non-reward earning.
40+
pub non_participation: Option<bool>,
41+
}
42+
43+
#[derive(uniffi::Record)]
44+
pub struct NonParticipationKeyRegistrationParams {
45+
/// Common transaction parameters.
46+
pub common_params: CommonParams,
47+
}
48+
49+
impl TryFrom<OnlineKeyRegistrationParams> for RustOnlineKeyRegistrationParams {
50+
type Error = UtilsError;
51+
52+
fn try_from(params: OnlineKeyRegistrationParams) -> Result<Self, Self::Error> {
53+
let common_params = params.common_params.try_into()?;
54+
55+
// Convert Vec<u8> to [u8; 32] for vote_key
56+
let vote_key: [u8; 32] =
57+
params
58+
.vote_key
59+
.try_into()
60+
.map_err(|_| UtilsError::UtilsError {
61+
message: "Vote key must be exactly 32 bytes".to_string(),
62+
})?;
63+
64+
// Convert Vec<u8> to [u8; 32] for selection_key
65+
let selection_key: [u8; 32] =
66+
params
67+
.selection_key
68+
.try_into()
69+
.map_err(|_| UtilsError::UtilsError {
70+
message: "Selection key must be exactly 32 bytes".to_string(),
71+
})?;
72+
73+
// Convert Option<Vec<u8>> to Option<[u8; 64]> for state_proof_key
74+
let state_proof_key = match params.state_proof_key {
75+
Some(key) => {
76+
let key_array: [u8; 64] = key.try_into().map_err(|_| UtilsError::UtilsError {
77+
message: "State proof key must be exactly 64 bytes".to_string(),
78+
})?;
79+
Some(key_array)
80+
}
81+
None => None,
82+
};
83+
84+
Ok(RustOnlineKeyRegistrationParams {
85+
common_params,
86+
vote_key,
87+
selection_key,
88+
vote_first: params.vote_first,
89+
vote_last: params.vote_last,
90+
vote_key_dilution: params.vote_key_dilution,
91+
state_proof_key,
92+
})
93+
}
94+
}
95+
96+
impl TryFrom<OfflineKeyRegistrationParams> for RustOfflineKeyRegistrationParams {
97+
type Error = UtilsError;
98+
99+
fn try_from(params: OfflineKeyRegistrationParams) -> Result<Self, Self::Error> {
100+
let common_params = params.common_params.try_into()?;
101+
Ok(RustOfflineKeyRegistrationParams {
102+
common_params,
103+
non_participation: params.non_participation,
104+
})
105+
}
106+
}
107+
108+
impl TryFrom<NonParticipationKeyRegistrationParams> for RustNonParticipationKeyRegistrationParams {
109+
type Error = UtilsError;
110+
111+
fn try_from(params: NonParticipationKeyRegistrationParams) -> Result<Self, Self::Error> {
112+
let common_params = params.common_params.try_into()?;
113+
Ok(RustNonParticipationKeyRegistrationParams { common_params })
114+
}
115+
}

crates/algokit_utils_ffi/src/transactions/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
pub mod asset_freeze;
12
pub mod common;
23
pub mod composer;
4+
pub mod key_registration;
35
pub mod payment;
4-
pub mod asset_freeze;

0 commit comments

Comments
 (0)