diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml
index a0a2244c1d..ba313b06f2 100644
--- a/.github/workflows/build-and-test.yml
+++ b/.github/workflows/build-and-test.yml
@@ -20,10 +20,9 @@ jobs:
matrix:
project:
[
- identity_core,
- identity_derive,
- identity_diff,
- identity_iota,
+ identity-core,
+ identity-diff,
+ identity-iota,
]
os: [ubuntu-latest, windows-latest]
diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml
index b08d10fd1a..b25fdf9862 100644
--- a/.github/workflows/clippy.yml
+++ b/.github/workflows/clippy.yml
@@ -20,10 +20,9 @@ jobs:
matrix:
project:
[
- identity_core,
- identity_derive,
- identity_diff,
- identity_iota,
+ identity-core,
+ identity-diff,
+ identity-iota,
bindings/wasm,
]
diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml
index 9dbcbe79f4..c49591c4b7 100644
--- a/.github/workflows/format.yml
+++ b/.github/workflows/format.yml
@@ -20,10 +20,9 @@ jobs:
matrix:
project:
[
- identity_core,
- identity_derive,
- identity_diff,
- identity_iota,
+ identity-core,
+ identity-diff,
+ identity-iota,
bindings/wasm,
]
diff --git a/Cargo.toml b/Cargo.toml
index ef680b3619..92b7abada3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,10 +1,14 @@
[workspace]
members = [
- "identity_core",
- "identity_derive",
- "identity_diff",
- "identity_iota",
+ "identity-core",
+ "identity-diff",
+ "identity-iota",
+
+ "examples/credential",
+ "examples/diff-chain",
+ "examples/resolution",
]
+
exclude = [
- "bindings/wasm",
+ "bindings/wasm",
]
diff --git a/bindings/wasm/Cargo.toml b/bindings/wasm/Cargo.toml
index ac015b5e89..3669f1dcb8 100644
--- a/bindings/wasm/Cargo.toml
+++ b/bindings/wasm/Cargo.toml
@@ -1,25 +1,26 @@
[package]
-name = "iota_identity_wasm"
-version = "0.0.1"
-authors = ["Team Identity"]
+name = "identity-wasm"
+version = "0.1.0"
+authors = ["IOTA Identity"]
edition = "2018"
+description = "Web Assembly bindings for the identity-rs crate."
+readme = "README.md"
repository = "https://github.com/iotaledger/identity.rs"
-description = "WASM version of Identity.rs"
+license = "Apache-2.0"
+keywords = ["iota", "tangle", "identity", "wasm"]
homepage = "https://www.iota.org"
-license = "MIT"
[lib]
crate-type = ["cdylib"]
[dependencies]
-console_error_panic_hook = "0.1"
+console_error_panic_hook = { version = "0.1" }
+identity-core = { version = "=0.1.0", path = "../../identity-core" }
+identity-iota = { version = "=0.1.0", path = "../../identity-iota" }
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
wasm-bindgen-futures = { version = "0.4", default-features = false }
-identity_core = { path = "../../identity_core" }
-identity_iota = { path = "../../identity_iota" }
-
[target.'cfg(target_arch = "wasm32")'.dependencies]
rand = { version = "0.7", default-features = false, features = ["wasm-bindgen"] }
diff --git a/bindings/wasm/build/docs.js b/bindings/wasm/build/docs.js
index 57db88a182..5504feb30a 100644
--- a/bindings/wasm/build/docs.js
+++ b/bindings/wasm/build/docs.js
@@ -2,7 +2,7 @@ const fs = require('fs')
const path = require('path')
const jsdoc2md = require('jsdoc-to-markdown')
-const importFile = path.join(__dirname, '../node/iota_identity_wasm.js')
+const importFile = path.join(__dirname, '../node/identity_wasm.js')
const exportFile = path.join(__dirname, '../docs/api-reference.md')
const docsRoot = path.join(__dirname, '../docs')
diff --git a/bindings/wasm/build/node.js b/bindings/wasm/build/node.js
index 3b86e0509b..57495ed763 100644
--- a/bindings/wasm/build/node.js
+++ b/bindings/wasm/build/node.js
@@ -2,7 +2,7 @@ const path = require('path')
const fs = require('fs')
// Add node fetch stuff (https://github.com/seanmonstar/reqwest/issues/910)
-const entryFilePathNode = path.join(__dirname, '../node/iota_identity_wasm.js')
+const entryFilePathNode = path.join(__dirname, '../node/identity_wasm.js')
const entryFileNode = fs.readFileSync(entryFilePathNode).toString()
let changedFileNode = entryFileNode.replace(
"let imports = {};",
diff --git a/bindings/wasm/build/web.js b/bindings/wasm/build/web.js
index b168d37e9e..693242db0c 100644
--- a/bindings/wasm/build/web.js
+++ b/bindings/wasm/build/web.js
@@ -1,7 +1,7 @@
const path = require('path')
const fs = require('fs')
-const entryFilePath = path.join(__dirname, '../web/iota_identity_wasm.js')
+const entryFilePath = path.join(__dirname, '../web/identity_wasm.js')
const entryFile = fs.readFileSync(entryFilePath).toString()
// comment out this code so it works for Webpack
let changedFile = entryFile.replace(
@@ -20,7 +20,7 @@ let changedFile = entryFile.replace(
// Create an init function which imports the wasm file
.replace(
"export default init;",
- "let __initializedIotaWasm = false\r\n\r\nexport function init(path) {\r\n if (__initializedIotaWasm) {\r\n return Promise.resolve(wasm)\r\n }\r\n return initWasm(path || \'iota_identity_wasm_bg.wasm\').then(() => {\r\n __initializedIotaWasm = true\r\n return wasm\r\n })\r\n}\r\n"
+ "let __initializedIotaWasm = false\r\n\r\nexport function init(path) {\r\n if (__initializedIotaWasm) {\r\n return Promise.resolve(wasm)\r\n }\r\n return initWasm(path || \'identity_wasm_bg.wasm\').then(() => {\r\n __initializedIotaWasm = true\r\n return wasm\r\n })\r\n}\r\n"
)
fs.writeFileSync(
@@ -28,7 +28,7 @@ fs.writeFileSync(
changedFile
)
-const entryFilePathTs = path.join(__dirname, '../web/iota_identity_wasm.d.ts')
+const entryFilePathTs = path.join(__dirname, '../web/identity_wasm.d.ts')
const entryFileTs = fs.readFileSync(entryFilePathTs).toString()
// Replace the init function in the ts file
let changedFileTs = entryFileTs.replace(
diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md
index d071e8b033..ab7adba579 100644
--- a/bindings/wasm/docs/api-reference.md
+++ b/bindings/wasm/docs/api-reference.md
@@ -52,7 +52,6 @@
* [.toString()](#DID+toString) ⇒ string
* _static_
* [.fromBase58Key(key, network)](#DID.fromBase58Key) ⇒ [DID
](#DID)
- * [.fromBase64Key(key, network)](#DID.fromBase64Key) ⇒ [DID
](#DID)
* [.parse(input)](#DID.parse) ⇒ [DID
](#DID)
@@ -108,18 +107,6 @@ Creates a new `DID` from a base58-encoded public key.
| key | string
|
| network | string
\| undefined
|
-
-
-### DID.fromBase64Key(key, network) ⇒ [DID
](#DID)
-Creates a new `DID` from a base64-encoded public key.
-
-**Kind**: static method of [DID
](#DID)
-
-| Param | Type |
-| --- | --- |
-| key | string
|
-| network | string
\| undefined
|
-
### DID.parse(input) ⇒ [DID
](#DID)
@@ -145,7 +132,7 @@ Parses a `DID` from the input string.
* [.proof](#Doc+proof) ⇒ any
* [.sign(key)](#Doc+sign) ⇒ any
* [.verify()](#Doc+verify) ⇒ boolean
- * [.diff(other, key)](#Doc+diff) ⇒ any
+ * [.diff(other, key, prev_msg)](#Doc+diff) ⇒ any
* [.verifyDiff(diff)](#Doc+verifyDiff) ⇒ boolean
* [.updateService(did, url, service_type)](#Doc+updateService)
* [.clearServices()](#Doc+clearServices)
@@ -209,7 +196,7 @@ Verify the signature with the authentication_key
**Kind**: instance method of [Doc
](#Doc)
-### doc.diff(other, key) ⇒ any
+### doc.diff(other, key, prev_msg) ⇒ any
Generate the difference between two DID Documents and sign it
**Kind**: instance method of [Doc
](#Doc)
@@ -218,6 +205,7 @@ Generate the difference between two DID Documents and sign it
| --- | --- |
| other | [Doc
](#Doc) |
| key | [Key
](#Key) |
+| prev_msg | string
|
@@ -389,7 +377,6 @@ Deserializes a `Doc` object from a JSON object.
* _static_
* [.generateEd25519()](#Key.generateEd25519) ⇒ [Key
](#Key)
* [.fromBase58(public_key, private_key)](#Key.fromBase58) ⇒ [Key
](#Key)
- * [.fromBase64(public_key, private_key)](#Key.fromBase64) ⇒ [Key
](#Key)
* [.fromJSON(json)](#Key.fromJSON) ⇒ [Key
](#Key)
@@ -438,18 +425,6 @@ Parses a `Key` object from base58-encoded public/private keys.
| public_key | string
|
| private_key | string
|
-
-
-### Key.fromBase64(public_key, private_key) ⇒ [Key
](#Key)
-Parses a `Key` object from base64-encoded public/private keys.
-
-**Kind**: static method of [Key
](#Key)
-
-| Param | Type |
-| --- | --- |
-| public_key | string
|
-| private_key | string
|
-
### Key.fromJSON(json) ⇒ [Key
](#Key)
diff --git a/bindings/wasm/src/did.rs b/bindings/wasm/src/did.rs
index 10e03512a5..4fc1cf3ea4 100644
--- a/bindings/wasm/src/did.rs
+++ b/bindings/wasm/src/did.rs
@@ -1,4 +1,4 @@
-use identity_core::utils::{decode_b58, decode_b64};
+use identity_core::utils::decode_b58;
use identity_iota::did::IotaDID;
use wasm_bindgen::prelude::*;
@@ -27,12 +27,6 @@ impl DID {
Self::create(&decode_b58(key).map_err(js_err)?, network.as_deref())
}
- /// Creates a new `DID` from a base64-encoded public key.
- #[wasm_bindgen(js_name = fromBase64Key)]
- pub fn from_base64_key(key: &str, network: Option) -> Result {
- Self::create(&decode_b64(key).map_err(js_err)?, network.as_deref())
- }
-
/// Parses a `DID` from the input string.
#[wasm_bindgen]
pub fn parse(input: String) -> Result {
diff --git a/bindings/wasm/src/iota.rs b/bindings/wasm/src/iota.rs
index c60525e3e7..4734d5013e 100644
--- a/bindings/wasm/src/iota.rs
+++ b/bindings/wasm/src/iota.rs
@@ -1,8 +1,8 @@
use identity_core::common::Object;
use identity_iota::{
client::{Client, ClientBuilder, Network, TxnPrinter},
+ credential::CredentialValidator,
did::IotaDID,
- vc::CredentialValidator,
};
use serde::Deserialize;
use wasm_bindgen::prelude::*;
diff --git a/bindings/wasm/src/key.rs b/bindings/wasm/src/key.rs
index eda47e5f22..db821c678a 100644
--- a/bindings/wasm/src/key.rs
+++ b/bindings/wasm/src/key.rs
@@ -2,7 +2,7 @@ use identity_core::{
crypto::{KeyPair, PublicKey, SecretKey},
did_doc::MethodType,
proof::JcsEd25519Signature2020,
- utils::{decode_b58, decode_b64, encode_b58},
+ utils::{decode_b58, encode_b58},
};
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
@@ -39,15 +39,6 @@ impl Key {
Ok(Self(KeyPair::new(public, private)))
}
- /// Parses a `Key` object from base64-encoded public/private keys.
- #[wasm_bindgen(js_name = fromBase64)]
- pub fn from_base64(public_key: &str, private_key: &str) -> Result {
- let public: PublicKey = decode_b64(public_key).map_err(js_err)?.into();
- let private: SecretKey = decode_b64(private_key).map_err(js_err)?.into();
-
- Ok(Self(KeyPair::new(public, private)))
- }
-
/// Returns the public key as a base58-encoded string.
#[wasm_bindgen(getter)]
pub fn public(&self) -> String {
diff --git a/bindings/wasm/src/vc.rs b/bindings/wasm/src/vc.rs
index 4f1ff6a7e3..2c6b9277bf 100644
--- a/bindings/wasm/src/vc.rs
+++ b/bindings/wasm/src/vc.rs
@@ -1,6 +1,6 @@
use identity_core::{
common::{OneOrMany, Url},
- vc::{Credential, CredentialBuilder, CredentialSubject, VerifiableCredential as VC},
+ credential::{Credential, CredentialBuilder, CredentialSubject, VerifiableCredential as VC},
};
use wasm_bindgen::prelude::*;
diff --git a/bindings/wasm/src/vp.rs b/bindings/wasm/src/vp.rs
index 44d3fcaa75..ed23714e42 100644
--- a/bindings/wasm/src/vp.rs
+++ b/bindings/wasm/src/vp.rs
@@ -1,6 +1,6 @@
use identity_core::{
common::{OneOrMany, Url},
- vc::{Presentation, PresentationBuilder, VerifiableCredential, VerifiablePresentation as VP},
+ credential::{Presentation, PresentationBuilder, VerifiableCredential, VerifiablePresentation as VP},
};
use wasm_bindgen::prelude::*;
diff --git a/examples/credential/Cargo.toml b/examples/credential/Cargo.toml
new file mode 100644
index 0000000000..bfe7e2c0b7
--- /dev/null
+++ b/examples/credential/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "identity-example-credential"
+version = "0.1.0"
+authors = ["IOTA Identity"]
+edition = "2018"
+publish = false
+
+[dependencies]
+identity-core = { path = "../../identity-core" }
+identity-iota = { path = "../../identity-iota" }
+smol = { version = "0.1.18", features = ["tokio02"] }
+smol-potat = { version = "0.3" }
diff --git a/identity_iota/examples/credential.rs b/examples/credential/src/main.rs
similarity index 94%
rename from identity_iota/examples/credential.rs
rename to examples/credential/src/main.rs
index 0939e13588..3e888b447d 100644
--- a/identity_iota/examples/credential.rs
+++ b/examples/credential/src/main.rs
@@ -1,22 +1,20 @@
//! A basic example that generates and publishes subject and issuer DID
//! Documents, creates a VerifiableCredential specifying claims about the
//! subject, and retrieves information through the CredentialValidator API.
-//!
-//! cargo run --example credential
use identity_core::{
common::{Url, Value},
convert::{FromJson as _, ToJson as _},
+ credential::{Credential, CredentialBuilder, CredentialSubject, VerifiableCredential},
did_doc::MethodScope,
did_url::DID,
json,
- vc::{Credential, CredentialBuilder, CredentialSubject, VerifiableCredential},
};
use identity_iota::{
client::Client,
+ credential::{CredentialValidation, CredentialValidator},
crypto::KeyPair,
did::IotaDocument,
error::Result,
- vc::{CredentialValidation, CredentialValidator},
};
// A helper function to generate and new DID Document/KeyPair, sign the
diff --git a/examples/diff-chain/Cargo.toml b/examples/diff-chain/Cargo.toml
new file mode 100644
index 0000000000..97a0f810f1
--- /dev/null
+++ b/examples/diff-chain/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "identity-example-diff-chain"
+version = "0.1.0"
+authors = ["IOTA Identity"]
+edition = "2018"
+publish = false
+
+[dependencies]
+identity-core = { path = "../../identity-core" }
+identity-iota = { path = "../../identity-iota" }
+smol = { version = "0.1.18", features = ["tokio02"] }
+smol-potat = { version = "0.3" }
diff --git a/identity_iota/examples/document_diff.rs b/examples/diff-chain/src/main.rs
similarity index 98%
rename from identity_iota/examples/document_diff.rs
rename to examples/diff-chain/src/main.rs
index 3e8a49e024..7b5655f3e2 100644
--- a/identity_iota/examples/document_diff.rs
+++ b/examples/diff-chain/src/main.rs
@@ -1,4 +1,5 @@
-//! cargo run --example document_diff
+//! An example that utilizes a diff and auth chain to publish updates to a
+//! DID Document.
use identity_core::{
did_doc::{MethodBuilder, MethodData, MethodRef, MethodType},
proof::JcsEd25519Signature2020,
diff --git a/examples/resolution/Cargo.toml b/examples/resolution/Cargo.toml
new file mode 100644
index 0000000000..bd42da12f3
--- /dev/null
+++ b/examples/resolution/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "identity-example-resolution"
+version = "0.1.0"
+authors = ["IOTA Identity"]
+edition = "2018"
+publish = false
+
+[dependencies]
+identity-core = { path = "../../identity-core" }
+identity-iota = { path = "../../identity-iota" }
+smol = { version = "0.1.18", features = ["tokio02"] }
+smol-potat = { version = "0.3" }
diff --git a/identity_iota/examples/document.rs b/examples/resolution/src/main.rs
similarity index 97%
rename from identity_iota/examples/document.rs
rename to examples/resolution/src/main.rs
index 2f85f12261..1953b4e592 100644
--- a/identity_iota/examples/document.rs
+++ b/examples/resolution/src/main.rs
@@ -1,7 +1,5 @@
//! A basic example that generates a DID Document, publishes it to the Tangle,
//! and retrieves information through DID Document resolution/dereferencing.
-//!
-//! cargo run --example document
use identity_core::resolver::{dereference, resolve, Dereference, Resolution};
use identity_iota::{
client::Client,
diff --git a/identity_core/Cargo.toml b/identity-core/Cargo.toml
similarity index 59%
rename from identity_core/Cargo.toml
rename to identity-core/Cargo.toml
index 00e3558950..9fda71d41c 100644
--- a/identity_core/Cargo.toml
+++ b/identity-core/Cargo.toml
@@ -1,9 +1,9 @@
[package]
-name = "identity_core"
+name = "identity-core"
version = "0.1.0"
authors = ["IOTA Identity"]
edition = "2018"
-description = "An implementation of the Decentralized Identifiers (DID) standard"
+description = "The core traits and types for the identity-rs library."
readme = "../README.md"
repository = "https://github.com/iotaledger/identity.rs"
license = "Apache-2.0"
@@ -11,23 +11,20 @@ keywords = ["iota", "tangle", "identity"]
homepage = "https://www.iota.org"
[dependencies]
-anyhow = { version = "1.0" }
+anyhow = { version = "1.0", default-features = false }
async-trait = { version = "0.1", default-features = false }
-base64 = { version = "0.12", default-features = false, features = ["std"] }
-bs58 = { version = "0.3", default-features = false, features = ["std"] }
+bs58 = { version = "0.4", default-features = false, features = ["std"] }
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
did_doc = { git = "https://github.com/l1h3r/did_doc", rev = "bb8bda8f6d39451b08e3cc198f956c212a3b87f8", default-features = false, features = ["std"] }
did_url = { version = "0.1", default-features = false, features = ["std", "serde"] }
ed25519-zebra = { version = "2.2", default-features = false }
-hex = { version = "0.4", default-features = false , features = ["std"] }
+identity-diff = { version = "=0.1.0", path = "../identity-diff", default-features = false }
lazy_static = { version = "1.4", default-features = false }
rand = { version = "0.7", default-features = false, features = ["getrandom"] }
-serde = { version = "1.0", features = ["derive"] }
+serde = { version = "1.0", default-features = false, features = ["std", "derive"] }
serde_jcs = { version = "0.1", default-features = false }
-serde_json = { version = "1.0", features = ["preserve_order"] }
-sha2 = { version = "0.9" }
+serde_json = { version = "1.0", default-features = false, features = ["preserve_order", "std"] }
+sha2 = { version = "0.9", default-features = false }
subtle = { version = "2.4" }
-thiserror = { version = "1.0" }
-zeroize = { version = "1.1" }
-
-identity_diff = { path = "../identity_diff", version = "0.1.0", features = ["diff_derive"] }
+thiserror = { version = "1.0", default-features = false }
+zeroize = { version = "1.1", default-features = false }
diff --git a/identity_core/src/common/context.rs b/identity-core/src/common/context.rs
similarity index 91%
rename from identity_core/src/common/context.rs
rename to identity-core/src/common/context.rs
index 90cd785692..f19fa5b73a 100644
--- a/identity_core/src/common/context.rs
+++ b/identity-core/src/common/context.rs
@@ -9,7 +9,9 @@ use crate::common::{Object, Url};
#[derive(Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Context {
+ /// A JSON-LD context expressed as a Url.
Url(Url),
+ /// A JSON-LD context expressed as a JSON object.
Obj(Object),
}
diff --git a/identity_core/src/common/mod.rs b/identity-core/src/common/mod.rs
similarity index 61%
rename from identity_core/src/common/mod.rs
rename to identity-core/src/common/mod.rs
index 5f5dd66be2..98de9328e5 100644
--- a/identity_core/src/common/mod.rs
+++ b/identity-core/src/common/mod.rs
@@ -1,7 +1,9 @@
-pub mod context;
-pub mod one_or_many;
-pub mod timestamp;
-pub mod url;
+//! Definitions of common types.
+
+mod context;
+mod one_or_many;
+mod timestamp;
+mod url;
pub use context::Context;
pub use did_doc::{Object, Value};
diff --git a/identity_core/src/common/one_or_many.rs b/identity-core/src/common/one_or_many.rs
similarity index 96%
rename from identity_core/src/common/one_or_many.rs
rename to identity-core/src/common/one_or_many.rs
index 5ebc0c6d91..b917799371 100644
--- a/identity_core/src/common/one_or_many.rs
+++ b/identity-core/src/common/one_or_many.rs
@@ -10,7 +10,9 @@ use core::{
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
#[serde(untagged)]
pub enum OneOrMany {
+ /// A single instance of `T`.
One(T),
+ /// Multiple instances of `T`.
Many(Vec),
}
@@ -83,7 +85,7 @@ impl OneOrMany {
&*self
}
- /// Consumes the `OneOrMany` and returns the contents as a `Vec`.
+ /// Consumes the [`OneOrMany`] and returns the contents as a [`Vec`].
pub fn into_vec(self) -> Vec {
match self {
Self::One(inner) => vec![inner],
diff --git a/identity_core/src/common/timestamp.rs b/identity-core/src/common/timestamp.rs
similarity index 91%
rename from identity_core/src/common/timestamp.rs
rename to identity-core/src/common/timestamp.rs
index 1173dc985c..b819ff5e37 100644
--- a/identity_core/src/common/timestamp.rs
+++ b/identity-core/src/common/timestamp.rs
@@ -14,7 +14,7 @@ use crate::error::{Error, Result};
pub struct Timestamp(DateTime);
impl Timestamp {
- /// Parses a `Timestamp` from the provided input string.
+ /// Parses a [`Timestamp`] from the provided input string.
pub fn parse(input: &str) -> Result {
DateTime::parse_from_rfc3339(input)
.map_err(Into::into)
@@ -22,17 +22,17 @@ impl Timestamp {
.map(Self)
}
- /// Creates a new `Timestamp` with the current date and time.
+ /// Creates a new [`Timestamp`] with the current date and time.
pub fn now() -> Self {
Self::parse(&Self::to_rfc3339(&Self(Utc::now()))).unwrap()
}
- /// Returns the `Timestamp` as a Unix timestamp.
+ /// Returns the [`Timestamp`] as a Unix timestamp.
pub fn to_unix(&self) -> i64 {
self.0.timestamp()
}
- /// Returns the `Timestamp` as an RFC 3339 `String`.
+ /// Returns the [`Timestamp`] as an RFC 3339 `String`.
pub fn to_rfc3339(&self) -> String {
self.0.to_rfc3339_opts(SecondsFormat::Secs, true)
}
diff --git a/identity_core/src/common/url.rs b/identity-core/src/common/url.rs
similarity index 87%
rename from identity_core/src/common/url.rs
rename to identity-core/src/common/url.rs
index 7af6ac673c..40383aeaed 100644
--- a/identity_core/src/common/url.rs
+++ b/identity-core/src/common/url.rs
@@ -14,17 +14,17 @@ use crate::error::{Error, Result};
pub struct Url(url::Url);
impl Url {
- /// Parses an absolute `Url` from the given input string.
+ /// Parses an absolute [`Url`] from the given input string.
pub fn parse(input: impl AsRef) -> Result {
url::Url::parse(input.as_ref()).map_err(Into::into).map(Self)
}
- /// Consumes the `Url` and returns the value as a `String`.
+ /// Consumes the [`Url`] and returns the value as a `String`.
pub fn into_string(self) -> String {
self.0.into_string()
}
- /// Parses the given input string as a `Url`, with `self` as the base `Url`.
+ /// Parses the given input string as a [`Url`], with `self` as the base Url.
pub fn join(&self, input: impl AsRef) -> Result {
self.0.join(input.as_ref()).map_err(Into::into).map(Self)
}
diff --git a/identity_core/src/convert/as_json.rs b/identity-core/src/convert/json.rs
similarity index 60%
rename from identity_core/src/convert/as_json.rs
rename to identity-core/src/convert/json.rs
index 4b8caf7c31..330b597ad5 100644
--- a/identity_core/src/convert/as_json.rs
+++ b/identity-core/src/convert/json.rs
@@ -1,22 +1,33 @@
use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
+/// A convenience-trait for types that can be serialized as JSON.
pub trait ToJson: Serialize {
+ /// Serialize `self` as a string of JSON.
fn to_json(&self) -> Result {
serde_json::to_string(self).map_err(Error::EncodeJSON)
}
+ /// Serialize `self` as a JSON byte vector.
fn to_json_vec(&self) -> Result> {
serde_json::to_vec(self).map_err(Error::EncodeJSON)
}
+ /// Serialize `self` as a [`serde_json::Value`].
fn to_json_value(&self) -> Result {
serde_json::to_value(self).map_err(Error::EncodeJSON)
}
+ /// Serialize `self` as a pretty-printed string of JSON.
fn to_json_pretty(&self) -> Result {
serde_json::to_string_pretty(self).map_err(Error::EncodeJSON)
}
+
+ /// Serialize `self` as a JSON byte vector, normalized using JSON
+ /// Canonicalization Scheme (JCS).
+ fn to_jcs(&self) -> Result> {
+ serde_jcs::to_vec(self).map_err(Error::EncodeJSON)
+ }
}
impl ToJson for T where T: Serialize {}
@@ -24,15 +35,19 @@ impl ToJson for T where T: Serialize {}
// =============================================================================
// =============================================================================
+/// A convenience-trait for types that can be deserialized from JSON.
pub trait FromJson: for<'de> Deserialize<'de> + Sized {
+ /// Deserialize `Self` from a string of JSON text.
fn from_json(json: &(impl AsRef + ?Sized)) -> Result {
serde_json::from_str(json.as_ref()).map_err(Error::DecodeJSON)
}
+ /// Deserialize `Self` from bytes of JSON text.
fn from_json_slice(json: &(impl AsRef<[u8]> + ?Sized)) -> Result {
serde_json::from_slice(json.as_ref()).map_err(Error::DecodeJSON)
}
+ /// Deserialize `Self` from a [`serde_json::Value`].
fn from_json_value(json: serde_json::Value) -> Result {
serde_json::from_value(json).map_err(Error::DecodeJSON)
}
@@ -43,30 +58,48 @@ impl FromJson for T where T: for<'de> Deserialize<'de> + Sized {}
// =============================================================================
// =============================================================================
+/// A convenience-trait for types that can be converted to and from JSON.
pub trait AsJson: FromJson + ToJson {
+ /// Deserialize `Self` from a string of JSON text.
fn from_json(json: &(impl AsRef + ?Sized)) -> Result {
::from_json(json)
}
+ /// Deserialize `Self` from bytes of JSON text.
fn from_json_slice(json: &(impl AsRef<[u8]> + ?Sized)) -> Result {
::from_json_slice(json)
}
+ /// Deserialize `Self` from a [`serde_json::Value`].
+ fn from_json_value(json: serde_json::Value) -> Result {
+ ::from_json_value(json)
+ }
+
+ /// Serialize `self` as a string of JSON.
fn to_json(&self) -> Result {
::to_json(self)
}
+ /// Serialize `self` as a JSON byte vector.
fn to_json_vec(&self) -> Result> {
::to_json_vec(self)
}
+ /// Serialize `self` as a [`serde_json::Value`].
fn to_json_value(&self) -> Result {
::to_json_value(self)
}
+ /// Serialize `self` as a pretty-printed string of JSON.
fn to_json_pretty(&self) -> Result {
::to_json_pretty(self)
}
+
+ /// Serialize `self` as a JSON byte vector, normalized using JSON
+ /// Canonicalization Scheme (JCS).
+ fn to_jcs(&self) -> Result> {
+ ::to_jcs(self)
+ }
}
impl AsJson for T where T: FromJson + ToJson {}
diff --git a/identity-core/src/convert/mod.rs b/identity-core/src/convert/mod.rs
new file mode 100644
index 0000000000..4c573a4f9f
--- /dev/null
+++ b/identity-core/src/convert/mod.rs
@@ -0,0 +1,6 @@
+//! Traits for conversions between types.
+
+mod json;
+mod serde_into;
+
+pub use self::{json::*, serde_into::*};
diff --git a/identity-core/src/convert/serde_into.rs b/identity-core/src/convert/serde_into.rs
new file mode 100644
index 0000000000..4a679f3b63
--- /dev/null
+++ b/identity-core/src/convert/serde_into.rs
@@ -0,0 +1,18 @@
+use crate::{
+ convert::{AsJson, ToJson},
+ error::Result,
+};
+
+/// An escape-hatch for converting between types that represent the same JSON
+/// structure.
+pub trait SerdeInto: ToJson {
+ /// Converts `self` to `T` by converting to/from JSON.
+ fn serde_into(&self) -> Result
+ where
+ T: AsJson,
+ {
+ ::to_json_value(self).and_then(::from_json_value)
+ }
+}
+
+impl SerdeInto for T where T: ToJson {}
diff --git a/identity_core/src/vc/credential.rs b/identity-core/src/credential/credential.rs
similarity index 98%
rename from identity_core/src/vc/credential.rs
rename to identity-core/src/credential/credential.rs
index adc771b369..03ff08ccfe 100644
--- a/identity_core/src/vc/credential.rs
+++ b/identity-core/src/credential/credential.rs
@@ -5,13 +5,13 @@ use serde::Serialize;
use crate::{
common::{Context, Object, OneOrMany, Timestamp, Url},
convert::ToJson as _,
- crypto::SecretKey,
- error::{Error, Result},
- proof::JcsEd25519Signature2020,
- vc::{
+ credential::{
CredentialBuilder, CredentialSchema, CredentialStatus, CredentialSubject, Evidence, Issuer, RefreshService,
TermsOfUse, VerifiableCredential,
},
+ crypto::SecretKey,
+ error::{Error, Result},
+ proof::JcsEd25519Signature2020,
};
lazy_static! {
@@ -139,6 +139,8 @@ impl Credential {
Ok(())
}
+ /// Creates a new [`VerifiableCredential`] by signing `self` with `document`
+ /// and `secret`.
pub fn sign<'a, Q, D1, D2, D3>(
self,
document: &Document,
@@ -182,7 +184,7 @@ where
#[cfg(test)]
mod tests {
- use crate::{convert::FromJson as _, vc::Credential};
+ use crate::{convert::FromJson as _, credential::Credential};
const JSON1: &str = include_str!("../../tests/fixtures/vc/credential-1.json");
const JSON2: &str = include_str!("../../tests/fixtures/vc/credential-2.json");
diff --git a/identity_core/src/vc/credential_builder.rs b/identity-core/src/credential/credential_builder.rs
similarity index 98%
rename from identity_core/src/vc/credential_builder.rs
rename to identity-core/src/credential/credential_builder.rs
index 13b9c5d3dc..21943d480f 100644
--- a/identity_core/src/vc/credential_builder.rs
+++ b/identity-core/src/credential/credential_builder.rs
@@ -1,9 +1,9 @@
use crate::{
common::{Context, Object, Timestamp, Url, Value},
- error::Result,
- vc::{
+ credential::{
Credential, CredentialSchema, CredentialStatus, CredentialSubject, Evidence, Issuer, RefreshService, TermsOfUse,
},
+ error::Result,
};
/// A `CredentialBuilder` is used to create a customized `Credential`.
@@ -185,7 +185,7 @@ mod tests {
use crate::{
common::{Object, Timestamp, Url},
convert::FromJson as _,
- vc::{Credential as Credential_, CredentialBuilder, CredentialSubject},
+ credential::{Credential as Credential_, CredentialBuilder, CredentialSubject},
};
type Credential = Credential_