Skip to content

Commit

Permalink
Remove AccountStorage (#774)
Browse files Browse the repository at this point in the history
* Remove `AccountStorage`

* Update Wasm bindings

* Fix README.md

* Fix example

* Fix Napi Stronghold password parameter

* Fix clippy warning in tests

* Make Napi Stronghold password optional

* Make Napi Stronghold password optional

* Change Stronghold password to be required
  • Loading branch information
cycraig authored Mar 29, 2022
1 parent 07b1c9d commit c9d8b78
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 109 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,24 @@ tokio = { version = "1.14", features = ["full"] }
use std::path::PathBuf;

use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::IdentitySetup;
use identity::account::Result;
use identity::account_storage::Stronghold;
use identity::iota::ResolvedIotaDocument;

#[tokio::main]
async fn main() -> Result<()> {
pretty_env_logger::init();

// The Stronghold settings for the storage.
// Stronghold settings.
let stronghold_path: PathBuf = "./example-strong.hodl".into();
let password: String = "my-password".into();
let stronghold: Stronghold = Stronghold::new(&stronghold_path, Some(password), None).await?;

// Create a new identity with default settings and
// Stronghold as the storage.
let account: Account = Account::builder()
.storage(AccountStorage::Stronghold(stronghold_path, Some(password)))
.storage(stronghold)
.create_identity(IdentitySetup::default())
.await?;

Expand Down
2 changes: 2 additions & 0 deletions bindings/stronghold-nodejs/.license_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Copyright {20\d{2}(-20\d{2})?} IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
4 changes: 2 additions & 2 deletions bindings/stronghold-nodejs/js/stronghold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Stronghold implements Storage {
this.napiStronghold = await NapiStronghold.new(snapshot, password, dropsave);
}

public static async build (snapshot: string, password: string, dropsave?: boolean) {
public static async build(snapshot: string, password: string, dropsave?: boolean) {
const stronghold = new Stronghold();
await stronghold.init(snapshot, password, dropsave)
return stronghold
Expand Down Expand Up @@ -88,4 +88,4 @@ export class Stronghold implements Storage {
let napiDID = NapiDID.fromJSON(did.toJSON());
return this.napiStronghold.purge(napiDID);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl NapiStronghold {
#[napi]
pub async fn new(snapshot: String, password: String, dropsave: Option<bool>) -> Result<NapiStronghold> {
Ok(NapiStronghold(
Stronghold::new(&snapshot, &*password, dropsave).await.napi_result()?,
Stronghold::new(&snapshot, password, dropsave).await.napi_result()?,
))
}

Expand Down
3 changes: 1 addition & 2 deletions bindings/wasm/src/account/wasm_account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::sync::Arc;

use identity::account::Account;
use identity::account::AccountBuilder;
use identity::account::AccountStorage;
use identity::account::PublishOptions;
use identity::account_storage::Storage;
use identity::crypto::SetSignature;
Expand Down Expand Up @@ -99,7 +98,7 @@ impl WasmAccount {
future_to_promise(async move {
// Create a new account since `delete_identity` consumes it.
let account: Result<AccountRc> = AccountBuilder::new()
.storage(AccountStorage::Custom(storage))
.storage_shared(storage)
.load_identity(did)
.await
.wasm_result();
Expand Down
4 changes: 1 addition & 3 deletions bindings/wasm/src/account/wasm_account/account_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;

use identity::account::AccountBuilder;
use identity::account::AccountStorage;
use identity::account::IdentitySetup;
use identity::iota::Client;
use identity::iota::ClientBuilder;
Expand Down Expand Up @@ -61,7 +59,7 @@ impl WasmAccountBuilder {
};

if let Some(storage) = builder_options.storage() {
builder = builder.storage(AccountStorage::Custom(Arc::new(storage)));
builder = builder.storage(storage);
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/account/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

use identity::account::Account;
use identity::account::AccountBuilder;
use identity::account::AccountStorage;
use identity::account::AutoSave;
use identity::account::IdentitySetup;
use identity::account::Result;
use identity::account_storage::MemStore;
use identity::iota::ClientBuilder;
use identity::iota::ExplorerUrl;
use identity::iota_core::IotaDID;
Expand Down Expand Up @@ -40,7 +40,7 @@ async fn main() -> Result<()> {
.autosave(AutoSave::Every) // save immediately after every action
.autosave(AutoSave::Batch(10)) // save after every 10 actions
.autopublish(true) // publish to the tangle automatically on every update
.storage(AccountStorage::Memory) // use the default in-memory storage
.storage(MemStore::new()) // use the default in-memory storage
.client_builder(
// Configure a client for the private network
ClientBuilder::new()
Expand Down
7 changes: 4 additions & 3 deletions examples/account/create_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use std::path::PathBuf;

use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::IdentitySetup;
use identity::account::Result;
use identity::account_storage::Stronghold;
use identity::iota::ExplorerUrl;
use identity::iota_core::IotaDID;

Expand All @@ -21,14 +21,15 @@ async fn main() -> Result<()> {
// Stronghold is an encrypted file that manages private keys.
// It implements best practices for security and is the recommended way of handling private keys.
let stronghold_path: PathBuf = "./example-strong.hodl".into();
let password: String = "my-password".into();
let password: String = "my-password".to_owned();
let stronghold: Stronghold = Stronghold::new(&stronghold_path, password, None).await?;

// Create a new identity using Stronghold as local storage.
//
// The creation step generates a keypair, builds an identity
// and publishes it to the IOTA mainnet.
let account: Account = Account::builder()
.storage(AccountStorage::Stronghold(stronghold_path, Some(password), None))
.storage(stronghold)
.create_identity(IdentitySetup::default())
.await?;

Expand Down
7 changes: 4 additions & 3 deletions examples/account/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use std::path::PathBuf;

use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::IdentitySetup;
use identity::account::Result;
use identity::account_storage::Stronghold;
use identity::core::Url;
use identity::iota::ExplorerUrl;
use identity::iota_core::IotaDID;
Expand All @@ -18,13 +18,14 @@ async fn main() -> Result<()> {

// Stronghold settings
let stronghold_path: PathBuf = "./example-strong.hodl".into();
let password: String = "my-password".into();
let password: String = "my-password".to_owned();
let stronghold: Stronghold = Stronghold::new(&stronghold_path, password, None).await?;

// Create a new Account with auto publishing set to false.
// This means updates are not pushed to the tangle automatically.
// Rather, when we publish, multiple updates are batched together.
let mut account: Account = Account::builder()
.storage(AccountStorage::Stronghold(stronghold_path, Some(password), None))
.storage(stronghold)
.autopublish(false)
.create_identity(IdentitySetup::default())
.await?;
Expand Down
7 changes: 4 additions & 3 deletions examples/account/manipulate_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use std::path::PathBuf;

use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::IdentitySetup;
use identity::account::MethodContent;
use identity::account::Result;
use identity::account_storage::Stronghold;
use identity::core::Url;
use identity::did::MethodRelationship;
use identity::iota::ExplorerUrl;
Expand All @@ -25,11 +25,12 @@ async fn main() -> Result<()> {

// Stronghold settings
let stronghold_path: PathBuf = "./example-strong.hodl".into();
let password: String = "my-password".into();
let password: String = "my-password".to_owned();
let stronghold: Stronghold = Stronghold::new(&stronghold_path, password, None).await?;

// Create a new Account with the default configuration
let mut account: Account = Account::builder()
.storage(AccountStorage::Stronghold(stronghold_path, Some(password), None))
.storage(stronghold)
.create_identity(IdentitySetup::default())
.await?;

Expand Down
8 changes: 4 additions & 4 deletions examples/account/multiple_identities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use std::path::PathBuf;

use identity::account::Account;
use identity::account::AccountBuilder;
use identity::account::AccountStorage;
use identity::account::IdentitySetup;
use identity::account::MethodContent;
use identity::account::Result;
use identity::account_storage::Stronghold;
use identity::iota::ExplorerUrl;
use identity::iota_core::IotaDID;

Expand All @@ -23,12 +23,12 @@ async fn main() -> Result<()> {
// Stronghold is an encrypted file that manages private keys.
// It implements best practices for security and is the recommended way of handling private keys.
let stronghold_path: PathBuf = "./example-strong.hodl".into();
let password: String = "my-password".into();
let password: String = "my-password".to_owned();
let stronghold: Stronghold = Stronghold::new(&stronghold_path, password, None).await?;

// Create an AccountBuilder to make it easier to create multiple identities.
// Every account created from the builder will use the same storage - stronghold in this case.
let mut builder: AccountBuilder =
Account::builder().storage(AccountStorage::Stronghold(stronghold_path, Some(password), None));
let mut builder: AccountBuilder = Account::builder().storage(stronghold);

// The creation step generates a keypair, builds an identity
// and publishes it to the IOTA mainnet.
Expand Down
7 changes: 4 additions & 3 deletions examples/account/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use std::path::PathBuf;

use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::IdentitySetup;
use identity::account::MethodContent;
use identity::account::Result;
use identity::account_storage::Stronghold;
use identity::core::json;
use identity::core::FromJson;
use identity::core::Url;
Expand All @@ -34,11 +34,12 @@ async fn main() -> Result<()> {

// Stronghold settings
let stronghold_path: PathBuf = "./example-strong.hodl".into();
let password: String = "my-password".into();
let password: String = "my-password".to_owned();
let stronghold: Stronghold = Stronghold::new(&stronghold_path, password, None).await?;

// Create a new Account with stronghold storage.
let mut account: Account = Account::builder()
.storage(AccountStorage::Stronghold(stronghold_path, Some(password), None))
.storage(stronghold)
.create_identity(IdentitySetup::default())
.await?;

Expand Down
7 changes: 4 additions & 3 deletions examples/account/unchecked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use std::path::PathBuf;

use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::IdentitySetup;
use identity::account::Result;
use identity::account_storage::Stronghold;
use identity::core::Timestamp;
use identity::iota::ExplorerUrl;
use identity::iota_core::IotaDID;
Expand All @@ -23,14 +23,15 @@ async fn main() -> Result<()> {
// Stronghold is an encrypted file that manages private keys.
// It implements best practices for security and is the recommended way of handling private keys.
let stronghold_path: PathBuf = "./example-strong.hodl".into();
let password: String = "my-password".into();
let password: String = "my-password".to_owned();
let stronghold: Stronghold = Stronghold::new(&stronghold_path, password, None).await?;

// Create a new identity using Stronghold as local storage.
//
// The creation step generates a keypair, builds an identity
// and publishes it to the IOTA mainnet.
let mut account: Account = Account::builder()
.storage(AccountStorage::Stronghold(stronghold_path, Some(password), None))
.storage(stronghold)
.create_identity(IdentitySetup::default())
.await?;

Expand Down
24 changes: 15 additions & 9 deletions identity-account-storage/src/storage/stronghold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ use std::sync::Arc;

use async_trait::async_trait;
use futures::executor;
use iota_stronghold::procedures::Ed25519Sign;
use iota_stronghold::procedures::GenerateKey;
use iota_stronghold::Location;
use zeroize::Zeroize;

use identity_core::convert::FromJson;
use identity_core::convert::ToJson;
use identity_core::crypto::PrivateKey;
use identity_core::crypto::PublicKey;
use identity_did::did::DID;
use identity_did::verification::MethodType;
use identity_iota_core::did::IotaDID;
use iota_stronghold::procedures::Ed25519Sign;
use iota_stronghold::procedures::GenerateKey;
use iota_stronghold::Location;

use crate::error::Result;
use crate::identity::ChainState;
Expand All @@ -36,16 +38,20 @@ pub struct Stronghold {
}

impl Stronghold {
pub async fn new<'a, T, U>(snapshot: &T, password: U, dropsave: Option<bool>) -> Result<Self>
/// Constructs a Stronghold storage instance.
///
/// Arguments:
///
/// * snapshot: path to a local Stronghold file, will be created if it does not exist.
/// * password: password for the Stronghold file.
/// * dropsave: save all changes when the instance is dropped. Default: true.
pub async fn new<'a, T>(snapshot: &T, mut password: String, dropsave: Option<bool>) -> Result<Self>
where
T: AsRef<Path> + ?Sized,
U: Into<Option<&'a str>>,
{
let snapshot: Snapshot = Snapshot::new(snapshot);

if let Some(password) = password.into() {
snapshot.load(derive_encryption_key(password)).await?;
}
snapshot.load(derive_encryption_key(&password)).await?;
password.zeroize();

Ok(Self {
snapshot: Arc::new(snapshot),
Expand Down
3 changes: 1 addition & 2 deletions identity-account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ rand = { version = "0.8", default-features = false, features = ["std", "std_rng"
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] }
strum = { version = "0.24.0", default-features = false, features = ["std", "derive"] }
thiserror = { version = "1.0" }
zeroize = { version = "1.4", optional = true }

[dev-dependencies]
futures = { version = "0.3" }
Expand All @@ -33,6 +32,6 @@ tokio = { version = "1.17.0", default-features = false, features = ["macros", "r
[features]
default = ["stronghold", "async", "send-sync-storage"]
mem-client = []
stronghold = ["identity-account-storage/stronghold", "zeroize"]
stronghold = ["identity-account-storage/stronghold"]
async = ["identity-iota/async"]
send-sync-storage = ["identity-account-storage/send-sync-storage"]
Loading

0 comments on commit c9d8b78

Please sign in to comment.