Skip to content

Commit

Permalink
Create rust client lib
Browse files Browse the repository at this point in the history
  • Loading branch information
ssantos21 committed May 26, 2024
1 parent f63427e commit d849680
Show file tree
Hide file tree
Showing 20 changed files with 108 additions and 39 deletions.
27 changes: 26 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["clients/rust", "server", "wasm", "lib", "token-server"]
members = ["clients/rust", "server", "wasm", "lib", "token-server", "clients/libs/rust"]
27 changes: 27 additions & 0 deletions clients/libs/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "mercuryrustlib"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
bech32 = { version = "0.9.1", default-features = false }
bitcoin = { version = "0.30.1", features = ["serde", "base64", "rand-std", "std", "bitcoinconsensus"], default-features = false }
bip39 = "1.2.0"
clap = { version = "4.2.5", features = ["derive"]}
chrono = "0.4.31"
config = "0.13.1"
electrum-client = "0.18.0"
hex = "0.4.3"
rand = "0.8.5"
reqwest = { version = "0.11.16", features = ["blocking", "json", "socks"] }
schemars = { version = "0.8.12", features = ["chrono", "uuid"] }
secp256k1-zkp = { git = "https://github.com/ssantos21/rust-secp256k1-zkp.git", branch = "blinded-musig-scheme", features = [ "rand-std", "bitcoin_hashes", "std" ] }
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
sqlx = { version = "0.7", features = [ "runtime-tokio", "sqlite", "time", "uuid" ] }
tokio = { version = "1.27.0", features = ["full"] }
uuid = { version = "1.3.1", features = ["v4", "serde"] }
mercurylib = { path = "../../../lib" }
2 changes: 2 additions & 0 deletions clients/libs/rust/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "1.76.0"
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,8 @@ impl ClientConfig {
}
}
}

pub async fn load() -> ClientConfig {
let client_config = ClientConfig::load().await;
client_config
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Result, Ok};
use mercurylib::{deposit::{create_deposit_msg1, create_aggregated_address}, wallet::{Wallet, BackupTx, CoinStatus, Coin}, transaction:: get_user_backup_address, utils::get_blockheight};

use crate::{sqlite_manager::update_wallet, get_wallet, client_config::ClientConfig, transaction::new_transaction};
use crate::{client_config::ClientConfig, sqlite_manager::{get_wallet, update_wallet}, transaction::new_transaction};

