From ca74b14ddb5053fa9efe9d175460a52715b7c26f Mon Sep 17 00:00:00 2001 From: max143672 Date: Mon, 2 Oct 2023 16:42:29 +0400 Subject: [PATCH] Refactor MultiSig creation to factory method To improve readability and maintainability, MultiSig struct instances creation scattered in different places are now replaced with a new factory method `MultiSig::new()`. Other changes include reduction of redundant struct declaration and removed the Debug trait from the Secret struct. --- .../src/runtime/account/variants/multisig.rs | 14 ++++---- wallet/core/src/runtime/wallet.rs | 34 ++++++++----------- wallet/core/src/secret.rs | 2 +- wallet/core/src/storage/account.rs | 12 +++++++ 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/wallet/core/src/runtime/account/variants/multisig.rs b/wallet/core/src/runtime/account/variants/multisig.rs index ba7ff8d3b6..5f0ab1b984 100644 --- a/wallet/core/src/runtime/account/variants/multisig.rs +++ b/wallet/core/src/runtime/account/variants/multisig.rs @@ -77,13 +77,13 @@ impl Account for MultiSig { fn as_storable(&self) -> Result { let settings = self.context().settings.clone().unwrap_or_default(); - let multisig = storage::MultiSig { - xpub_keys: self.xpub_keys.clone(), - ecdsa: self.ecdsa, - cosigner_index: self.cosigner_index, - minimum_signatures: self.minimum_signatures, - prv_key_data_ids: self.prv_key_data_ids.clone(), - }; + let multisig = storage::MultiSig::new( + self.xpub_keys.clone(), + self.prv_key_data_ids.clone(), + self.cosigner_index, + self.minimum_signatures, + self.ecdsa, + ); let account = storage::Account::new(*self.id(), None, settings, storage::AccountData::MultiSig(multisig)); diff --git a/wallet/core/src/runtime/wallet.rs b/wallet/core/src/runtime/wallet.rs index dc5c0c0f47..65c43b3f12 100644 --- a/wallet/core/src/runtime/wallet.rs +++ b/wallet/core/src/runtime/wallet.rs @@ -486,13 +486,13 @@ impl Wallet { runtime::MultiSig::try_new( self, settings, - MultiSig { - xpub_keys: Arc::new(xpub_keys), - prv_key_data_ids: Some(Arc::new(prv_key_data_ids)), - cosigner_index: Some(min_cosigner_index), - minimum_signatures: args.minimum_signatures, - ecdsa: false, - }, + MultiSig::new( + Arc::new(xpub_keys), + Some(Arc::new(prv_key_data_ids)), + Some(min_cosigner_index), + args.minimum_signatures, + false, + ), None, ) .await?, @@ -502,13 +502,7 @@ impl Wallet { runtime::MultiSig::try_new( self, settings, - MultiSig { - xpub_keys: Arc::new(xpub_keys), - prv_key_data_ids: None, - cosigner_index: None, - minimum_signatures: args.minimum_signatures, - ecdsa: false, - }, + MultiSig::new(Arc::new(xpub_keys), None, None, args.minimum_signatures, false), None, ) .await?, @@ -893,13 +887,13 @@ impl Wallet { runtime::MultiSig::try_new( self, storage::Settings::default(), - MultiSig { - xpub_keys: Arc::new(xpub_keys), - prv_key_data_ids: Some(Arc::new(prv_key_data_ids)), - cosigner_index: Some(min_cosigner_index), + MultiSig::new( + Arc::new(xpub_keys), + Some(Arc::new(prv_key_data_ids)), + Some(min_cosigner_index), minimum_signatures, - ecdsa: false, - }, + false, + ), None, ) .await?, diff --git a/wallet/core/src/secret.rs b/wallet/core/src/secret.rs index 7a1873c4bf..3e6d079bba 100644 --- a/wallet/core/src/secret.rs +++ b/wallet/core/src/secret.rs @@ -1,6 +1,6 @@ use zeroize::Zeroize; -#[derive(Clone, Debug)] +#[derive(Clone)] pub struct Secret(Vec); impl Secret { diff --git a/wallet/core/src/storage/account.rs b/wallet/core/src/storage/account.rs index 0a14ead2d9..3d90fd718f 100644 --- a/wallet/core/src/storage/account.rs +++ b/wallet/core/src/storage/account.rs @@ -59,6 +59,18 @@ pub struct MultiSig { pub ecdsa: bool, } +impl MultiSig { + pub fn new( + xpub_keys: Arc>, + prv_key_data_ids: Option>>, + cosigner_index: Option, + minimum_signatures: u16, + ecdsa: bool, + ) -> Self { + Self { version: MULTISIG_ACCOUNT_VERSION, xpub_keys, prv_key_data_ids, cosigner_index, minimum_signatures, ecdsa } + } +} + const KEYPAIR_ACCOUNT_VERSION: u16 = 0; #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "lowercase")]