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

Make Wasm support dependent on target_arch rather than feature #666

Merged
merged 14 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
3 changes: 1 addition & 2 deletions bindings/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ wasm-bindgen-futures = { version = "0.4", default-features = false }
version = "=0.5.0-dev.4"
path = "../../identity"
default-features = false
features = ["wasm"]

[dev-dependencies]
wasm-bindgen-test = { version = "0.3" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
[target.'cfg(all(target_arch = "wasm32", target_os = "wasi"))'.dependencies]
PhilippGackstatter marked this conversation as resolved.
Show resolved Hide resolved
getrandom = { version = "0.2", features = ["js"] }
parking_lot = { version = "0.11.2", features = ["wasm-bindgen"] }

Expand Down
35 changes: 26 additions & 9 deletions examples/account/multiple_identities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn main() -> Result<()> {
let account1: Account = builder.create_identity(IdentitySetup::default()).await?;

// Create a second identity which uses the same storage.
let _account2: Account = builder.create_identity(IdentitySetup::default()).await?;
let mut account2: Account = builder.create_identity(IdentitySetup::default()).await?;

// Retrieve the did of the identity that account1 manages.
let iota_did1: IotaDID = account1.did().to_owned();
Expand All @@ -46,20 +46,37 @@ async fn main() -> Result<()> {
// We can load the identity from storage into an account using the builder.
let mut account1: Account = builder.load_identity(iota_did1).await?;

// Now we can modify the identity.
account1
.update_identity()
.create_method()
.fragment("my-key")
.apply()
.await?;
let account1_did: IotaDID = account1.did().clone();

// Now we can make modifications to the identity.
// We can even do so concurrently by spawning tasks.
let task1 = tokio::spawn(async move {
account1
.update_identity()
.create_method()
.fragment("my-key")
.apply()
.await
});

let task2 = tokio::spawn(async move {
account2
.update_identity()
.create_method()
.fragment("my-other-key")
.apply()
.await
});

task1.await.expect("task1 failed to execute to completion")?;
task2.await.expect("task2 failed to execute to completion")?;
PhilippGackstatter marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +71 to +72
Copy link
Contributor

@cycraig cycraig Feb 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently join is still suggested to await both tasks but it really doesn't matter since it doesn't affect the parallelism as you demonstrated.

https://docs.rs/tokio/latest/tokio/macro.join.html#runtime-characteristics

If parallelism is required, spawn each async expression using tokio::spawn and pass the join handle to join!.

No changes requested, just wanted to drop a link to the quote.


// Prints the Identity Resolver Explorer URL.
// The entire history can be observed on this page by clicking "Loading History".
let explorer: &ExplorerUrl = ExplorerUrl::mainnet();
println!(
"[Example] Explore the DID Document = {}",
explorer.resolver_url(account1.did())?
explorer.resolver_url(&account1_did)?
);

Ok(())
Expand Down
1 change: 0 additions & 1 deletion identity-account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,5 @@ stronghold = [
"actix",
"tokio/rt-multi-thread",
]
wasm = ["identity-iota/wasm"]
async = ["identity-iota/async"]
default = ["stronghold", "async"]
13 changes: 13 additions & 0 deletions identity-account/src/tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,16 @@ async fn network_resilient_test(
}
Ok(())
}

// Ensure that a future that contains an account is `Send` at compile-time.
async fn _assert_account_send() -> Result<()> {
fn assert_future_send<T: std::future::Future + Send>(_: T) {}

let mut account: Account = AccountBuilder::default()
.create_identity(IdentitySetup::default())
.await?;

assert_future_send(account.update_identity().create_method().apply());

Ok(())
}
olivereanderson marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 2 additions & 3 deletions identity-comm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ strum = { version = "0.21", features = ["derive"] }
thiserror = { version = "1.0" }
uuid = { version = "0.8", features = ["serde", "v4"], default-features = false }

[features]
# Enables Web Assembly support
wasm = ["uuid/wasm-bindgen"]
[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies]
uuid = { version = "0.8", features = ["serde", "v4", "wasm-bindgen"], default-features = false }
PhilippGackstatter marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 3 additions & 4 deletions identity-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ base64 = { version = "0.13", default-features = false, features = ["std"] }
bs58 = { version = "0.4", default-features = false, features = ["std"] }
hex = { version = "0.4", default-features = false }
identity-diff = { version = "=0.5.0-dev.4", path = "../identity-diff", default-features = false }
js-sys = { version = "0.3.55", default-features = false, optional = true }
multibase = { version = "0.9", default-features = false, features = ["std"] }
roaring = { version = "0.7", default-features = false }
serde = { version = "1.0", default-features = false, features = ["std", "derive"] }
Expand All @@ -34,15 +33,15 @@ version = "0.7"
default-features = false
features = ["blake2b", "ed25519", "random", "sha"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
PhilippGackstatter marked this conversation as resolved.
Show resolved Hide resolved
js-sys = { version = "0.3.55", default-features = false }

[dev-dependencies]
proptest = "1.0.0"
quickcheck = { version = "1.0" }
quickcheck_macros = { version = "1.0" }
rand = { version = "0.8" }

[features]
wasm = ["js-sys"]

[package.metadata.docs.rs]
# To build locally:
# RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features --no-deps --workspace --open
Expand Down
4 changes: 2 additions & 2 deletions identity-core/src/common/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Timestamp {
/// fractional seconds truncated.
///
/// See the [`datetime` DID-core specification](https://www.w3.org/TR/did-core/#production).
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasm")))]
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
pub fn now_utc() -> Self {
Self(truncate_fractional_seconds(OffsetDateTime::now_utc()))
}
Expand All @@ -49,7 +49,7 @@ impl Timestamp {
/// fractional seconds truncated.
///
/// See the [`datetime` DID-core specification](https://www.w3.org/TR/did-core/#production).
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasm"))]
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
pub fn now_utc() -> Self {
let milliseconds_since_unix_epoch: i64 = js_sys::Date::now() as i64;
let seconds: i64 = milliseconds_since_unix_epoch / 1000;
Expand Down
13 changes: 8 additions & 5 deletions identity-iota/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ thiserror = { version = "1.0", default-features = false }

[dependencies.iota-client]
git = "https://github.com/iotaledger/iota.rs"
rev = "c7d1cc4bae4ef00f16314239463ce115f8c6b35a"
rev = "c3c16d14449cb4f4b49cb3362daa12344d6f2aef"
default-features = false
features = ["pow-fallback"]
features = []

[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies.iota-client]
git = "https://github.com/iotaledger/iota.rs"
rev = "c3c16d14449cb4f4b49cb3362daa12344d6f2aef"
default-features = false
features = ["wasm"]

[dependencies.iota-crypto]
version = "0.7"
Expand All @@ -49,9 +55,6 @@ default = ["async"]
# Enables async runtime support (Tokio)
async = ["iota-client/async"]

# Enables Web Assembly support
wasm = ["iota-client/wasm"]

[package.metadata.docs.rs]
# To build locally:
# RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features --no-deps --workspace --open
Expand Down
3 changes: 0 additions & 3 deletions identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ default = ["async"]
# Enables async runtime support (Tokio)
async = ["identity-iota/async"]

# Enables Web Assembly support
wasm = ["identity-iota/wasm", "identity-comm/wasm", "identity-core/wasm"]

# Enables support for secure storage of DID Documents
account = ["identity-account"]

Expand Down