Skip to content

Commit

Permalink
Add ic-wasm feature, that allows compiling to IC-compatible WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and przydatek committed May 28, 2024
1 parent c69566c commit f3015f2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
11 changes: 8 additions & 3 deletions identity_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ rust-version.workspace = true
description = "The core traits and types for the identity-rs library."

[dependencies]
iota-crypto = { version = "0.23", default-features = false, features = ["ed25519", "random", "sha", "x25519", "std"] }
ic-cdk = { version = "0.13", optional = true }
iota-crypto = { version = "0.23", default-features = false, features = ["ed25519", "sha", "x25519", "std"] }
multibase = { version = "0.9", default-features = false, features = ["std"] }
serde = { workspace = true, features = ["std"] }
serde_json = { workspace = true, features = ["std"] }
Expand All @@ -22,8 +23,8 @@ time = { version = "0.3.23", default-features = false, features = ["std", "serde
url = { version = "2.4", default-features = false, features = ["serde"] }
zeroize = { version = "1.6", default-features = false }

[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies]
js-sys = { version = "0.3.55", default-features = false }
[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi"), not(feature = "ic-wasm")))'.dependencies]
js-sys = { version = "0.3.55", default-features = false, optional = true}

[dev-dependencies]
proptest = { version = "1.0.0" }
Expand All @@ -38,3 +39,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[lints]
workspace = true

[features]
default = ["ic-wasm"]
ic-wasm = ["dep:ic-cdk"]
13 changes: 12 additions & 1 deletion identity_core/src/common/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,24 @@ 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")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), not(feature = "ic-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")
}
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "ic-wasm"))]
pub fn now_utc() -> Self {
Self(truncate_fractional_seconds(
OffsetDateTime::from_unix_timestamp(
(ic_cdk::api::time() / 1_000_000_000)
.try_into()
.expect("Failed converting to u64"),
)
.expect("Wrong unix timestamp"),
))
}

/// Returns the `Timestamp` as an [RFC 3339](https://tools.ietf.org/html/rfc3339) `String`.
pub fn to_rfc3339(&self) -> String {
Expand Down
3 changes: 2 additions & 1 deletion identity_credential/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["revocation-bitmap", "validator", "credential", "presentation", "domain-linkage-fetch", "sd-jwt"]
default = ["revocation-bitmap", "validator", "credential", "presentation", "domain-linkage-fetch", "sd-jwt", "ic-wasm"]
credential = []
presentation = ["credential"]
revocation-bitmap = ["dep:flate2", "dep:roaring"]
Expand All @@ -59,6 +59,7 @@ domain-linkage = ["validator"]
domain-linkage-fetch = ["domain-linkage", "dep:reqwest", "dep:futures"]
sd-jwt = ["credential", "validator", "dep:sd-jwt-payload"]
jpt-bbs-plus = ["credential", "validator", "dep:zkryptium", "dep:json-proof-token"]
ic-wasm = []

[lints]
workspace = true

0 comments on commit f3015f2

Please sign in to comment.