Skip to content

Commit

Permalink
feat: order networks selection based on the selected account id (bubb…
Browse files Browse the repository at this point in the history
…le up more relevant networks) (near#225)

Co-authored-by: Vlad Frolov <[email protected]>
  • Loading branch information
thaodt and frol authored Aug 28, 2023
1 parent 07b4aa4 commit 23bb067
Show file tree
Hide file tree
Showing 41 changed files with 569 additions and 374 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,29 @@ impl PrintKeypairToTerminalContext {
impl From<PrintKeypairToTerminalContext> for crate::commands::ActionContext {
fn from(item: PrintKeypairToTerminalContext) -> Self {
let on_after_getting_network_callback: crate::commands::OnAfterGettingNetworkCallback =
std::sync::Arc::new(move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: item.signer_account_id.clone(),
receiver_id: item.signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.public_key.clone(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.permission.clone(),
std::sync::Arc::new({
let signer_account_id = item.signer_account_id.clone();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_account_id.clone(),
receiver_id: signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.public_key.clone(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.permission.clone(),
},
},
},
)],
})
)],
})
}
});

Self {
global_context: item.global_context,
interacting_with_account_ids: vec![item.signer_account_id],
on_after_getting_network_callback,
on_before_signing_callback: std::sync::Arc::new(
|_prepolulated_unsinged_transaction, _network_config| Ok(()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,31 @@ impl SaveKeypairToKeychainContext {

impl From<SaveKeypairToKeychainContext> for crate::commands::ActionContext {
fn from(item: SaveKeypairToKeychainContext) -> Self {
let credentials_home_dir = item.global_context.config.credentials_home_dir.clone();

let on_after_getting_network_callback: crate::commands::OnAfterGettingNetworkCallback =
std::sync::Arc::new(move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: item.signer_account_id.clone(),
receiver_id: item.signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.public_key.clone(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.permission.clone(),
std::sync::Arc::new({
let signer_account_id = item.signer_account_id.clone();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_account_id.clone(),
receiver_id: signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.public_key.clone(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.permission.clone(),
},
},
},
)],
})
)],
})
}
});

let on_before_sending_transaction_callback: crate::transaction_signature_options::OnBeforeSendingTransactionCallback =
std::sync::Arc::new(
std::sync::Arc::new({
let credentials_home_dir = item.global_context.config.credentials_home_dir.clone();

move |signed_transaction, network_config, storage_message| {
let key_pair_properties_buf = serde_json::to_string(&item.key_pair_properties)?;
*storage_message = crate::common::save_access_key_to_keychain(
Expand All @@ -72,11 +76,12 @@ impl From<SaveKeypairToKeychainContext> for crate::commands::ActionContext {
)
})?;
Ok(())
},
);
}
});

Self {
global_context: item.global_context,
interacting_with_account_ids: vec![item.signer_account_id],
on_after_getting_network_callback,
on_before_signing_callback: std::sync::Arc::new(
|_prepolulated_unsinged_transaction, _network_config| Ok(()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@ impl SaveKeypairToMacosKeychainContext {
impl From<SaveKeypairToMacosKeychainContext> for crate::commands::ActionContext {
fn from(item: SaveKeypairToMacosKeychainContext) -> Self {
let on_after_getting_network_callback: crate::commands::OnAfterGettingNetworkCallback =
std::sync::Arc::new(move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: item.0.signer_account_id.clone(),
receiver_id: item.0.signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.0.public_key.clone(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.0.permission.clone(),
std::sync::Arc::new({
let signer_account_id = item.0.signer_account_id.clone();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_account_id.clone(),
receiver_id: signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.0.public_key.clone(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.0.permission.clone(),
},
},
},
)],
})
)],
})
}
});
let on_before_sending_transaction_callback: crate::transaction_signature_options::OnBeforeSendingTransactionCallback =
std::sync::Arc::new(
Expand All @@ -49,8 +53,10 @@ impl From<SaveKeypairToMacosKeychainContext> for crate::commands::ActionContext
Ok(())
},
);

Self {
global_context: item.0.global_context,
interacting_with_account_ids: vec![item.0.signer_account_id],
on_after_getting_network_callback,
on_before_signing_callback: std::sync::Arc::new(
|_prepolulated_unsinged_transaction, _network_config| Ok(()),
Expand Down
32 changes: 19 additions & 13 deletions src/commands/account/add_key/use_ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,29 @@ impl AddLedgerKeyActionContext {
impl From<AddLedgerKeyActionContext> for crate::commands::ActionContext {
fn from(item: AddLedgerKeyActionContext) -> Self {
let on_after_getting_network_callback: crate::commands::OnAfterGettingNetworkCallback =
std::sync::Arc::new(move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: item.signer_account_id.clone(),
receiver_id: item.signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.public_key.clone().into(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.permission.clone(),
std::sync::Arc::new({
let signer_account_id = item.signer_account_id.clone();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_account_id.clone(),
receiver_id: signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.public_key.clone().into(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.permission.clone(),
},
},
},
)],
})
)],
})
}
});

Self {
global_context: item.global_context,
interacting_with_account_ids: vec![item.signer_account_id],
on_after_getting_network_callback,
on_before_signing_callback: std::sync::Arc::new(
|_prepolulated_unsinged_transaction, _network_config| Ok(()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,29 @@ impl AddAccessWithSeedPhraseActionContext {
impl From<AddAccessWithSeedPhraseActionContext> for crate::commands::ActionContext {
fn from(item: AddAccessWithSeedPhraseActionContext) -> Self {
let on_after_getting_network_callback: crate::commands::OnAfterGettingNetworkCallback =
std::sync::Arc::new(move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: item.signer_account_id.clone(),
receiver_id: item.signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.public_key.clone(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.permission.clone(),
std::sync::Arc::new({
let signer_account_id = item.signer_account_id.clone();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_account_id.clone(),
receiver_id: signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.public_key.clone(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.permission.clone(),
},
},
},
)],
})
)],
})
}
});

