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
Expand Up @@ -23,4 +23,7 @@ book
# ignore index.html
index.html

# ignore files generated by example
*.hodl

!/bindings/wasm/static/index.html
20 changes: 4 additions & 16 deletions examples/Cargo.toml
Expand Up @@ -15,37 +15,25 @@ 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"

[[example]]
name = "account_stronghold"
path = "account/stronghold.rs"

[[example]]
name = "account_private_tangle"
path = "account/private_tangle.rs"

[[example]]
name = "create_did"
path = "low-level-api/create_did.rs"
Expand Down
17 changes: 8 additions & 9 deletions examples/README.md
Expand Up @@ -18,28 +18,27 @@ For Instance, to run the example `getting_started`, use
cargo run --example getting_started
```

The following examples are available for using the basic account (A high level API):
The following examples are available for using the basic account (A high-level API):

| # | Name | Information |
| :--: | :----------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------- |
| 1 | [getting_started](./getting_started.rs) | Introductory example for you to test whether the library is set up / working properly and compiles. |
| 2 | [account_basic](./account/basic.rs) | A basic example that generates and publishes a DID Document, the fundamental building block for decentralized identity. |
| 3 | [account_config](./account/config.rs) | This example goes into more detail regarding the usage of DID Documents. |
| 4 | [account_methods](./account/methods.rs) | A basic example that generates and publishes subject and issuer DID Documents, then creates a Verifiable Credential (vc) specifying claims about the subject, and retrieves information through the CredentialValidator API. |
| 5 | [account_services](./account/services.rs) | This example explains how to create a Verifiable Presentation from a set of credentials and sign it. |
| 6 | [account_signing](./account/signing.rs) | A basic example that generates a DID Document, publishes it to the Tangle, and retrieves information through DID Document resolution/dereferencing. |
| 7 | [account_stronghold](./account/stronghold.rs) | An example that utilizes a diff and integration chain to publish updates to a DID Document. |
| 2 | [account_create_did](./account/create_did.rs) | A basic example that generates and publishes a DID Document, the fundamental building block for decentralized identity. |
| 3 | [account_config](./account/config.rs) | How to configure the account to work with different networks and other settings. |
| 4 | [account_manipulate](./account/manipulate_did.rs) | How to manipulate a DID Document by adding/removing Verification Methods and Services. |
| 5 | [account_lazy](./account/lazy.rs) | How to take control over publishing DID updates manually, instead of the default automated behavior. |
| 6 | [account_signing](./account/signing.rs) | Using a DID to sign arbitrary statements and validating them. |


The following examples are available for using the low-level APIs, which provides more flexibility at the cost of complexity:

| # | Name | Information |
| :--: | :----------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------- |
| 1 | [create_did](./low-level-api/create_did.rs) | A basic example that generates and publishes a DID Document, the fundamental building block for decentralized identity. |
| 2 | [manipulate_did](low-level-api/manipulate_did.rs) | This example demonstrates how to perform a basic update to integration chain of a DID Document. |
| 2 | [manipulate_did](low-level-api/manipulate_did.rs) | This example demonstrates how to perform a basic update to the integration chain of a DID Document. |
| 3 | [diff_chain](low-level-api/diff_chain.rs) | This example demonstrates how to perform a basic update to the diff chain of a DID Document. |
| 4 | [resolve_history](low-level-api/resolve_history.rs) | Advanced example that performs multiple diff chain and integration chain updates and demonstrates how to resolve the DID Document history to view these chains. |
| 5 | [create_vc](./low-level-api/create_vc.rs) | Generates and publishes subject and issuer DID Documents, then creates a Verifiable Credential (vc) specifying claims about the subject, and retrieves information through the CredentialValidator API. |
| 5 | [create_vc](./low-level-api/create_vc.rs) | Generates and publishes subject and issuer DID Documents, then creates a Verifiable Credential (VC) specifying claims about the subject, and retrieves information through the CredentialValidator API. |
| 6 | [create_vp](./low-level-api/create_vp.rs) | This example explains how to create a Verifiable Presentation from a set of credentials and sign it. |
| 7 | [resolution](./low-level-api/resolution.rs) | A basic example that shows how to retrieve information through DID Document resolution/dereferencing. |
| 8 | [revoke_vc](./low-level-api/revoke_vc.rs) | A basic example that shows how to retrieve information through DID Document resolution/dereferencing. |
Expand Down
40 changes: 0 additions & 40 deletions examples/account/basic.rs

This file was deleted.

49 changes: 45 additions & 4 deletions examples/account/config.rs
Expand Up @@ -6,13 +6,26 @@
use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::AutoSave;
use identity::account::IdentityCreate;
use identity::account::IdentityState;
use identity::account::Result;
use identity::iota::IotaDID;
use identity::iota::Network;

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

// Set-up for private Tangle
// This name needs to match the id of the network or part of it.
// The id of the one-click private tangle is `private-tangle` but we can only use 6 characters, we use recommend
// `tangle`. As an example we are treating the devnet as a `private-tangle`,
// there are easier ways to change to devnet via `Network::Devnet`
let network_name = "dev"; // Replace this with a `tangle` for the one-click private tangle.
let network = Network::try_from_name(network_name)?;
// In a locally running private tangle, this would often be `http://127.0.0.1:14265/`
let private_node_url = "https://api.lb-0.h.chrysalis-devnet.iota.cafe"; // Replace with your node of choice
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved

