drk/deploy: Implement deploy auth listing

This commit is contained in:
parazyd
2024-04-09 10:06:55 +02:00
parent f011b02336
commit faf96deeaa
2 changed files with 55 additions and 1 deletions

View File

@@ -19,8 +19,10 @@
use lazy_static::lazy_static;
use rand::rngs::OsRng;
use darkfi::{Error, Result};
use darkfi_sdk::crypto::{ContractId, Keypair, DEPLOYOOOR_CONTRACT_ID};
use darkfi_serial::serialize_async;
use darkfi_serial::{deserialize_async, serialize_async};
use rusqlite::types::Value;
use crate::{error::WalletDbResult, Drk};
@@ -62,4 +64,32 @@ impl Drk {
Ok(())
}
/// List contract deploy authorities from the wallet
pub async fn list_deploy_auth(&self) -> Result<Vec<(ContractId, bool)>> {
let rows = match self.wallet.query_multiple(&DEPLOY_AUTH_TABLE, &[], &[]).await {
Ok(r) => r,
Err(e) => {
return Err(Error::RusqliteError(format!(
"[list_deploy_auth] Deploy auth retrieval failed: {e:?}",
)))
}
};
let mut ret = Vec::with_capacity(rows.len());
for row in rows {
let Value::Blob(ref auth_bytes) = row[0] else {
return Err(Error::ParseFailed("[list_deploy_auth] Failed to parse keypair bytes"))
};
let deploy_auth: Keypair = deserialize_async(auth_bytes).await?;
let Value::Integer(frozen) = row[1] else {
return Err(Error::ParseFailed("[list_deploy_auth] Failed to parse \"is_frozen\""))
};
ret.push((ContractId::derive_public(deploy_auth.public), frozen != 0))
}
Ok(ret)
}
}

View File

@@ -487,6 +487,9 @@ enum TokenSubcmd {
enum ContractSubcmd {
/// Generate a new deploy authority
GenerateDeploy,
/// List deploy authorities in the wallet
List,
/*
/// Deploy a smart contract
Deploy {
@@ -1660,6 +1663,27 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
Ok(())
}
ContractSubcmd::List => {
let drk = Drk::new(args.wallet_path, args.wallet_pass, args.endpoint, ex).await?;
let auths = drk.list_deploy_auth().await?;
let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
table.set_titles(row!["Contract ID", "Frozen"]);
for (contract_id, frozen) in auths {
table.add_row(row![contract_id, frozen]);
}
if table.is_empty() {
eprintln!("No deploy authorities found");
} else {
println!("{table}");
}
Ok(())
}
},
}
}