Self {
global_context: item.global_context,
interacting_with_account_ids: vec![item.signer_account_id],
on_after_getting_network_callback,
on_before_signing_callback: std::sync::Arc::new(
|_prepolulated_unsinged_transaction, _network_config| Ok(()),
Expand Down
32 changes: 19 additions & 13 deletions src/commands/account/add_key/use_public_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,29 @@ impl AddAccessKeyActionContext {
impl From<AddAccessKeyActionContext> for crate::commands::ActionContext {
fn from(item: AddAccessKeyActionContext) -> Self {
let on_after_getting_network_callback: crate::commands::OnAfterGettingNetworkCallback =
std::sync::Arc::new(move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: item.signer_account_id.clone(),
receiver_id: item.signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.public_key.clone().into(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.permission.clone(),
std::sync::Arc::new({
let signer_account_id = item.signer_account_id.clone();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: signer_account_id.clone(),
receiver_id: signer_account_id.clone(),
actions: vec![near_primitives::transaction::Action::AddKey(
near_primitives::transaction::AddKeyAction {
public_key: item.public_key.clone().into(),
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: item.permission.clone(),
},
},
},
)],
})
)],
})
}
});

Self {
global_context: item.global_context,
interacting_with_account_ids: vec![item.signer_account_id],
on_after_getting_network_callback,
on_before_signing_callback: std::sync::Arc::new(
|_prepolulated_unsinged_transaction, _network_config| Ok(()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct NewAccount {
#[derive(Debug, Clone)]
pub struct NewAccountContext {
global_context: crate::GlobalContext,
new_account_id: crate::types::account_id::AccountId,
new_account_id: near_primitives::types::AccountId,
initial_balance: crate::common::NearBalance,
}

Expand All @@ -35,7 +35,7 @@ impl NewAccountContext {
) -> color_eyre::eyre::Result<Self> {
Ok(Self {
global_context: previous_context,
new_account_id: scope.new_account_id.clone(),
new_account_id: scope.new_account_id.clone().into(),
initial_balance: scope.initial_balance.clone(),
})
}
Expand Down Expand Up @@ -154,7 +154,7 @@ pub struct AccountPropertiesContext {

#[derive(Debug, Clone)]
pub struct AccountProperties {
pub new_account_id: crate::types::account_id::AccountId,
pub new_account_id: near_primitives::types::AccountId,
pub public_key: near_crypto::PublicKey,
pub initial_balance: crate::common::NearBalance,
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ impl From<SignerAccountIdContext> for crate::commands::ActionContext {

let on_after_getting_network_callback: crate::commands::OnAfterGettingNetworkCallback =
std::sync::Arc::new({
let new_account_id: near_primitives::types::AccountId =
item.account_properties.new_account_id.clone().into();
let new_account_id = item.account_properties.new_account_id.clone();
let signer_id = item.signer_account_id.clone();

move |network_config| {
Expand Down Expand Up @@ -150,6 +149,10 @@ impl From<SignerAccountIdContext> for crate::commands::ActionContext {

Self {
global_context,
interacting_with_account_ids: vec![
item.signer_account_id,
item.account_properties.new_account_id,
],
on_after_getting_network_callback,
on_before_signing_callback: std::sync::Arc::new(
|_prepolulated_unsinged_transaction, _network_config| Ok(()),
Expand All @@ -164,11 +167,10 @@ impl SignerAccountId {
fn input_signer_account_id(
context: &super::AccountPropertiesContext,
) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
let parent_account_id = context
.account_properties
.new_account_id
.clone()
.get_parent_account_id_from_sub_account();
let parent_account_id =
crate::types::account_id::AccountId::get_parent_account_id_from_sub_account(
context.account_properties.new_account_id.clone().into(),
);
if !parent_account_id.0.is_top_level() {
Ok(Some(parent_account_id))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Network {
fn input_network_name(
context: &super::SponsorServiceContext,
) -> color_eyre::eyre::Result<Option<String>> {
crate::common::input_network_name(&context.config)
crate::common::input_network_name(&context.config, &[context.new_account_id.clone().into()])
}
}

Expand Down
26 changes: 16 additions & 10 deletions src/commands/account/delete_account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,25 @@ impl BeneficiaryAccountContext {
impl From<BeneficiaryAccountContext> for crate::commands::ActionContext {
fn from(item: BeneficiaryAccountContext) -> Self {
let on_after_getting_network_callback: crate::commands::OnAfterGettingNetworkCallback =
std::sync::Arc::new(move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: item.account_id.clone(),
receiver_id: item.account_id.clone(),
actions: vec![near_primitives::transaction::Action::DeleteAccount(
near_primitives::transaction::DeleteAccountAction {
beneficiary_id: item.beneficiary_account_id.clone(),
},
)],
})
std::sync::Arc::new({
let account_id = item.account_id.clone();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
signer_id: account_id.clone(),
receiver_id: account_id.clone(),
actions: vec![near_primitives::transaction::Action::DeleteAccount(
near_primitives::transaction::DeleteAccountAction {
beneficiary_id: item.beneficiary_account_id.clone(),
},
)],
})
}
});

Self {
global_context: item.global_context,
interacting_with_account_ids: vec![item.account_id],
on_after_getting_network_callback,
on_before_signing_callback: std::sync::Arc::new(
|_prepolulated_unsinged_transaction, _network_config| Ok(()),
Expand Down
Loading

0 comments on commit 23bb067

Please sign in to comment.