|
| 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 | +} |
0 commit comments