pub async fn get_deposit_bitcoin_address(client_config: &ClientConfig, wallet_name: &str, token_id: &str, amount: u32) -> Result<String> {

Expand Down Expand Up @@ -100,10 +100,6 @@ pub async fn init(client_config: &ClientConfig, wallet: &Wallet, token_id: uuid:

let deposit_msg_1_response: mercurylib::deposit::DepositMsg1Response = serde_json::from_str(value.as_str())?;

// println!("value: {:?}", value);

// println!("response: {:?}", deposit_msg_1_response);

let deposit_init_result = mercurylib::deposit::handle_deposit_msg_1_response(&coin, &deposit_msg_1_response)?;

let coin = wallet.coins.last_mut().unwrap();
Expand Down
26 changes: 26 additions & 0 deletions clients/libs/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pub mod broadcast_backup_tx;
pub mod client_config;
pub mod coin_status;
pub mod deposit;
pub mod sqlite_manager;
pub mod transaction;
pub mod transfer_receiver;
pub mod transfer_sender;
pub mod utils;
pub mod wallet;
pub mod withdraw;

pub fn add(left: usize, right: usize) -> usize {
left + right
}

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

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion clients/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ serde_json = "1.0.96"
sqlx = { version = "0.7", features = [ "runtime-tokio", "sqlite", "time", "uuid" ] }
tokio = { version = "1.27.0", features = ["full"] }
uuid = { version = "1.3.1", features = ["v4", "serde"] }
mercurylib = { path = "../../lib" }
mercuryrustlib = { path = "../libs/rust" }
47 changes: 16 additions & 31 deletions clients/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
mod client_config;
mod wallet;
mod utils;
mod sqlite_manager;
mod deposit;
mod transaction;
mod broadcast_backup_tx;
mod withdraw;
mod transfer_sender;
mod transfer_receiver;
mod coin_status;

use anyhow::Result;
use clap::{Parser, Subcommand};
use client_config::ClientConfig;
use serde_json::json;

use crate::{wallet::create_wallet, sqlite_manager::get_wallet};

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
Expand Down Expand Up @@ -81,38 +66,38 @@ async fn main() -> Result<()> {

let cli = Cli::parse();

let client_config = ClientConfig::load().await;
let client_config = mercuryrustlib::client_config::load().await;

match cli.command {
Commands::CreateWallet { name } => {
let wallet = create_wallet(
let wallet = mercuryrustlib::wallet::create_wallet(
&name,
&client_config).await?;

sqlite_manager::insert_wallet(&client_config.pool, &wallet).await?;
mercuryrustlib::sqlite_manager::insert_wallet(&client_config.pool, &wallet).await?;
println!("Wallet created: {:?}", wallet);
},
Commands::NewToken { } => {
let token_id = deposit::get_token(&client_config).await?;
let token_id = mercuryrustlib::deposit::get_token(&client_config).await?;

let obj = json!({"token": token_id});

println!("{}", serde_json::to_string_pretty(&obj).unwrap());
},
Commands::NewDepositAddress { wallet_name, token_id, amount } => {
let address = deposit::get_deposit_bitcoin_address(&client_config, &wallet_name, &token_id, amount).await?;
let address = mercuryrustlib::deposit::get_deposit_bitcoin_address(&client_config, &wallet_name, &token_id, amount).await?;

let obj = json!({"address": address});

println!("{}", serde_json::to_string_pretty(&obj).unwrap());
},
Commands::BroadcastBackupTransaction { wallet_name, statechain_id, to_address, fee_rate } => {
coin_status::update_coins(&client_config, &wallet_name).await?;
broadcast_backup_tx::execute(&client_config, &wallet_name, &statechain_id, &to_address, fee_rate).await?;
mercuryrustlib::coin_status::update_coins(&client_config, &wallet_name).await?;
mercuryrustlib::broadcast_backup_tx::execute(&client_config, &wallet_name, &statechain_id, &to_address, fee_rate).await?;
},
Commands::ListStatecoins { wallet_name } => {
coin_status::update_coins(&client_config, &wallet_name).await?;
let wallet: mercurylib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;
mercuryrustlib::coin_status::update_coins(&client_config, &wallet_name).await?;
let wallet = mercuryrustlib::sqlite_manager::get_wallet(&client_config.pool, &wallet_name).await?;

let mut coins_json = Vec::new();

Expand All @@ -134,11 +119,11 @@ async fn main() -> Result<()> {
println!("{}", coins_json_string);
},
Commands::Withdraw { wallet_name, statechain_id, to_address, fee_rate } => {
coin_status::update_coins(&client_config, &wallet_name).await?;
withdraw::execute(&client_config, &wallet_name, &statechain_id, &to_address, fee_rate).await?;
mercuryrustlib::coin_status::update_coins(&client_config, &wallet_name).await?;
mercuryrustlib::withdraw::execute(&client_config, &wallet_name, &statechain_id, &to_address, fee_rate).await?;
},
Commands::NewTransferAddress { wallet_name, generate_batch_id } => {
let address = transfer_receiver::new_transfer_address(&client_config, &wallet_name).await?;
let address = mercuryrustlib::transfer_receiver::new_transfer_address(&client_config, &wallet_name).await?;

let mut obj = json!({"new_transfer_address:": address});

Expand All @@ -152,16 +137,16 @@ async fn main() -> Result<()> {
println!("{}", serde_json::to_string_pretty(&obj).unwrap());
},
Commands::TransferSend { wallet_name, statechain_id, to_address, batch_id } => {
coin_status::update_coins(&client_config, &wallet_name).await?;
transfer_sender::execute(&client_config, &to_address, &wallet_name, &statechain_id, batch_id).await?;
mercuryrustlib::coin_status::update_coins(&client_config, &wallet_name).await?;
mercuryrustlib::transfer_sender::execute(&client_config, &to_address, &wallet_name, &statechain_id, batch_id).await?;

let obj = json!({"Transfer": "sent"});

println!("{}", serde_json::to_string_pretty(&obj).unwrap());
},
Commands::TransferReceive { wallet_name } => {
coin_status::update_coins(&client_config, &wallet_name).await?;
let received_statechain_ids = transfer_receiver::execute(&client_config, &wallet_name).await?;
mercuryrustlib::coin_status::update_coins(&client_config, &wallet_name).await?;
let received_statechain_ids = mercuryrustlib::transfer_receiver::execute(&client_config, &wallet_name).await?;

let obj = json!(received_statechain_ids);

Expand Down
3 changes: 3 additions & 0 deletions server/src/endpoints/transfer_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub async fn validate_batch_transfer(statechain_entity: &State<StateChainEntity>
let (batch_id, batch_time) = batch_info.unwrap();

if !is_batch_expired(&statechain_entity, batch_time) {

// TODO: check if the batch is complete. If complete, should return success.

// the batch time has not expired
return BatchTransferValidationResult::StatecoinBatchLockedError("Statecoin batch locked (the batch time has not expired).".to_string())
} else {
Expand Down

0 comments on commit d849680

Please sign in to comment.