Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Add interactive stake allow
Browse files Browse the repository at this point in the history
Resolves #98
  • Loading branch information
Neotamandua committed Oct 5, 2023
1 parent 47fa443 commit 827a061
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Add interactive stake allow [[#98](https://github.com/dusk-network/wallet-cli/issues/98)]

### Fixed

- Fix staking address display [#204]
Expand Down Expand Up @@ -431,6 +434,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Implementation of `Store` trait from `wallet-core`
- Implementation of `State` and `Prover` traits from `wallet-core`

[#98]: https://github.com/dusk-network/wallet-cli/issues/98
[#179]: https://github.com/dusk-network/wallet-cli/issues/179
[#204]: https://github.com/dusk-network/wallet-cli/issues/204
[#182]: https://github.com/dusk-network/wallet-cli/issues/182
Expand Down
8 changes: 8 additions & 0 deletions src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ enum CommandMenuItem {
History,
Transfer,
Stake,
StakeAllow,
StakeInfo,
Unstake,
Withdraw,
Expand All @@ -212,6 +213,7 @@ fn menu_op(
.add(CMI::History, "Transaction History")
.add(CMI::Transfer, "Transfer Dusk")
.add(CMI::Stake, "Stake Dusk")
.add(CMI::StakeAllow, "Allow staking key")
.add(CMI::StakeInfo, "Check existing stake")
.add(CMI::Unstake, "Unstake Dusk")
.add(CMI::Withdraw, "Withdraw staking reward")
Expand Down Expand Up @@ -244,6 +246,12 @@ fn menu_op(
gas_limit: prompt::request_gas_limit(DEFAULT_STAKE_GAS_LIMIT)?,
gas_price: prompt::request_gas_price()?,
})),
CMI::StakeAllow => AddrOp::Run(Box::new(Command::StakeAllow {
addr: Some(addr),
key: prompt::request_provisioner_key()?,
gas_limit: prompt::request_gas_limit(DEFAULT_STAKE_GAS_LIMIT)?,
gas_price: prompt::request_gas_price()?,
})),
CMI::StakeInfo => AddrOp::Run(Box::new(Command::StakeInfo {
addr: Some(addr),
reward: false,
Expand Down
31 changes: 30 additions & 1 deletion src/bin/io/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use requestty::Question;
use dusk_wallet::{Address, Dusk, Lux};

use dusk_wallet::gas;
use dusk_wallet::{MAX_CONVERTIBLE, MIN_CONVERTIBLE};
use dusk_wallet::{MAX_CONVERTIBLE, MIN_CONVERTIBLE, PROVISIONER_KEY_LENGTH};
use sha2::{Digest, Sha256};

/// Request the user to authenticate with a password
Expand Down Expand Up @@ -231,6 +231,22 @@ fn check_valid_denom(value: f64, balance: Dusk) -> Result<(), String> {
}
}

fn check_valid_pkey(key: &str) -> Result<(), String> {
let key_length = match bs58::decode(key).into_vec() {
Ok(v) => v.len(),
Err(_) => {
return Err(
"Please introduce a valid provisioner key (Base58)".to_string()
)
}
};
if key_length == PROVISIONER_KEY_LENGTH {
Ok(())
} else {
Err(format!("Invalid provisioner key length. Key should be {} bytes long but was {} bytes long", PROVISIONER_KEY_LENGTH, key_length))
}
}

/// Request amount of tokens
pub(crate) fn request_token_amt(
action: &str,
Expand Down Expand Up @@ -280,6 +296,19 @@ pub(crate) fn request_gas_price() -> anyhow::Result<Lux> {
Ok(*price)
}

/// Request provisioner key
pub(crate) fn request_provisioner_key() -> anyhow::Result<String> {
// let the user input the provisioner key
let q = Question::input("key")
.message("Please enter the provisioner key:")
.validate_on_key(|pkey, _| check_valid_pkey(pkey).is_ok())
.validate(|pkey, _| check_valid_pkey(pkey))
.build();

let a = requestty::prompt_one(q)?;
Ok(a.as_string().expect("answer to be a string").to_owned())
}

/// Request Dusk block explorer to be opened
pub(crate) fn launch_explorer(url: String) -> Result<()> {
let q = requestty::Question::confirm("launch")
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ pub const MIN_CONVERTIBLE: Dusk = Dusk::new(1);
pub const EPOCH: u64 = 2160;
/// Max addresses the wallet can store
pub const MAX_ADDRESSES: usize = 255;
/// The length of a provisioner key
pub const PROVISIONER_KEY_LENGTH: usize = 96;

0 comments on commit 827a061

Please sign in to comment.