Skip to content

Commit

Permalink
Fix Timestamp in the Wasm bindings (#541)
Browse files Browse the repository at this point in the history
Fixed `Timestamp` in the Wasm bindings.
  • Loading branch information
Oliver E. Anderson authored Dec 13, 2021
1 parent 6a5d86b commit 44233cc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
8 changes: 4 additions & 4 deletions bindings/wasm/tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions identity-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions identity-core/src/common/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"]
Expand Down

0 comments on commit 44233cc

Please sign in to comment.