Skip to content

Commit 4fbcce1

Browse files
authored
Merge pull request #364 from Concordium/feature/RUN-77-update-rust-bindings-presentations-prove
initial bindings work to expose prove function for PresentationV1
2 parents 12c6a21 + 1bcae76 commit 4fbcce1

File tree

4 files changed

+111
-32
lines changed

4 files changed

+111
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.flattened-pom.xml
44
pom.xml.versionsBackup
55
.idea/
6+
*.DS_STORE

concordium-base

Submodule concordium-base updated 221 files

crypto-jni/Cargo.lock

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crypto-jni/src/lib.rs

Lines changed: 104 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use concordium_base::{
2020
types::GlobalContext,
2121
},
2222
transactions::{AddBakerKeysMarker, BakerKeysPayload, ConfigureBakerKeysPayload},
23-
web3id::{Request, Web3IdAttribute},
23+
web3id::{v1::anchor::VerificationRequestData, Request, Web3IdAttribute},
2424
};
2525
use core::slice;
2626
use ed25519_dalek::*;
@@ -44,7 +44,7 @@ use wallet_library::{
4444
CredentialDeploymentDetails, CredentialDeploymentPayload,
4545
},
4646
identity::{create_identity_object_request_v1_aux, create_identity_recovery_request_aux},
47-
proofs::Web3IdProofInput,
47+
proofs::{PresentationV1Input, VerificationRequestV1Input, Web3IdProofInput},
4848
statement::{
4949
AcceptableAtomicStatement, AcceptableRequest, RequestCheckError, WalletConfigRules,
5050
},
@@ -227,14 +227,14 @@ enum JNIErrorResponseType {
227227
#[derive(SerdeSerialize, SerdeDeserialize)]
228228
#[allow(non_snake_case)]
229229
struct JNIErrorResponse {
230-
errorType: JNIErrorResponseType,
230+
errorType: JNIErrorResponseType,
231231
errorMessage: String,
232232
}
233233

234234
impl<T> From<serde_json::Error> for CryptoJniResult<T> {
235235
fn from(e: serde_json::Error) -> Self {
236236
let error = JNIErrorResponse {
237-
errorType: JNIErrorResponseType::JsonDeserialization,
237+
errorType: JNIErrorResponseType::JsonDeserialization,
238238
errorMessage: e.to_string(),
239239
};
240240
CryptoJniResult::Err(error)
@@ -244,7 +244,7 @@ impl<T> From<serde_json::Error> for CryptoJniResult<T> {
244244
impl<T> From<Utf8Error> for CryptoJniResult<T> {
245245
fn from(e: Utf8Error) -> Self {
246246
let error = JNIErrorResponse {
247-
errorType: JNIErrorResponseType::Utf8Decode,
247+
errorType: JNIErrorResponseType::Utf8Decode,
248248
errorMessage: e.to_string(),
249249
};
250250
CryptoJniResult::Err(error)
@@ -254,7 +254,7 @@ impl<T> From<Utf8Error> for CryptoJniResult<T> {
254254
impl<T> From<jni::errors::Error> for CryptoJniResult<T> {
255255
fn from(e: jni::errors::Error) -> Self {
256256
let error = JNIErrorResponse {
257-
errorType: JNIErrorResponseType::NativeConversion,
257+
errorType: JNIErrorResponseType::NativeConversion,
258258
errorMessage: e.to_string(),
259259
};
260260
CryptoJniResult::Err(error)
@@ -264,7 +264,17 @@ impl<T> From<jni::errors::Error> for CryptoJniResult<T> {
264264
impl<T> From<concordium_base::web3id::ProofError> for CryptoJniResult<T> {
265265
fn from(e: concordium_base::web3id::ProofError) -> Self {
266266
let error = JNIErrorResponse {
267-
errorType: JNIErrorResponseType::NativeConversion,
267+
errorType: JNIErrorResponseType::NativeConversion,
268+
errorMessage: e.to_string(),
269+
};
270+
CryptoJniResult::Err(error)
271+
}
272+
}
273+
274+
impl<T> From<concordium_base::web3id::v1::ProveError> for CryptoJniResult<T> {
275+
fn from(e: concordium_base::web3id::v1::ProveError) -> Self {
276+
let error = JNIErrorResponse {
277+
errorType: JNIErrorResponseType::NativeConversion,
268278
errorMessage: e.to_string(),
269279
};
270280
CryptoJniResult::Err(error)
@@ -275,7 +285,7 @@ impl<T> From<concordium_base::web3id::ProofError> for CryptoJniResult<T> {
275285
impl<T> From<&str> for CryptoJniResult<T> {
276286
fn from(e: &str) -> Self {
277287
let error = JNIErrorResponse {
278-
errorType: JNIErrorResponseType::PayloadCreation,
288+
errorType: JNIErrorResponseType::PayloadCreation,
279289
errorMessage: e.to_string(),
280290
};
281291
CryptoJniResult::Err(error)
@@ -285,7 +295,7 @@ impl<T> From<&str> for CryptoJniResult<T> {
285295
impl<T> From<anyhow::Error> for CryptoJniResult<T> {
286296
fn from(e: anyhow::Error) -> Self {
287297
let error = JNIErrorResponse {
288-
errorType: JNIErrorResponseType::ParameterSerialization,
298+
errorType: JNIErrorResponseType::ParameterSerialization,
289299
errorMessage: e.to_string(),
290300
};
291301
CryptoJniResult::Err(error)
@@ -324,9 +334,9 @@ fn decrypt_encrypted_amount(
324334
#[serde(bound(serialize = "C: Curve", deserialize = "C: Curve"))]
325335
#[serde(rename_all = "camelCase")]
326336
struct JniInput<C: Curve> {
327-
global: GlobalContext<C>,
328-
amount: Amount,
329-
sender_secret_key: elgamal::SecretKey<C>,
337+
global: GlobalContext<C>,
338+
amount: Amount,
339+
sender_secret_key: elgamal::SecretKey<C>,
330340
input_encrypted_amount: IndexedEncryptedAmount<C>,
331341
}
332342

@@ -364,10 +374,10 @@ pub extern "system" fn Java_com_concordium_sdk_crypto_CryptoJniNative_createSecT
364374

365375
let input_amount: AggregatedDecryptedAmount<ArCurve> = AggregatedDecryptedAmount {
366376
agg_encrypted_amount: input.input_encrypted_amount.encrypted_chunks,
367-
agg_index: encrypted_transfers::types::EncryptedAmountAggIndex {
377+
agg_index: encrypted_transfers::types::EncryptedAmountAggIndex {
368378
index: input.input_encrypted_amount.index.index,
369379
},
370-
agg_amount: decrypted_amount,
380+
agg_amount: decrypted_amount,
371381
};
372382

373383
let mut csprng = thread_rng();
@@ -390,10 +400,10 @@ pub extern "system" fn Java_com_concordium_sdk_crypto_CryptoJniNative_createSecT
390400
#[serde(bound(serialize = "C: Curve", deserialize = "C: Curve"))]
391401
#[serde(rename_all = "camelCase")]
392402
struct TransferJniInput<C: Curve> {
393-
global: GlobalContext<C>,
394-
receiver_public_key: elgamal::PublicKey<C>,
395-
sender_secret_key: elgamal::SecretKey<C>,
396-
amount_to_send: Amount,
403+
global: GlobalContext<C>,
404+
receiver_public_key: elgamal::PublicKey<C>,
405+
sender_secret_key: elgamal::SecretKey<C>,
406+
amount_to_send: Amount,
397407
input_encrypted_amount: IndexedEncryptedAmount<C>,
398408
}
399409

@@ -433,10 +443,10 @@ pub extern "system" fn Java_com_concordium_sdk_crypto_CryptoJniNative_generateEn
433443

434444
let input_amount: AggregatedDecryptedAmount<ArCurve> = AggregatedDecryptedAmount {
435445
agg_encrypted_amount: input.input_encrypted_amount.encrypted_chunks,
436-
agg_index: encrypted_transfers::types::EncryptedAmountAggIndex {
446+
agg_index: encrypted_transfers::types::EncryptedAmountAggIndex {
437447
index: input.input_encrypted_amount.index.index,
438448
},
439-
agg_amount: decrypted_amount,
449+
agg_amount: decrypted_amount,
440450
};
441451

442452
let mut csprng = thread_rng();
@@ -471,7 +481,7 @@ pub extern "system" fn Java_com_concordium_sdk_crypto_CryptoJniNative_generateBa
471481
#[serde(rename_all = "camelCase")]
472482
struct AddBakerPayloadInput {
473483
pub sender: AccountAddress,
474-
pub keys: base::BakerKeyPairs,
484+
pub keys: base::BakerKeyPairs,
475485
}
476486

477487
type AddBakerResult = CryptoJniResult<BakerKeysPayload<AddBakerKeysMarker>>;
@@ -713,7 +723,7 @@ fn get_string(env: JNIEnv, java_string: JString) -> Result<String, JNIErrorRespo
713723
Err(err) => {
714724
return Err(JNIErrorResponse {
715725
errorMessage: err.to_string(),
716-
errorType: JNIErrorResponseType::JsonDeserialization,
726+
errorType: JNIErrorResponseType::JsonDeserialization,
717727
})
718728
}
719729
};
@@ -723,7 +733,7 @@ fn get_string(env: JNIEnv, java_string: JString) -> Result<String, JNIErrorRespo
723733
Err(err) => {
724734
return Err(JNIErrorResponse {
725735
errorMessage: err.to_string(),
726-
errorType: JNIErrorResponseType::Utf8Decode,
736+
errorType: JNIErrorResponseType::Utf8Decode,
727737
})
728738
}
729739
};
@@ -735,7 +745,7 @@ fn get_string(env: JNIEnv, java_string: JString) -> Result<String, JNIErrorRespo
735745
/// the network in string form.
736746
struct SeedAndNet {
737747
seed_as_hex: String,
738-
net_as_str: String,
748+
net_as_str: String,
739749
}
740750

741751
/// Parse the hex encoded seed and the network from Java types to
@@ -749,7 +759,7 @@ fn get_seed_and_net(
749759
let net = get_string(env, net_as_str)?;
750760
Ok(SeedAndNet {
751761
seed_as_hex: seed,
752-
net_as_str: net,
762+
net_as_str: net,
753763
})
754764
}
755765

@@ -1331,7 +1341,7 @@ pub extern "system" fn Java_com_concordium_sdk_crypto_CryptoJniNative_createWeb3
13311341
impl<T> From<wallet_library::statement::RequestCheckError> for CryptoJniResult<T> {
13321342
fn from(e: wallet_library::statement::RequestCheckError) -> Self {
13331343
let error = JNIErrorResponse {
1334-
errorType: JNIErrorResponseType::NativeConversion,
1344+
errorType: JNIErrorResponseType::NativeConversion,
13351345
errorMessage: e.to_string(),
13361346
};
13371347
CryptoJniResult::Err(error)
@@ -1361,7 +1371,9 @@ pub extern "system" fn Java_com_concordium_sdk_crypto_CryptoJniNative_isAcceptab
13611371
Err(err) => return ErrorResult::from(err).to_jstring(&env),
13621372
};
13631373

1364-
match request.acceptable_request(&wallet_library::default_wallet_config::default_wallet_config()) {
1374+
match request
1375+
.acceptable_request(&wallet_library::default_wallet_config::default_wallet_config())
1376+
{
13651377
Ok(r) => r,
13661378
Err(err) => return ErrorResult::Ok(Some(err.to_string())).to_jstring(&env),
13671379
};
@@ -1470,3 +1482,68 @@ pub extern "system" fn Java_com_concordium_sdk_crypto_CryptoJniNative_isAcceptab
14701482

14711483
ErrorResult::Ok(None).to_jstring(&env)
14721484
}
1485+
1486+
/// The JNI wrapper for creating a PresentationV1 for the given statement.
1487+
/// * `input` - the JSON string of [`wallet_library::proofs::PresentationV1Input`]
1488+
#[no_mangle]
1489+
#[allow(non_snake_case)]
1490+
pub extern "system" fn Java_com_concordium_sdk_crypto_CryptoJniNative_createPresentation(
1491+
env: JNIEnv,
1492+
_: JClass,
1493+
input: JString,
1494+
) -> jstring {
1495+
let input_string = match get_string(env, input) {
1496+
Ok(s) => s,
1497+
Err(err) => return StringResult::Err(err).to_jstring(&env),
1498+
};
1499+
1500+
let proofInput: PresentationV1Input = match serde_json::from_str(&input_string) {
1501+
Ok(req) => req,
1502+
Err(err) => return StringResult::from(err).to_jstring(&env),
1503+
};
1504+
1505+
let presentation = match proofInput.prove() {
1506+
Ok(r) => r,
1507+
Err(err) => return StringResult::from(err).to_jstring(&env),
1508+
};
1509+
1510+
let presentation_string = match to_string(&presentation) {
1511+
Ok(r) => r,
1512+
Err(err) => return StringResult::from(err).to_jstring(&env),
1513+
};
1514+
1515+
CryptoJniResult::Ok(presentation_string).to_jstring(&env)
1516+
}
1517+
1518+
/// The JNI wrapper for computing the anchor hash for VerificationRequestData
1519+
/// * `input` - the JSON string of [`wallet_library::proofs::VerificationRequestV1Input`]
1520+
#[no_mangle]
1521+
#[allow(non_snake_case)]
1522+
pub extern "system" fn Java_com_concordium_sdk_crypto_CryptoJniNative_computeAnchorHash(
1523+
env: JNIEnv,
1524+
_: JClass,
1525+
input: JString,
1526+
) -> jstring {
1527+
let input_string = match get_string(env, input) {
1528+
Ok(s) => s,
1529+
Err(err) => return StringResult::Err(err).to_jstring(&env),
1530+
};
1531+
1532+
let verification_request_v1_input: VerificationRequestV1Input =
1533+
match serde_json::from_str(&input_string) {
1534+
Ok(req) => req,
1535+
Err(err) => return StringResult::from(err).to_jstring(&env),
1536+
};
1537+
1538+
let verification_request_data = VerificationRequestData {
1539+
context: verification_request_v1_input.context,
1540+
subject_claims: verification_request_v1_input.subject_claims,
1541+
};
1542+
1543+
let hash_string = match to_string(&verification_request_data.hash()) {
1544+
Ok(r) => r,
1545+
Err(err) => return StringResult::from(err).to_jstring(&env),
1546+
};
1547+
1548+
CryptoJniResult::Ok(hash_string).to_jstring(&env)
1549+
}

0 commit comments

Comments
 (0)