diff --git a/Cargo.lock b/Cargo.lock index 2fdb9609..fe672c0f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3379,7 +3379,7 @@ dependencies = [ [[package]] name = "token-server" -version = "0.1.0" +version = "0.1.1" dependencies = [ "config", "hex", diff --git a/clients/libs/rust/migrations/0001_signer_data_table.sql b/clients/libs/rust/migrations/0001_signer_data_table.sql index 93511757..df3f25dc 100644 --- a/clients/libs/rust/migrations/0001_signer_data_table.sql +++ b/clients/libs/rust/migrations/0001_signer_data_table.sql @@ -4,6 +4,7 @@ CREATE TABLE IF NOT EXISTS wallet ( ); CREATE TABLE IF NOT EXISTS backup_txs ( + wallet_name TEXT NOT NULL, statechain_id TEXT NOT NULL, txs TEXT NOT NULL ); diff --git a/clients/libs/rust/src/broadcast_backup_tx.rs b/clients/libs/rust/src/broadcast_backup_tx.rs index db544d38..7278b56b 100644 --- a/clients/libs/rust/src/broadcast_backup_tx.rs +++ b/clients/libs/rust/src/broadcast_backup_tx.rs @@ -16,7 +16,7 @@ pub async fn execute(client_config: &ClientConfig, wallet_name: &str, statechain } } - let backup_txs = get_backup_txs(&client_config.pool, &statechain_id).await?; + let backup_txs = get_backup_txs(&client_config.pool, &wallet.name, &statechain_id).await?; // If the user sends to himself, he will have two coins with same statechain_id // In this case, we need to find the one with the lowest locktime diff --git a/clients/libs/rust/src/coin_status.rs b/clients/libs/rust/src/coin_status.rs index 664138a7..2f811434 100644 --- a/clients/libs/rust/src/coin_status.rs +++ b/clients/libs/rust/src/coin_status.rs @@ -238,7 +238,7 @@ pub async fn update_coins(client_config: &ClientConfig, wallet_name: &str) -> Re let backup_tx = deposit_result.backup_tx; wallet.activities.push(activity); - insert_backup_txs(&client_config.pool, &coin.statechain_id.as_ref().unwrap(), &[backup_tx].to_vec()).await?; + insert_backup_txs(&client_config.pool, &wallet.name, &coin.statechain_id.as_ref().unwrap(), &[backup_tx].to_vec()).await?; } } else if coin.status == CoinStatus::IN_TRANSFER { diff --git a/clients/libs/rust/src/sqlite_manager.rs b/clients/libs/rust/src/sqlite_manager.rs index 088fb753..fc14571b 100644 --- a/clients/libs/rust/src/sqlite_manager.rs +++ b/clients/libs/rust/src/sqlite_manager.rs @@ -53,13 +53,14 @@ pub async fn update_wallet(pool: &Pool, wallet: &Wallet) -> Result<()> { Ok(()) } -pub async fn insert_backup_txs(pool: &Pool, statechain_id: &str, backup_txs: &Vec) -> Result<()> { +pub async fn insert_backup_txs(pool: &Pool, wallet_name: &str, statechain_id: &str, backup_txs: &Vec) -> Result<()> { let backup_txs_json = json!(backup_txs).to_string(); - let query = "INSERT INTO backup_txs (statechain_id, txs) VALUES ($1, $2)"; + let query = "INSERT INTO backup_txs (wallet_name, statechain_id, txs) VALUES ($1, $2, $3)"; let _ = sqlx::query(query) + .bind(wallet_name) .bind(statechain_id) .bind(backup_txs_json) .execute(pool) @@ -68,27 +69,29 @@ pub async fn insert_backup_txs(pool: &Pool, statechain_id: &str, backup_ Ok(()) } -pub async fn update_backup_txs(pool: &Pool, statechain_id: &str, backup_txs: &Vec) -> Result<()> { +pub async fn update_backup_txs(pool: &Pool, wallet_name: &str, statechain_id: &str, backup_txs: &Vec) -> Result<()> { let backup_txs_json = json!(backup_txs).to_string(); - let query = "UPDATE backup_txs SET txs = $1 WHERE statechain_id = $2"; + let query = "UPDATE backup_txs SET txs = $1 WHERE statechain_id = $2 AND wallet_name = $3"; let _ = sqlx::query(query) .bind(backup_txs_json) .bind(statechain_id) + .bind(wallet_name) .execute(pool) .await?; Ok(()) } -pub async fn get_backup_txs(pool: &Pool, statechain_id: &str,) -> Result> { +pub async fn get_backup_txs(pool: &Pool, wallet_name: &str, statechain_id: &str,) -> Result> { - let query = "SELECT txs FROM backup_txs WHERE statechain_id = $1"; + let query = "SELECT txs FROM backup_txs WHERE statechain_id = $1 AND wallet_name = $2"; let row = sqlx::query(query) .bind(statechain_id) + .bind(wallet_name) .fetch_one(pool) .await?; @@ -103,23 +106,25 @@ pub async fn get_backup_txs(pool: &Pool, statechain_id: &str,) -> Result Ok(backup_txs) } -pub async fn insert_or_update_backup_txs(pool: &Pool, statechain_id: &str, backup_txs: &Vec) -> Result<()> { +pub async fn insert_or_update_backup_txs(pool: &Pool, wallet_name: &str, statechain_id: &str, backup_txs: &Vec) -> Result<()> { let mut transaction = pool.begin().await?; let backup_txs_json = json!(backup_txs).to_string(); - let query = "DELETE FROM backup_txs WHERE statechain_id = $1"; + let query = "DELETE FROM backup_txs WHERE statechain_id = $1 AND wallet_name = $2"; let _ = sqlx::query(query) .bind(statechain_id) + .bind(wallet_name) .execute(&mut *transaction) .await?; - let query = "INSERT INTO backup_txs (statechain_id, txs) VALUES ($1, $2)"; + let query = "INSERT INTO backup_txs (statechain_id, wallet_name, txs) VALUES ($1, $2, $3)"; let _ = sqlx::query(query) .bind(statechain_id) + .bind(wallet_name) .bind(backup_txs_json) .execute(&mut *transaction) .await?; diff --git a/clients/libs/rust/src/transfer_receiver.rs b/clients/libs/rust/src/transfer_receiver.rs index 3e4d560e..3af84631 100644 --- a/clients/libs/rust/src/transfer_receiver.rs +++ b/clients/libs/rust/src/transfer_receiver.rs @@ -74,7 +74,7 @@ pub async fn execute(client_config: &ClientConfig, wallet_name: &str) -> Result< let mut coin = coin.unwrap(); - let message_result = process_encrypted_message(client_config, &mut coin, enc_message, &wallet.network, &info_config, blockheight, &mut temp_activities).await; + let message_result = process_encrypted_message(client_config, &mut coin, enc_message, &wallet.network, &wallet.name, &info_config, blockheight, &mut temp_activities).await; if message_result.is_err() { println!("Error: {}", message_result.err().unwrap().to_string()); @@ -102,7 +102,7 @@ pub async fn execute(client_config: &ClientConfig, wallet_name: &str) -> Result< let mut new_coin = new_coin.unwrap(); - let message_result = process_encrypted_message(client_config, &mut new_coin, enc_message, &wallet.network, &info_config, blockheight, &mut temp_activities).await; + let message_result = process_encrypted_message(client_config, &mut new_coin, enc_message, &wallet.network, &wallet.name, &info_config, blockheight, &mut temp_activities).await; if message_result.is_err() { println!("Error: {}", message_result.err().unwrap().to_string()); @@ -154,7 +154,7 @@ pub struct MessageResult { pub statechain_id: Option, } -async fn process_encrypted_message(client_config: &ClientConfig, coin: &mut Coin, enc_message: &str, network: &str, info_config: &InfoConfig, blockheight: u32, activities: &mut Vec) -> Result { +async fn process_encrypted_message(client_config: &ClientConfig, coin: &mut Coin, enc_message: &str, network: &str, wallet_name: &str, info_config: &InfoConfig, blockheight: u32, activities: &mut Vec) -> Result { let client_auth_key = coin.auth_privkey.clone(); let new_user_pubkey = coin.user_pubkey.clone(); @@ -278,7 +278,7 @@ async fn process_encrypted_message(client_config: &ClientConfig, coin: &mut Coin activities.push(activity); - insert_or_update_backup_txs(&client_config.pool, &transfer_msg.statechain_id, &transfer_msg.backup_transactions).await?; + insert_or_update_backup_txs(&client_config.pool, wallet_name, &transfer_msg.statechain_id, &transfer_msg.backup_transactions).await?; Ok(MessageResult { is_batch_locked: false, diff --git a/clients/libs/rust/src/transfer_sender.rs b/clients/libs/rust/src/transfer_sender.rs index 6904465a..1dcfd1da 100644 --- a/clients/libs/rust/src/transfer_sender.rs +++ b/clients/libs/rust/src/transfer_sender.rs @@ -21,7 +21,7 @@ pub async fn execute( return Err(anyhow!("Invalid address")); } - let mut backup_transactions = get_backup_txs(&client_config.pool, &statechain_id).await?; + let mut backup_transactions = get_backup_txs(&client_config.pool, &wallet.name, &statechain_id).await?; if backup_transactions.len() == 0 { return Err(anyhow!("No backup transaction associated with this statechain ID were found")); @@ -129,7 +129,7 @@ pub async fn execute( return Err(anyhow::anyhow!("Failed to update transfer message".to_string())); } - update_backup_txs(&client_config.pool, &coin.statechain_id.as_ref().unwrap(), &backup_transactions).await?; + update_backup_txs(&client_config.pool, &wallet.name, &coin.statechain_id.as_ref().unwrap(), &backup_transactions).await?; let date = Utc::now(); // This will get the current date and time in UTC let iso_string = date.to_rfc3339(); // Converts the date to an ISO 8601 string diff --git a/clients/libs/rust/src/withdraw.rs b/clients/libs/rust/src/withdraw.rs index 27fb2b67..37c9475e 100644 --- a/clients/libs/rust/src/withdraw.rs +++ b/clients/libs/rust/src/withdraw.rs @@ -15,7 +15,7 @@ pub async fn execute(client_config: &ClientConfig, wallet_name: &str, statechain return Err(anyhow!("Invalid address")); } - let backup_txs = get_backup_txs(&client_config.pool, &statechain_id).await?; + let backup_txs = get_backup_txs(&client_config.pool, &wallet.name, &statechain_id).await?; if backup_txs.len() == 0 { return Err(anyhow!("No backup transaction associated with this statechain ID were found"));