Skip to content

Commit

Permalink
Did peer refactoring, new display macro (#1034)
Browse files Browse the repository at this point in the history
* Did peer refactoring, add examples, new display macro (#1034)

Signed-off-by: Patrik Stas <[email protected]>
  • Loading branch information
Patrik-Stas authored Nov 1, 2023
1 parent 4e65aec commit 88f6e8e
Show file tree
Hide file tree
Showing 52 changed files with 548 additions and 297 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
sudo apt-get install -y libsodium-dev libssl-dev libzmq3-dev
- name: "Verify clippy across the entire workspace with default features"
run: |
cargo clippy --tests --all-features
cargo clippy --examples --tests --all-features
env:
RUSTFLAGS: -D warnings

Expand Down Expand Up @@ -354,7 +354,7 @@ jobs:
rust-toolchain-version: ${{ env.RUST_TOOLCHAIN_VERSON }}
- name: "Run resolver tests"
run: |
RUST_TEST_THREADS=1 cargo test -p did_doc -p did_parser -p did_resolver -p did_resolver_registry -p did_resolver_sov -p did_resolver_web -p did_doc_sov -p did_key -p did_peer --test "*"
RUST_TEST_THREADS=1 cargo test --examples -p did_doc -p did_parser -p did_resolver -p did_resolver_registry -p did_resolver_sov -p did_resolver_web -p did_doc_sov -p did_key -p did_peer --test "*"
test-integration-node-wrapper:
needs: workflow-setup
Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ members = [
"wallet_migrator",
"tools/simple_message_relay",
"tools/test_utils",
"tools/libvcx_logger"
"tools/libvcx_logger",
"tools/display_as_json"
]

[workspace.package]
Expand Down
5 changes: 4 additions & 1 deletion did_doc/src/schema/did_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ pub struct DidDocument<E> {
extra: HashMap<String, Value>,
}

impl Display for DidDocument<()> {
impl<E> Display for DidDocument<E>
where
E: Display + Serialize,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let json = serde_json::to_string(self).unwrap();
write!(f, "{}", json)
Expand Down
1 change: 1 addition & 0 deletions did_doc_sov/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ public_key = { path = "../public_key" } # TODO: Remove after transition to new D
serde = { version = "1.0.159", default-features = false, features = ["derive"] }
serde_json = "1.0.95"
thiserror = "1.0.40"
display_as_json = { path = "../tools/display_as_json" }
3 changes: 2 additions & 1 deletion did_doc_sov/src/extra_fields/aip1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use display_as_json::Display;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default, Display)]
#[serde(deny_unknown_fields)]
pub struct ExtraFieldsAIP1 {}
3 changes: 2 additions & 1 deletion did_doc_sov/src/extra_fields/didcommv1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use display_as_json::Display;
use serde::{Deserialize, Serialize};

use super::{AcceptType, KeyKind};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default, Display)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct ExtraFieldsDidCommV1 {
Expand Down
3 changes: 2 additions & 1 deletion did_doc_sov/src/extra_fields/didcommv2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use display_as_json::Display;
use serde::{Deserialize, Serialize};

use super::{AcceptType, KeyKind};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default, Display)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct ExtraFieldsDidCommV2 {
Expand Down
3 changes: 2 additions & 1 deletion did_doc_sov/src/extra_fields/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use display_as_json::Display;
use serde::{Deserialize, Serialize};

use super::KeyKind;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default, Display)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct ExtraFieldsLegacy {
Expand Down
2 changes: 1 addition & 1 deletion did_doc_sov/src/extra_fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Display for KeyKind {
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, display_as_json::Display)]
#[serde(untagged)]
pub enum ExtraFieldsSov {
DIDCommV1(didcommv1::ExtraFieldsDidCommV1),
Expand Down
2 changes: 2 additions & 0 deletions did_doc_sov/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate display_as_json;

pub mod error;
pub mod extra_fields;
// TODO: Remove once migration is done
Expand Down
1 change: 1 addition & 0 deletions did_peer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ multibase = "0.9.1"
unsigned-varint = "0.7.1"
once_cell = "1.18.0"
sha256 = "1.1.4"
display_as_json = { path = "../tools/display_as_json" }

[dev-dependencies]
tokio = { version = "1.27.0", default-features = false, features = ["macros", "rt"] }
31 changes: 31 additions & 0 deletions did_peer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# did_peer

## Overview
Rust crate for creation, parsing, validation, and resolution of [Peer DIDs](https://identity.foundation/peer-did-method-spec).
Peer DIDs are a special type of decentralized identifiers designed for direct peer-to-peer interactions, without the
need for a blockchain or other centralized registry.

## Features
- **Numalgo Support**: The library implements various version of did:peer. The different versions are referred to as "numalgos".
Currently supports numalgo 1, 2, and 3.
- **DID Parsing**: Capability to parse `did:peer` strings, ensuring they comply with the Peer DID specifications.
- **DID Creation from DIDDoc**: Functionality to create `did:peer` identifiers from DID documents.
- **Numalgo Conversion**: Ability to convert between different numalgos, specifically from Numalgo 2 to Numalgo 3.
- **Validation**: Verification that DIDs adhere to the required specifications and format.

## Getting Started
### Installation
Add the Peer DID library as a dependency in your `Cargo.toml` file:
```toml
[dependencies]
peer_did = { tag = "0.61.0", git = "https://github.com/hyperledger/aries-vcx" }
```

## Demo
To get you off the ground, have a look at the [demo](./examples/demo.rs). It demonstrates how to create, parse. You can
run the demo with the following command:
```bash
cargo run --example demo
```


69 changes: 69 additions & 0 deletions did_peer/examples/demo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::error::Error;

use did_doc::schema::{
did_doc::DidDocument,
service::ServiceBuilder,
types::{uri::Uri, url::Url},
verification_method::{VerificationMethod, VerificationMethodType},
};
use did_doc_sov::extra_fields::{didcommv1::ExtraFieldsDidCommV1, ExtraFieldsSov, KeyKind};
use did_parser::{Did, DidUrl};
use did_peer::peer_did::{
numalgos::{numalgo2::Numalgo2, numalgo3::Numalgo3},
PeerDid,
};

fn main() -> Result<(), Box<dyn Error>> {
demo()
}

fn demo() -> Result<(), Box<dyn Error>> {
let recipient_key = KeyKind::Value("foo".to_string());
let sov_service_extra = ExtraFieldsSov::DIDCommV1(
ExtraFieldsDidCommV1::builder()
.set_recipient_keys(vec![recipient_key])
.build(),
);
let service = ServiceBuilder::<ExtraFieldsSov>::new(
Uri::new("xyz://example.org")?,
Url::new("http://example.org")?,
sov_service_extra,
)
.add_service_type("DIDCommMessaging".to_string())?
.build();

let did_url = DidUrl::parse("did:foo:bar#key-1".into())?;
let did = Did::parse("did:foo:bar".into())?;
let verification_method = VerificationMethod::builder(
did_url,
did.clone(),
VerificationMethodType::Ed25519VerificationKey2018,
)
.add_public_key_base64("Zm9vYmFyCg".to_string())
.build();

let ddo = DidDocument::builder(did)
.add_verification_method(verification_method)
.add_service(service)
.build();
println!("Did document: \n{}", serde_json::to_string_pretty(&ddo)?);

let peer_did_2 = PeerDid::<Numalgo2>::from_did_doc(ddo.clone())?;
println!("as did:peer numalgo(2): {}", peer_did_2);

let peer_did_3 = PeerDid::<Numalgo3>::from_did_doc(ddo)?;
println!("as did:peer numalgo(3): {}", peer_did_3);

let peer_did_3_v2 = peer_did_2.to_numalgo3()?;
println!(
"as did:peer numalgo(2) converted to numalgo(3): {}",
peer_did_3_v2
);

Ok(())
}

#[test]
fn demo_test() -> Result<(), Box<dyn Error>> {
demo()
}
2 changes: 1 addition & 1 deletion did_peer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::convert::Infallible;
use did_doc::schema::verification_method::VerificationMethodType;
use thiserror::Error;

use crate::peer_did::numalgos::NumalgoKind;
use crate::peer_did::numalgos::kind::NumalgoKind;

#[derive(Debug, Error)]
pub enum DidPeerError {
Expand Down
5 changes: 3 additions & 2 deletions did_peer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate display_as_json;

pub mod error;
mod numalgos;
pub mod peer_did;
pub mod peer_did_resolver;
pub mod resolver;
2 changes: 0 additions & 2 deletions did_peer/src/numalgos/mod.rs

This file was deleted.

21 changes: 0 additions & 21 deletions did_peer/src/numalgos/numalgo2/generate/mod.rs

This file was deleted.

8 changes: 0 additions & 8 deletions did_peer/src/numalgos/numalgo2/mod.rs

This file was deleted.

35 changes: 0 additions & 35 deletions did_peer/src/numalgos/numalgo3/generate.rs

This file was deleted.

3 changes: 0 additions & 3 deletions did_peer/src/numalgos/numalgo3/mod.rs

This file was deleted.

23 changes: 0 additions & 23 deletions did_peer/src/peer_did/generate.rs

This file was deleted.

Loading

0 comments on commit 88f6e8e

Please sign in to comment.