From 56d7a89f7718b1922ba3a374f881d09095a8b4f8 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 3 Oct 2024 14:48:44 +0300 Subject: [PATCH] fix(dpp): identity factory doesn't handle versions --- .../rs-dpp/src/identity/identity_factory.rs | 62 +++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/packages/rs-dpp/src/identity/identity_factory.rs b/packages/rs-dpp/src/identity/identity_factory.rs index 0b378a2fc8..48f7369818 100644 --- a/packages/rs-dpp/src/identity/identity_factory.rs +++ b/packages/rs-dpp/src/identity/identity_factory.rs @@ -42,6 +42,7 @@ use crate::state_transition::identity_credit_transfer_transition::v0::IdentityCr use crate::state_transition::identity_credit_transfer_transition::IdentityCreditTransferTransition; #[cfg(all(feature = "state-transitions", feature = "client"))] use crate::state_transition::identity_credit_withdrawal_transition::v0::IdentityCreditWithdrawalTransitionV0; +#[cfg(all(feature = "state-transitions", feature = "client"))] use crate::state_transition::identity_credit_withdrawal_transition::v1::IdentityCreditWithdrawalTransitionV1; #[cfg(all(feature = "state-transitions", feature = "client"))] use crate::state_transition::identity_credit_withdrawal_transition::IdentityCreditWithdrawalTransition; @@ -237,19 +238,58 @@ impl IdentityFactory { output_script: Option, identity_nonce: IdentityNonce, ) -> Result { - let identity_credit_withdrawal_transition = IdentityCreditWithdrawalTransitionV1 { - identity_id, - amount, - core_fee_per_byte, - pooling, - output_script, - nonce: identity_nonce, - ..Default::default() + let platform_version = PlatformVersion::get(self.protocol_version)?; + + let identity_credit_withdrawal_transition = match platform_version + .dpp + .state_transitions + .identities + .credit_withdrawal + .default_constructor + { + 0 => { + let output_script = output_script.ok_or_else(|| { + ProtocolError::Generic( + "Output script is required for IdentityCreditWithdrawalTransitionV0" + .to_string(), + ) + })?; + + let transition = IdentityCreditWithdrawalTransitionV0 { + identity_id, + amount, + core_fee_per_byte, + pooling, + output_script, + nonce: identity_nonce, + ..Default::default() + }; + + IdentityCreditWithdrawalTransition::from(transition) + } + 1 => { + let transition = IdentityCreditWithdrawalTransitionV1 { + identity_id, + amount, + core_fee_per_byte, + pooling, + output_script, + nonce: identity_nonce, + ..Default::default() + }; + + IdentityCreditWithdrawalTransition::from(transition) + } + version => { + return Err(ProtocolError::UnknownVersionMismatch { + method: "create_identity_credit_withdrawal_transition".to_string(), + known_versions: vec![0, 1], + received: version, + }); + } }; - Ok(IdentityCreditWithdrawalTransition::from( - identity_credit_withdrawal_transition, - )) + Ok(identity_credit_withdrawal_transition) } #[cfg(all(feature = "state-transitions", feature = "client"))]