From 97de8cc2b677d040caeeec2dc230ceeb7efcb8bb Mon Sep 17 00:00:00 2001 From: parazyd Date: Fri, 24 Feb 2023 13:23:23 +0100 Subject: [PATCH] drk: Implement token mint authority import. --- bin/drk/src/main.rs | 16 +++++++++- bin/drk/src/wallet_token.rs | 58 +++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 bin/drk/src/wallet_token.rs diff --git a/bin/drk/src/main.rs b/bin/drk/src/main.rs index e4f2edf3f..e853ece1b 100644 --- a/bin/drk/src/main.rs +++ b/bin/drk/src/main.rs @@ -74,6 +74,9 @@ use wallet_dao::DaoParams; /// Wallet functionality related to Money mod wallet_money; +/// Wallet functionality related to arbitrary tokens +mod wallet_token; + #[derive(Parser)] #[command(about = cli_desc!())] struct Args { @@ -1134,7 +1137,18 @@ async fn main() -> Result<()> { }, Subcmd::Token(cmd) => match cmd { - TokenSubcmd::Import => todo!(), + TokenSubcmd::Import => { + let mut buf = String::new(); + stdin().read_to_string(&mut buf)?; + let mint_authority = + SecretKey::from_str(buf.trim()).with_context(|| "Invalid secret key")?; + + let drk = Drk::new(args.endpoint).await?; + drk.import_mint_authority(mint_authority).await?; + + Ok(()) + } + TokenSubcmd::GenerateMint => todo!(), TokenSubcmd::List => todo!(), TokenSubcmd::Mint { token, amount, recipient } => todo!(), diff --git a/bin/drk/src/wallet_token.rs b/bin/drk/src/wallet_token.rs new file mode 100644 index 000000000..6a3b2cf19 --- /dev/null +++ b/bin/drk/src/wallet_token.rs @@ -0,0 +1,58 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2023 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::{rpc::jsonrpc::JsonRequest, wallet::walletdb::QueryType, Result}; +use darkfi_money_contract::client::{ + MONEY_TOKENS_IS_FROZEN, MONEY_TOKENS_MINT_AUTHORITY, MONEY_TOKENS_TABLE, MONEY_TOKENS_TOKEN_ID, +}; +use darkfi_sdk::crypto::{SecretKey, TokenId}; +use darkfi_serial::serialize; +use serde_json::json; + +use super::Drk; + +impl Drk { + /// Import a token mint authority into the wallet + pub async fn import_mint_authority(&self, mint_authority: SecretKey) -> Result<()> { + let token_id = TokenId::derive(mint_authority); + let is_frozen = 0; + + let query = format!( + "INSERT INTO {} ({}, {}, {}) VALUES (?1, ?2, ?3);", + MONEY_TOKENS_TABLE, + MONEY_TOKENS_MINT_AUTHORITY, + MONEY_TOKENS_TOKEN_ID, + MONEY_TOKENS_IS_FROZEN, + ); + + let params = json!([ + query, + QueryType::Blob as u8, + serialize(&mint_authority), + QueryType::Blob as u8, + serialize(&token_id), + QueryType::Integer as u8, + is_frozen, + ]); + + let req = JsonRequest::new("wallet.exec_sql", params); + let _ = self.rpc_client.request(req).await?; + + Ok(()) + } +}