Skip to content

Commit

Permalink
Update WASM and rename lib
Browse files Browse the repository at this point in the history
  • Loading branch information
ssantos21 committed Apr 27, 2024
1 parent 33b545a commit 9660b00
Show file tree
Hide file tree
Showing 40 changed files with 116 additions and 3,142 deletions.
37 changes: 18 additions & 19 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions clients/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +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"] }

[dependencies.mercury-lib]
path = "../../lib"
mercurylib = { path = "../../lib" }
6 changes: 3 additions & 3 deletions clients/rust/src/broadcast_backup_tx.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{client_config::ClientConfig, sqlite_manager::{get_backup_txs, get_wallet, update_wallet}};
use anyhow::{anyhow, Result};
use electrum_client::ElectrumApi;
use mercury_lib::wallet::{cpfp_tx, CoinStatus};
use mercurylib::wallet::{cpfp_tx, CoinStatus};

pub async fn execute(client_config: &ClientConfig, wallet_name: &str, statechain_id: &str, to_address: &str, fee_rate: Option<u64>) -> Result<()> {

let mut wallet: mercury_lib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;
let mut wallet: mercurylib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;

let is_address_valid = mercury_lib::validate_address(to_address, &wallet.network)?;
let is_address_valid = mercurylib::validate_address(to_address, &wallet.network)?;

if !is_address_valid {
return Err(anyhow!("Invalid address"));
Expand Down
4 changes: 2 additions & 2 deletions clients/rust/src/coin_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;

use bitcoin::Address;
use electrum_client::{ElectrumApi, ListUnspentRes};
use mercury_lib::wallet::{CoinStatus, Coin, Activity, BackupTx};
use mercurylib::wallet::{CoinStatus, Coin, Activity, BackupTx};
use anyhow::{anyhow, Result, Ok};
use serde_json::Value;

Expand Down Expand Up @@ -174,7 +174,7 @@ async fn check_withdrawal(client_config: &ClientConfig, coin: &mut Coin) -> Resu

pub async fn update_coins(client_config: &ClientConfig, wallet_name: &str) -> Result<()> {

let mut wallet: mercury_lib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;
let mut wallet: mercurylib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;

let network = wallet.network.clone();

Expand Down
8 changes: 4 additions & 4 deletions clients/rust/src/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Result, Ok};
use mercury_lib::{deposit::{create_deposit_msg1, create_aggregated_address}, wallet::{Wallet, BackupTx, CoinStatus, Coin}, transaction:: get_user_backup_address, utils::get_blockheight};
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};

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

let value = response.text().await?;

let deposit_msg_1_response: mercury_lib::deposit::DepositMsg1Response = serde_json::from_str(value.as_str())?;
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 = mercury_lib::deposit::handle_deposit_msg_1_response(&coin, &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 Expand Up @@ -134,7 +134,7 @@ pub async fn get_token(client_config: &ClientConfig) -> Result<String> {

let value = response.text().await?;

let token: mercury_lib::deposit::TokenID = serde_json::from_str(value.as_str())?;
let token: mercurylib::deposit::TokenID = serde_json::from_str(value.as_str())?;

return Ok(token.token_id);
}
2 changes: 1 addition & 1 deletion clients/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async fn main() -> Result<()> {
},
Commands::ListStatecoins { wallet_name } => {
coin_status::update_coins(&client_config, &wallet_name).await?;
let wallet: mercury_lib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;
let wallet: mercurylib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;
for coin in wallet.coins.iter() {
println!("----\ncoin.user_pubkey: {}", coin.user_pubkey);
println!("coin.aggregated_address: {}", coin.aggregated_address.as_ref().unwrap_or(&"".to_string()));
Expand Down
2 changes: 1 addition & 1 deletion clients/rust/src/sqlite_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mercury_lib::wallet::{Wallet, BackupTx};
use mercurylib::wallet::{Wallet, BackupTx};
use serde_json::json;
use sqlx::{Pool, Sqlite, Row};
use anyhow::{anyhow, Result};
Expand Down
6 changes: 3 additions & 3 deletions clients/rust/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use electrum_client::ElectrumApi;
use mercury_lib::{transaction::{SignFirstRequestPayload, PartialSignatureRequestPayload, PartialSignatureResponsePayload, get_partial_sig_request, create_signature, new_backup_transaction}, wallet::Coin};
use mercurylib::{transaction::{SignFirstRequestPayload, PartialSignatureRequestPayload, PartialSignatureResponsePayload, get_partial_sig_request, create_signature, new_backup_transaction}, wallet::Coin};
use anyhow::Result;
use secp256k1_zkp::musig::MusigPartialSignature;
use crate::{client_config::ClientConfig, utils::info_config};
Expand All @@ -16,7 +16,7 @@ pub async fn new_transaction(

// TODO: validate address first

let coin_nonce = mercury_lib::transaction::create_and_commit_nonces(&coin)?;
let coin_nonce = mercurylib::transaction::create_and_commit_nonces(&coin)?;
coin.secret_nonce = Some(coin_nonce.secret_nonce);
coin.public_nonce = Some(coin_nonce.public_nonce);
coin.blinding_factor = Some(coin_nonce.blinding_factor);
Expand Down Expand Up @@ -84,7 +84,7 @@ pub async fn sign_first(client_config: &ClientConfig, sign_first_request_payload

let value = request.json(&sign_first_request_payload).send().await?.text().await?;

let sign_first_response_payload: mercury_lib::transaction::SignFirstResponsePayload = serde_json::from_str(value.as_str())?;
let sign_first_response_payload: mercurylib::transaction::SignFirstResponsePayload = serde_json::from_str(value.as_str())?;

let mut server_pubnonce_hex = sign_first_response_payload.server_pubnonce.to_string();

Expand Down
8 changes: 4 additions & 4 deletions clients/rust/src/transfer_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{anyhow, Result};
use bitcoin::{Txid, Address};
use chrono::Utc;
use electrum_client::ElectrumApi;
use mercury_lib::{transfer::receiver::{GetMsgAddrResponsePayload, verify_transfer_signature, StatechainInfoResponsePayload, validate_tx0_output_pubkey, verify_latest_backup_tx_pays_to_user_pubkey, TxOutpoint, verify_transaction_signature, verify_blinded_musig_scheme, create_transfer_receiver_request_payload, TransferReceiverRequestPayload, get_new_key_info}, wallet::{Coin, Activity, CoinStatus}, utils::{get_network, InfoConfig, get_blockheight}};
use mercurylib::{transfer::receiver::{GetMsgAddrResponsePayload, verify_transfer_signature, StatechainInfoResponsePayload, validate_tx0_output_pubkey, verify_latest_backup_tx_pays_to_user_pubkey, TxOutpoint, verify_transaction_signature, verify_blinded_musig_scheme, create_transfer_receiver_request_payload, TransferReceiverRequestPayload, get_new_key_info}, wallet::{Coin, Activity, CoinStatus}, utils::{get_network, InfoConfig, get_blockheight}};
use serde_json::Value;

pub async fn new_transfer_address(client_config: &ClientConfig, wallet_name: &str) -> Result<String>{
Expand Down Expand Up @@ -80,11 +80,11 @@ async fn process_encrypted_message(client_config: &ClientConfig, coin: &mut Coin

for enc_message in enc_messages {

let transfer_msg = mercury_lib::transfer::receiver::decrypt_transfer_msg(enc_message, &client_auth_key)?;
let transfer_msg = mercurylib::transfer::receiver::decrypt_transfer_msg(enc_message, &client_auth_key)?;

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

let tx0_outpoint = mercury_lib::transfer::receiver::get_tx0_outpoint(&transfer_msg.backup_transactions)?;
let tx0_outpoint = mercurylib::transfer::receiver::get_tx0_outpoint(&transfer_msg.backup_transactions)?;

println!("tx0_outpoint: {:?}", tx0_outpoint);

Expand Down Expand Up @@ -253,7 +253,7 @@ async fn get_statechain_info(statechain_id: &str, client_config: &ClientConfig)
}

async fn verify_tx0_output_is_unspent_and_confirmed(electrum_client: &electrum_client::Client, tx0_outpoint: &TxOutpoint, tx0_hex: &str, coin: &mut Coin, network: &str, confirmation_target: u32) -> Result<bool> {
let output_address = mercury_lib::transfer::receiver::get_output_address_from_tx0(&tx0_outpoint, &tx0_hex, &network)?;
let output_address = mercurylib::transfer::receiver::get_output_address_from_tx0(&tx0_outpoint, &tx0_hex, &network)?;

let network = get_network(&network)?;
let address = Address::from_str(&output_address)?.require_network(network)?;
Expand Down
6 changes: 3 additions & 3 deletions clients/rust/src/transfer_sender.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{client_config::ClientConfig, sqlite_manager::{get_wallet, get_backup_txs, update_wallet, update_backup_txs}, transaction::new_transaction};
use anyhow::{anyhow, Result};
use chrono::Utc;
use mercury_lib::{wallet::{Coin, BackupTx, Activity, CoinStatus}, utils::get_blockheight, decode_transfer_address, transfer::sender::{TransferSenderRequestPayload, TransferSenderResponsePayload, create_transfer_signature, create_transfer_update_msg}};
use mercurylib::{wallet::{Coin, BackupTx, Activity, CoinStatus}, utils::get_blockheight, decode_transfer_address, transfer::sender::{TransferSenderRequestPayload, TransferSenderResponsePayload, create_transfer_signature, create_transfer_update_msg}};

pub async fn execute(client_config: &ClientConfig, recipient_address: &str, wallet_name: &str, statechain_id: &str) -> Result<()> {

let mut wallet: mercury_lib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;
let mut wallet: mercurylib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;

let is_address_valid = mercury_lib::validate_address(recipient_address, &wallet.network)?;
let is_address_valid = mercurylib::validate_address(recipient_address, &wallet.network)?;

if !is_address_valid {
return Err(anyhow!("Invalid address"));
Expand Down
2 changes: 1 addition & 1 deletion clients/rust/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use chrono::Utc;
use electrum_client::ElectrumApi;
use mercury_lib::{utils::{ServerConfig, InfoConfig}, wallet::Activity};
use mercurylib::{utils::{ServerConfig, InfoConfig}, wallet::Activity};
use anyhow::{Result, Ok};

use crate::client_config::ClientConfig;
Expand Down
2 changes: 1 addition & 1 deletion clients/rust/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use electrum_client::ElectrumApi;
use mercury_lib::wallet::{generate_mnemonic, Settings, Wallet};
use mercurylib::wallet::{generate_mnemonic, Settings, Wallet};

use crate::{utils::info_config, client_config::ClientConfig};

Expand Down
6 changes: 3 additions & 3 deletions clients/rust/src/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::{client_config::ClientConfig, sqlite_manager::{get_wallet, update_wal
use anyhow::{anyhow, Result};
use chrono::Utc;
use electrum_client::ElectrumApi;
use mercury_lib::wallet::{BackupTx, Activity, CoinStatus};
use mercurylib::wallet::{BackupTx, Activity, CoinStatus};

pub async fn execute(client_config: &ClientConfig, wallet_name: &str, statechain_id: &str, to_address: &str, fee_rate: Option<u64>) -> Result<()>{

let mut wallet: mercury_lib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;
let mut wallet: mercurylib::wallet::Wallet = get_wallet(&client_config.pool, &wallet_name).await?;

let is_address_valid = mercury_lib::validate_address(to_address, &wallet.network)?;
let is_address_valid = mercurylib::validate_address(to_address, &wallet.network)?;

if !is_address_valid {
return Err(anyhow!("Invalid address"));
Expand Down
1 change: 0 additions & 1 deletion enclave/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ App/Enclave_u.h
Enclave/Enclave_t.c
Enclave/Enclave_t.h
app
Settings.toml
node.sealed_seed
2 changes: 2 additions & 0 deletions enclave/Settings.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[intel_sgx]
database_connection_string = "postgresql://postgres:postgres@localhost/enclave"
6 changes: 4 additions & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "mercury-lib"
name = "mercurylib"
version = "0.1.0"
edition = "2021"

Expand All @@ -9,7 +9,9 @@ edition = "2021"
bitcoin = { version = "0.30.1", features = ["serde", "base64", "rand-std", "std"], default-features = false }
bip39 = "1.2.0"
bech32 = { version = "0.9.1", default-features = false }
ecies = {version = "0.2", default-features = false, features = ["pure"]}
# TODO: Cannot update Cargo because it will get once_cell 1.19.0 and will break the ecies dependency
# When version 0.27.0 is released, update the ecies dependency to use it
ecies = {version = "0.2.6", default-features = false, features = ["pure"]}
hex = "0.4.3"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
Expand Down
4 changes: 1 addition & 3 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ rand = "0.8.5"
hex = "0.4.3"
secp256k1-zkp = { git = "https://github.com/ssantos21/rust-secp256k1-zkp.git", branch = "blinded-musig-scheme", features = [ "rand-std", "bitcoin_hashes", "std" ] }
# secp256k1-zkp = { path = "../ss-rust-secp256k1-zkp", features = [ "rand-std", "bitcoin_hashes", "std" ] }

[dependencies.mercury-lib]
path = "../lib"
mercurylib = { path = "../lib" }
10 changes: 5 additions & 5 deletions server/src/endpoints/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub async fn set_token_spent(pool: &sqlx::PgPool, token_id: &str) {
transaction.commit().await.unwrap();
}

pub async fn check_existing_key(pool: &sqlx::PgPool, auth_key: &XOnlyPublicKey) -> Option<mercury_lib::deposit::DepositMsg1Response> {
pub async fn check_existing_key(pool: &sqlx::PgPool, auth_key: &XOnlyPublicKey) -> Option<mercurylib::deposit::DepositMsg1Response> {

let row = sqlx::query(
"SELECT statechain_id, server_public_key \
Expand All @@ -78,7 +78,7 @@ pub async fn check_existing_key(pool: &sqlx::PgPool, auth_key: &XOnlyPublicKey)
let server_public_key_bytes = row_ur.get::<Vec<u8>, _>(1);
let server_pubkey = PublicKey::from_slice(&server_public_key_bytes).unwrap();

let deposit_msg1_response = mercury_lib::deposit::DepositMsg1Response {
let deposit_msg1_response = mercurylib::deposit::DepositMsg1Response {
server_pubkey: server_pubkey.to_string(),
statechain_id: row_ur.get(0),
};
Expand All @@ -103,7 +103,7 @@ pub async fn get_token(statechain_entity: &State<StateChainEntity>) -> status::C

insert_new_token(&statechain_entity.pool, &token_id).await;

let token = mercury_lib::deposit::TokenID {
let token = mercurylib::deposit::TokenID {
token_id
};

Expand All @@ -113,7 +113,7 @@ pub async fn get_token(statechain_entity: &State<StateChainEntity>) -> status::C
}

#[post("/deposit/init/pod", format = "json", data = "<deposit_msg1>")]
pub async fn post_deposit(statechain_entity: &State<StateChainEntity>, deposit_msg1: Json<mercury_lib::deposit::DepositMsg1>) -> status::Custom<Json<Value>> {
pub async fn post_deposit(statechain_entity: &State<StateChainEntity>, deposit_msg1: Json<mercurylib::deposit::DepositMsg1>) -> status::Custom<Json<Value>> {

let statechain_entity = statechain_entity.inner();

Expand Down Expand Up @@ -214,7 +214,7 @@ pub async fn post_deposit(statechain_entity: &State<StateChainEntity>, deposit_m

set_token_spent(&statechain_entity.pool, &token_id).await;

let deposit_msg1_response = mercury_lib::deposit::DepositMsg1Response {
let deposit_msg1_response = mercurylib::deposit::DepositMsg1Response {
server_pubkey: server_pubkey.to_string(),
statechain_id,
};
Expand Down
6 changes: 3 additions & 3 deletions server/src/endpoints/sign.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mercury_lib::transaction::SignFirstRequestPayload;
use mercurylib::transaction::SignFirstRequestPayload;
use rocket::{State, serde::json::Json, response::status, http::Status};
use secp256k1_zkp::musig::MusigSession;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -86,7 +86,7 @@ pub async fn sign_first(statechain_entity: &State<StateChainEntity>, sign_first_

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

let response: mercury_lib::transaction::SignFirstResponsePayload = serde_json::from_str(value.as_str()).expect(&format!("failed to parse: {}", value.as_str()));
let response: mercurylib::transaction::SignFirstResponsePayload = serde_json::from_str(value.as_str()).expect(&format!("failed to parse: {}", value.as_str()));

let mut server_pubnonce_hex = response.server_pubnonce.clone();

Expand Down Expand Up @@ -126,7 +126,7 @@ pub async fn update_signature_data_challenge(pool: &sqlx::PgPool, server_pub_non
}

#[post("/sign/second", format = "json", data = "<partial_signature_request_payload>")]
pub async fn sign_second (statechain_entity: &State<StateChainEntity>, partial_signature_request_payload: Json<mercury_lib::transaction::PartialSignatureRequestPayload>) -> status::Custom<Json<Value>> {
pub async fn sign_second (statechain_entity: &State<StateChainEntity>, partial_signature_request_payload: Json<mercurylib::transaction::PartialSignatureRequestPayload>) -> status::Custom<Json<Value>> {

let statechain_entity = statechain_entity.inner();

Expand Down
Loading

0 comments on commit 9660b00

Please sign in to comment.