mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
darkfid: implement get_keys rpc function to return all saved addresses
This commit is contained in:
@@ -170,12 +170,30 @@ impl Darkfid {
|
||||
|
||||
// --> {"method": "get_keys", "params": []}
|
||||
// <-- {"result": "[vdNS7oBj7KvsMWWmo9r96SV4SqATLrGsH2a3PGpCfJC, ... ]"}
|
||||
// Note: the first address in the returned vector is the default address
|
||||
async fn get_keys(&self, id: Value, _params: Value) -> JsonResult {
|
||||
match self.client.lock().await.get_keypairs().await {
|
||||
Ok(_) => {
|
||||
// TODO
|
||||
JsonResult::Resp(jsonresp(json!(vec!["ADDRESS", "ADDRESS"]), id))
|
||||
}
|
||||
let result: Result<Vec<String>> = async {
|
||||
let keypairs = self.client.lock().await.get_keypairs().await?;
|
||||
let default_keypair = self.client.lock().await.get_default_keypair().await?;
|
||||
|
||||
let mut addresses: Vec<String> = keypairs
|
||||
.iter()
|
||||
.filter_map(|k| {
|
||||
if *k == default_keypair {
|
||||
return None
|
||||
}
|
||||
Some(Address::from(k.public).to_string())
|
||||
})
|
||||
.collect();
|
||||
|
||||
addresses.insert(0, Address::from(default_keypair.public).to_string());
|
||||
|
||||
Ok(addresses)
|
||||
}
|
||||
.await;
|
||||
|
||||
match result {
|
||||
Ok(addresses) => JsonResult::Resp(jsonresp(json!(addresses), id)),
|
||||
Err(err) => JsonResult::Err(jsonerr(ServerError(-32003), Some(err.to_string()), id)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,8 +232,12 @@ async fn start(config: &DrkConfig, options: CliDrk) -> Result<()> {
|
||||
let reply = client.get_keys().await?;
|
||||
println!("Wallet addresses: ");
|
||||
if reply.as_array().is_some() {
|
||||
for address in reply.as_array().unwrap() {
|
||||
println!("- {}", address);
|
||||
for (i, address) in reply.as_array().unwrap().iter().enumerate() {
|
||||
if i == 0 {
|
||||
println!("- [X] {}", address);
|
||||
} else {
|
||||
println!("- [ ] {}", address);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("Empty!!",);
|
||||
|
||||
@@ -384,6 +384,10 @@ impl Client {
|
||||
self.wallet.get_keypairs().await
|
||||
}
|
||||
|
||||
pub async fn get_default_keypair(&self) -> Result<Keypair> {
|
||||
self.wallet.get_default_keypair().await
|
||||
}
|
||||
|
||||
pub async fn key_gen(&self) -> Result<()> {
|
||||
self.wallet.key_gen().await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user