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

Chore/combine examples #420

Merged
merged 16 commits into from
Oct 15, 2021
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ book
# ignore index.html
index.html

# ignore files generated by example
*.hodl

!/bindings/wasm/static/index.html
12 changes: 4 additions & 8 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,21 @@ name = "getting_started"
path = "getting_started.rs"

[[example]]
name = "account_basic"
path = "account/basic.rs"
name = "account_create_did"
path = "account/create_did.rs"

[[example]]
name = "account_config"
path = "account/config.rs"

[[example]]
name = "account_methods"
path = "account/methods.rs"
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved
name = "account_manipulate"
path = "account/manipulate_did.rs"
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved

[[example]]
name = "account_lazy"
path = "account/lazy.rs"

[[example]]
name = "account_services"
path = "account/services.rs"

[[example]]
name = "account_signing"
path = "account/signing.rs"
Expand Down
41 changes: 0 additions & 41 deletions examples/account/basic.rs

This file was deleted.

56 changes: 56 additions & 0 deletions examples/account/create_did.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! cargo run --example create_did
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved

use std::path::PathBuf;

use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::IdentityCreate;
use identity::account::IdentitySnapshot;
use identity::account::Result;
use identity::iota::IotaDID;

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

// Calls the create_identity function
let (account, iota_did): (Account, IotaDID) = run().await?;

// Retrieve the DID from the newly created Identity state.
let snapshot: IdentitySnapshot = account.find_identity(iota_did.clone()).await?.unwrap();

// Print the local state of the DID Document
println!("[Example] Local Document from {} = {:#?}", iota_did.clone(), snapshot.identity().to_document());
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved
// Prints the Identity Resolver Explorer URL, the entire history can be observed on this page by "Loading History".
println!("[Example] Explore the DID Document = {}", format!("{}/{}", iota_did.network()?.explorer_url().unwrap().to_string(), iota_did.to_string()));
Ok(())
}


pub async fn run() -> Result<(Account, IotaDID)> {
// Sets the location and password for the Stronghold
//
// Stronghold is an encrypted file that manages private keys.
// It implements all security recommendation and is the recommended way of handling private keys.
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved
let stronghold_path: PathBuf = "./example-strong.hodl".into();
let password: String = "my-password".into();

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

// Create a new Identity with default settings
//
// This step generates a keypair, creates an identity and publishes it too the IOTA mainnet.
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved
let snapshot: IdentitySnapshot = account.create_identity(IdentityCreate::default()).await?;

// Retrieve the DID from the newly created Identity state.
let did: &IotaDID = snapshot.identity().try_did()?;

Ok((account, did.clone()))
}
60 changes: 60 additions & 0 deletions examples/account/manipulate_did.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! cargo run --example account_manipulate

use identity::account::Account;
use identity::account::Result;
use identity::did::MethodScope;
use identity::iota::IotaDID;
use identity::core::Url;

mod create_did;

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

//Calls the create_identity function from the create_did example - this is not part of the framework, but rather reusing the previous example.
let (account, iota_did): (Account, IotaDID) = create_did::run().await?;

// Add another Ed25519 verification method to the identity
account
.update_identity(&iota_did)
.create_method()
.fragment("my-next-key")
.apply()
.await?;

// Associate the newly created method with additional verification relationships
account
.update_identity(&iota_did)
.attach_method()
.fragment("my-next-key")
.scope(MethodScope::CapabilityDelegation)
.scope(MethodScope::CapabilityInvocation)
.apply()
.await?;

// Add a new service to the identity.
account
.update_identity(&iota_did)
.create_service()
.fragment("my-service-1")
.type_("MyCustomService")
.endpoint(Url::parse("https://example.com")?)
.apply()
.await?;

// Remove the Ed25519 verification method
account
.update_identity(&iota_did)
.delete_method()
.fragment("my-next-key")
.apply()
.await?;

//Prints the Identity Resolver Explorer URL, the entire history can be observed on this page by "Loading History".
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved
println!("[Example] Explore the DID Document = {}", format!("{}/{}", iota_did.network()?.explorer_url().unwrap().to_string(), iota_did.to_string()));
Ok(())
}
4 changes: 2 additions & 2 deletions identity-iota/src/tangle/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const NETWORK_NAME_MAIN: &str = "main";
const NETWORK_NAME_TEST: &str = "test";

lazy_static! {
static ref EXPLORER_MAIN: Url = Url::parse("https://explorer.iota.org/mainnet").unwrap();
static ref EXPLORER_TEST: Url = Url::parse("https://explorer.iota.org/testnet").unwrap();
static ref EXPLORER_MAIN: Url = Url::parse("https://explorer.iota.org/mainnet/identity-resolver").unwrap();
static ref EXPLORER_TEST: Url = Url::parse("https://explorer.iota.org/testnet/identity-resolver/").unwrap();
static ref NODE_MAIN: Url = Url::parse("https://chrysalis-nodes.iota.org").unwrap();
static ref NODE_TEST: Url = Url::parse("https://api.lb-0.testnet.chrysalis2.com").unwrap();
}
Expand Down