Skip to content

Commit

Permalink
fix: rename master secret to link secret
Browse files Browse the repository at this point in the history
Signed-off-by: blu3beri <[email protected]>
  • Loading branch information
berendsliedrecht committed Mar 21, 2023
1 parent 742e2d2 commit 0936b2c
Show file tree
Hide file tree
Showing 41 changed files with 502 additions and 475 deletions.
6 changes: 6 additions & 0 deletions include/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ Generate the header file:
```sh
cbindgen --config include/cbindgen.toml --crate anoncreds --output include/libanoncreds.h
```

Copy to React Native:

```sh
cp include/libanoncreds.h wrappers/javascript/anoncreds-react-native/cpp/include/
```
10 changes: 5 additions & 5 deletions include/libanoncreds.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,13 @@ ErrorCode anoncreds_create_credential_offer(FfiStr schema_id,
ErrorCode anoncreds_create_credential_request(FfiStr entropy,
FfiStr prover_did,
ObjectHandle cred_def,
ObjectHandle master_secret,
FfiStr master_secret_id,
FfiStr link_secret,
FfiStr link_secret_id,
ObjectHandle cred_offer,
ObjectHandle *cred_req_p,
ObjectHandle *cred_req_meta_p);

ErrorCode anoncreds_create_master_secret(ObjectHandle *master_secret_p);
ErrorCode anoncreds_create_link_secret(const char **link_secret_p);

ErrorCode anoncreds_create_or_update_revocation_state(ObjectHandle rev_reg_def,
ObjectHandle rev_status_list,
Expand All @@ -306,7 +306,7 @@ ErrorCode anoncreds_create_presentation(ObjectHandle pres_req,
struct FfiList_FfiCredentialProve credentials_prove,
FfiStrList self_attest_names,
FfiStrList self_attest_values,
ObjectHandle master_secret,
FfiStr link_secret,
struct FfiList_ObjectHandle schemas,
FfiStrList schema_ids,
struct FfiList_ObjectHandle cred_defs,
Expand Down Expand Up @@ -354,7 +354,7 @@ ErrorCode anoncreds_object_get_type_name(ObjectHandle handle, const char **resul

ErrorCode anoncreds_process_credential(ObjectHandle cred,
ObjectHandle cred_req_metadata,
ObjectHandle master_secret,
FfiStr link_secret,
ObjectHandle cred_def,
ObjectHandle rev_reg_def,
ObjectHandle *cred_p);
Expand Down
66 changes: 33 additions & 33 deletions src/data_types/cred_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ impl CredentialRequest {

#[derive(Debug, Deserialize, Serialize)]
pub struct CredentialRequestMetadata {
pub master_secret_blinding_data: ursa::cl::CredentialSecretsBlindingFactors,
pub link_secret_blinding_data: ursa::cl::CredentialSecretsBlindingFactors,
pub nonce: Nonce,
pub master_secret_name: String,
pub link_secret_name: String,
}

impl Validatable for CredentialRequestMetadata {}
Expand All @@ -95,7 +95,7 @@ mod cred_req_tests {
data_types::{
cred_def::{CredentialDefinition, CredentialKeyCorrectnessProof, SignatureType},
cred_offer::CredentialOffer,
master_secret::MasterSecret,
link_secret::LinkSecret,
schema::AttributeNames,
},
issuer::{create_credential_definition, create_credential_offer, create_schema},
Expand All @@ -112,7 +112,7 @@ mod cred_req_tests {

const ENTROPY: Option<&str> = Some("entropy");
const PROVER_DID: Option<&str> = Some(LEGACY_DID_IDENTIFIER);
const MASTER_SERCET_ID: &str = "master:secret:id";
const LINK_SERCET_ID: &str = "link:secret:id";

fn cred_def() -> Result<(CredentialDefinition, CredentialKeyCorrectnessProof)> {
let credential_definition_issuer_id = "sample:id";
Expand All @@ -133,8 +133,8 @@ mod cred_req_tests {
Ok((cred_def.0, cred_def.2))
}

fn master_secret() -> MasterSecret {
MasterSecret::new().unwrap()
fn link_secret() -> LinkSecret {
LinkSecret::new().unwrap()
}

fn credential_offer(
Expand All @@ -155,15 +155,15 @@ mod cred_req_tests {
#[test]
fn create_credential_request_with_valid_input() -> Result<()> {
let (cred_def, correctness_proof) = cred_def()?;
let master_secret = master_secret();
let link_secret = link_secret();
let credential_offer = credential_offer(correctness_proof, false)?;

let res = create_credential_request(
ENTROPY,
None,
&cred_def,
&master_secret,
MASTER_SERCET_ID,
&link_secret,
LINK_SERCET_ID,
&credential_offer,
);

Expand All @@ -175,15 +175,15 @@ mod cred_req_tests {
#[test]
fn create_credential_request_with_valid_input_legacy() -> Result<()> {
let (cred_def, correctness_proof) = cred_def()?;
let master_secret = master_secret();
let link_secret = link_secret();
let credential_offer = credential_offer(correctness_proof, true)?;

let res = create_credential_request(
None,
PROVER_DID,
&cred_def,
&master_secret,
MASTER_SERCET_ID,
&link_secret,
LINK_SERCET_ID,
&credential_offer,
);

Expand All @@ -195,15 +195,15 @@ mod cred_req_tests {
#[test]
fn create_credential_request_with_invalid_new_identifiers_and_prover_did() -> Result<()> {
let (cred_def, correctness_proof) = cred_def()?;
let master_secret = master_secret();
let link_secret = link_secret();
let credential_offer = credential_offer(correctness_proof, false)?;

let res = create_credential_request(
None,
PROVER_DID,
&cred_def,
&master_secret,
MASTER_SERCET_ID,
&link_secret,
LINK_SERCET_ID,
&credential_offer,
);

Expand All @@ -215,15 +215,15 @@ mod cred_req_tests {
#[test]
fn create_credential_request_with_invalid_prover_did_and_entropy() -> Result<()> {
let (cred_def, correctness_proof) = cred_def()?;
let master_secret = master_secret();
let link_secret = link_secret();
let credential_offer = credential_offer(correctness_proof, true)?;

let res = create_credential_request(
ENTROPY,
PROVER_DID,
&cred_def,
&master_secret,
MASTER_SERCET_ID,
&link_secret,
LINK_SERCET_ID,
&credential_offer,
);

Expand All @@ -235,15 +235,15 @@ mod cred_req_tests {
#[test]
fn create_credential_request_with_invalid_prover_did() -> Result<()> {
let (cred_def, correctness_proof) = cred_def()?;
let master_secret = master_secret();
let link_secret = link_secret();
let credential_offer = credential_offer(correctness_proof, true)?;

let res = create_credential_request(
None,
ENTROPY,
&cred_def,
&master_secret,
MASTER_SERCET_ID,
&link_secret,
LINK_SERCET_ID,
&credential_offer,
);

Expand All @@ -255,15 +255,15 @@ mod cred_req_tests {
#[test]
fn create_credential_request_with_no_entropy_or_prover_did() -> Result<()> {
let (cred_def, correctness_proof) = cred_def()?;
let master_secret = master_secret();
let link_secret = link_secret();
let credential_offer = credential_offer(correctness_proof, true)?;

let res = create_credential_request(
None,
None,
&cred_def,
&master_secret,
MASTER_SERCET_ID,
&link_secret,
LINK_SERCET_ID,
&credential_offer,
);

Expand All @@ -275,15 +275,15 @@ mod cred_req_tests {
#[test]
fn create_credential_request_json_contains_entropy() -> Result<()> {
let (cred_def, correctness_proof) = cred_def()?;
let master_secret = master_secret();
let link_secret = link_secret();
let credential_offer = credential_offer(correctness_proof, false)?;

let res = create_credential_request(
ENTROPY,
None,
&cred_def,
&master_secret,
MASTER_SERCET_ID,
&link_secret,
LINK_SERCET_ID,
&credential_offer,
)
.unwrap();
Expand All @@ -298,15 +298,15 @@ mod cred_req_tests {
#[test]
fn create_credential_request_json_contains_prover_did_with_legacy_identifiers() -> Result<()> {
let (cred_def, correctness_proof) = cred_def()?;
let master_secret = master_secret();
let link_secret = link_secret();
let credential_offer = credential_offer(correctness_proof, true)?;

let res = create_credential_request(
None,
PROVER_DID,
&cred_def,
&master_secret,
MASTER_SERCET_ID,
&link_secret,
LINK_SERCET_ID,
&credential_offer,
)
.unwrap();
Expand All @@ -321,15 +321,15 @@ mod cred_req_tests {
#[test]
fn create_credential_request_json_contains_entropy_with_legacy_identifiers() -> Result<()> {
let (cred_def, correctness_proof) = cred_def()?;
let master_secret = master_secret();
let link_secret = link_secret();
let credential_offer = credential_offer(correctness_proof, false)?;

let res = create_credential_request(
ENTROPY,
None,
&cred_def,
&master_secret,
MASTER_SERCET_ID,
&link_secret,
LINK_SERCET_ID,
&credential_offer,
)
.unwrap();
Expand Down
127 changes: 127 additions & 0 deletions src/data_types/link_secret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use crate::error::ConversionError;
use std::fmt;
use ursa::{
bn::BigNumber,
cl::{prover::Prover as UrsaProver, MasterSecret},
};
pub struct LinkSecret(pub ursa::bn::BigNumber);

impl LinkSecret {
#[must_use]
pub fn new() -> Result<Self, ConversionError> {
let value = UrsaProver::new_master_secret()
.and_then(|v| v.value())
.map_err(|err| {
ConversionError::from_msg(format!("Error creating link secret: {err}"))
})?;

Ok(Self(value))
}

pub fn try_clone(&self) -> Result<Self, ConversionError> {
let cloned = self.0.try_clone().map_err(|err| {
ConversionError::from_msg(format!("Error cloning link secret: {err}"))
})?;

Ok(Self(cloned))
}
}

impl fmt::Debug for LinkSecret {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("LinkSecret")
.field(if cfg!(test) { &self.0 } else { &"<hidden>" })
.finish()
}
}

impl TryInto<MasterSecret> for LinkSecret {
type Error = ConversionError;

fn try_into(self) -> Result<MasterSecret, Self::Error> {
let j = serde_json::json!({
"ms": self.0
});
serde_json::from_value(j)
.map_err(|err| ConversionError::from_msg(format!("Error creating link secret: {err}")))
}
}

impl TryInto<MasterSecret> for &LinkSecret {
type Error = ConversionError;

fn try_into(self) -> Result<MasterSecret, Self::Error> {
let j = serde_json::json!({
"ms": self.0
});

serde_json::from_value(j)
.map_err(|err| ConversionError::from_msg(format!("Error creating link secret: {err}")))
}
}

impl TryInto<String> for LinkSecret {
type Error = ConversionError;

fn try_into(self) -> Result<String, Self::Error> {
self.0
.to_dec()
.map_err(|err| ConversionError::from_msg(format!("Error creating link secret: {err}")))
}
}

impl TryFrom<&str> for LinkSecret {
type Error = ConversionError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(Self(BigNumber::from_dec(value).map_err(|err| {
ConversionError::from_msg(format!("Error creating link secret: {err}"))
})?))
}
}

#[cfg(test)]
mod link_secret_tests {
use super::*;

#[test]
fn should_create_new_link_secret() {
let link_secret = LinkSecret::new();
assert!(link_secret.is_ok());
}

#[test]
fn should_convert_between_string_and_link_secret_roundtrip() {
let ls = "123";
let link_secret = LinkSecret::try_from(ls).expect("Error creating link secret");
let link_secret_str: String = link_secret.try_into().expect("Error creating link secret");
assert_eq!(link_secret_str, ls);
}

#[test]
fn should_convert_between_master_secret() {
let link_secret = LinkSecret::new().expect("Unable to create link secret");
let master_secret: MasterSecret = link_secret
.try_clone()
.expect("Error cloning link secret")
.try_into()
.expect("error converting link secret");

assert_eq!(
link_secret.0,
master_secret
.value()
.expect("Error getting value from master secret")
);
}

#[test]
fn should_clone_link_secret() {
let link_secret = LinkSecret::new().expect("Unable to create link secret");
let cloned_link_secret = link_secret
.try_clone()
.expect("Unable to clone link secret");

assert_eq!(link_secret.0, cloned_link_secret.0);
}
}
Loading

0 comments on commit 0936b2c

Please sign in to comment.