Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to configure account clients #293

Merged
merged 3 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/account/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::AutoSave;
use identity::account::Result;
use identity::iota::Network;

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -20,6 +21,14 @@ async fn main() -> Result<()> {
.dropsave(false) // save the account state on drop
.milestone(4) // save a snapshot every 4 actions
.storage(AccountStorage::Memory) // use the default in-memory storage adapter
// configure the mainnet Tangle client
.client(Network::Mainnet, |scope| {
l1h3r marked this conversation as resolved.
Show resolved Hide resolved
scope
.node("https://chrysalis-nodes.iota.org")
.unwrap() // unwrap is safe, we provided a valid node URL
.permanode("https://chrysalis-chronicle.iota.org/api/mainnet/", None, None)
.unwrap() // unwrap is safe, we provided a valid permanode URL
})
.build()
.await?;

Expand Down
5 changes: 5 additions & 0 deletions identity-account/src/account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use identity_did::verification::MethodType;
use identity_iota::did::DocumentDiff;
use identity_iota::did::IotaDID;
use identity_iota::did::IotaDocument;
use identity_iota::tangle::Client;
use identity_iota::tangle::ClientMap;
use identity_iota::tangle::MessageId;
use identity_iota::tangle::TangleResolve;
Expand Down Expand Up @@ -93,6 +94,10 @@ impl Account {
self.state.actions.load(OSC)
}

pub fn set_client(&self, client: Client) -> Result<()> {
self.state.clients.insert(client).map_err(Into::into)
}

// ===========================================================================
// Identity
// ===========================================================================
Expand Down
35 changes: 30 additions & 5 deletions identity-account/src/account/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use hashbrown::HashMap;
use identity_iota::tangle::ClientBuilder;
use identity_iota::tangle::Network;
use std::path::PathBuf;
use zeroize::Zeroize;

Expand All @@ -25,6 +28,7 @@ pub enum AccountStorage {
pub struct AccountBuilder {
config: Config,
storage: AccountStorage,
clients: Option<HashMap<Network, ClientBuilder>>,
}

impl AccountBuilder {
Expand All @@ -33,6 +37,7 @@ impl AccountBuilder {
Self {
config: Config::new(),
storage: AccountStorage::Memory,
clients: None,
}
}

Expand Down Expand Up @@ -60,10 +65,22 @@ impl AccountBuilder {
self
}

/// Apply configuration to the IOTA Tangle client for the given `Network`.
pub fn client<F>(mut self, network: Network, f: F) -> Self
where
F: Fn(ClientBuilder) -> ClientBuilder,
l1h3r marked this conversation as resolved.
Show resolved Hide resolved
{
self
.clients
.get_or_insert_with(HashMap::new)
.insert(network, f(ClientBuilder::new().network(network)));
self
}

/// Creates a new [Account] based on the builder configuration.
pub async fn build(self) -> Result<Account> {
match self.storage {
AccountStorage::Memory => Account::with_config(MemStore::new(), self.config).await,
pub async fn build(mut self) -> Result<Account> {
let account: Account = match self.storage {
AccountStorage::Memory => Account::with_config(MemStore::new(), self.config).await?,
AccountStorage::Stronghold(snapshot, password) => {
let passref: Option<&str> = password.as_deref();
let adapter: Stronghold = Stronghold::new(&snapshot, passref).await?;
Expand All @@ -72,10 +89,18 @@ impl AccountBuilder {
password.zeroize();
}

Account::with_config(adapter, self.config).await
Account::with_config(adapter, self.config).await?
}
AccountStorage::Custom(adapter) => Account::with_config(adapter, self.config).await?,
};

if let Some(clients) = self.clients.take() {
for (_, client) in clients.into_iter() {
account.set_client(client.build().await?)?;
}
AccountStorage::Custom(adapter) => Account::with_config(adapter, self.config).await,
}

Ok(account)
}
}

Expand Down
10 changes: 10 additions & 0 deletions identity-iota/src/tangle/client_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ impl ClientMap {
Client::from_builder(builder).await.map(Self::from_client)
}

pub fn insert(&self, client: Client) -> Result<()> {
self
.data
.write()
.map_err(|_| Error::SharedWritePoisoned)?
.insert(client.network, Arc::new(client));

Ok(())
}

pub async fn publish_document(&self, document: &IotaDocument) -> Result<Receipt> {
let network: Network = document.id().network();
let client: Arc<Client> = self.client(network).await?;
Expand Down