diff --git a/bin/drk2/Cargo.toml b/bin/drk2/Cargo.toml index ad0ed9d6c..01cb19870 100644 --- a/bin/drk2/Cargo.toml +++ b/bin/drk2/Cargo.toml @@ -17,6 +17,7 @@ darkfi-sdk = {path = "../../src/sdk", features = ["async"]} darkfi-serial = {path = "../../src/serial"} # Misc +blake3 = "1.5.0" bs58 = "0.5.0" log = "0.4.20" prettytable-rs = "0.10.0" diff --git a/bin/drk2/src/dao.rs b/bin/drk2/src/dao.rs index ab143f1c3..9f8a0b056 100644 --- a/bin/drk2/src/dao.rs +++ b/bin/drk2/src/dao.rs @@ -16,16 +16,242 @@ * along with this program. If not, see . */ -use darkfi_dao_contract::client::{ - DAO_TREES_COL_DAOS_TREE, DAO_TREES_COL_PROPOSALS_TREE, DAO_TREES_TABLE, -}; -use darkfi_sdk::crypto::MerkleTree; -use darkfi_serial::serialize; +use std::fmt; -use crate::{error::WalletDbResult, Drk}; +use rusqlite::types::Value; + +use darkfi::{tx::Transaction, util::parse::encode_base10, Error, Result}; +use darkfi_dao_contract::{ + client::{ + DaoVoteNote, DAO_DAOS_COL_CALL_INDEX, DAO_DAOS_COL_DAO_ID, DAO_DAOS_COL_LEAF_POSITION, + DAO_DAOS_COL_TX_HASH, DAO_DAOS_TABLE, DAO_PROPOSALS_COL_AMOUNT, + DAO_PROPOSALS_COL_BULLA_BLIND, DAO_PROPOSALS_COL_CALL_INDEX, DAO_PROPOSALS_COL_DAO_ID, + DAO_PROPOSALS_COL_LEAF_POSITION, DAO_PROPOSALS_COL_MONEY_SNAPSHOT_TREE, + DAO_PROPOSALS_COL_RECV_PUBLIC, DAO_PROPOSALS_COL_SENDCOIN_TOKEN_ID, + DAO_PROPOSALS_COL_TX_HASH, DAO_PROPOSALS_TABLE, DAO_TREES_COL_DAOS_TREE, + DAO_TREES_COL_PROPOSALS_TREE, DAO_TREES_TABLE, DAO_VOTES_COL_ALL_VOTE_BLIND, + DAO_VOTES_COL_ALL_VOTE_VALUE, DAO_VOTES_COL_CALL_INDEX, DAO_VOTES_COL_PROPOSAL_ID, + DAO_VOTES_COL_TX_HASH, DAO_VOTES_COL_VOTE_OPTION, DAO_VOTES_COL_YES_VOTE_BLIND, + DAO_VOTES_TABLE, + }, + model::{DaoBulla, DaoMintParams, DaoProposeParams, DaoVoteParams}, + DaoFunction, +}; +use darkfi_sdk::{ + bridgetree, + crypto::{ + poseidon_hash, Keypair, MerkleNode, MerkleTree, PublicKey, SecretKey, TokenId, + DAO_CONTRACT_ID, + }, + pasta::pallas, +}; +use darkfi_serial::{async_trait, deserialize, serialize, SerialDecodable, SerialEncodable}; + +use crate::{ + convert_named_params, + error::{WalletDbError, WalletDbResult}, + money::BALANCE_BASE10_DECIMALS, + Drk, +}; + +#[derive(SerialEncodable, SerialDecodable, Clone)] +pub struct DaoProposalInfo { + pub dest: PublicKey, + pub amount: u64, + pub token_id: TokenId, + pub blind: pallas::Base, +} + +#[derive(SerialEncodable, SerialDecodable)] +pub struct DaoProposeNote { + pub proposal: DaoProposalInfo, +} + +#[derive(Debug, Clone)] +/// Parameters representing an intialized DAO, optionally deployed on-chain +pub struct Dao { + /// Numeric identifier for the DAO + pub id: u64, + /// Named identifier for the DAO + pub name: String, + /// The minimum amount of governance tokens needed to open a proposal + pub proposer_limit: u64, + /// Minimal threshold of participating total tokens needed for a proposal to pass + pub quorum: u64, + /// The ratio of winning/total votes needed for a proposal to pass + pub approval_ratio_base: u64, + pub approval_ratio_quot: u64, + /// DAO's governance token ID + pub gov_token_id: TokenId, + /// Secret key for the DAO + pub secret_key: SecretKey, + /// DAO bulla blind + pub bulla_blind: pallas::Base, + /// Leaf position of the DAO in the Merkle tree of DAOs + pub leaf_position: Option, + /// The transaction hash where the DAO was deployed + pub tx_hash: Option, + /// The call index in the transaction where the DAO was deployed + pub call_index: Option, +} + +impl Dao { + pub fn bulla(&self) -> DaoBulla { + let (x, y) = PublicKey::from_secret(self.secret_key).xy(); + + DaoBulla::from(poseidon_hash([ + pallas::Base::from(self.proposer_limit), + pallas::Base::from(self.quorum), + pallas::Base::from(self.approval_ratio_quot), + pallas::Base::from(self.approval_ratio_base), + self.gov_token_id.inner(), + x, + y, + self.bulla_blind, + ])) + } + + pub fn keypair(&self) -> Keypair { + let public = PublicKey::from_secret(self.secret_key); + Keypair { public, secret: self.secret_key } + } +} + +impl fmt::Display for Dao { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let s = format!( + "{}\n{}\n{}: {}\n{}: {}\n{}: {} ({})\n{}: {} ({})\n{}: {}\n{}: {}\n{}: {}\n{}: {}\n{}: {:?}\n{}: {:?}\n{}: {:?}\n{}: {:?}", + "DAO Parameters", + "==============", + "Name", + self.name, + "Bulla", + self.bulla(), + "Proposer limit", + encode_base10(self.proposer_limit, BALANCE_BASE10_DECIMALS), + self.proposer_limit, + "Quorum", + encode_base10(self.quorum, BALANCE_BASE10_DECIMALS), + self.quorum, + "Approval ratio", + self.approval_ratio_quot as f64 / self.approval_ratio_base as f64, + "Governance Token ID", + self.gov_token_id, + "Public key", + PublicKey::from_secret(self.secret_key), + "Secret key", + self.secret_key, + "Bulla blind", + self.bulla_blind, + "Leaf position", + self.leaf_position, + "Tx hash", + self.tx_hash, + "Call idx", + self.call_index, + ); + + write!(f, "{}", s) + } +} + +#[derive(Debug, Clone)] +/// Parameters representing an initialized DAO proposal, optionally deployed on-chain +pub struct DaoProposal { + /// Numeric identifier for the proposal + pub id: u64, + /// The DAO bulla related to this proposal + pub dao_bulla: DaoBulla, + /// Recipient of this proposal's funds + pub recipient: PublicKey, + /// Amount of this proposal + pub amount: u64, + /// Token ID to be sent + pub token_id: TokenId, + /// Proposal's bulla blind + pub bulla_blind: pallas::Base, + /// Leaf position of this proposal in the Merkle tree of proposals + pub leaf_position: Option, + /// Snapshotted Money Merkle tree + pub money_snapshot_tree: Option, + /// Transaction hash where this proposal was proposed + pub tx_hash: Option, + /// call index in the transaction where this proposal was proposed + pub call_index: Option, + /// The vote ID we've voted on this proposal + pub vote_id: Option, +} + +impl DaoProposal { + pub fn bulla(&self) -> pallas::Base { + let (dest_x, dest_y) = self.recipient.xy(); + + poseidon_hash([ + dest_x, + dest_y, + pallas::Base::from(self.amount), + self.token_id.inner(), + self.dao_bulla.inner(), + self.bulla_blind, + ]) + } +} + +impl fmt::Display for DaoProposal { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let s = format!( + concat!( + "Proposal parameters\n", + "===================\n", + "DAO Bulla: {}\n", + "Recipient: {}\n", + "Proposal amount: {} ({})\n", + "Proposal Token ID: {:?}\n", + "Proposal bulla blind: {:?}\n", + "Proposal leaf position: {:?}\n", + "Proposal tx hash: {:?}\n", + "Proposal call index: {:?}\n", + "Proposal vote ID: {:?}", + ), + self.dao_bulla, + self.recipient, + encode_base10(self.amount, BALANCE_BASE10_DECIMALS), + self.amount, + self.token_id, + self.bulla_blind, + self.leaf_position, + self.tx_hash, + self.call_index, + self.vote_id, + ); + + write!(f, "{}", s) + } +} + +#[derive(Debug, Clone)] +/// Parameters representing a vote we've made on a DAO proposal +pub struct DaoVote { + /// Numeric identifier for the vote + pub id: u64, + /// Numeric identifier for the proposal related to this vote + pub proposal_id: u64, + /// The vote + pub vote_option: bool, + /// Blinding factor for the yes vote + pub yes_vote_blind: pallas::Scalar, + /// Value of all votes + pub all_vote_value: u64, + /// Blinding facfor of all votes + pub all_vote_blind: pallas::Scalar, + /// Transaction hash where this vote was casted + pub tx_hash: Option, + /// call index in the transaction where this vote was casted + pub call_index: Option, +} impl Drk { - /// Initialize wallet with tables for the DAO contract + /// Initialize wallet with tables for the DAO contract. pub async fn initialize_dao(&self) -> WalletDbResult<()> { // Initialize DAO wallet schema let wallet_schema = include_str!("../../../src/contract/dao/wallet.sql"); @@ -66,4 +292,633 @@ impl Drk { .exec_sql(&query, rusqlite::params![serialize(daos_tree), serialize(proposals_tree)]) .await } + + /// Fetch DAO Merkle trees from the wallet + pub async fn get_dao_trees(&self) -> Result<(MerkleTree, MerkleTree)> { + let row = match self.wallet.query_single(DAO_TREES_TABLE, &[], &[]).await { + Ok(r) => r, + Err(e) => { + return Err(Error::RusqliteError(format!( + "[get_dao_trees] Trees retrieval failed: {e:?}" + ))) + } + }; + + let Value::Blob(ref daos_tree_bytes) = row[0] else { + return Err(Error::ParseFailed("[get_dao_trees] DAO tree bytes parsing failed")) + }; + let daos_tree = deserialize(daos_tree_bytes)?; + + let Value::Blob(ref proposals_tree_bytes) = row[1] else { + return Err(Error::ParseFailed("[get_dao_trees] Proposals tree bytes parsing failed")) + }; + let proposals_tree = deserialize(proposals_tree_bytes)?; + + Ok((daos_tree, proposals_tree)) + } + + /// Fetch all DAO secret keys from the wallet + pub async fn get_dao_secrets(&self) -> Result> { + let daos = self.get_daos().await?; + let mut ret = Vec::with_capacity(daos.len()); + for dao in daos { + ret.push(dao.secret_key); + } + + Ok(ret) + } + + /// Fetch all known DAOs from the wallet. + pub async fn get_daos(&self) -> Result> { + let rows = match self.wallet.query_multiple(DAO_DAOS_TABLE, &[], &[]).await { + Ok(r) => r, + Err(e) => { + return Err(Error::RusqliteError(format!("[get_daos] DAOs retrieval failed: {e:?}"))) + } + }; + + let mut daos = Vec::with_capacity(rows.len()); + for row in rows { + let Value::Integer(id) = row[0] else { + return Err(Error::ParseFailed("[get_daos] ID parsing failed")) + }; + let Ok(id) = u64::try_from(id) else { + return Err(Error::ParseFailed("[get_daos] ID parsing failed")) + }; + + let Value::Text(ref name) = row[1] else { + return Err(Error::ParseFailed("[get_daos] Name parsing failed")) + }; + let name = name.clone(); + + let Value::Blob(ref proposer_limit_bytes) = row[2] else { + return Err(Error::ParseFailed("[get_daos] Proposer limit bytes parsing failed")) + }; + let proposer_limit = deserialize(proposer_limit_bytes)?; + + let Value::Blob(ref quorum_bytes) = row[3] else { + return Err(Error::ParseFailed("[get_daos] Quorum bytes parsing failed")) + }; + let quorum = deserialize(quorum_bytes)?; + + let Value::Integer(approval_ratio_base) = row[4] else { + return Err(Error::ParseFailed("[get_daos] Approval ratio base parsing failed")) + }; + let Ok(approval_ratio_base) = u64::try_from(approval_ratio_base) else { + return Err(Error::ParseFailed("[get_daos] Approval ratio base parsing failed")) + }; + + let Value::Integer(approval_ratio_quot) = row[5] else { + return Err(Error::ParseFailed("[get_daos] Approval ratio quot parsing failed")) + }; + let Ok(approval_ratio_quot) = u64::try_from(approval_ratio_quot) else { + return Err(Error::ParseFailed("[get_daos] Approval ratio quot parsing failed")) + }; + + let Value::Blob(ref gov_token_bytes) = row[6] else { + return Err(Error::ParseFailed("[get_daos] Gov token bytes parsing failed")) + }; + let gov_token_id = deserialize(gov_token_bytes)?; + + let Value::Blob(ref secret_bytes) = row[7] else { + return Err(Error::ParseFailed("[get_daos] Secret key bytes parsing failed")) + }; + let secret_key = deserialize(secret_bytes)?; + + let Value::Blob(ref bulla_blind_bytes) = row[8] else { + return Err(Error::ParseFailed("[get_daos] Bulla blind bytes parsing failed")) + }; + let bulla_blind = deserialize(bulla_blind_bytes)?; + + let Value::Blob(ref leaf_position_bytes) = row[9] else { + return Err(Error::ParseFailed("[get_daos] Leaf position bytes parsing failed")) + }; + let leaf_position = if leaf_position_bytes.is_empty() { + None + } else { + Some(deserialize(leaf_position_bytes)?) + }; + + let Value::Blob(ref tx_hash_bytes) = row[10] else { + return Err(Error::ParseFailed("[get_daos] Transaction hash bytes parsing failed")) + }; + let tx_hash = + if tx_hash_bytes.is_empty() { None } else { Some(deserialize(tx_hash_bytes)?) }; + + let Value::Integer(call_index) = row[11] else { + return Err(Error::ParseFailed("[get_daos] Call index parsing failed")) + }; + let Ok(call_index) = u32::try_from(call_index) else { + return Err(Error::ParseFailed("[get_daos] Call index parsing failed")) + }; + let call_index = Some(call_index); + + let dao = Dao { + id, + name, + proposer_limit, + quorum, + approval_ratio_base, + approval_ratio_quot, + gov_token_id, + secret_key, + bulla_blind, + leaf_position, + tx_hash, + call_index, + }; + + daos.push(dao); + } + + // Here we sort the vec by ID. The SQL SELECT statement does not guarantee + // this, so just do it here. + daos.sort_by(|a, b| a.id.cmp(&b.id)); + Ok(daos) + } + + /// Fetch all known DAO proposals from the wallet given a DAO ID + pub async fn get_dao_proposals(&self, dao_id: u64) -> Result> { + let daos = self.get_daos().await?; + let Some(dao) = daos.get(dao_id as usize - 1) else { + return Err(Error::RusqliteError(format!( + "[get_dao_proposals] DAO with ID {dao_id} not found in wallet" + ))) + }; + + let rows = match self + .wallet + .query_multiple( + DAO_PROPOSALS_TABLE, + &[], + convert_named_params! {(DAO_PROPOSALS_COL_DAO_ID, dao_id)}, + ) + .await + { + Ok(r) => r, + Err(e) => { + return Err(Error::RusqliteError(format!( + "[get_dao_proposals] Proposals retrieval failed: {e:?}" + ))) + } + }; + + let mut proposals = Vec::with_capacity(rows.len()); + for row in rows { + let Value::Integer(id) = row[0] else { + return Err(Error::ParseFailed("[get_dao_proposals] ID parsing failed")) + }; + let Ok(id) = u64::try_from(id) else { + return Err(Error::ParseFailed("[get_dao_proposals] ID parsing failed")) + }; + + let Value::Integer(dao_id) = row[1] else { + return Err(Error::ParseFailed("[get_dao_proposals] DAO ID parsing failed")) + }; + let Ok(dao_id) = u64::try_from(dao_id) else { + return Err(Error::ParseFailed("[get_dao_proposals] DAO ID parsing failed")) + }; + assert!(dao_id == dao.id); + let dao_bulla = dao.bulla(); + + let Value::Blob(ref recipient_bytes) = row[2] else { + return Err(Error::ParseFailed( + "[get_dao_proposals] Recipient bytes bytes parsing failed", + )) + }; + let recipient = deserialize(recipient_bytes)?; + + let Value::Blob(ref amount_bytes) = row[3] else { + return Err(Error::ParseFailed("[get_dao_proposals] Amount bytes parsing failed")) + }; + let amount = deserialize(amount_bytes)?; + + let Value::Blob(ref token_id_bytes) = row[4] else { + return Err(Error::ParseFailed("[get_dao_proposals] Token ID bytes parsing failed")) + }; + let token_id = deserialize(token_id_bytes)?; + + let Value::Blob(ref bulla_blind_bytes) = row[5] else { + return Err(Error::ParseFailed( + "[get_dao_proposals] Bulla blind bytes parsing failed", + )) + }; + let bulla_blind = deserialize(bulla_blind_bytes)?; + + let Value::Blob(ref leaf_position_bytes) = row[6] else { + return Err(Error::ParseFailed( + "[get_dao_proposals] Leaf position bytes parsing failed", + )) + }; + let leaf_position = if leaf_position_bytes.is_empty() { + None + } else { + Some(deserialize(leaf_position_bytes)?) + }; + + let Value::Blob(ref money_snapshot_tree_bytes) = row[7] else { + return Err(Error::ParseFailed( + "[get_dao_proposals] Money snapshot tree bytes parsing failed", + )) + }; + let money_snapshot_tree = if money_snapshot_tree_bytes.is_empty() { + None + } else { + Some(deserialize(money_snapshot_tree_bytes)?) + }; + + let Value::Blob(ref tx_hash_bytes) = row[8] else { + return Err(Error::ParseFailed( + "[get_dao_proposals] Transaction hash bytes parsing failed", + )) + }; + let tx_hash = + if tx_hash_bytes.is_empty() { None } else { Some(deserialize(tx_hash_bytes)?) }; + + let Value::Integer(call_index) = row[9] else { + return Err(Error::ParseFailed("[get_dao_proposals] Call index parsing failed")) + }; + let Ok(call_index) = u32::try_from(call_index) else { + return Err(Error::ParseFailed("[get_dao_proposals] Call index parsing failed")) + }; + let call_index = Some(call_index); + + let Value::Blob(ref vote_id_bytes) = row[10] else { + return Err(Error::ParseFailed("[get_dao_proposals] Vote ID bytes parsing failed")) + }; + let vote_id = + if vote_id_bytes.is_empty() { None } else { Some(deserialize(vote_id_bytes)?) }; + + let proposal = DaoProposal { + id, + dao_bulla, + recipient, + amount, + token_id, + bulla_blind, + leaf_position, + money_snapshot_tree, + tx_hash, + call_index, + vote_id, + }; + + proposals.push(proposal); + } + + // Here we sort the vec by ID. The SQL SELECT statement does not guarantee + // this, so just do it here. + proposals.sort_by(|a, b| a.id.cmp(&b.id)); + Ok(proposals) + } + + /// Append data related to DAO contract transactions into the wallet database. + /// Optionally, if `confirm` is true, also append the data in the Merkle trees, etc. + pub async fn apply_tx_dao_data(&self, tx: &Transaction, confirm: bool) -> Result<()> { + let cid = *DAO_CONTRACT_ID; + let mut daos = self.get_daos().await?; + let mut daos_to_confirm = vec![]; + let (mut daos_tree, mut proposals_tree) = self.get_dao_trees().await?; + + // DAOs that have been minted + let mut new_dao_bullas: Vec<(DaoBulla, Option, u32)> = vec![]; + // DAO proposals that have been minted + let mut new_dao_proposals: Vec<( + DaoProposeParams, + Option, + Option, + u32, + )> = vec![]; + let mut our_proposals: Vec = vec![]; + // DAO votes that have been seen + let mut new_dao_votes: Vec<(DaoVoteParams, Option, u32)> = vec![]; + let mut dao_votes: Vec = vec![]; + + // Run through the transaction and see what we got: + for (i, call) in tx.calls.iter().enumerate() { + if call.data.contract_id == cid && call.data.data[0] == DaoFunction::Mint as u8 { + eprintln!("Found Dao::Mint in call {i}"); + let params: DaoMintParams = deserialize(&call.data.data[1..])?; + let tx_hash = if confirm { Some(blake3::hash(&serialize(tx))) } else { None }; + new_dao_bullas.push((params.dao_bulla, tx_hash, i as u32)); + continue + } + + if call.data.contract_id == cid && call.data.data[0] == DaoFunction::Propose as u8 { + eprintln!("Found Dao::Propose in call {i}"); + let params: DaoProposeParams = deserialize(&call.data.data[1..])?; + let tx_hash = if confirm { Some(blake3::hash(&serialize(tx))) } else { None }; + // We need to clone the tree here for reproducing the snapshot Merkle root + let money_tree = if confirm { Some(self.get_money_tree().await?) } else { None }; + new_dao_proposals.push((params, money_tree, tx_hash, i as u32)); + continue + } + + if call.data.contract_id == cid && call.data.data[0] == DaoFunction::Vote as u8 { + eprintln!("Found Dao::Vote in call {i}"); + let params: DaoVoteParams = deserialize(&call.data.data[1..])?; + let tx_hash = if confirm { Some(blake3::hash(&serialize(tx))) } else { None }; + new_dao_votes.push((params, tx_hash, i as u32)); + continue + } + + if call.data.contract_id == cid && call.data.data[0] == DaoFunction::Exec as u8 { + // This seems to not need any special action + eprintln!("Found Dao::Exec in call {i}"); + continue + } + } + + // This code should only be executed when finalized blocks are being scanned. + // Here we write the tx metadata, and actually do Merkle tree appends so we + // have to make sure it's the same for everyone. + if confirm { + for new_bulla in new_dao_bullas { + daos_tree.append(MerkleNode::from(new_bulla.0.inner())); + for dao in daos.iter_mut() { + if dao.bulla() == new_bulla.0 { + eprintln!( + "Found minted DAO {}, noting down for wallet update", + new_bulla.0 + ); + // We have this DAO imported in our wallet. Add the metadata: + dao.leaf_position = daos_tree.mark(); + dao.tx_hash = new_bulla.1; + dao.call_index = Some(new_bulla.2); + daos_to_confirm.push(dao.clone()); + } + } + } + + for proposal in new_dao_proposals { + proposals_tree.append(MerkleNode::from(proposal.0.proposal_bulla.inner())); + + // If we're able to decrypt this note, that's the way to link it + // to a specific DAO. + for dao in &daos { + if let Ok(note) = proposal.0.note.decrypt::(&dao.secret_key) { + // We managed to decrypt it. Let's place this in a proper + // DaoProposal object. We assume we can just increment the + // ID by looking at how many proposals we already have. + // We also assume we don't mantain duplicate DAOs in the + // wallet. + eprintln!("Managed to decrypt DAO proposal note"); + let daos_proposals = self.get_dao_proposals(dao.id).await?; + let our_prop = DaoProposal { + // This ID stuff is flaky. + id: daos_proposals.len() as u64 + our_proposals.len() as u64 + 1, + dao_bulla: dao.bulla(), + recipient: note.proposal.dest, + amount: note.proposal.amount, + token_id: note.proposal.token_id, + bulla_blind: note.proposal.blind, + leaf_position: proposals_tree.mark(), + money_snapshot_tree: proposal.1, + tx_hash: proposal.2, + call_index: Some(proposal.3), + vote_id: None, + }; + + our_proposals.push(our_prop); + break + } + } + } + + for vote in new_dao_votes { + for dao in &daos { + if let Ok(note) = vote.0.note.decrypt::(&dao.secret_key) { + eprintln!("Managed to decrypt DAO proposal vote note"); + let daos_proposals = self.get_dao_proposals(dao.id).await?; + let mut proposal_id = None; + + for i in daos_proposals { + if i.bulla() == vote.0.proposal_bulla.inner() { + proposal_id = Some(i.id); + break + } + } + + if proposal_id.is_none() { + eprintln!("Warning: Decrypted DaoVoteNote but did not find proposal"); + break + } + + let v = DaoVote { + id: 0, + proposal_id: proposal_id.unwrap(), + vote_option: note.vote_option, + yes_vote_blind: note.yes_vote_blind, + all_vote_value: note.all_vote_value, + all_vote_blind: note.all_vote_blind, + tx_hash: vote.1, + call_index: Some(vote.2), + }; + + dao_votes.push(v); + } + } + } + } + + if confirm { + if let Err(e) = self.put_dao_trees(&daos_tree, &proposals_tree).await { + return Err(Error::RusqliteError(format!( + "[apply_tx_dao_data] Put DAO tree failed: {e:?}" + ))) + } + if let Err(e) = self.confirm_daos(&daos_to_confirm).await { + return Err(Error::RusqliteError(format!( + "[apply_tx_dao_data] Confirm DAOs failed: {e:?}" + ))) + } + self.put_dao_proposals(&our_proposals).await?; + if let Err(e) = self.put_dao_votes(&dao_votes).await { + return Err(Error::RusqliteError(format!( + "[apply_tx_dao_data] Put DAO votes failed: {e:?}" + ))) + } + } + + Ok(()) + } + + /// Confirm already imported DAO metadata into the wallet. + /// Here we just write the leaf position, tx hash, and call index. + /// Panics if the fields are None. + pub async fn confirm_daos(&self, daos: &[Dao]) -> WalletDbResult<()> { + for dao in daos { + let query = format!( + "UPDATE {} SET {} = ?1, {} = ?2, {} = ?3 WHERE {} = {};", + DAO_DAOS_TABLE, + DAO_DAOS_COL_LEAF_POSITION, + DAO_DAOS_COL_TX_HASH, + DAO_DAOS_COL_CALL_INDEX, + DAO_DAOS_COL_DAO_ID, + dao.id, + ); + self.wallet + .exec_sql( + &query, + rusqlite::params![ + serialize(&dao.leaf_position.unwrap()), + serialize(&dao.tx_hash.unwrap()), + dao.call_index.unwrap() + ], + ) + .await?; + } + + Ok(()) + } + + /// Unconfirm imported DAOs by removing the leaf position, txid, and call index. + pub async fn unconfirm_daos(&self, daos: &[Dao]) -> WalletDbResult<()> { + for dao in daos { + let query = format!( + "UPDATE {} SET {} = ?1, {} = ?2, {} = ?3 WHERE {} = {};", + DAO_DAOS_TABLE, + DAO_DAOS_COL_LEAF_POSITION, + DAO_DAOS_COL_TX_HASH, + DAO_DAOS_COL_CALL_INDEX, + DAO_DAOS_COL_DAO_ID, + dao.id, + ); + self.wallet + .exec_sql(&query, rusqlite::params![None::>, None::>, None::,]) + .await?; + } + + Ok(()) + } + + /// Import given DAO proposals into the wallet + pub async fn put_dao_proposals(&self, proposals: &[DaoProposal]) -> Result<()> { + let daos = self.get_daos().await?; + + for proposal in proposals { + let Some(dao) = daos.iter().find(|x| x.bulla() == proposal.dao_bulla) else { + return Err(Error::RusqliteError( + "[put_dao_proposals] Couldn't find respective DAO".to_string(), + )) + }; + + let query = format!( + "INSERT INTO {} ({}, {}, {}, {}, {}, {}, {}, {}, {}) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9);", + DAO_PROPOSALS_TABLE, + DAO_PROPOSALS_COL_DAO_ID, + DAO_PROPOSALS_COL_RECV_PUBLIC, + DAO_PROPOSALS_COL_AMOUNT, + DAO_PROPOSALS_COL_SENDCOIN_TOKEN_ID, + DAO_PROPOSALS_COL_BULLA_BLIND, + DAO_PROPOSALS_COL_LEAF_POSITION, + DAO_PROPOSALS_COL_MONEY_SNAPSHOT_TREE, + DAO_PROPOSALS_COL_TX_HASH, + DAO_PROPOSALS_COL_CALL_INDEX, + ); + + if let Err(e) = self + .wallet + .exec_sql( + &query, + rusqlite::params![ + dao.id, + serialize(&proposal.recipient), + serialize(&proposal.amount), + serialize(&proposal.token_id), + serialize(&proposal.bulla_blind), + serialize(&proposal.leaf_position.unwrap()), + serialize(&proposal.money_snapshot_tree.clone().unwrap()), + serialize(&proposal.tx_hash.unwrap()), + proposal.call_index, + ], + ) + .await + { + return Err(Error::RusqliteError(format!( + "[put_dao_proposals] Proposal insert failed: {e:?}" + ))) + }; + } + + Ok(()) + } + + /// Import given DAO votes into the wallet + pub async fn put_dao_votes(&self, votes: &[DaoVote]) -> WalletDbResult<()> { + for vote in votes { + eprintln!("Importing DAO vote into wallet"); + + let query = format!( + "INSERT INTO {} ({}, {}, {}, {}, {}, {}, {}) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7);", + DAO_VOTES_TABLE, + DAO_VOTES_COL_PROPOSAL_ID, + DAO_VOTES_COL_VOTE_OPTION, + DAO_VOTES_COL_YES_VOTE_BLIND, + DAO_VOTES_COL_ALL_VOTE_VALUE, + DAO_VOTES_COL_ALL_VOTE_BLIND, + DAO_VOTES_COL_TX_HASH, + DAO_VOTES_COL_CALL_INDEX, + ); + + self.wallet + .exec_sql( + &query, + rusqlite::params![ + vote.proposal_id, + vote.vote_option as u64, + serialize(&vote.yes_vote_blind), + serialize(&vote.all_vote_value), + serialize(&vote.all_vote_blind), + serialize(&vote.tx_hash.unwrap()), + vote.call_index.unwrap(), + ], + ) + .await?; + + eprintln!("DAO vote added to wallet"); + } + + Ok(()) + } + + /// Reset the DAO Merkle trees in the wallet + pub async fn reset_dao_trees(&self) -> WalletDbResult<()> { + eprintln!("Resetting DAO Merkle trees"); + let tree = MerkleTree::new(100); + self.put_dao_trees(&tree, &tree).await?; + eprintln!("Successfully reset DAO Merkle trees"); + + Ok(()) + } + + /// Reset confirmed DAOs in the wallet + pub async fn reset_daos(&self) -> WalletDbResult<()> { + eprintln!("Resetting DAO confirmations"); + let daos = match self.get_daos().await { + Ok(d) => d, + Err(e) => { + eprintln!("[reset_daos] DAOs retrieval failed: {e:?}"); + return Err(WalletDbError::GenericError); + } + }; + self.unconfirm_daos(&daos).await?; + eprintln!("Successfully unconfirmed DAOs"); + + Ok(()) + } + + /// Reset all DAO proposals in the wallet + pub async fn reset_dao_proposals(&self) -> WalletDbResult<()> { + eprintln!("Resetting DAO proposals"); + let query = format!("DELETE FROM {};", DAO_PROPOSALS_TABLE); + self.wallet.exec_sql(&query, &[]).await + } + + /// Reset all DAO votes in the wallet + pub async fn reset_dao_votes(&self) -> WalletDbResult<()> { + eprintln!("Resetting DAO votes"); + let query = format!("DELETE FROM {};", DAO_VOTES_TABLE); + self.wallet.exec_sql(&query, &[]).await + } } diff --git a/bin/drk2/src/error.rs b/bin/drk2/src/error.rs index bafda15ce..bf9877621 100644 --- a/bin/drk2/src/error.rs +++ b/bin/drk2/src/error.rs @@ -35,4 +35,7 @@ pub enum WalletDbError { QueryFinalizationFailed = -32122, ParseColumnValueError = -32123, RowNotFound = -32124, + + // Generic error + GenericError = -32130, } diff --git a/bin/drk2/src/main.rs b/bin/drk2/src/main.rs index 7134d52a1..00ee6aa42 100644 --- a/bin/drk2/src/main.rs +++ b/bin/drk2/src/main.rs @@ -16,7 +16,14 @@ * along with this program. If not, see . */ -use std::{fs, io::stdin, process::exit, sync::Arc, time::Instant}; +use std::{ + fs, + io::{stdin, Read}, + process::exit, + str::FromStr, + sync::Arc, + time::Instant, +}; use prettytable::{format, row, Table}; use smol::stream::StreamExt; @@ -26,15 +33,23 @@ use url::Url; use darkfi::{ async_daemonize, cli_desc, rpc::{client::RpcClient, jsonrpc::JsonRequest, util::JsonValue}, + tx::Transaction, util::{parse::encode_base10, path::expand_path}, Result, }; -use darkfi_sdk::pasta::pallas; +use darkfi_money_contract::model::Coin; +use darkfi_sdk::{ + crypto::TokenId, + pasta::{group::ff::PrimeField, pallas}, +}; use darkfi_serial::{deserialize, serialize}; /// Error codes mod error; +/// darkfid JSON-RPC related methods +mod rpc; + /// CLI utility functions mod cli_util; use cli_util::kaching; @@ -46,6 +61,9 @@ use money::BALANCE_BASE10_DECIMALS; /// Wallet functionality related to Dao mod dao; +/// Wallet functionality related to transactions history +mod txs_history; + /// Wallet database operations handler mod walletdb; use walletdb::{WalletDb, WalletPtr}; @@ -137,6 +155,82 @@ enum Subcmd { /// Print all the coins in the wallet coins: bool, }, + + /// Unspend a coin + Unspend { + /// base58-encoded coin to mark as unspent + coin: String, + }, + + // TODO: Transfer + + // TODO: OTC + /// Inspect a transaction from stdin + Inspect, + + /// Read a transaction from stdin and broadcast it + Broadcast, + + /// This subscription will listen for incoming blocks from darkfid and look + /// through their transactions to see if there's any that interest us. + /// With `drk` we look at transactions calling the money contract so we can + /// find coins sent to us and fill our wallet with the necessary metadata. + Subscribe, + + // TODO: DAO + /// Scan the blockchain and parse relevant transactions + Scan { + #[structopt(long)] + /// Reset Merkle tree and start scanning from first block + reset: bool, + + #[structopt(long)] + /// List all available checkpoints + list: bool, + + #[structopt(long)] + /// Reset Merkle tree to checkpoint index and start scanning + checkpoint: Option, + }, + + // TODO: Explorer + /// Manage Token aliases + Alias { + #[structopt(subcommand)] + /// Sub command to execute + command: AliasSubcmd, + }, + // TODO: Token +} + +#[derive(Clone, Debug, Deserialize, StructOpt)] +enum AliasSubcmd { + /// Create a Token alias + Add { + /// Token alias + alias: String, + + /// Token to create alias for + token: String, + }, + + /// Print alias info of optional arguments. + /// If no argument is provided, list all the aliases in the wallet. + Show { + /// Token alias to search for + #[structopt(short, long)] + alias: Option, + + /// Token to search alias for + #[structopt(short, long)] + token: Option, + }, + + /// Remove a Token alias + Remove { + /// Token alias to remove + alias: String, + }, } /// CLI-util structure @@ -357,7 +451,7 @@ async fn realmain(args: Args, ex: Arc>) -> Result<()> { if let Ok(line) = line { let bytes = bs58::decode(&line.trim()).into_vec()?; let Ok(secret) = deserialize(&bytes) else { - eprintln!("Warning: Failed to deserialize secret on line {}", i); + eprintln!("Warning: Failed to deserialize secret on line {i}"); continue }; secrets.push(secret); @@ -437,12 +531,189 @@ async fn realmain(args: Args, ex: Arc>) -> Result<()> { ]); } - println!("{table}"); + eprintln!("{table}"); return Ok(()) } unreachable!() } + + Subcmd::Unspend { coin } => { + let bytes: [u8; 32] = match bs58::decode(&coin).into_vec()?.try_into() { + Ok(b) => b, + Err(e) => { + eprintln!("Invalid coin: {e:?}"); + exit(2); + } + }; + + let elem: pallas::Base = match pallas::Base::from_repr(bytes).into() { + Some(v) => v, + None => { + eprintln!("Invalid coin"); + exit(2); + } + }; + + let coin = Coin::from(elem); + let drk = Drk::new(args.wallet_path, args.wallet_pass, args.endpoint, ex).await?; + if let Err(e) = drk.unspend_coin(&coin).await { + eprintln!("Failed to mark coin as unspent: {e:?}"); + exit(2); + } + + Ok(()) + } + + Subcmd::Inspect => { + let mut buf = String::new(); + stdin().read_to_string(&mut buf)?; + let bytes = bs58::decode(&buf.trim()).into_vec()?; + let tx: Transaction = deserialize(&bytes)?; + eprintln!("{tx:#?}"); + Ok(()) + } + + Subcmd::Broadcast => { + eprintln!("Reading transaction from stdin..."); + let mut buf = String::new(); + stdin().read_to_string(&mut buf)?; + let bytes = bs58::decode(&buf.trim()).into_vec()?; + let tx = deserialize(&bytes)?; + + let drk = Drk::new(args.wallet_path, args.wallet_pass, args.endpoint, ex).await?; + + let txid = match drk.broadcast_tx(&tx).await { + Ok(t) => t, + Err(e) => { + eprintln!("Failed to broadcast transaction: {e:?}"); + exit(2); + } + }; + + eprintln!("Transaction ID: {txid}"); + + Ok(()) + } + + Subcmd::Subscribe => { + let drk = + Drk::new(args.wallet_path, args.wallet_pass, args.endpoint.clone(), ex.clone()) + .await?; + + if let Err(e) = drk.subscribe_blocks(args.endpoint, ex).await { + eprintln!("Block subscription failed: {e:?}"); + exit(2); + } + + Ok(()) + } + + Subcmd::Scan { reset, list, checkpoint } => { + let drk = + Drk::new(args.wallet_path, args.wallet_pass, args.endpoint.clone(), ex.clone()) + .await?; + + if reset { + eprintln!("Reset requested."); + if let Err(e) = drk.scan_blocks(true).await { + eprintln!("Failed during scanning: {e:?}"); + exit(2); + } + eprintln!("Finished scanning blockchain"); + + return Ok(()) + } + + if list { + eprintln!("List requested."); + // TODO: implement + + return Ok(()) + } + + if let Some(c) = checkpoint { + eprintln!("Checkpoint requested: {c}"); + // TODO: implement + + return Ok(()) + } + + if let Err(e) = drk.scan_blocks(false).await { + eprintln!("Failed during scanning: {e:?}"); + exit(2); + } + eprintln!("Finished scanning blockchain"); + + Ok(()) + } + + Subcmd::Alias { command } => match command { + AliasSubcmd::Add { alias, token } => { + if alias.chars().count() > 5 { + eprintln!("Error: Alias exceeds 5 characters"); + exit(2); + } + + let token_id = match TokenId::from_str(token.as_str()) { + Ok(t) => t, + Err(e) => { + eprintln!("Invalid Token ID: {e:?}"); + exit(2); + } + }; + + let drk = Drk::new(args.wallet_path, args.wallet_pass, args.endpoint, ex).await?; + if let Err(e) = drk.add_alias(alias, token_id).await { + eprintln!("Failed to add alias: {e:?}"); + exit(2); + } + + Ok(()) + } + + AliasSubcmd::Show { alias, token } => { + let token_id = match token { + Some(t) => match TokenId::from_str(t.as_str()) { + Ok(t) => Some(t), + Err(e) => { + eprintln!("Invalid Token ID: {e:?}"); + exit(2); + } + }, + None => None, + }; + + let drk = Drk::new(args.wallet_path, args.wallet_pass, args.endpoint, ex).await?; + let map = drk.get_aliases(alias, token_id).await?; + + // Create a prettytable with the new data: + let mut table = Table::new(); + table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR); + table.set_titles(row!["Alias", "Token ID"]); + for (alias, token_id) in map.iter() { + table.add_row(row![alias, token_id]); + } + + if table.is_empty() { + eprintln!("No aliases found"); + } else { + eprintln!("{table}"); + } + + Ok(()) + } + + AliasSubcmd::Remove { alias } => { + let drk = Drk::new(args.wallet_path, args.wallet_pass, args.endpoint, ex).await?; + if let Err(e) = drk.remove_alias(alias).await { + eprintln!("Failed to remove alias: {e:?}"); + exit(2); + } + + Ok(()) + } + }, } } diff --git a/bin/drk2/src/money.rs b/bin/drk2/src/money.rs index 9ca778393..1195665b4 100644 --- a/bin/drk2/src/money.rs +++ b/bin/drk2/src/money.rs @@ -21,19 +21,31 @@ use std::collections::HashMap; use rand::rngs::OsRng; use rusqlite::types::Value; -use darkfi::{zk::halo2::Field, Error, Result}; +use darkfi::{tx::Transaction, zk::halo2::Field, Error, Result}; use darkfi_money_contract::{ client::{ - MoneyNote, OwnCoin, MONEY_ALIASES_TABLE, MONEY_COINS_COL_IS_SPENT, MONEY_COINS_TABLE, + MoneyNote, OwnCoin, MONEY_ALIASES_COL_ALIAS, MONEY_ALIASES_COL_TOKEN_ID, + MONEY_ALIASES_TABLE, MONEY_COINS_COL_COIN, MONEY_COINS_COL_IS_SPENT, + MONEY_COINS_COL_LEAF_POSITION, MONEY_COINS_COL_MEMO, MONEY_COINS_COL_NULLIFIER, + MONEY_COINS_COL_SECRET, MONEY_COINS_COL_SERIAL, MONEY_COINS_COL_SPEND_HOOK, + MONEY_COINS_COL_TOKEN_BLIND, MONEY_COINS_COL_TOKEN_ID, MONEY_COINS_COL_USER_DATA, + MONEY_COINS_COL_VALUE, MONEY_COINS_COL_VALUE_BLIND, MONEY_COINS_TABLE, MONEY_INFO_COL_LAST_SCANNED_SLOT, MONEY_INFO_TABLE, MONEY_KEYS_COL_IS_DEFAULT, MONEY_KEYS_COL_KEY_ID, MONEY_KEYS_COL_PUBLIC, MONEY_KEYS_COL_SECRET, MONEY_KEYS_TABLE, + MONEY_TOKENS_COL_IS_FROZEN, MONEY_TOKENS_COL_TOKEN_ID, MONEY_TOKENS_TABLE, MONEY_TREE_COL_TREE, MONEY_TREE_TABLE, }, - model::Coin, + model::{ + Coin, MoneyTokenFreezeParamsV1, MoneyTokenMintParamsV1, MoneyTransferParamsV1, Output, + }, + MoneyFunction, }; use darkfi_sdk::{ bridgetree, - crypto::{Keypair, MerkleNode, MerkleTree, Nullifier, PublicKey, SecretKey, TokenId}, + crypto::{ + poseidon_hash, Keypair, MerkleNode, MerkleTree, Nullifier, PublicKey, SecretKey, TokenId, + MONEY_CONTRACT_ID, + }, pasta::pallas, }; use darkfi_serial::{deserialize, serialize}; @@ -41,13 +53,13 @@ use darkfi_serial::{deserialize, serialize}; use crate::{ convert_named_params, error::{WalletDbError, WalletDbResult}, - Drk, + kaching, Drk, }; pub const BALANCE_BASE10_DECIMALS: usize = 8; impl Drk { - /// Initialize wallet with tables for the Money contract + /// Initialize wallet with tables for the Money contract. pub async fn initialize_money(&self) -> WalletDbResult<()> { // Initialize Money wallet schema let wallet_schema = include_str!("../../../src/contract/money/wallet.sql"); @@ -198,7 +210,7 @@ impl Drk { Ok(vec) } - /// Fetch all secret keys from the wallet + /// Fetch all secret keys from the wallet. pub async fn get_money_secrets(&self) -> Result> { let rows = match self.wallet.query_multiple(MONEY_KEYS_TABLE, &[MONEY_KEYS_COL_SECRET], &[]).await @@ -400,6 +412,18 @@ impl Drk { Ok(owncoins) } + /// Create an alias record for provided Token ID. + pub async fn add_alias(&self, alias: String, token_id: TokenId) -> WalletDbResult<()> { + eprintln!("Generating alias {alias} for Token: {token_id}"); + let query = format!( + "INSERT OR REPLACE INTO {} ({}, {}) VALUES (?1, ?2);", + MONEY_ALIASES_TABLE, MONEY_ALIASES_COL_ALIAS, MONEY_ALIASES_COL_TOKEN_ID, + ); + self.wallet + .exec_sql(&query, rusqlite::params![serialize(&alias), serialize(&token_id)]) + .await + } + /// Fetch all aliases from the wallet. /// Optionally filter using alias name and/or token id. pub async fn get_aliases( @@ -458,6 +482,24 @@ impl Drk { Ok(map) } + /// Remove provided alias record from the wallet database. + pub async fn remove_alias(&self, alias: String) -> WalletDbResult<()> { + eprintln!("Removing alias: {alias}"); + let query = + format!("DELETE FROM {} WHERE {} = ?1;", MONEY_ALIASES_TABLE, MONEY_ALIASES_COL_ALIAS,); + self.wallet.exec_sql(&query, rusqlite::params![serialize(&alias)]).await + } + + /// Mark a given coin in the wallet as unspent. + pub async fn unspend_coin(&self, coin: &Coin) -> WalletDbResult<()> { + let is_spend = 0; + let query = format!( + "UPDATE {} SET {} = ?1 WHERE {} = ?2", + MONEY_COINS_TABLE, MONEY_COINS_COL_IS_SPENT, MONEY_COINS_COL_COIN, + ); + self.wallet.exec_sql(&query, rusqlite::params![is_spend, serialize(&coin.inner())]).await + } + /// Replace the Money Merkle tree in the wallet. pub async fn put_money_tree(&self, tree: &MerkleTree) -> WalletDbResult<()> { // First we remove old record @@ -470,7 +512,7 @@ impl Drk { self.wallet.exec_sql(&query, rusqlite::params![serialize(tree)]).await } - /// Fetch the Money Merkle tree from the wallet + /// Fetch the Money Merkle tree from the wallet. pub async fn get_money_tree(&self) -> Result { let row = match self.wallet.query_single(MONEY_TREE_TABLE, &[MONEY_TREE_COL_TREE], &[]).await { @@ -489,7 +531,7 @@ impl Drk { Ok(tree) } - /// Get the last scanned slot from the wallet + /// Get the last scanned slot from the wallet. pub async fn last_scanned_slot(&self) -> WalletDbResult { let ret = self .wallet @@ -504,4 +546,222 @@ impl Drk { Ok(slot) } + + /// Append data related to Money contract transactions into the wallet database. + pub async fn apply_tx_money_data(&self, tx: &Transaction, _confirm: bool) -> Result<()> { + let cid = *MONEY_CONTRACT_ID; + + let mut nullifiers: Vec = vec![]; + let mut outputs: Vec = vec![]; + let mut freezes: Vec = vec![]; + + for (i, call) in tx.calls.iter().enumerate() { + if call.data.contract_id == cid && call.data.data[0] == MoneyFunction::TransferV1 as u8 + { + eprintln!("Found Money::TransferV1 in call {i}"); + let params: MoneyTransferParamsV1 = deserialize(&call.data.data[1..])?; + + for input in params.inputs { + nullifiers.push(input.nullifier); + } + + for output in params.outputs { + outputs.push(output); + } + + continue + } + + if call.data.contract_id == cid && call.data.data[0] == MoneyFunction::OtcSwapV1 as u8 { + eprintln!("Found Money::OtcSwapV1 in call {i}"); + let params: MoneyTransferParamsV1 = deserialize(&call.data.data[1..])?; + + for input in params.inputs { + nullifiers.push(input.nullifier); + } + + for output in params.outputs { + outputs.push(output); + } + + continue + } + + if call.data.contract_id == cid && call.data.data[0] == MoneyFunction::TokenMintV1 as u8 + { + eprintln!("Found Money::MintV1 in call {i}"); + let params: MoneyTokenMintParamsV1 = deserialize(&call.data.data[1..])?; + outputs.push(params.output); + continue + } + + if call.data.contract_id == cid && + call.data.data[0] == MoneyFunction::TokenFreezeV1 as u8 + { + eprintln!("Found Money::FreezeV1 in call {i}"); + let params: MoneyTokenFreezeParamsV1 = deserialize(&call.data.data[1..])?; + let token_id = TokenId::derive_public(params.signature_public); + freezes.push(token_id); + } + } + + let secrets = self.get_money_secrets().await?; + let dao_secrets = self.get_dao_secrets().await?; + let mut tree = self.get_money_tree().await?; + + let mut owncoins = vec![]; + + for output in outputs { + let coin = output.coin; + + // Append the new coin to the Merkle tree. Every coin has to be added. + tree.append(MerkleNode::from(coin.inner())); + + // Attempt to decrypt the note + for secret in secrets.iter().chain(dao_secrets.iter()) { + if let Ok(note) = output.note.decrypt::(secret) { + eprintln!("Successfully decrypted a Money Note"); + eprintln!("Witnessing coin in Merkle tree"); + let leaf_position = tree.mark().unwrap(); + + let owncoin = OwnCoin { + coin, + note: note.clone(), + secret: *secret, + nullifier: Nullifier::from(poseidon_hash([secret.inner(), note.serial])), + leaf_position, + }; + + owncoins.push(owncoin); + } + } + } + + if let Err(e) = self.put_money_tree(&tree).await { + return Err(Error::RusqliteError(format!( + "[apply_tx_money_data] Put Money tree failed: {e:?}" + ))) + } + if !nullifiers.is_empty() { + self.mark_spent_coins(&nullifiers).await?; + } + + // This is the SQL query we'll be executing to insert new coins + // into the wallet + let query = format!( + "INSERT INTO {} ({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13);", + MONEY_COINS_TABLE, + MONEY_COINS_COL_COIN, + MONEY_COINS_COL_IS_SPENT, + MONEY_COINS_COL_SERIAL, + MONEY_COINS_COL_VALUE, + MONEY_COINS_COL_TOKEN_ID, + MONEY_COINS_COL_SPEND_HOOK, + MONEY_COINS_COL_USER_DATA, + MONEY_COINS_COL_VALUE_BLIND, + MONEY_COINS_COL_TOKEN_BLIND, + MONEY_COINS_COL_SECRET, + MONEY_COINS_COL_NULLIFIER, + MONEY_COINS_COL_LEAF_POSITION, + MONEY_COINS_COL_MEMO, + ); + + eprintln!("Found {} OwnCoin(s) in transaction", owncoins.len()); + for owncoin in &owncoins { + eprintln!("OwnCoin: {:?}", owncoin.coin); + let params = rusqlite::params![ + serialize(&owncoin.coin), + 0, // <-- is_spent + serialize(&owncoin.note.serial), + serialize(&owncoin.note.value), + serialize(&owncoin.note.token_id), + serialize(&owncoin.note.spend_hook), + serialize(&owncoin.note.user_data), + serialize(&owncoin.note.value_blind), + serialize(&owncoin.note.token_blind), + serialize(&owncoin.secret), + serialize(&owncoin.nullifier), + serialize(&owncoin.leaf_position), + serialize(&owncoin.note.memo), + ]; + + if let Err(e) = self.wallet.exec_sql(&query, params).await { + return Err(Error::RusqliteError(format!( + "[apply_tx_money_data] Inserting Money coin failed: {e:?}" + ))) + } + } + + for token_id in freezes { + let query = format!( + "UPDATE {} SET {} = 1 WHERE {} = ?1;", + MONEY_TOKENS_TABLE, MONEY_TOKENS_COL_IS_FROZEN, MONEY_TOKENS_COL_TOKEN_ID, + ); + + if let Err(e) = + self.wallet.exec_sql(&query, rusqlite::params![serialize(&token_id)]).await + { + return Err(Error::RusqliteError(format!( + "[apply_tx_money_data] Inserting Money coin failed: {e:?}" + ))) + } + } + + if !owncoins.is_empty() { + kaching().await; + } + + Ok(()) + } + + /// Mark a coin in the wallet as spent + pub async fn mark_spent_coin(&self, coin: &Coin) -> WalletDbResult<()> { + let query = format!( + "UPDATE {} SET {} = ?1 WHERE {} = ?2;", + MONEY_COINS_TABLE, MONEY_COINS_COL_IS_SPENT, MONEY_COINS_COL_COIN + ); + let is_spent = 1; + self.wallet.exec_sql(&query, rusqlite::params![is_spent, serialize(&coin.inner())]).await + } + + /// Marks all coins in the wallet as spent, if their nullifier is in the given set + pub async fn mark_spent_coins(&self, nullifiers: &[Nullifier]) -> Result<()> { + if nullifiers.is_empty() { + return Ok(()) + } + + for (coin, _) in self.get_coins(false).await? { + if nullifiers.contains(&coin.nullifier) { + if let Err(e) = self.mark_spent_coin(&coin.coin).await { + return Err(Error::RusqliteError(format!( + "[mark_spent_coins] Marking spent coin failed: {e:?}" + ))) + } + } + } + + Ok(()) + } + + /// Reset the Money Merkle tree in the wallet + pub async fn reset_money_tree(&self) -> WalletDbResult<()> { + eprintln!("Resetting Money Merkle tree"); + let mut tree = MerkleTree::new(100); + tree.append(MerkleNode::from(pallas::Base::ZERO)); + let _ = tree.mark().unwrap(); + self.put_money_tree(&tree).await?; + eprintln!("Successfully reset Money Merkle tree"); + + Ok(()) + } + + /// Reset the Money coins in the wallet + pub async fn reset_money_coins(&self) -> WalletDbResult<()> { + eprintln!("Resetting coins"); + let query = format!("DELETE FROM {};", MONEY_COINS_TABLE); + self.wallet.exec_sql(&query, &[]).await?; + eprintln!("Successfully reset coins"); + + Ok(()) + } } diff --git a/bin/drk2/src/rpc.rs b/bin/drk2/src/rpc.rs new file mode 100644 index 000000000..cf0be384d --- /dev/null +++ b/bin/drk2/src/rpc.rs @@ -0,0 +1,317 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2024 Dyne.org foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +use std::sync::Arc; + +use url::Url; + +use darkfi::{ + blockchain::BlockInfo, + rpc::{ + client::RpcClient, + jsonrpc::{JsonRequest, JsonResult}, + util::JsonValue, + }, + system::{StoppableTask, Subscriber}, + tx::Transaction, + Error, Result, +}; +use darkfi_money_contract::client::{MONEY_INFO_COL_LAST_SCANNED_SLOT, MONEY_INFO_TABLE}; +use darkfi_serial::{deserialize, serialize}; + +use super::{ + error::{WalletDbError, WalletDbResult}, + Drk, +}; + +impl Drk { + /// Subscribes to darkfid's JSON-RPC notification endpoint that serves + /// new finalized blocks. Upon receiving them, all the transactions are + /// scanned and we check if any of them call the money contract, and if + /// the payments are intended for us. If so, we decrypt them and append + /// the metadata to our wallet. + pub async fn subscribe_blocks( + &self, + endpoint: Url, + ex: Arc>, + ) -> Result<()> { + let req = JsonRequest::new("blockchain.last_known_slot", JsonValue::Array(vec![])); + let rep = self.rpc_client.request(req).await?; + let last_known = *rep.get::().unwrap() as u64; + let last_scanned = match self.last_scanned_slot().await { + Ok(l) => l, + Err(e) => { + return Err(Error::RusqliteError(format!( + "[subscribe_blocks] Retrieving last scanned slot failed: {e:?}" + ))) + } + }; + + if last_known != last_scanned { + eprintln!("Warning: Last scanned slot is not the last known slot."); + eprintln!("You should first fully scan the blockchain, and then subscribe"); + return Err(Error::RusqliteError( + "[subscribe_blocks] Blockchain not fully scanned".to_string(), + )) + } + + eprintln!("Subscribing to receive notifications of incoming blocks"); + let subscriber = Subscriber::new(); + let subscription = subscriber.clone().subscribe().await; + let _ex = ex.clone(); + StoppableTask::new().start( + // Weird hack to prevent lifetimes hell + async move { + let ex = _ex.clone(); + let rpc_client = RpcClient::new(endpoint, ex).await?; + let req = JsonRequest::new("blockchain.subscribe_blocks", JsonValue::Array(vec![])); + rpc_client.subscribe(req, subscriber).await + }, + |res| async move { + match res { + Ok(()) => { + eprintln!("wtf"); + } + Err(e) => eprintln!("[subscribe_blocks] JSON-RPC server error: {e:?}"), + } + }, + Error::RpcServerStopped, + ex, + ); + eprintln!("Detached subscription to background"); + eprintln!("All is good. Waiting for block notifications..."); + + let e = loop { + match subscription.receive().await { + JsonResult::Notification(n) => { + eprintln!("Got Block notification from darkfid subscription"); + if n.method != "blockchain.subscribe_blocks" { + break Error::UnexpectedJsonRpc(format!( + "Got foreign notification from darkfid: {}", + n.method + )) + } + + // Verify parameters + if !n.params.is_array() { + break Error::UnexpectedJsonRpc( + "Received notification params are not an array".to_string(), + ) + } + let params = n.params.get::>().unwrap(); + if params.is_empty() { + break Error::UnexpectedJsonRpc( + "Notification parameters are empty".to_string(), + ) + } + + for param in params { + let param = param.get::().unwrap(); + let bytes = bs58::decode(param).into_vec()?; + + let block_data: BlockInfo = deserialize(&bytes)?; + eprintln!("======================================="); + eprintln!("Block header:\n{:#?}", block_data.header); + eprintln!("======================================="); + + eprintln!("Deserialized successfully. Scanning block..."); + if let Err(e) = self.scan_block_money(&block_data).await { + return Err(Error::RusqliteError(format!( + "[subscribe_blocks] Scaning blocks for Money failed: {e:?}" + ))) + } + self.scan_block_dao(&block_data).await?; + if let Err(e) = self + .update_tx_history_records_status(&block_data.txs, "Finalized") + .await + { + return Err(Error::RusqliteError(format!( + "[subscribe_blocks] Update transaction history record status failed: {e:?}" + ))) + } + } + } + + JsonResult::Error(e) => { + // Some error happened in the transmission + break Error::UnexpectedJsonRpc(format!("Got error from JSON-RPC: {e:?}")) + } + + x => { + // And this is weird + break Error::UnexpectedJsonRpc(format!( + "Got unexpected data from JSON-RPC: {x:?}" + )) + } + } + }; + + Err(e) + } + + /// `scan_block_money` will go over transactions in a block and fetch the ones dealing + /// with the money contract. Then over all of them, try to see if any are related + /// to us. If any are found, the metadata is extracted and placed into the wallet + /// for future use. + async fn scan_block_money(&self, block: &BlockInfo) -> Result<()> { + eprintln!("[Money] Iterating over {} transactions", block.txs.len()); + + for tx in block.txs.iter() { + self.apply_tx_money_data(tx, true).await?; + } + + // Write this slot into `last_scanned_slot` + let query = + format!("UPDATE {} SET {} = ?1;", MONEY_INFO_TABLE, MONEY_INFO_COL_LAST_SCANNED_SLOT); + if let Err(e) = self.wallet.exec_sql(&query, rusqlite::params![block.header.height]).await { + return Err(Error::RusqliteError(format!( + "[scan_block_money] Update last scanned slot failed: {e:?}" + ))) + } + + Ok(()) + } + + /// `scan_block_dao` will go over transactions in a block and fetch the ones dealing + /// with the dao contract. Then over all of them, try to see if any are related + /// to us. If any are found, the metadata is extracted and placed into the wallet + /// for future use. + async fn scan_block_dao(&self, block: &BlockInfo) -> Result<()> { + eprintln!("[DAO] Iterating over {} transactions", block.txs.len()); + for tx in block.txs.iter() { + self.apply_tx_dao_data(tx, true).await?; + } + + Ok(()) + } + + /// Scans the blockchain starting from the last scanned slot, for relevant + /// money transfer transactions. If reset flag is provided, Merkle tree state + /// and coins are reset, and start scanning from beginning. Alternatively, + /// it looks for a checkpoint in the wallet to reset and start scanning from. + pub async fn scan_blocks(&self, reset: bool) -> WalletDbResult<()> { + let mut sl = if reset { + self.reset_money_tree().await?; + self.reset_money_coins().await?; + self.reset_dao_trees().await?; + self.reset_daos().await?; + self.reset_dao_proposals().await?; + self.reset_dao_votes().await?; + self.update_all_tx_history_records_status("Rejected").await?; + 0 + } else { + self.last_scanned_slot().await? + }; + + let req = JsonRequest::new("blockchain.last_known_slot", JsonValue::Array(vec![])); + let rep = match self.rpc_client.request(req).await { + Ok(r) => r, + Err(e) => { + eprintln!("[scan_blocks] RPC client request failed: {e:?}"); + return Err(WalletDbError::GenericError) + } + }; + let last = *rep.get::().unwrap() as u64; + + eprintln!("Requested to scan from slot number: {sl}"); + eprintln!("Last known slot number reported by darkfid: {last}"); + + // Already scanned last known slot + if sl == last { + return Ok(()) + } + + while sl <= last { + eprint!("Requesting slot {}... ", sl); + let requested_block = match self.get_block_by_slot(sl).await { + Ok(r) => r, + Err(e) => { + eprintln!("[scan_blocks] RPC client request failed: {e:?}"); + return Err(WalletDbError::GenericError) + } + }; + if let Some(block) = requested_block { + eprintln!("Found"); + if let Err(e) = self.scan_block_money(&block).await { + eprintln!("[scan_blocks] Scan block Money failed: {e:?}"); + return Err(WalletDbError::GenericError) + }; + if let Err(e) = self.scan_block_dao(&block).await { + eprintln!("[scan_blocks] Scan block DAO failed: {e:?}"); + return Err(WalletDbError::GenericError) + }; + self.update_tx_history_records_status(&block.txs, "Finalized").await?; + } else { + eprintln!("Not found"); + // Write down the slot number into back to the wallet + // This might be a bit intense, but we accept it for now. + let query = format!( + "UPDATE {} SET {} = ?1;", + MONEY_INFO_TABLE, MONEY_INFO_COL_LAST_SCANNED_SLOT + ); + self.wallet.exec_sql(&query, rusqlite::params![sl]).await?; + } + sl += 1; + } + + Ok(()) + } + + // Queries darkfid for a block with given slot + async fn get_block_by_slot(&self, slot: u64) -> Result> { + let req = JsonRequest::new( + "blockchain.get_slot", + JsonValue::Array(vec![JsonValue::String(slot.to_string())]), + ); + + // This API is weird, we need some way of telling it's an empty slot and + // not an error + match self.rpc_client.request(req).await { + Ok(params) => { + let param = params.get::().unwrap(); + let bytes = bs58::decode(param).into_vec()?; + let block = deserialize(&bytes)?; + Ok(Some(block)) + } + + Err(_) => Ok(None), + } + } + + /// Broadcast a given transaction to darkfid and forward onto the network. + /// Returns the transaction ID upon success + pub async fn broadcast_tx(&self, tx: &Transaction) -> Result { + eprintln!("Broadcasting transaction..."); + + let params = + JsonValue::Array(vec![JsonValue::String(bs58::encode(&serialize(tx)).into_string())]); + let req = JsonRequest::new("tx.broadcast", params); + let rep = self.rpc_client.request(req).await?; + + let txid = rep.get::().unwrap().clone(); + + // Store transactions history record + if let Err(e) = self.insert_tx_history_record(tx).await { + return Err(Error::RusqliteError(format!( + "[broadcast_tx] Inserting transaction history record failed: {e:?}" + ))) + } + + Ok(txid) + } +} diff --git a/bin/drk2/src/txs_history.rs b/bin/drk2/src/txs_history.rs new file mode 100644 index 000000000..3ec336016 --- /dev/null +++ b/bin/drk2/src/txs_history.rs @@ -0,0 +1,105 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2024 Dyne.org foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +use darkfi::tx::Transaction; +use darkfi_serial::serialize; + +use super::{ + error::{WalletDbError, WalletDbResult}, + Drk, +}; + +// Wallet SQL table constant names. These have to represent the `wallet.sql` +// SQL schema. +const WALLET_TXS_HISTORY_TABLE: &str = "transactions_history"; +const WALLET_TXS_HISTORY_COL_TX_HASH: &str = "transaction_hash"; +const WALLET_TXS_HISTORY_COL_STATUS: &str = "status"; +const WALLET_TXS_HISTORY_COL_TX: &str = "tx"; + +impl Drk { + /// Insert a [`Transaction`] history record into the wallet. + pub async fn insert_tx_history_record(&self, tx: &Transaction) -> WalletDbResult<()> { + let query = format!( + "INSERT INTO {} ({}, {}, {}) VALUES (?1, ?2, ?3);", + WALLET_TXS_HISTORY_TABLE, + WALLET_TXS_HISTORY_COL_TX_HASH, + WALLET_TXS_HISTORY_COL_STATUS, + WALLET_TXS_HISTORY_COL_TX, + ); + let Ok(tx_hash) = tx.hash() else { return Err(WalletDbError::QueryPreparationFailed) }; + self.wallet + .exec_sql( + &query, + rusqlite::params![ + tx_hash.to_string(), + "Broadcasted", + bs58::encode(&serialize(tx)).into_string() + ], + ) + .await + } + + /// Update a transactions history record status to the given one. + pub async fn update_tx_history_record_status( + &self, + tx_hash: &str, + status: &str, + ) -> WalletDbResult<()> { + let query = format!( + "UPDATE {} SET {} = ?1 WHERE {} = ?2;", + WALLET_TXS_HISTORY_TABLE, WALLET_TXS_HISTORY_COL_STATUS, WALLET_TXS_HISTORY_COL_TX_HASH, + ); + self.wallet.exec_sql(&query, rusqlite::params![status, tx_hash]).await + } + + /// Update given transactions history record statuses to the given one. + pub async fn update_tx_history_records_status( + &self, + txs: &Vec, + status: &str, + ) -> WalletDbResult<()> { + if txs.is_empty() { + return Ok(()) + } + + let mut txs_hashes = Vec::with_capacity(txs.len()); + for tx in txs { + let Ok(tx_hash) = tx.hash() else { return Err(WalletDbError::QueryPreparationFailed) }; + txs_hashes.push(tx_hash); + } + let txs_hashes_string = format!("{:?}", txs_hashes).replace('[', "(").replace(']', ")"); + let query = format!( + "UPDATE {} SET {} = ?1 WHERE {} IN {};", + WALLET_TXS_HISTORY_TABLE, + WALLET_TXS_HISTORY_COL_STATUS, + WALLET_TXS_HISTORY_COL_TX_HASH, + txs_hashes_string + ); + + self.wallet.exec_sql(&query, rusqlite::params![status]).await + } + + /// Update all transaction history records statuses to the given one. + pub async fn update_all_tx_history_records_status(&self, status: &str) -> WalletDbResult<()> { + let query = format!( + "UPDATE {} SET {} = ?1", + WALLET_TXS_HISTORY_TABLE, WALLET_TXS_HISTORY_COL_STATUS, + ); + self.wallet.exec_sql(&query, rusqlite::params![status]).await + } +}