From 44233ccc03967b18368c56f8461b366cec19a92d Mon Sep 17 00:00:00 2001 From: "Oliver E. Anderson" Date: Mon, 13 Dec 2021 11:36:33 +0100 Subject: [PATCH] Fix `Timestamp` in the Wasm bindings (#541) Fixed `Timestamp` in the Wasm bindings. --- bindings/wasm/tests/wasm.rs | 8 ++++---- identity-core/Cargo.toml | 4 ++++ identity-core/src/common/timestamp.rs | 13 +++++++++++++ identity/Cargo.toml | 4 ++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/bindings/wasm/tests/wasm.rs b/bindings/wasm/tests/wasm.rs index 9aa93254e3..d27c874ee5 100644 --- a/bindings/wasm/tests/wasm.rs +++ b/bindings/wasm/tests/wasm.rs @@ -70,7 +70,7 @@ fn test_js_error_from_wasm_error() { assert_eq!(js_error.message(), "Error message"); } -#[test] +#[wasm_bindgen_test] fn test_did() { let key = KeyPair::new(KeyType::Ed25519).unwrap(); let did = WasmDID::new(&key, None).unwrap(); @@ -88,7 +88,7 @@ fn test_did() { assert_eq!(base58.network_name(), "dev"); } -#[test] +#[wasm_bindgen_test] fn test_did_url() { // Base DID Url let key = KeyPair::new(KeyType::Ed25519).unwrap(); @@ -114,7 +114,7 @@ fn test_did_url() { ); } -#[test] +#[wasm_bindgen_test] fn test_document_new() { let keypair: KeyPair = KeyPair::new(KeyType::Ed25519).unwrap(); let mut document: WasmDocument = WasmDocument::new(&keypair, None, None).unwrap(); @@ -126,7 +126,7 @@ fn test_document_new() { assert!(document.verify_self_signed()); } -#[test] +#[wasm_bindgen_test] fn test_document_network() { let keypair: KeyPair = KeyPair::new(KeyType::Ed25519).unwrap(); let document: WasmDocument = WasmDocument::new(&keypair, Some("dev".to_owned()), None).unwrap(); diff --git a/identity-core/Cargo.toml b/identity-core/Cargo.toml index f3e196e458..bbd6984aad 100644 --- a/identity-core/Cargo.toml +++ b/identity-core/Cargo.toml @@ -15,6 +15,7 @@ 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.4.0", 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"] } @@ -39,6 +40,9 @@ 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 diff --git a/identity-core/src/common/timestamp.rs b/identity-core/src/common/timestamp.rs index 37632fa04d..96d5e5cc90 100644 --- a/identity-core/src/common/timestamp.rs +++ b/identity-core/src/common/timestamp.rs @@ -37,10 +37,23 @@ 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")))] pub fn now_utc() -> Self { Self(truncate_fractional_seconds(OffsetDateTime::now_utc())) } + /// Creates a new `Timestamp` with the current date and time, normalized to UTC+00:00 with + /// 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"))] + 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; + // expect is okay, we assume the current time is between 0AD and 9999AD + Self::from_unix(seconds).expect("Timestamp failed to convert system datetime") + } + /// Returns the `Timestamp` as an RFC 3339 `String`. /// /// See: https://tools.ietf.org/html/rfc3339 diff --git a/identity/Cargo.toml b/identity/Cargo.toml index 1c8b77b5ae..8b32e5df7b 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -14,7 +14,7 @@ description = "Tools for working with Self-sovereign Identity." [dependencies] identity-account = { version = "=0.4.0", path = "../identity-account", optional = true } identity-comm = { version = "=0.4.0", path = "../identity-comm", optional = true } -identity-core = { version = "=0.4.0", path = "../identity-core" } +identity-core = { version = "=0.4.0", path = "../identity-core", default-features = false } identity-credential = { version = "=0.4.0", path = "../identity-credential" } identity-did = { version = "=0.4.0", path = "../identity-did" } identity-iota = { version = "=0.4.0", path = "../identity-iota", default-features = false } @@ -34,7 +34,7 @@ default = ["async"] async = ["identity-iota/async"] # Enables Web Assembly support -wasm = ["identity-iota/wasm", "identity-comm/wasm"] +wasm = ["identity-iota/wasm", "identity-comm/wasm", "identity-core/wasm"] # Enables support for secure storage of DID Documents account = ["identity-account"]