Przeglądaj źródła

drk: List imported token authorities.

parazyd 3 lat temu
rodzic
commit
bc09a2c6ff
2 zmienionych plików z 60 dodań i 3 usunięć
  1. 20 1
      bin/drk/src/main.rs
  2. 40 2
      bin/drk/src/wallet_token.rs

+ 20 - 1
bin/drk/src/main.rs

@@ -1164,7 +1164,26 @@ async fn main() -> Result<()> {
                 Ok(())
             }
 
-            TokenSubcmd::List => todo!(),
+            TokenSubcmd::List => {
+                let drk = Drk::new(args.endpoint).await?;
+                let tokens = drk.list_tokens().await?;
+
+                let mut table = Table::new();
+                table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
+                table.set_titles(row!["Token ID", "Mint Authority", "Frozen"]);
+
+                for (token_id, authority, frozen) in tokens {
+                    table.add_row(row![token_id, authority, frozen]);
+                }
+
+                if table.is_empty() {
+                    println!("No tokens found");
+                } else {
+                    println!("{}", table);
+                }
+
+                Ok(())
+            }
             TokenSubcmd::Mint { token, amount, recipient } => todo!(),
             TokenSubcmd::Freeze { token } => todo!(),
         },

+ 40 - 2
bin/drk/src/wallet_token.rs

@@ -16,12 +16,13 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use darkfi::{rpc::jsonrpc::JsonRequest, wallet::walletdb::QueryType, Result};
+use anyhow::{anyhow, Result};
+use darkfi::{rpc::jsonrpc::JsonRequest, wallet::walletdb::QueryType};
 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 darkfi_serial::{deserialize, serialize};
 use serde_json::json;
 
 use super::Drk;
@@ -55,4 +56,41 @@ impl Drk {
 
         Ok(())
     }
+
+    pub async fn list_tokens(&self) -> Result<Vec<(TokenId, SecretKey, bool)>> {
+        let mut ret = vec![];
+
+        let query = format!("SELECT * FROM {};", MONEY_TOKENS_TABLE);
+
+        let params = json!([
+            query,
+            QueryType::Blob as u8,
+            MONEY_TOKENS_MINT_AUTHORITY,
+            QueryType::Blob as u8,
+            MONEY_TOKENS_TOKEN_ID,
+            QueryType::Integer as u8,
+            MONEY_TOKENS_IS_FROZEN,
+        ]);
+
+        let req = JsonRequest::new("wallet.query_row_multi", params);
+        let rep = self.rpc_client.request(req).await?;
+
+        let Some(rows) = rep.as_array() else {
+            return Err(anyhow!("[list_tokens] Unexpected response from darkfid: {}", rep));
+        };
+
+        for row in rows {
+            let auth_bytes: Vec<u8> = serde_json::from_value(row[0].clone())?;
+            let mint_authority = deserialize(&auth_bytes)?;
+
+            let token_bytes: Vec<u8> = serde_json::from_value(row[1].clone())?;
+            let token_id = deserialize(&token_bytes)?;
+
+            let frozen: i32 = serde_json::from_value(row[2].clone())?;
+
+            ret.push((token_id, mint_authority, frozen != 0));
+        }
+
+        Ok(ret)
+    }
 }