Skip to content

Commit 942e693

Browse files
Merge pull request #42 from andrewwhitehead/fix/ffi-usize
Switch usize to i64 in FFI
2 parents 09b2c4d + 57db0bc commit 942e693

File tree

11 files changed

+46
-68
lines changed

11 files changed

+46
-68
lines changed

indy-credx/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "indy-credx"
3-
version = "1.0.2"
3+
version = "1.0.3"
44
authors = ["Hyperledger Indy Contributors <[email protected]>"]
55
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)."
66
edition = "2021"

indy-credx/src/ffi/credential.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ pub extern "C" fn credx_create_credential(
7070
"Mismatch between length of attribute names and raw values"
7171
));
7272
}
73-
let enc_values = attr_enc_values.as_slice();
73+
let enc_values = attr_enc_values.as_slice()?;
7474
let mut cred_values = MakeCredentialValues::default();
75-
let mut attr_idx = 0;
76-
for (name, raw) in attr_names
77-
.as_slice()
78-
.into_iter()
79-
.zip(attr_raw_values.as_slice())
75+
for (attr_idx, (name, raw)) in attr_names
76+
.as_slice()?
77+
.iter()
78+
.zip(attr_raw_values.as_slice()?)
79+
.enumerate()
8080
{
8181
let name = name
8282
.as_opt_str()
@@ -96,12 +96,11 @@ pub extern "C" fn credx_create_credential(
9696
} else {
9797
cred_values.add_raw(name, raw)?;
9898
}
99-
attr_idx += 1;
10099
}
101100
let revocation_config = if !revocation.is_null() {
102101
let revocation = unsafe { &*revocation };
103102
let mut reg_used = HashSet::new();
104-
for reg_idx in revocation.reg_used.as_slice() {
103+
for reg_idx in revocation.reg_used.as_slice()? {
105104
reg_used.insert(
106105
(*reg_idx)
107106
.try_into()
@@ -157,7 +156,7 @@ pub extern "C" fn credx_encode_credential_attributes(
157156
) -> ErrorCode {
158157
catch_error(|| {
159158
let mut result = String::new();
160-
for raw_val in attr_raw_values.as_slice() {
159+
for raw_val in attr_raw_values.as_slice()? {
161160
let enc_val = encode_credential_attribute(
162161
raw_val
163162
.as_opt_str()

indy-credx/src/ffi/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use once_cell::sync::Lazy;
1111
static LAST_ERROR: Lazy<RwLock<Option<Error>>> = Lazy::new(|| RwLock::new(None));
1212

1313
#[derive(Debug, PartialEq, Copy, Clone, Serialize)]
14-
#[repr(usize)]
14+
#[repr(i64)]
1515
pub enum ErrorCode {
1616
Success = 0,
1717
Input = 1,

indy-credx/src/ffi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ffi_support::define_string_destructor!(credx_string_free);
1010
#[no_mangle]
1111
pub extern "C" fn credx_buffer_free(buffer: ByteBuffer) {
1212
ffi_support::abort_on_panic::with_abort_on_panic(|| {
13-
drop(buffer.destroy_into_vec().zeroize());
13+
buffer.destroy_into_vec().zeroize();
1414
})
1515
}
1616

indy-credx/src/ffi/presentation.rs

+11-30
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,16 @@ pub extern "C" fn credx_create_presentation(
7676
));
7777
}
7878

79-
let entries = {
80-
let credentials = credentials.as_slice();
81-
credentials.into_iter().try_fold(
82-
Vec::with_capacity(credentials.len()),
83-
|mut r, ffi_entry| {
84-
r.push(ffi_entry.load()?);
85-
Result::Ok(r)
86-
},
87-
)?
88-
};
89-
90-
let schemas = IndyObjectList::load(schemas.as_slice())?;
91-
let cred_defs = IndyObjectList::load(cred_defs.as_slice())?;
79+
let entries = credentials.try_collect(|entry| entry.load())?;
80+
let schemas = IndyObjectList::load(schemas.as_slice()?)?;
81+
let cred_defs = IndyObjectList::load(cred_defs.as_slice()?)?;
9282

9383
let self_attested = if !self_attest_names.is_empty() {
9484
let mut self_attested = HashMap::new();
9585
for (name, raw) in self_attest_names
96-
.as_slice()
97-
.into_iter()
98-
.zip(self_attest_values.as_slice())
86+
.as_slice()?
87+
.iter()
88+
.zip(self_attest_values.as_slice()?)
9989
{
10090
let name = name
10191
.as_opt_str()
@@ -125,7 +115,7 @@ pub extern "C" fn credx_create_presentation(
125115
.transpose()?,
126116
);
127117

128-
for prove in credentials_prove.as_slice() {
118+
for prove in credentials_prove.as_slice()? {
129119
if prove.entry_idx < 0 {
130120
return Err(err_msg!("Invalid credential index"));
131121
}
@@ -239,19 +229,10 @@ fn _credx_verify_presentation(
239229
result_p: *mut i8,
240230
) -> ErrorCode {
241231
catch_error(|| {
242-
let schemas = IndyObjectList::load(schemas.as_slice())?;
243-
let cred_defs = IndyObjectList::load(cred_defs.as_slice())?;
244-
let rev_reg_defs = IndyObjectList::load(rev_reg_defs.as_slice())?;
245-
let rev_reg_entries = {
246-
let entries = rev_reg_entries.as_slice();
247-
entries.into_iter().try_fold(
248-
Vec::with_capacity(entries.len()),
249-
|mut r, ffi_entry| {
250-
r.push(ffi_entry.load()?);
251-
Result::Ok(r)
252-
},
253-
)?
254-
};
232+
let schemas = IndyObjectList::load(schemas.as_slice()?)?;
233+
let cred_defs = IndyObjectList::load(cred_defs.as_slice()?)?;
234+
let rev_reg_defs = IndyObjectList::load(rev_reg_defs.as_slice()?)?;
235+
let rev_reg_entries = rev_reg_entries.try_collect(|entry| entry.load())?;
255236
let mut rev_regs = HashMap::new();
256237
for (idx, entry, timestamp) in rev_reg_entries.iter() {
257238
if *idx > rev_reg_defs.len() {

indy-credx/src/ffi/revocation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ pub extern "C" fn credx_update_revocation_registry(
9999
catch_error(|| {
100100
check_useful_c_ptr!(rev_reg_p);
101101
check_useful_c_ptr!(rev_reg_delta_p);
102-
let issued = registry_indices_to_set(issued.as_slice().into_iter().cloned())?;
103-
let revoked = registry_indices_to_set(revoked.as_slice().into_iter().cloned())?;
102+
let issued = registry_indices_to_set(issued.as_slice()?.iter().cloned())?;
103+
let revoked = registry_indices_to_set(revoked.as_slice()?.iter().cloned())?;
104104
let (rev_reg, rev_reg_delta) = update_revocation_registry(
105105
cred_def.load()?.cast_ref()?,
106106
rev_reg_def.load()?.cast_ref()?,

indy-credx/src/ffi/util.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,26 @@ use crate::error::Result;
88
#[derive(Debug)]
99
#[repr(C)]
1010
pub struct FfiList<'a, T> {
11-
count: usize,
11+
count: i64,
1212
data: *const T,
1313
_pd: PhantomData<&'a ()>,
1414
}
1515

1616
impl<'a, T> FfiList<'a, T> {
1717
#[inline]
18-
pub fn as_slice(&self) -> &[T] {
19-
if self.data.is_null() {
20-
&[]
18+
pub fn as_slice(&self) -> Result<&[T]> {
19+
if self.data.is_null() || self.count == 0 {
20+
Ok(&[])
21+
} else if self.count < 0 {
22+
return Err(err_msg!(Input, "Invalid index for result set"));
2123
} else {
22-
unsafe { slice::from_raw_parts(self.data, self.count) }
24+
Ok(unsafe { slice::from_raw_parts(self.data, self.count as usize) })
2325
}
2426
}
2527

2628
#[inline]
27-
pub fn try_collect<R>(&self, mut f: impl FnMut(&T) -> Result<R>) -> Result<Vec<R>> {
28-
self.as_slice()
29-
.into_iter()
30-
.try_fold(Vec::with_capacity(self.len()), |mut rs, v| {
31-
rs.push(f(v)?);
32-
Ok(rs)
33-
})
29+
pub fn try_collect<R>(&self, f: impl FnMut(&T) -> Result<R>) -> Result<Vec<R>> {
30+
self.as_slice()?.iter().map(f).collect()
3431
}
3532

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

4138
#[inline]
42-
pub fn len(&self) -> usize {
39+
pub fn len(&self) -> i64 {
4340
if self.data.is_null() {
4441
0
4542
} else {

indy-credx/src/services/tails.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cell::{RefCell, RefMut};
22
use std::fmt::Debug;
33
use std::fs::File;
4-
use std::io::{self, BufReader, BufWriter, Read, Seek, SeekFrom, Write};
4+
use std::io::{self, BufReader, BufWriter, Read, Seek, Write};
55
use std::path::{Path, PathBuf};
66

77
use indy_utils::base58;
@@ -135,17 +135,17 @@ impl TailsWriter for TailsFileWriter {
135135
let mut buf = BufWriter::new(file);
136136
let mut hasher = Sha256::default();
137137
let version = &[0u8, 2u8];
138-
buf.write(version)?;
138+
buf.write_all(version)?;
139139
hasher.update(version);
140140
while let Some(tail) = generator.try_next()? {
141141
let tail_bytes = tail.to_bytes()?;
142-
buf.write(&tail_bytes)?;
142+
buf.write_all(&tail_bytes)?;
143143
hasher.update(&tail_bytes);
144144
}
145145
let mut file = buf
146146
.into_inner()
147147
.map_err(|e| err_msg!("Error flushing output file: {e}"))?;
148-
let tails_size = file.seek(SeekFrom::Current(0))?;
148+
let tails_size = file.stream_position()?;
149149
let hash = base58::encode(hasher.finalize());
150150
let target_path = self.root_path.join(&hash);
151151
drop(file);

indy-credx/src/services/verifier.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ fn verify_requested_restrictions(
678678
if pred_sub_proof_index == attr_sub_proof_index {
679679
for name in attr_info.values.keys() {
680680
let raw_val = attr_info.values.get(name).unwrap().raw.as_str();
681-
attr_value_map.insert(name.clone(), Some(raw_val.clone()));
681+
attr_value_map.insert(name.clone(), Some(raw_val));
682682
}
683683
}
684684
}

wrappers/python/indy_credx/bindings.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
c_char_p,
1717
c_int8,
1818
c_int64,
19-
c_size_t,
2019
c_ubyte,
2120
pointer,
2221
)
@@ -230,7 +229,7 @@ def _cleanup(cls, buffer: c_char_p):
230229

231230
class FfiObjectHandleList(Structure):
232231
_fields_ = [
233-
("count", c_size_t),
232+
("count", c_int64),
234233
("data", POINTER(ObjectHandle)),
235234
]
236235

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

247246
class FfiIntList(Structure):
248247
_fields_ = [
249-
("count", c_size_t),
248+
("count", c_int64),
250249
("data", POINTER(c_int64)),
251250
]
252251

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

263262
class FfiStrList(Structure):
264263
_fields_ = [
265-
("count", c_size_t),
264+
("count", c_int64),
266265
("data", POINTER(c_char_p)),
267266
]
268267

@@ -796,7 +795,9 @@ def verify_presentation(
796795
entry_list.count = len(rev_regs)
797796
entry_list.data = (RevocationEntry * entry_list.count)(*rev_regs)
798797
do_call(
799-
"credx_verify_presentation_legacy" if accept_legacy_revocation else "credx_verify_presentation",
798+
"credx_verify_presentation_legacy"
799+
if accept_legacy_revocation
800+
else "credx_verify_presentation",
800801
presentation,
801802
pres_req,
802803
FfiObjectHandleList.create(schemas),

wrappers/python/indy_credx/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""indy_credx library wrapper version."""
22

3-
__version__ = "1.0.2"
3+
__version__ = "1.0.3"

0 commit comments

Comments
 (0)