Skip to content

Commit

Permalink
Merge pull request #40 from andrewwhitehead/upd/dalek2
Browse files Browse the repository at this point in the history
Update dalek dependencies & refactor
  • Loading branch information
andrewwhitehead authored Oct 3, 2023
2 parents 942e693 + 684cafa commit 5123366
Show file tree
Hide file tree
Showing 56 changed files with 428 additions and 742 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ jobs:
- name: Debug build
run: cargo build --all-targets --features vendored

- name: Test indy-utils
run: cargo test --manifest-path indy-utils/Cargo.toml

# - name: Test indy-data-types (CL)
# run: cargo test --manifest-path indy-data-types/Cargo.toml --features cl

Expand Down
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
[workspace]
resolver = "2"

members = [
"indy-credx",
"indy-data-types",
"indy-test-utils",
"indy-utils"
]
members = ["indy-credx", "indy-data-types"]

[profile.release]
panic = "abort"
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ Shared Rust libraries for Hyperledger Indy.

- `indy-data-types`: Data type definitions for Schemas, Credential Definitions and other types related to credential issuance and processing.

- `indy-test-utils`: Utilities for use in integration tests.

- `indy-utils`: Standard wrappers around binary data encodings. Includes support for normalizing transactions for signing, deriving DIDs and verification keys.

## Credit

