wallet_token.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 darkfi::{rpc::jsonrpc::JsonRequest, wallet::walletdb::QueryType, Result};
  19. use darkfi_money_contract::client::{
  20. MONEY_TOKENS_IS_FROZEN, MONEY_TOKENS_MINT_AUTHORITY, MONEY_TOKENS_TABLE, MONEY_TOKENS_TOKEN_ID,
  21. };
  22. use darkfi_sdk::crypto::{SecretKey, TokenId};
  23. use darkfi_serial::serialize;
  24. use serde_json::json;
  25. use super::Drk;
  26. impl Drk {
  27. /// Import a token mint authority into the wallet
  28. pub async fn import_mint_authority(&self, mint_authority: SecretKey) -> Result<()> {
  29. let token_id = TokenId::derive(mint_authority);
  30. let is_frozen = 0;
  31. let query = format!(
  32. "INSERT INTO {} ({}, {}, {}) VALUES (?1, ?2, ?3);",
  33. MONEY_TOKENS_TABLE,
  34. MONEY_TOKENS_MINT_AUTHORITY,
  35. MONEY_TOKENS_TOKEN_ID,
  36. MONEY_TOKENS_IS_FROZEN,
  37. );
  38. let params = json!([
  39. query,
  40. QueryType::Blob as u8,
  41. serialize(&mint_authority),
  42. QueryType::Blob as u8,
  43. serialize(&token_id),
  44. QueryType::Integer as u8,
  45. is_frozen,
  46. ]);
  47. let req = JsonRequest::new("wallet.exec_sql", params);
  48. let _ = self.rpc_client.request(req).await?;
  49. Ok(())
  50. }
  51. }