Skip to content

Commit

Permalink
Implement cli transfer and withdraw calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-D-coder committed Jun 25, 2024
1 parent f78dc5f commit afafb9f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kairos-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ thiserror = "1"
kairos-crypto = { path = "../kairos-crypto", features = ["std"] }
kairos-tx = { path = "../kairos-tx" }
kairos-server = { path = "../kairos-server" }
axum-extra = { version = "0.9", features = [ "typed-routing" ] }
reqwest = { version = "0.12", features = ["blocking", "json"] }
serde_json = "1.0"
serde = "1.0"
Expand Down
31 changes: 26 additions & 5 deletions kairos-cli/src/commands/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use crate::common::args::{AmountArg, PrivateKeyPathArg};
use crate::client::KairosClientError;
use crate::common::args::{AmountArg, NonceArg, PrivateKeyPathArg};
use crate::error::CliError;
use crate::utils::parse_hex_string;

use axum_extra::routing::TypedPath;
use kairos_crypto::error::CryptoError;
use kairos_crypto::implementations::Signer;
use kairos_crypto::SignerCore;
use kairos_crypto::SignerFsExtension;

use clap::Parser;
use kairos_server::routes::transfer::TransferPath;
use kairos_server::routes::PayloadBody;
use kairos_tx::asn::{SigningPayload, Transfer};
use reqwest::Url;

#[derive(Parser)]
pub struct Args {
Expand All @@ -17,17 +23,32 @@ pub struct Args {
amount: AmountArg,
#[clap(flatten)]
private_key_path: PrivateKeyPathArg,
#[clap(flatten)]
nonce: NonceArg,
}

pub fn run(args: Args) -> Result<String, CliError> {
let _recipient = Signer::from_public_key(args.recipient)?.to_public_key()?;
let _amount: u64 = args.amount.field;
let _signer =
pub async fn run(args: Args, kairos_server_address: Url) -> Result<String, CliError> {
let recipient = Signer::from_public_key(args.recipient)?.to_public_key()?;
let amount: u64 = args.amount.field;
let signer =
Signer::from_private_key_file(args.private_key_path.field).map_err(CryptoError::from)?;
let nonce = args.nonce.val;

// TODO: Create transaction and sign it with `signer`.

// TODO: Send transaction to the network, using Rust SDK.
reqwest::Client::new()
.post(kairos_server_address.join(TransferPath::PATH).unwrap())
.json(&PayloadBody {
public_key: signer.to_public_key()?,
payload: SigningPayload::new(nonce, Transfer::new(recipient, amount))
.try_into()
.unwrap(),
signature: vec![],
})
.send()
.await
.map_err(KairosClientError::from)?;

Ok("ok".to_string())
}
31 changes: 26 additions & 5 deletions kairos-cli/src/commands/withdraw.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
use crate::common::args::{AmountArg, PrivateKeyPathArg};
use crate::client::KairosClientError;
use crate::common::args::{AmountArg, NonceArg, PrivateKeyPathArg};
use crate::error::CliError;

use kairos_crypto::error::CryptoError;
use kairos_crypto::implementations::Signer;
use kairos_crypto::SignerFsExtension;
use kairos_crypto::{SignerCore, SignerFsExtension};

use clap::Parser;
use kairos_server::routes::transfer::TransferPath;
use kairos_server::routes::withdraw::WithdrawPath;
use kairos_server::routes::PayloadBody;
use kairos_tx::asn::{SigningPayload, Withdrawal};
use reqwest::Url;

#[derive(Parser)]
pub struct Args {
#[clap(flatten)]
amount: AmountArg,
#[clap(flatten)]
private_key_path: PrivateKeyPathArg,
#[clap(flatten)]
nonce: NonceArg,
}

pub fn run(args: Args) -> Result<String, CliError> {
let _amount: u64 = args.amount.field;
let _signer =
pub async fn run(args: Args, kairos_server_address: Url) -> Result<String, CliError> {
let amount: u64 = args.amount.field;
let signer =
Signer::from_private_key_file(args.private_key_path.field).map_err(CryptoError::from)?;
let nonce = args.nonce.val;

// TODO: Create transaction and sign it with `signer`.

// TODO: Send transaction to the network, using Rust SDK.
reqwest::Client::new()
.post(kairos_server_address.join(WithdrawPath::PATH).unwrap())
.json(&PayloadBody {
public_key: signer.to_public_key()?,
payload: SigningPayload::new(nonce, Withdrawal::new(amount))
.try_into()
.unwrap(),
signature: vec![],
})
.send()
.await
.map_err(KairosClientError::from)?;

Ok("ok".to_string())
}
6 changes: 6 additions & 0 deletions kairos-cli/src/common/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ pub struct PrivateKeyPathArg {
#[arg(id = "private-key", long, short = 'k', value_name = "FILE_PATH")]
pub field: PathBuf,
}

#[derive(Args, Debug)]
pub struct NonceArg {
#[arg(id = "nonce", long, short, value_name = "NUM")]
pub val: u64,
}
4 changes: 2 additions & 2 deletions kairos-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn run(
) -> Result<String, CliError> {
match command {
Command::Deposit(args) => commands::deposit::run(args, kairos_server_address),
Command::Transfer(args) => commands::transfer::run(args),
Command::Withdraw(args) => commands::withdraw::run(args),
Command::Transfer(args) => commands::transfer::run(args, kairos_server_address),
Command::Withdraw(args) => commands::withdraw::run(args, kairos_server_address),
}
}

0 comments on commit afafb9f

Please sign in to comment.