Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suggestions for all relevant JS commands #345

Merged
merged 42 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
228a8a3
initial fixing suggested commands for js versions + test
garikbesson May 23, 2024
6acbce5
building suggestion for js version of commands
garikbesson Jun 4, 2024
6fc63a8
account creation tests + fixes
garikbesson Jun 10, 2024
447807f
tests for deleting account
garikbesson Jun 10, 2024
a761226
updated test framework
gagdiez Jun 11, 2024
c61b700
fix regex in tests
garikbesson Jun 12, 2024
f8b59b4
tests for checking storage/state functions
garikbesson Jun 12, 2024
63dbdbb
contract, key actions testcases, fixed delete_key and deploy functions
garikbesson Jun 13, 2024
fe10614
credentials, keys and near actions tests
garikbesson Jun 17, 2024
8153485
removed debug logs
garikbesson Jun 17, 2024
437037b
Update src/js_command_match/add_credentials.rs
gagdiez Jun 19, 2024
42d3305
Update src/js_command_match/add_credentials.rs
gagdiez Jun 19, 2024
7579ca2
experimental JS commands translation
frol Jun 19, 2024
c0d8ce5
refactored the unit-test to use clap::Parser
frol Jun 25, 2024
1fdac41
using keychain for creating account (js syntax)
garikbesson Jun 27, 2024
388a7d3
CreateAccountArgs::to_cli_args unit tests
garikbesson Jun 28, 2024
50b13c3
removed integration js tests, added add_credentials unit tests, moved…
garikbesson Jun 28, 2024
38345ea
AddKeyArgs::to_cli_args unit tests
garikbesson Jun 29, 2024
b68e151
CallArgs::to_cli_args unit tests
garikbesson Jun 29, 2024
0b74e6a
DeleteAccountArgs::to_cli_args unit tests
garikbesson Jun 29, 2024
bceba36
[...]Args::to_cli_args unit tests
garikbesson Jul 2, 2024
63f10a4
chore: format files
gagdiez Jul 4, 2024
ee2deb2
chore: re-organize and evaluate
gagdiez Jul 4, 2024
b1b6835
fix: various commands
gagdiez Jul 5, 2024
c31b332
fix: merge conflict
gagdiez Jul 5, 2024
78f031c
chore: format
gagdiez Jul 5, 2024
d767b84
chore: remove unnecesary pub
gagdiez Jul 5, 2024
3f7034d
fix: add back existing pub
gagdiez Jul 5, 2024
e544418
feat: clippy
gagdiez Jul 5, 2024
8900733
Apply suggestions from code review
gagdiez Jul 5, 2024
26521a3
apply code suggestions
gagdiez Jul 5, 2024
be125ee
minor refactoring
frol Jul 5, 2024
e594f6f
suggestion for refactoring (two options)
frol Jul 5, 2024
9e0fb9a
apply minor fixes after review
garikbesson Jul 8, 2024
24cff01
refactored suggestions unit tests
garikbesson Jul 9, 2024
46da562
wip
gagdiez Jul 9, 2024
cabe809
Merge branch 'main' into fix-js-commands
gagdiez Jul 9, 2024
0ee1f8e
feat: add path to use-ledger
gagdiez Jul 9, 2024
b02c2d0
fix: aliases and style
gagdiez Jul 9, 2024
0d4b4dd
more code style fixes
garikbesson Jul 9, 2024
ef37494
fixes aliases indexes in unit tests
garikbesson Jul 9, 2024
45e983b
tested JS commands aliases (added missing 'keys', 'import-account', a…
frol Jul 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions src/js_command_match/account/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
use crate::js_command_match::constants::{
DEFAULT_SEED_PHRASE_PATH, INITIAL_BALANCE_ALIASES, LEDGER_PATH_ALIASES, NETWORK_ID_ALIASES,
PK_LEDGER_PATH_ALIASES, PUBLIC_KEY_ALIASES, SEED_PHRASE_ALIASES, SIGN_WITH_LEDGER_ALIASES,
USE_ACCOUNT_ALIASES, USE_FAUCET_ALIASES, USE_LEDGER_PK_ALIASES,
};

#[derive(Debug, Clone, clap::Parser)]
#[clap(alias("create"))]
pub struct CreateAccountArgs {
new_account_id: String,
#[clap(long, aliases = USE_FAUCET_ALIASES, default_value_t = false)]
use_faucet: bool,
#[clap(long, aliases = USE_ACCOUNT_ALIASES, conflicts_with = "use_faucet")]
use_account: Option<String>,
#[clap(long, aliases = INITIAL_BALANCE_ALIASES, default_value = "1")]
initial_balance: String,
#[clap(long, aliases = PUBLIC_KEY_ALIASES)]
public_key: Option<String>,
#[clap(long, aliases = SEED_PHRASE_ALIASES, conflicts_with = "public_key")]
seed_phrase: Option<String>,
#[clap(long, aliases = SIGN_WITH_LEDGER_ALIASES, default_value_t = false, conflicts_with="use_faucet")]
sign_with_ledger: bool,
#[clap(long, aliases = LEDGER_PATH_ALIASES, default_value = DEFAULT_SEED_PHRASE_PATH)]
ledger_path: String,
#[clap(long, aliases = USE_LEDGER_PK_ALIASES, default_value_t = false, conflicts_with = "public_key")]
use_ledger_pk: bool,
#[clap(long, aliases = PK_LEDGER_PATH_ALIASES, default_value = DEFAULT_SEED_PHRASE_PATH)]
pk_ledger_path: String,
#[clap(long, aliases = NETWORK_ID_ALIASES)]
network_id: Option<String>,
}

impl CreateAccountArgs {
pub fn to_cli_args(&self, network_config: String) -> Vec<String> {
let network_id = self.network_id.clone().unwrap_or(network_config);

let mut command = vec!["account".to_string(), "create-account".to_string()];

if self.use_faucet {
command.push("sponsor-by-faucet-service".to_string());
command.push(self.new_account_id.to_owned());
} else {
command.push("fund-myself".to_string());
command.push(self.new_account_id.to_owned());
command.push(format!("{} NEAR", self.initial_balance));
}

if self.use_ledger_pk {
command.push("use-ledger".to_owned());

// add after issue with ledger key is resolved
// command.push(format!("--seed-phrase-hd-path {}", self.pk_ledger_path.clone().unwrap()));
frol marked this conversation as resolved.
Show resolved Hide resolved
} else if let Some(seed_phrase) = &self.seed_phrase {
command.push("use-manually-provided-seed-phrase".to_string());
command.push(seed_phrase.clone());
} else if let Some(public_key) = &self.public_key {
command.push("use-manually-provided-public-key".to_string());
command.push(public_key.clone());
} else {
command.push("autogenerate-new-keypair".to_string());
command.push("save-to-keychain".to_string());
}

if !self.use_faucet {
command.push("sign-as".to_string());
command.push(
self.use_account
.to_owned()
.expect("Valid master account must be provided"),
);
};

command.push("network-config".to_string());
command.push(network_id);

if self.use_faucet {
command.push("create".to_string());
} else {
if self.sign_with_ledger {
command.push("sign-with-ledger".to_string());
command.push("--seed-phrase-hd-path".to_string());
command.push(self.ledger_path.to_owned());
} else {
command.push("sign-with-keychain".to_string());
}
command.push("send".to_string());
}

command
}
}

#[cfg(test)]
mod tests {
use super::super::super::JsCmd;
use super::*;
use clap::Parser;

#[test]
fn create_account_v1() {
for (input, expected_output) in [
(
format!("near create bob.testnet --{}", USE_FAUCET_ALIASES[0]),
"account create-account sponsor-by-faucet-service bob.testnet autogenerate-new-keypair save-to-keychain network-config testnet create"
),
(
format!("near create bob.testnet --{}", USE_FAUCET_ALIASES[1]),
"account create-account sponsor-by-faucet-service bob.testnet autogenerate-new-keypair save-to-keychain network-config testnet create"
),
(
format!("near create bob.testnet --{} alice.testnet", USE_ACCOUNT_ALIASES[0]),
"account create-account fund-myself bob.testnet '1 NEAR' autogenerate-new-keypair save-to-keychain sign-as alice.testnet network-config testnet sign-with-keychain send"
),
(
format!("near create bob.testnet --useAccount alice.testnet --{} 0.1", INITIAL_BALANCE_ALIASES[0]),
"account create-account fund-myself bob.testnet '0.1 NEAR' autogenerate-new-keypair save-to-keychain sign-as alice.testnet network-config testnet sign-with-keychain send"
),
(
format!("near create bob.testnet --{} 'crisp clump stay mean dynamic become fashion mail bike disorder chronic sight' --useFaucet", SEED_PHRASE_ALIASES[0]),
"account create-account sponsor-by-faucet-service bob.testnet use-manually-provided-seed-phrase 'crisp clump stay mean dynamic become fashion mail bike disorder chronic sight' network-config testnet create"
),
(
format!("near create bob.testnet --useAccount alice.testnet --{} 78MziB9aTNsu19MHHVrfWy762S5mAqXgCB6Vgvrv9uGV --initialBalance 0.1", PUBLIC_KEY_ALIASES[0]),
"account create-account fund-myself bob.testnet '0.1 NEAR' use-manually-provided-public-key 78MziB9aTNsu19MHHVrfWy762S5mAqXgCB6Vgvrv9uGV sign-as alice.testnet network-config testnet sign-with-keychain send"
),
(
format!("near create bob.testnet --{} --useFaucet", USE_LEDGER_PK_ALIASES[0]),
"account create-account sponsor-by-faucet-service bob.testnet use-ledger network-config testnet create"
),
(
"near create bob.testnet --useAccount alice.testnet --signWithLedger --networkId testnet".to_string(),
"account create-account fund-myself bob.testnet '1 NEAR' autogenerate-new-keypair save-to-keychain sign-as alice.testnet network-config testnet sign-with-ledger --seed-phrase-hd-path '44'\\''/397'\\''/0'\\''/0'\\''/1'\\''' send"
),
(
"near create bob.near --useAccount alice.near --signWithLedger --ledgerPath \"44'/397'/0'/0'/2'\" --networkId mainnet".to_string(),
"account create-account fund-myself bob.near '1 NEAR' autogenerate-new-keypair save-to-keychain sign-as alice.near network-config mainnet sign-with-ledger --seed-phrase-hd-path '44'\\''/397'\\''/0'\\''/0'\\''/2'\\''' send"
)
] {
let input_cmd = shell_words::split(&input).expect("Input command must be a valid shell command");
let JsCmd::CreateAccount(create_account_args) = JsCmd::parse_from(&input_cmd) else {
panic!("CreateAccount command was expected, but something else was parsed out from {input}");
};
assert_eq!(
shell_words::join(CreateAccountArgs::to_cli_args(&create_account_args, "testnet".to_string())),
expected_output
);
}
}

#[test]
fn create_account_v2() {
for (input, expected_output) in [
(
"near create bob.testnet --useAccount alice.testnet --signWithLedger --networkId testnet".to_string(),
"account create-account fund-myself bob.testnet '1 NEAR' autogenerate-new-keypair save-to-keychain sign-as alice.testnet network-config testnet sign-with-ledger --seed-phrase-hd-path '44'\\''/397'\\''/0'\\''/0'\\''/1'\\''' send"
),
(
"near create bob.near --useAccount alice.near --signWithLedger --ledgerPath \"44'/397'/0'/0'/2'\" --networkId mainnet".to_string(),
"account create-account fund-myself bob.near '1 NEAR' autogenerate-new-keypair save-to-keychain sign-as alice.near network-config mainnet sign-with-ledger --seed-phrase-hd-path '44'\\''/397'\\''/0'\\''/0'\\''/2'\\''' send"
)
].into_iter().chain(
USE_FAUCET_ALIASES.iter().map(|use_faucet_alias| (
format!("near create bob.testnet --{use_faucet_alias}"),
"account create-account sponsor-by-faucet-service bob.testnet autogenerate-new-keypair save-to-keychain network-config testnet create"
))
).chain(
USE_ACCOUNT_ALIASES.iter().map(|use_account_alias| (
format!("near create bob.testnet --{use_account_alias} alice.testnet"),
"account create-account fund-myself bob.testnet '1 NEAR' autogenerate-new-keypair save-to-keychain sign-as alice.testnet network-config testnet sign-with-keychain send"
))
).chain(
INITIAL_BALANCE_ALIASES.iter().map(|initial_balance_alias| (
format!("near create bob.testnet --useAccount alice.testnet --{initial_balance_alias} 0.1"),
"account create-account fund-myself bob.testnet '0.1 NEAR' autogenerate-new-keypair save-to-keychain sign-as alice.testnet network-config testnet sign-with-keychain send"
))
).chain(
SEED_PHRASE_ALIASES.iter().map(|seed_phrase_alias| (
format!("near create bob.testnet --{seed_phrase_alias} 'crisp clump stay mean dynamic become fashion mail bike disorder chronic sight' --useFaucet"),
"account create-account sponsor-by-faucet-service bob.testnet use-manually-provided-seed-phrase 'crisp clump stay mean dynamic become fashion mail bike disorder chronic sight' network-config testnet create"
))
).chain(
PUBLIC_KEY_ALIASES.iter().map(|public_key_alias| (
format!("near create bob.testnet --useAccount alice.testnet --{public_key_alias} 78MziB9aTNsu19MHHVrfWy762S5mAqXgCB6Vgvrv9uGV --initialBalance 0.1"),
"account create-account fund-myself bob.testnet '0.1 NEAR' use-manually-provided-public-key 78MziB9aTNsu19MHHVrfWy762S5mAqXgCB6Vgvrv9uGV sign-as alice.testnet network-config testnet sign-with-keychain send"
))
).chain(
USE_LEDGER_PK_ALIASES.iter().map(|use_ledger_pk_alias| (
format!("near create bob.testnet --{use_ledger_pk_alias} --useFaucet"),
"account create-account sponsor-by-faucet-service bob.testnet use-ledger network-config testnet create"
))
) {
let input_cmd = shell_words::split(&input).expect("Input command must be a valid shell command");
let JsCmd::CreateAccount(create_account_args) = JsCmd::parse_from(&input_cmd) else {
panic!("CreateAccount command was expected, but something else was parsed out from {input}");
};
assert_eq!(
shell_words::join(CreateAccountArgs::to_cli_args(&create_account_args, "testnet".to_string())),
expected_output
);
}
}
}
142 changes: 142 additions & 0 deletions src/js_command_match/account/delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use crate::js_command_match::constants::{
DEFAULT_SEED_PHRASE_PATH, LEDGER_PATH_ALIASES, NETWORK_ID_ALIASES, SIGN_WITH_LEDGER_ALIASES,
};

#[derive(Debug, Clone, clap::Parser)]
#[clap(alias("delete"))]
pub struct DeleteAccountArgs {
account_id: String,
beneficiary_id: String,
#[clap(long, aliases = SIGN_WITH_LEDGER_ALIASES, default_value_t = false)]
sign_with_ledger: bool,
#[clap(long, aliases = LEDGER_PATH_ALIASES, default_value = Some(DEFAULT_SEED_PHRASE_PATH))]
ledger_path: Option<String>,
#[clap(long, aliases = NETWORK_ID_ALIASES, default_value=None)]
network_id: Option<String>,
#[clap(long, default_value_t = false)]
force: bool,
}

impl DeleteAccountArgs {
pub fn to_cli_args(&self, network_config: String) -> Vec<String> {
let network_id = self.network_id.clone().unwrap_or(network_config);

let mut command = vec![
"account".to_string(),
"delete-account".to_string(),
self.account_id.to_owned(),
"beneficiary".to_string(),
self.beneficiary_id.to_owned(),
];

command.push("network-config".to_string());
command.push(network_id);

if self.sign_with_ledger {
command.push("sign-with-ledger".to_string());
command.push("--seed-phrase-hd-path".to_string());
command.push(self.ledger_path.to_owned().unwrap_or_default());
} else {
command.push("sign-with-keychain".to_string());
}

if self.force {
command.push("send".to_string());
}

command
}
}

#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;

#[test]
fn delete_account_using_ledger_testnet() {
let account_id = "bob.testnet";
let beneficiary_id = "alice.testnet";

for use_ledger_parameter_alias in SIGN_WITH_LEDGER_ALIASES {
let delete_args = DeleteAccountArgs::parse_from(&[
"near",
account_id,
beneficiary_id,
&format!("--{use_ledger_parameter_alias}"),
"--force",
]);
let result = DeleteAccountArgs::to_cli_args(&delete_args, "testnet".to_string());
assert_eq!(
result.join(" "),
format!(
"account delete-account {account_id} beneficiary {beneficiary_id} network-config testnet sign-with-ledger --seed-phrase-hd-path {DEFAULT_SEED_PHRASE_PATH} send",
)
)
}
}

#[test]
fn delete_account_using_ledger_and_custom_path_testnet() {
let account_id = "bob.testnet";
let beneficiary_id = "alice.testnet";

for use_ledger_alias in SIGN_WITH_LEDGER_ALIASES {
let delete_args = DeleteAccountArgs::parse_from(&[
"near",
account_id,
beneficiary_id,
&format!("--{use_ledger_alias}"),
"--ledgerPath",
DEFAULT_SEED_PHRASE_PATH,
"--force",
]);
let result = DeleteAccountArgs::to_cli_args(&delete_args, "testnet".to_string());
assert_eq!(
result.join(" "),
format!(
"account delete-account {account_id} beneficiary {beneficiary_id} network-config testnet sign-with-ledger --seed-phrase-hd-path {DEFAULT_SEED_PHRASE_PATH} send",
)
)
}
}

#[test]
fn delete_account_using_ledger_mainnet() {
let account_id = "bob.testnet";
let beneficiary_id = "alice.testnet";
let network_id = "mainnet";

let delete_args = DeleteAccountArgs::parse_from(&[
"near",
account_id,
beneficiary_id,
"--signWithLedger",
"--networkId",
network_id,
"--force",
]);
let result = DeleteAccountArgs::to_cli_args(&delete_args, "testnet".to_string());
assert_eq!(
result.join(" "),
format!(
"account delete-account {account_id} beneficiary {beneficiary_id} network-config {network_id} sign-with-ledger --seed-phrase-hd-path {DEFAULT_SEED_PHRASE_PATH} send",
)
)
}

#[test]
fn delete_account_using_keychain_testnet() {
let account_id = "bob.testnet";
let beneficiary_id = "alice.testnet";
let delete_args =
DeleteAccountArgs::parse_from(&["near", account_id, beneficiary_id, "--force"]);
let result = DeleteAccountArgs::to_cli_args(&delete_args, "testnet".to_string());
assert_eq!(
result.join(" "),
format!(
"account delete-account {account_id} beneficiary {beneficiary_id} network-config testnet sign-with-keychain send",
)
);
}
}
42 changes: 42 additions & 0 deletions src/js_command_match/account/login.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::js_command_match::constants::NETWORK_ID_ALIASES;

#[derive(Debug, Clone, clap::Parser)]
/// This is a legacy `legacy` command. Once you run it with the specified arguments, new syntax command will be suggested.
pub struct LoginArgs {
#[clap(long, aliases = NETWORK_ID_ALIASES, default_value=None)]
network_id: Option<String>,
}

impl LoginArgs {
pub fn to_cli_args(&self, network_config: String) -> Vec<String> {
let network_id = self.network_id.clone().unwrap_or(network_config);

let command = vec![
"account".to_string(),
"import-account".to_string(),
"using-web-wallet".to_string(),
"network-config".to_string(),
network_id,
];

command
}
}

#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;

#[test]
fn login() {
for network_id in vec!["testnet", "mainnet"] {
let login_args = LoginArgs::parse_from(&["near", "--networkId", network_id]);
let result = LoginArgs::to_cli_args(&login_args, "testnet".to_string());
assert_eq!(
result.join(" "),
format!("account import-account using-web-wallet network-config {network_id}",)
);
}
}
}
4 changes: 4 additions & 0 deletions src/js_command_match/account/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod create;
pub mod delete;
pub mod login;
pub mod state;
Loading
Loading