Skip to content

Commit

Permalink
Refactor MultiSig creation to factory method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
biryukovmaxim committed Oct 6, 2023
1 parent 47fa5a5 commit c75844a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
14 changes: 7 additions & 7 deletions wallet/core/src/runtime/account/variants/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ impl Account for MultiSig {
fn as_storable(&self) -> Result<storage::account::Account> {
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));

Expand Down
34 changes: 14 additions & 20 deletions wallet/core/src/runtime/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand All @@ -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?,
Expand Down Expand Up @@ -892,13 +886,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?,
Expand Down
2 changes: 1 addition & 1 deletion wallet/core/src/secret.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use zeroize::Zeroize;

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Secret(Vec<u8>);

impl Secret {
Expand Down
12 changes: 12 additions & 0 deletions wallet/core/src/storage/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ pub struct MultiSig {
pub ecdsa: bool,
}

impl MultiSig {
pub fn new(
xpub_keys: Arc<Vec<String>>,
prv_key_data_ids: Option<Arc<Vec<PrvKeyDataId>>>,
cosigner_index: Option<u8>,
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")]
Expand Down

0 comments on commit c75844a

Please sign in to comment.