wallet_token.rs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use anyhow::{anyhow, Result};
  19. use darkfi::{rpc::jsonrpc::JsonRequest, wallet::walletdb::QueryType};
  20. use darkfi_money_contract::client::{
  21. MONEY_TOKENS_COL_IS_FROZEN, MONEY_TOKENS_COL_MINT_AUTHORITY, MONEY_TOKENS_COL_TOKEN_ID,
  22. MONEY_TOKENS_TABLE,
  23. };
  24. use darkfi_sdk::crypto::{SecretKey, TokenId};
  25. use darkfi_serial::{deserialize, serialize};
  26. use serde_json::json;
  27. use super::Drk;
  28. impl Drk {
  29. /// Import a token mint authority into the wallet
  30. pub async fn import_mint_authority(&self, mint_authority: SecretKey) -> Result<()> {
  31. let token_id = TokenId::derive(mint_authority);
  32. let is_frozen = 0;
  33. let query = format!(
  34. "INSERT INTO {} ({}, {}, {}) VALUES (?1, ?2, ?3);",
  35. MONEY_TOKENS_TABLE,
  36. MONEY_TOKENS_COL_MINT_AUTHORITY,
  37. MONEY_TOKENS_COL_TOKEN_ID,
  38. MONEY_TOKENS_COL_IS_FROZEN,
  39. );
  40. let params = json!([
  41. query,
  42. QueryType::Blob as u8,
  43. serialize(&mint_authority),
  44. QueryType::Blob as u8,
  45. serialize(&token_id),
  46. QueryType::Integer as u8,
  47. is_frozen,
  48. ]);
  49. let req = JsonRequest::new("wallet.exec_sql", params);
  50. let _ = self.rpc_client.request(req).await?;
  51. Ok(())
  52. }
  53. pub async fn list_tokens(&self) -> Result<Vec<(TokenId, SecretKey, bool)>> {
  54. let mut ret = vec![];
  55. let query = format!("SELECT * FROM {};", MONEY_TOKENS_TABLE);
  56. let params = json!([
  57. query,
  58. QueryType::Blob as u8,
  59. MONEY_TOKENS_COL_MINT_AUTHORITY,
  60. QueryType::Blob as u8,
  61. MONEY_TOKENS_COL_TOKEN_ID,
  62. QueryType::Integer as u8,
  63. MONEY_TOKENS_COL_IS_FROZEN,
  64. ]);
  65. let req = JsonRequest::new("wallet.query_row_multi", params);
  66. let rep = self.rpc_client.request(req).await?;
  67. let Some(rows) = rep.as_array() else {
  68. return Err(anyhow!("[list_tokens] Unexpected response from darkfid: {}", rep))
  69. };
  70. for row in rows {
  71. let auth_bytes: Vec<u8> = serde_json::from_value(row[0].clone())?;
  72. let mint_authority = deserialize(&auth_bytes)?;
  73. let token_bytes: Vec<u8> = serde_json::from_value(row[1].clone())?;
  74. let token_id = deserialize(&token_bytes)?;
  75. let frozen: i32 = serde_json::from_value(row[2].clone())?;
  76. ret.push((token_id, mint_authority, frozen != 0));
  77. }
  78. Ok(ret)
  79. }
  80. }