Skip to content

Commit

Permalink
Merge pull request #83 from blu3beri/fix/nodejs-tests
Browse files Browse the repository at this point in the history
fix(js): Node.js tests working
  • Loading branch information
berendsliedrecht authored Jan 26, 2023
2 parents 3a33b71 + 470645a commit bc7953f
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 157 deletions.
38 changes: 10 additions & 28 deletions include/libanoncreds.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,10 @@ typedef struct FfiList_FfiStr {

typedef struct FfiList_FfiStr FfiStrList;

typedef struct FfiList_i64 {
size_t count;
const int64_t *data;
} FfiList_i64;

typedef struct FfiCredRevInfo {
ObjectHandle reg_def;
ObjectHandle reg_def_private;
ObjectHandle registry;
int64_t reg_idx;
struct FfiList_i64 reg_used;
FfiStr tails_path;
} FfiCredRevInfo;

Expand Down Expand Up @@ -213,17 +206,6 @@ typedef struct FfiList_i32 {
const int32_t *data;
} FfiList_i32;

typedef struct FfiRevocationEntry {
int64_t def_entry_idx;
ObjectHandle entry;
int64_t timestamp;
} FfiRevocationEntry;

typedef struct FfiList_FfiRevocationEntry {
size_t count;
const struct FfiRevocationEntry *data;
} FfiList_FfiRevocationEntry;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand Down Expand Up @@ -287,15 +269,15 @@ ErrorCode anoncreds_create_presentation(ObjectHandle pres_req,
FfiStrList cred_def_ids,
ObjectHandle *presentation_p);

ErrorCode anoncreds_create_revocation_registry(ObjectHandle cred_def,
FfiStr cred_def_id,
FfiStr issuer_id,
FfiStr tag,
FfiStr rev_reg_type,
int64_t max_cred_num,
FfiStr tails_dir_path,
ObjectHandle *reg_def_p,
ObjectHandle *reg_def_private_p);
ErrorCode anoncreds_create_revocation_registry_def(ObjectHandle cred_def,
FfiStr cred_def_id,
FfiStr issuer_id,
FfiStr tag,
FfiStr rev_reg_type,
int64_t max_cred_num,
FfiStr tails_dir_path,
ObjectHandle *reg_def_p,
ObjectHandle *reg_def_private_p);

ErrorCode anoncreds_create_revocation_status_list(FfiStr rev_reg_def_id,
ObjectHandle rev_reg_def,
Expand Down Expand Up @@ -357,7 +339,7 @@ ErrorCode anoncreds_verify_presentation(ObjectHandle presentation,
FfiStrList cred_def_ids,
struct FfiList_ObjectHandle rev_reg_defs,
FfiStrList rev_reg_def_ids,
struct FfiList_FfiRevocationEntry rev_reg_entries,
struct FfiList_ObjectHandle rev_status_list,
int8_t *result_p);

char *anoncreds_version(void);
Expand Down
2 changes: 1 addition & 1 deletion src/data_types/rev_reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl From<&RevocationStatusList> for Option<ursa::cl::RevocationRegistry> {
}

impl From<&RevocationStatusList> for Option<RevocationRegistry> {
fn from(rev_status_list: &RevocationStatusList) -> Option<RevocationRegistry> {
fn from(rev_status_list: &RevocationStatusList) -> Self {
rev_status_list
.registry
.clone()
Expand Down
42 changes: 14 additions & 28 deletions src/ffi/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::object::{AnonCredsObject, AnonCredsObjectList, ObjectHandle};
use super::util::{FfiList, FfiStrList};
use crate::data_types::cred_def::{CredentialDefinition, CredentialDefinitionId};
use crate::data_types::presentation::Presentation;
use crate::data_types::rev_reg::RevocationRegistryId;
use crate::data_types::rev_reg::RevocationStatusList;
use crate::data_types::rev_reg_def::{
RevocationRegistryDefinition, RevocationRegistryDefinitionId,
};
Expand Down Expand Up @@ -195,29 +195,6 @@ pub extern "C" fn anoncreds_create_presentation(
})
}

#[derive(Debug)]
#[repr(C)]
pub struct FfiRevocationEntry {
def_entry_idx: i64,
entry: ObjectHandle,
timestamp: i64,
}

impl FfiRevocationEntry {
fn load(&self) -> Result<(usize, AnonCredsObject, u64)> {
let def_entry_idx = self
.def_entry_idx
.try_into()
.map_err(|_| err_msg!("Invalid revocation registry entry index"))?;
let entry = self.entry.load()?;
let timestamp = self
.timestamp
.try_into()
.map_err(|_| err_msg!("Invalid timestamp for revocation entry"))?;
Ok((def_entry_idx, entry, timestamp))
}
}

#[no_mangle]
pub extern "C" fn anoncreds_verify_presentation(
presentation: ObjectHandle,
Expand All @@ -228,7 +205,7 @@ pub extern "C" fn anoncreds_verify_presentation(
cred_def_ids: FfiStrList,
rev_reg_defs: FfiList<ObjectHandle>,
rev_reg_def_ids: FfiStrList,
rev_reg_status_list: FfiList<FfiRevocationEntry>,
rev_status_list: FfiList<ObjectHandle>,
result_p: *mut i8,
) -> ErrorCode {
catch_error(|| {
Expand Down Expand Up @@ -279,14 +256,23 @@ pub extern "C" fn anoncreds_verify_presentation(
&rev_reg_def_identifiers,
)?;

let rev_reg_defs = match rev_reg_defs.is_empty() {
false => Some(&rev_reg_defs),
true => None,
};

let rev_status_list: AnonCredsObjectList =
AnonCredsObjectList::load(rev_status_list.as_slice())?;
let rev_status_list: Result<Vec<&RevocationStatusList>> = rev_status_list.refs();
let rev_status_list = rev_status_list.ok();

let verify = verify_presentation(
presentation.load()?.cast_ref()?,
pres_req.load()?.cast_ref()?,
&schemas,
&cred_defs,
Some(&rev_reg_defs),
// TODO: issue 74 FFI
None,
rev_reg_defs,
rev_status_list,
)?;
unsafe { *result_p = verify as i8 };
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/services/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn verify_presentation(
schemas: &HashMap<&SchemaId, &Schema>,
cred_defs: &HashMap<&CredentialDefinitionId, &CredentialDefinition>,
rev_reg_defs: Option<&HashMap<&RevocationRegistryDefinitionId, &RevocationRegistryDefinition>>,
rev_status_lists: Option<Vec<RevocationStatusList>>,
rev_status_lists: Option<Vec<&RevocationStatusList>>,
) -> Result<bool> {
trace!("verify >>> presentation: {:?}, pres_req: {:?}, schemas: {:?}, cred_defs: {:?}, rev_reg_defs: {:?} rev_status_lists: {:?}",
presentation, pres_req, schemas, cred_defs, rev_reg_defs, rev_status_lists);
Expand Down Expand Up @@ -121,10 +121,10 @@ pub fn verify_presentation(
.ok_or_else(|| err_msg!(Unexpected, "RevStatusList missing timestamp"))?;

let rev_reg: ursa::cl::RevocationRegistry =
Into::<Option<ursa::cl::RevocationRegistry>>::into(list)
Into::<Option<ursa::cl::RevocationRegistry>>::into(*list)
.ok_or_else(|| err_msg!(Unexpected, "RevStatusList missing Accum"))?;

if map.get(&id).map(|t| t.get(&timestamp)).flatten().is_some() {
if map.get(&id).and_then(|t| t.get(&timestamp)).is_some() {
return Err(err_msg!(
Unexpected,
"Duplicated timestamp for Revocation Status List"
Expand Down
4 changes: 2 additions & 2 deletions tests/anoncreds_demos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ fn anoncreds_with_revocation_works_for_single_issuer_single_prover() {
let cred_def_id = CredentialDefinitionId::new_unchecked(CRED_DEF_ID);
cred_defs.insert(&cred_def_id, &cred_def_pub);

let mut rev_status_list = vec![issued_rev_status_list.clone()];
let mut rev_status_list = vec![&issued_rev_status_list];

// Prover creates presentation
let presentation = _create_presentation(
Expand Down Expand Up @@ -448,7 +448,7 @@ fn anoncreds_with_revocation_works_for_single_issuer_single_prover() {
.unwrap();

// update rev_status_lists
rev_status_list.push(revoked_status_list);
rev_status_list.push(&revoked_status_list);

let rev_state = prover::create_or_update_revocation_state(
&rev_reg_def_pub.value.tails_location,
Expand Down
8 changes: 4 additions & 4 deletions tests/utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ impl<'a> Mock<'a> {
HashMap::from_iter(self.ledger.cred_defs.iter());
let mut rev_status_lists = vec![];

self.ledger.revcation_list.iter().for_each(|(_, v)| {
v.iter()
.for_each(|(_, list)| rev_status_lists.push(list.clone()))
});
self.ledger
.revcation_list
.iter()
.for_each(|(_, v)| v.iter().for_each(|(_, list)| rev_status_lists.push(list)));

let rev_reg_def_map = HashMap::from_iter(self.ledger.rev_reg_defs.iter());

Expand Down
72 changes: 31 additions & 41 deletions wrappers/javascript/anoncreds-nodejs/src/NodeJSAnoncreds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {
NativeCredentialEntry,
NativeCredentialProve,
Anoncreds,
NativeRevocationEntry,
NativeCredentialRevocationConfig,
} from '@hyperledger/anoncreds-shared'

Expand All @@ -21,8 +20,6 @@ import {
CredentialProveStruct,
CredentialEntryListStruct,
CredentialProveListStruct,
RevocationEntryListStruct,
RevocationEntryStruct,
allocateInt8Buffer,
CredRevInfoStruct,
allocateByteBuffer,
Expand Down Expand Up @@ -50,7 +47,6 @@ export class NodeJSAnoncreds implements Anoncreds {

const ret = allocatePointer()

// @ts-ignore
nativeAnoncreds.anoncreds_create_schema(name, version, issuerId, attributeNames, ret)
handleError()

Expand Down Expand Up @@ -174,10 +170,9 @@ export class NodeJSAnoncreds implements Anoncreds {
credentialDefinitionPrivate,
credentialOffer,
credentialRequest,
// @ts-ignore
attributeNames,
attributeRawValues,
attributeEncodedValues,
attributeNames as unknown as Buffer,
attributeRawValues as unknown as Buffer,
attributeEncodedValues as unknown as Buffer,
revocationRegistryId,
revocationStatusList,
revocationConfiguration.ref(),
Expand All @@ -193,7 +188,6 @@ export class NodeJSAnoncreds implements Anoncreds {

const ret = allocateStringBuffer()

// @ts-ignore
nativeAnoncreds.anoncreds_encode_credential_attributes(attributeRawValues, ret)
handleError()

Expand All @@ -219,7 +213,6 @@ export class NodeJSAnoncreds implements Anoncreds {
credentialRequestMetadata,
masterSecret,
credentialDefinition,
// @ts-ignore
revocationRegistryDefinition,
ret
)
Expand Down Expand Up @@ -306,7 +299,6 @@ export class NodeJSAnoncreds implements Anoncreds {
const credentialProves = options.credentialsProve.map((value) => {
const { entryIndex: entry_idx, isPredicate: is_predictable, reveal, referent } = serializeArguments(value)

// @ts-ignore
return CredentialProveStruct({ entry_idx, referent, is_predictable, reveal })
})

Expand Down Expand Up @@ -360,16 +352,15 @@ export class NodeJSAnoncreds implements Anoncreds {

nativeAnoncreds.anoncreds_create_presentation(
presentationRequest,
// @ts-ignore
credentialEntryList,
credentialProveList,
selfAttestNames,
selfAttestValues,
credentialEntryList as unknown as Buffer,
credentialProveList as unknown as Buffer,
selfAttestNames as unknown as Buffer,
selfAttestValues as unknown as Buffer,
masterSecret,
schemas,
schemaIds,
credentialDefinitions,
credentialDefinitionIds,
schemas as unknown as Buffer,
schemaIds as unknown as Buffer,
credentialDefinitions as unknown as Buffer,
credentialDefinitionIds as unknown as Buffer,
ret
)
handleError()
Expand All @@ -380,38 +371,37 @@ export class NodeJSAnoncreds implements Anoncreds {
presentation: ObjectHandle
presentationRequest: ObjectHandle
schemas: ObjectHandle[]
schemaIds: string[]
credentialDefinitions: ObjectHandle[]
revocationRegistryDefinitions: ObjectHandle[]
revocationEntries: NativeRevocationEntry[]
credentialDefinitionIds: string[]
revocationRegistryDefinitions?: ObjectHandle[]
revocationRegistryDefinitionIds?: string[]
revocationStatusLists?: ObjectHandle[]
}): boolean {
const { presentation, presentationRequest, schemas, credentialDefinitions, revocationRegistryDefinitions } =
serializeArguments(options)

const revocationRegistries =
options.revocationEntries.length > 0
? RevocationEntryListStruct({
count: options.revocationEntries.length,
// @ts-ignore
data: options.revocationEntries.map(({ revocationRegistryDefinitionEntryIndex, entry, timestamp }) => {
return RevocationEntryStruct({
def_entry_idx: revocationRegistryDefinitionEntryIndex,
entry: entry.handle,
timestamp: timestamp,
})
}),
})
: undefined
const {
presentation,
presentationRequest,
schemas,
credentialDefinitions,
revocationRegistryDefinitions,
revocationStatusLists,
revocationRegistryDefinitionIds,
schemaIds,
credentialDefinitionIds,
} = serializeArguments(options)

const ret = allocateInt8Buffer()

nativeAnoncreds.anoncreds_verify_presentation(
presentation,
presentationRequest,
// @ts-ignore
schemas,
schemaIds,
credentialDefinitions,
credentialDefinitionIds,
revocationRegistryDefinitions,
revocationRegistries,
revocationRegistryDefinitionIds,
revocationStatusLists,
ret
)
handleError()
Expand Down
Loading

0 comments on commit bc7953f

Please sign in to comment.