The initial implementation of `indy-shared-rs` was developed by the Verifiable Organizations Network (VON) team based at the Province of British Columbia, and derives largely from the implementations within [Hyperledger Indy-SDK](https://github.com/hyperledger/indy-sdk). To learn more about VON and what's happening with decentralized identity in British Columbia, please go to [https://vonx.io](https://vonx.io).
Expand Down
6 changes: 2 additions & 4 deletions indy-credx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "indy-credx"
version = "1.0.3"
version = "1.1.0"
authors = ["Hyperledger Indy Contributors <[email protected]>"]
description = "Verifiable credential issuance and presentation for Hyperledger Indy (https://www.hyperledger.org/projects), which provides a distributed-ledger-based foundation for self-sovereign identity (https://sovrin.org)."
edition = "2021"
Expand All @@ -25,16 +25,14 @@ vendored = ["indy-data-types/vendored"]
[dependencies]
env_logger = { version = "0.10", optional = true }
ffi-support = { version = "0.4.0", optional = true }
indy-data-types = { version = "0.6.1", features = [
indy-data-types = { version = "0.7", features = [
"cl_native",
], path = "../indy-data-types" }
indy-utils = { version = "0.6.0", default-features = false, path = "../indy-utils" }
log = "0.4"
once_cell = "1"
rand = "0.8"
regex = "1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sha2 = "0.10"
thiserror = "1.0"
zeroize = { version = "1", optional = true }
4 changes: 2 additions & 2 deletions indy-credx/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ impl From<ErrorKind> for Error {
}
}

impl From<indy_utils::ValidationError> for Error {
fn from(err: indy_utils::ValidationError) -> Self {
impl From<indy_data_types::ValidationError> for Error {
fn from(err: indy_data_types::ValidationError) -> Self {
Error::from_opt_msg(ErrorKind::Input, err.context)
}
}
Expand Down
3 changes: 2 additions & 1 deletion indy-credx/src/ffi/cred_def.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::os::raw::c_char;
use std::str::FromStr;

use ffi_support::{rust_string_to_c, FfiStr};
use indy_utils::Qualifiable;
use indy_data_types::Qualifiable;

use super::error::{catch_error, ErrorCode};
use super::object::{IndyObjectId, ObjectHandle};
Expand Down
2 changes: 1 addition & 1 deletion indy-credx/src/ffi/cred_offer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ffi_support::FfiStr;
use indy_utils::Qualifiable;
use indy_data_types::Qualifiable;

use super::error::{catch_error, ErrorCode};
use super::object::ObjectHandle;
Expand Down
2 changes: 1 addition & 1 deletion indy-credx/src/ffi/cred_req.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ffi_support::FfiStr;
use indy_utils::Qualifiable;
use indy_data_types::Qualifiable;

use super::error::{catch_error, ErrorCode};
use super::object::ObjectHandle;
Expand Down
49 changes: 43 additions & 6 deletions indy-credx/src/ffi/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::os::raw::c_char;
use std::sync::{Arc, Mutex};
use std::sync::{atomic::AtomicUsize, Arc, Mutex};

use ffi_support::{rust_string_to_c, ByteBuffer};
use indy_data_types::{Validatable, ValidationError};
use once_cell::sync::Lazy;
use serde::Serialize;

Expand All @@ -16,9 +17,17 @@ use crate::error::Result;
pub(crate) static FFI_OBJECTS: Lazy<Mutex<BTreeMap<ObjectHandle, IndyObject>>> =
Lazy::new(|| Mutex::new(BTreeMap::new()));

indy_utils::new_handle_type!(ObjectHandle, FFI_OBJECT_COUNTER);
static FFI_OBJECT_COUNTER: AtomicUsize = AtomicUsize::new(0);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(transparent)]
pub struct ObjectHandle(pub usize);

impl ObjectHandle {
pub fn next() -> Self {
Self(FFI_OBJECT_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1)
}

pub(crate) fn create<O: AnyIndyObject + 'static>(value: O) -> Result<Self> {
let handle = Self::next();
FFI_OBJECTS
Expand Down Expand Up @@ -62,9 +71,32 @@ impl ObjectHandle {
}
}

impl Default for ObjectHandle {
fn default() -> Self {
Self(0)
impl std::fmt::Display for ObjectHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}({})", stringify!($newtype), self.0)
}
}

impl std::ops::Deref for ObjectHandle {
type Target = usize;
fn deref(&self) -> &usize {
&self.0
}
}

impl PartialEq<usize> for ObjectHandle {
fn eq(&self, other: &usize) -> bool {
self.0 == *other
}
}

impl Validatable for ObjectHandle {
fn validate(&self) -> std::result::Result<(), ValidationError> {
if **self == 0 {
Err("Invalid handle: zero".into())
} else {
Ok(())
}
}
}

Expand All @@ -74,6 +106,7 @@ pub(crate) struct IndyObject(Arc<dyn AnyIndyObject>);

impl IndyObject {
pub fn new<O: AnyIndyObject + 'static>(value: O) -> Self {
assert!(std::mem::size_of::<O>() != 0);
Self(Arc::new(value))
}

Expand All @@ -97,6 +130,10 @@ impl IndyObject {

impl PartialEq for IndyObject {
fn eq(&self, other: &IndyObject) -> bool {
#[allow(clippy::vtable_address_comparisons)]
// this is allowed only because we create all such objects
// in one place (the `new` method) and ensure they are not
// zero-sized.
Arc::ptr_eq(&self.0, &other.0)
}
}
Expand Down Expand Up @@ -214,7 +251,7 @@ pub(crate) struct IndyObjectList(Vec<IndyObject>);
impl IndyObjectList {
pub fn load(handles: &[ObjectHandle]) -> Result<Self> {
let loaded = handles
.into_iter()
.iter()
.map(ObjectHandle::load)
.collect::<Result<_>>()?;
Ok(Self(loaded))
Expand Down
1 change: 1 addition & 0 deletions indy-credx/src/ffi/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ pub extern "C" fn credx_verify_presentation_legacy(
)
}

#[allow(clippy::too_many_arguments)]
fn _credx_verify_presentation(
presentation: ObjectHandle,
pres_req: ObjectHandle,
Expand Down
3 changes: 2 additions & 1 deletion indy-credx/src/ffi/revocation.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::BTreeSet;
use std::convert::TryInto;
use std::os::raw::c_char;
use std::str::FromStr;

use ffi_support::{rust_string_to_c, FfiStr};
use indy_utils::Qualifiable;
use indy_data_types::Qualifiable;

use super::error::{catch_error, ErrorCode};
use super::object::{IndyObject, IndyObjectId, ObjectHandle};
Expand Down
2 changes: 1 addition & 1 deletion indy-credx/src/ffi/schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::os::raw::c_char;

use ffi_support::{rust_string_to_c, FfiStr};
use indy_utils::Qualifiable;
use indy_data_types::Qualifiable;

use super::error::{catch_error, ErrorCode};
use super::object::{IndyObjectId, ObjectHandle};
Expand Down
2 changes: 1 addition & 1 deletion indy-credx/src/services/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::anoncreds_clsignatures::{
use crate::error::Result;

pub fn attr_common_view(attr: &str) -> String {
attr.replace(" ", "").to_lowercase()
attr.replace(' ', "").to_lowercase()
}

pub fn build_credential_schema(attrs: &HashSet<String>) -> Result<CredentialSchema> {
Expand Down
40 changes: 15 additions & 25 deletions indy-credx/src/services/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use indy_data_types::anoncreds::{
},
schema::SchemaV1,
};
use indy_utils::{Qualifiable, Validatable};
use indy_data_types::{Qualifiable, Validatable};

use super::tails::TailsWriter;

Expand All @@ -29,7 +29,7 @@ pub fn create_schema(
origin_did, schema_name, schema_version, attr_names);

origin_did.validate()?;
let schema_id = SchemaId::new(&origin_did, schema_name, schema_version);
let schema_id = SchemaId::new(origin_did, schema_name, schema_version);
let schema = SchemaV1 {
id: schema_id,
name: schema_name.to_string(),
Expand Down Expand Up @@ -57,12 +57,12 @@ pub fn make_credential_definition_id(
};
let schema_infix_id = schema_seq_no
.map(|n| SchemaId(n.to_string()))
.unwrap_or(schema_id.clone());
.unwrap_or(schema_id);

Ok(CredentialDefinitionId::new(
origin_did,
&schema_infix_id,
&signature_type.to_str(),
signature_type.to_str(),
tag,
))
}
Expand All @@ -84,9 +84,7 @@ pub fn create_credential_definition(
config
);

let schema = match schema {
Schema::SchemaV1(s) => s,
};
let Schema::SchemaV1(schema) = schema;
let cred_def_id =
make_credential_definition_id(origin_did, &schema.id, schema.seq_no, tag, signature_type)?;

Expand Down Expand Up @@ -142,9 +140,7 @@ pub fn make_revocation_registry_id(
tag: &str,
rev_reg_type: RegistryType,
) -> Result<RevocationRegistryId> {
let cred_def = match cred_def {
CredentialDefinition::CredentialDefinitionV1(c) => c,
};
let CredentialDefinition::CredentialDefinitionV1(cred_def) = cred_def;

let origin_did = match (origin_did.get_method(), cred_def.id.get_method()) {
(None, Some(_)) => {
Expand All @@ -157,9 +153,9 @@ pub fn make_revocation_registry_id(
};

Ok(RevocationRegistryId::new(
&origin_did,
origin_did,
&cred_def.id,
&rev_reg_type.to_str(),
rev_reg_type.to_str(),
tag,
))
}
Expand All @@ -186,9 +182,7 @@ where

let rev_reg_id = make_revocation_registry_id(origin_did, cred_def, tag, rev_reg_type)?;

let cred_def = match cred_def {
CredentialDefinition::CredentialDefinitionV1(c) => c,
};
let CredentialDefinition::CredentialDefinitionV1(cred_def) = cred_def;
let credential_pub_key = cred_def.get_public_key().map_err(err_map!(
Unexpected,
"Error fetching public key from credential definition"
Expand All @@ -211,13 +205,13 @@ where
max_cred_num,
issuance_type,
public_keys: rev_keys_pub,
tails_location: tails_location.clone(),
tails_location,
tails_hash,
};

let revoc_reg_def = RevocationRegistryDefinition::RevocationRegistryDefinitionV1(
RevocationRegistryDefinitionV1 {
id: rev_reg_id.clone(),
id: rev_reg_id,
revoc_def_type: rev_reg_type,
tag: tag.to_string(),
cred_def_id: cred_def.id.clone(),
Expand Down Expand Up @@ -260,9 +254,7 @@ pub fn update_revocation_registry(
))?
}
};
let rev_reg_def = match rev_reg_def {
RevocationRegistryDefinition::RevocationRegistryDefinitionV1(v1) => v1,
};
let RevocationRegistryDefinition::RevocationRegistryDefinitionV1(rev_reg_def) = rev_reg_def;
let mut rev_reg = match rev_reg {
RevocationRegistry::RevocationRegistryV1(v1) => v1.value.clone(),
};
Expand Down Expand Up @@ -292,9 +284,7 @@ pub fn create_credential_offer(

let nonce = Nonce::new().map_err(err_map!(Unexpected, "Error creating nonce"))?;

let cred_def = match cred_def {
CredentialDefinition::CredentialDefinitionV1(c) => c,
};
let CredentialDefinition::CredentialDefinitionV1(cred_def) = cred_def;

let key_correctness_proof = correctness_proof
.try_clone()
Expand Down Expand Up @@ -370,8 +360,8 @@ pub fn create_credential(
)?;

let cred_rev_reg_id = match cred_offer.method_name.as_ref() {
Some(ref _method_name) => Some(reg_reg_id.to_unqualified()),
_ => Some(reg_reg_id.clone()),
Some(_method_name) => Some(reg_reg_id.to_unqualified()),
_ => Some(reg_reg_id),
};
(
credential_signature,
Expand Down
Loading

0 comments on commit 5123366

Please sign in to comment.