wallet_token.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_IS_FROZEN, MONEY_TOKENS_MINT_AUTHORITY, MONEY_TOKENS_TABLE, MONEY_TOKENS_TOKEN_ID,
  22. };
  23. use darkfi_sdk::crypto::{SecretKey, TokenId};
  24. use darkfi_serial::{deserialize, serialize};
  25. use serde_json::json;
  26. use super::Drk;
  27. impl Drk {
  28. /// Import a token mint authority into the wallet
  29. pub async fn import_mint_authority(&self, mint_authority: SecretKey) -> Result<()> {
  30. let token_id = TokenId::derive(mint_authority);
  31. let is_frozen = 0;
  32. let query = format!(
  33. "INSERT INTO {} ({}, {}, {}) VALUES (?1, ?2, ?3);",
  34. MONEY_TOKENS_TABLE,
  35. MONEY_TOKENS_MINT_AUTHORITY,
  36. MONEY_TOKENS_TOKEN_ID,
  37. MONEY_TOKENS_IS_FROZEN,
  38. );
  39. let params = json!([
  40. query,
  41. QueryType::Blob as u8,
  42. serialize(&mint_authority),
  43. QueryType::Blob as u8,
  44. serialize(&token_id),
  45. QueryType::Integer as u8,
  46. is_frozen,
  47. ]);
  48. let req = JsonRequest::new("wallet.exec_sql", params);
  49. let _ = self.rpc_client.request(req).await?;
  50. Ok(())
  51. }
  52. pub async fn list_tokens(&self) -> Result<Vec<(TokenId, SecretKey, bool)>> {
  53. let mut ret = vec![];
  54. let query = format!("SELECT * FROM {};", MONEY_TOKENS_TABLE);
  55. let params = json!([
  56. query,
  57. QueryType::Blob as u8,
  58. MONEY_TOKENS_MINT_AUTHORITY,
  59. QueryType::Blob as u8,
  60. MONEY_TOKENS_TOKEN_ID,
  61. QueryType::Integer as u8,
  62. MONEY_TOKENS_IS_FROZEN,
  63. ]);
  64. let req = JsonRequest::new("wallet.query_row_multi", params);
  65. let rep = self.rpc_client.request(req).await?;
  66. let Some(rows) = rep.as_array() else {
  67. return Err(anyhow!("[list_tokens] Unexpected response from darkfid: {}", rep));
  68. };
  69. for row in rows {
  70. let auth_bytes: Vec<u8> = serde_json::from_value(row[0].clone())?;
  71. let mint_authority = deserialize(&auth_bytes)?;
  72. let token_bytes: Vec<u8> = serde_json::from_value(row[1].clone())?;
  73. let token_id = deserialize(&token_bytes)?;
  74. let frozen: i32 = serde_json::from_value(row[2].clone())?;
  75. ret.push((token_id, mint_authority, frozen != 0));
  76. }
  77. Ok(ret)
  78. }
  79. }