Skip to content

Commit

Permalink
Added support for the "get_attribute" and "set_attribute" methods
Browse files Browse the repository at this point in the history
  • Loading branch information
refring committed Jun 30, 2023
1 parent fc531e4 commit 3832432
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,35 @@ impl WalletClient {

Ok((u16::try_from(major)?, u16::try_from(minor)?))
}

/// Returns an attribute as a string or an error when there is no attribute for the given key
pub async fn get_attribute(&self, key: String) -> anyhow::Result<String> {
let params = empty().chain(once(("key", key.into())));

#[derive(Deserialize)]
struct Rsp {
value: String,
}

Ok(self
.inner
.request::<Rsp>("get_attribute", RpcParams::map(params))
.await?
.value)
}

/// Set an arbitrary attribute which is saved in the wallet
pub async fn set_attribute(&self, key: String, value: String) -> anyhow::Result<()> {
let params = empty()
.chain(once(("key", key.into())))
.chain(once(("value", value.into())));

self.inner
.request::<IgnoredAny>("set_attribute", RpcParams::map(params))
.await?;

Ok(())
}
}

#[cfg(test)]
Expand Down
16 changes: 16 additions & 0 deletions tests/clients_tests/basic_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,21 @@ pub async fn run() {

helpers::wallet::sign_and_verify_assert_ok(&wallet, "test message").await;

helpers::wallet::get_attribute_error(&wallet, "nonexistingattribute".to_string()).await;

helpers::wallet::set_attribute_assert_ok(
&wallet,
"attribute_key".to_string(),
"attribute_value".to_string(),
)
.await;

helpers::wallet::get_attribute_assert_ok(
&wallet,
"attribute_key".to_string(),
"attribute_value".to_string(),
)
.await;

helpers::wallet::close_wallet_assert_ok(&wallet).await;
}
14 changes: 14 additions & 0 deletions tests/clients_tests/helpers/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,17 @@ pub async fn sign_and_verify_assert_ok(wallet: &WalletClient, message: &str) {
.unwrap();
assert!(signature_ok);
}

pub async fn get_attribute_error(wallet: &WalletClient, key: String) {
let err = wallet.get_attribute(key).await.unwrap_err();
assert_eq!(err.to_string(), "Server error: Attribute not found.");
}

pub async fn get_attribute_assert_ok(wallet: &WalletClient, key: String, expected_value: String) {
let value_from_wallet = wallet.get_attribute(key).await.unwrap();
assert_eq!(value_from_wallet, expected_value);
}

pub async fn set_attribute_assert_ok(wallet: &WalletClient, key: String, value: String) {
wallet.set_attribute(key, value).await.unwrap()
}

0 comments on commit 3832432

Please sign in to comment.