// Create a new Account with explicit configuration
let account: Account = Account::builder()
.autosave(AutoSave::Never) // never auto-save. rely on the drop save
Expand All @@ -21,18 +34,46 @@ 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
// configure a mainnet Tangle client with node and permanode
.client(Network::Mainnet, |builder| {
builder
.node("https://chrysalis-nodes.iota.org")
.node("https://chrysalis-nodes.iota.org") // Manipulate this in order to manually appoint nodes
.unwrap() // unwrap is safe, we provided a valid node URL
.permanode("https://chrysalis-chronicle.iota.org/api/mainnet/", None, None)
.permanode("https://chrysalis-chronicle.iota.org/api/mainnet/", None, None) // Set a permanode from the same network (Important)
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved
.unwrap() // unwrap is safe, we provided a valid permanode URL
})
// Configure a client for the private network, overwrites the mainnet client above
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved
// Also set the URL that points to the REST API of the node
.client(network, |builder| {
// unwrap is safe, we provided a valid node URL
builder.node(private_node_url).unwrap()
})
.build()
.await?;

println!("[Example] Account = {:#?}", account);
// Create an Identity specifically for the mainnet, replace with network_name variable for private Tangle
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved
let id_create = IdentityCreate::new().network(network_name)?;

// Create a new Identity with the network name set.
let identity: IdentityState = match account.create_identity(id_create).await {
Ok(identity) => identity,
Err(err) => {
eprintln!("[Example] Error: {:?} {}", err, err.to_string());
eprintln!("[Example] Is your Tangle node listening on {}?", private_node_url);
return Ok(());
}
};
let iota_did: &IotaDID = identity.try_did()?;

// 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()
)
);
JelleMillenaar marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}
54 changes: 54 additions & 0 deletions examples/account/create_did.rs
@@ -0,0 +1,54 @@
// 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::IdentityState;
use identity::account::Result;
use identity::iota::IotaDID;

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

// 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 identity: IdentityState = account.create_identity(IdentityCreate::default()).await?;
let iota_did: &IotaDID = identity.try_did()?;

// Print the local state of the DID Document
println!(
"[Example] Local Document from {} = {:#?}",
iota_did,
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(())
}
25 changes: 20 additions & 5 deletions examples/account/lazy.rs
Expand Up @@ -2,8 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

//! cargo run --example account_lazy
use std::path::PathBuf;

use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::IdentityCreate;
use identity::account::IdentityState;
use identity::account::Result;
Expand All @@ -14,10 +16,18 @@ use identity::iota::IotaDID;
async fn main() -> Result<()> {
pretty_env_logger::init();

// Stronghold settings
let stronghold_path: PathBuf = "./example-strong.hodl".into();
let password: String = "my-password".into();

// 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 account: Account = Account::builder().autopublish(false).build().await?;
let account: Account = Account::builder()
.storage(AccountStorage::Stronghold(stronghold_path, Some(password)))
.autopublish(false)
.build()
.await?;

// Create a new Identity with default settings.
// The identity will only be written to the local storage - not published to the tangle.
Expand Down Expand Up @@ -60,10 +70,15 @@ async fn main() -> Result<()> {
// Publish the updates as one message to the tangle.
account.publish_updates(did).await?;

// Resolve the document to confirm its consistency.
let doc = account.resolve_identity(did).await?;

println!("[Example] Document: {:#?}", doc);
// 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!(
"{}/{}",
did.network()?.explorer_url().unwrap().to_string(),
did.to_string()
)
);

Ok(())
}