Skip to content

Commit

Permalink
switch usize to i64 in FFI
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead committed Sep 20, 2023
1 parent 09b2c4d commit 8135940
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 61 deletions.
17 changes: 8 additions & 9 deletions indy-credx/src/ffi/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ pub extern "C" fn credx_create_credential(
"Mismatch between length of attribute names and raw values"
));
}
let enc_values = attr_enc_values.as_slice();
let enc_values = attr_enc_values.as_slice()?;
let mut cred_values = MakeCredentialValues::default();
let mut attr_idx = 0;
for (name, raw) in attr_names
.as_slice()
.into_iter()
.zip(attr_raw_values.as_slice())
for (attr_idx, (name, raw)) in attr_names
.as_slice()?
.iter()
.zip(attr_raw_values.as_slice()?)
.enumerate()
{
let name = name
.as_opt_str()
Expand All @@ -96,12 +96,11 @@ pub extern "C" fn credx_create_credential(
} else {
cred_values.add_raw(name, raw)?;
}
attr_idx += 1;
}
let revocation_config = if !revocation.is_null() {
let revocation = unsafe { &*revocation };
let mut reg_used = HashSet::new();
for reg_idx in revocation.reg_used.as_slice() {
for reg_idx in revocation.reg_used.as_slice()? {
reg_used.insert(
(*reg_idx)
.try_into()
Expand Down Expand Up @@ -157,7 +156,7 @@ pub extern "C" fn credx_encode_credential_attributes(
) -> ErrorCode {
catch_error(|| {
let mut result = String::new();
for raw_val in attr_raw_values.as_slice() {
for raw_val in attr_raw_values.as_slice()? {
let enc_val = encode_credential_attribute(
raw_val
.as_opt_str()
Expand Down
2 changes: 1 addition & 1 deletion indy-credx/src/ffi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use once_cell::sync::Lazy;
static LAST_ERROR: Lazy<RwLock<Option<Error>>> = Lazy::new(|| RwLock::new(None));

#[derive(Debug, PartialEq, Copy, Clone, Serialize)]
#[repr(usize)]
#[repr(i64)]
pub enum ErrorCode {
Success = 0,
Input = 1,
Expand Down
2 changes: 1 addition & 1 deletion indy-credx/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ffi_support::define_string_destructor!(credx_string_free);
#[no_mangle]
pub extern "C" fn credx_buffer_free(buffer: ByteBuffer) {
ffi_support::abort_on_panic::with_abort_on_panic(|| {
drop(buffer.destroy_into_vec().zeroize());
buffer.destroy_into_vec().zeroize();
})
}

Expand Down
41 changes: 11 additions & 30 deletions indy-credx/src/ffi/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,16 @@ pub extern "C" fn credx_create_presentation(
));
}

let entries = {
let credentials = credentials.as_slice();
credentials.into_iter().try_fold(
Vec::with_capacity(credentials.len()),
|mut r, ffi_entry| {
r.push(ffi_entry.load()?);
Result::Ok(r)
},
)?
};

let schemas = IndyObjectList::load(schemas.as_slice())?;
let cred_defs = IndyObjectList::load(cred_defs.as_slice())?;
let entries = credentials.try_collect(|entry| entry.load())?;
let schemas = IndyObjectList::load(schemas.as_slice()?)?;
let cred_defs = IndyObjectList::load(cred_defs.as_slice()?)?;

let self_attested = if !self_attest_names.is_empty() {
let mut self_attested = HashMap::new();
for (name, raw) in self_attest_names
.as_slice()
.into_iter()
.zip(self_attest_values.as_slice())
.as_slice()?
.iter()
.zip(self_attest_values.as_slice()?)
{
let name = name
.as_opt_str()
Expand Down Expand Up @@ -125,7 +115,7 @@ pub extern "C" fn credx_create_presentation(
.transpose()?,
);

for prove in credentials_prove.as_slice() {
for prove in credentials_prove.as_slice()? {
if prove.entry_idx < 0 {
return Err(err_msg!("Invalid credential index"));
}
Expand Down Expand Up @@ -239,19 +229,10 @@ fn _credx_verify_presentation(
result_p: *mut i8,
) -> ErrorCode {
catch_error(|| {
let schemas = IndyObjectList::load(schemas.as_slice())?;
let cred_defs = IndyObjectList::load(cred_defs.as_slice())?;
let rev_reg_defs = IndyObjectList::load(rev_reg_defs.as_slice())?;
let rev_reg_entries = {
let entries = rev_reg_entries.as_slice();
entries.into_iter().try_fold(
Vec::with_capacity(entries.len()),
|mut r, ffi_entry| {
r.push(ffi_entry.load()?);
Result::Ok(r)
},
)?
};
let schemas = IndyObjectList::load(schemas.as_slice()?)?;
let cred_defs = IndyObjectList::load(cred_defs.as_slice()?)?;
let rev_reg_defs = IndyObjectList::load(rev_reg_defs.as_slice()?)?;
let rev_reg_entries = rev_reg_entries.try_collect(|entry| entry.load())?;
let mut rev_regs = HashMap::new();
for (idx, entry, timestamp) in rev_reg_entries.iter() {
if *idx > rev_reg_defs.len() {
Expand Down
4 changes: 2 additions & 2 deletions indy-credx/src/ffi/revocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ pub extern "C" fn credx_update_revocation_registry(
catch_error(|| {
check_useful_c_ptr!(rev_reg_p);
check_useful_c_ptr!(rev_reg_delta_p);
let issued = registry_indices_to_set(issued.as_slice().into_iter().cloned())?;
let revoked = registry_indices_to_set(revoked.as_slice().into_iter().cloned())?;
let issued = registry_indices_to_set(issued.as_slice()?.iter().cloned())?;
let revoked = registry_indices_to_set(revoked.as_slice()?.iter().cloned())?;
let (rev_reg, rev_reg_delta) = update_revocation_registry(
cred_def.load()?.cast_ref()?,
rev_reg_def.load()?.cast_ref()?,
Expand Down
23 changes: 10 additions & 13 deletions indy-credx/src/ffi/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,26 @@ use crate::error::Result;
#[derive(Debug)]
#[repr(C)]
pub struct FfiList<'a, T> {
count: usize,
count: i64,
data: *const T,
_pd: PhantomData<&'a ()>,
}

impl<'a, T> FfiList<'a, T> {
#[inline]
pub fn as_slice(&self) -> &[T] {
if self.data.is_null() {
&[]
pub fn as_slice(&self) -> Result<&[T]> {
if self.data.is_null() || self.count == 0 {
Ok(&[])
} else if self.count < 0 {
return Err(err_msg!(Input, "Invalid index for result set"));
} else {
unsafe { slice::from_raw_parts(self.data, self.count) }
Ok(unsafe { slice::from_raw_parts(self.data, self.count as usize) })
}
}

#[inline]
pub fn try_collect<R>(&self, mut f: impl FnMut(&T) -> Result<R>) -> Result<Vec<R>> {
self.as_slice()
.into_iter()
.try_fold(Vec::with_capacity(self.len()), |mut rs, v| {
rs.push(f(v)?);
Ok(rs)
})
pub fn try_collect<R>(&self, f: impl FnMut(&T) -> Result<R>) -> Result<Vec<R>> {
self.as_slice()?.iter().map(f).collect()
}

#[inline]
Expand All @@ -39,7 +36,7 @@ impl<'a, T> FfiList<'a, T> {
}

#[inline]
pub fn len(&self) -> usize {
pub fn len(&self) -> i64 {
if self.data.is_null() {
0
} else {
Expand Down
11 changes: 6 additions & 5 deletions wrappers/python/indy_credx/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
c_char_p,
c_int8,
c_int64,
c_size_t,
c_ubyte,
pointer,
)
Expand Down Expand Up @@ -230,7 +229,7 @@ def _cleanup(cls, buffer: c_char_p):

class FfiObjectHandleList(Structure):
_fields_ = [
("count", c_size_t),
("count", c_int64),
("data", POINTER(ObjectHandle)),
]

Expand All @@ -246,7 +245,7 @@ def create(cls, values: Optional[Sequence[ObjectHandle]]) -> "FfiObjectHandleLis

class FfiIntList(Structure):
_fields_ = [
("count", c_size_t),
("count", c_int64),
("data", POINTER(c_int64)),
]

Expand All @@ -262,7 +261,7 @@ def create(cls, values: Optional[Sequence[str]]) -> "FfiIntList":

class FfiStrList(Structure):
_fields_ = [
("count", c_size_t),
("count", c_int64),
("data", POINTER(c_char_p)),
]

Expand Down Expand Up @@ -796,7 +795,9 @@ def verify_presentation(
entry_list.count = len(rev_regs)
entry_list.data = (RevocationEntry * entry_list.count)(*rev_regs)
do_call(
"credx_verify_presentation_legacy" if accept_legacy_revocation else "credx_verify_presentation",
"credx_verify_presentation_legacy"
if accept_legacy_revocation
else "credx_verify_presentation",
presentation,
pres_req,
FfiObjectHandleList.create(schemas),
Expand Down

0 comments on commit 8135940

Please sign in to comment.