Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Dec 15, 2024
2 parents 9cded45 + 56c6e58 commit 8faaf3e
Show file tree
Hide file tree
Showing 47 changed files with 1,813 additions and 336 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/sargon-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon-uniffi"
# Don't forget to update version in crates/sargon/Cargo.toml
version = "1.1.82"
version = "1.1.84"
edition = "2021"
build = "build.rs"

Expand Down
11 changes: 7 additions & 4 deletions crates/sargon-uniffi/src/core/error/common_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,18 +828,21 @@ pub enum CommonError {
#[error("Signing was rejected by the user")]
SigningRejected = 10232,

#[error("Failed to automatically build shield, reason: '{underlying}'")]
AutomaticShieldBuildingFailure { underlying: String } = 10233,

#[error("No Transaction Signing Factor")]
NoTransactionSigningFactorInstance = 10233,
NoTransactionSigningFactorInstance = 10234,

#[error("Authentication Signing FactorInstance not securified")]
AuthenticationSigningFactorInstanceNotSecurified = 10234,
AuthenticationSigningFactorInstanceNotSecurified = 10235,

#[error("SecurityEntityControl has no QueuedTransaction, unable to mark it as cancelled")]
SecurityEntityControlHasNoProvisionallyQueuedTransaction = 10235,
SecurityEntityControlHasNoProvisionallyQueuedTransaction = 10236,

#[error("SecurityEntityControl has QueuedTransaction, unable override it, use `cancel_queued_transaction`")]
SecurityEntityControlCannotChangeProvisionalAlreadyHasQueuedTransaction =
10236,
10237,
}

#[uniffi::export]
Expand Down
51 changes: 41 additions & 10 deletions crates/sargon-uniffi/src/core/types/collections/internal_mapping.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::prelude::*;

use sargon::IdentifiedVecOf;

// From InternalType =================================================================================================
use sargon::{IdentifiedVecOf, IndexSet};

// ==========================
// === From InternalType ====
// ==========================
pub trait FromInternal<InternalType, Type> {
fn into_type(self) -> Type;
}

// ===== Vec ======
impl<InternalElement, Element> FromInternal<Vec<InternalElement>, Vec<Element>>
for Vec<InternalElement>
where
Expand All @@ -18,6 +20,7 @@ where
}
}

// === IdentifiedVec ====
impl<InternalElement, Element>
FromInternal<IdentifiedVecOf<InternalElement>, Vec<Element>>
for IdentifiedVecOf<InternalElement>
Expand All @@ -30,30 +33,58 @@ where
}
}

// Into InternalType =================================================================================================
// ==== IndexSet ======
impl<InternalElement, Element>
FromInternal<IndexSet<InternalElement>, Vec<Element>>
for IndexSet<InternalElement>
where
Element: From<InternalElement>,
{
fn into_type(self) -> Vec<Element> {
self.into_iter().map(Element::from).collect()
}
}

// ==========================
// === Into InternalType ====
// ==========================

pub trait IntoInternal<Type, InternalType> {
fn into_internal(self) -> InternalType;
}

impl<InternalElement, Element>
IntoInternal<Vec<Element>, IdentifiedVecOf<InternalElement>>
// ===== Vec ========
impl<InternalElement, Element> IntoInternal<Vec<Element>, Vec<InternalElement>>
for Vec<Element>
where
InternalElement: Debug + PartialEq + Eq + Clone + sargon::Identifiable,
Element: Into<InternalElement>,
{
fn into_internal(self) -> IdentifiedVecOf<InternalElement> {
fn into_internal(self) -> Vec<InternalElement> {
self.into_iter().map(Into::into).collect()
}
}

impl<InternalElement, Element> IntoInternal<Vec<Element>, Vec<InternalElement>>
// ==== IndexSet ======
impl<InternalElement, Element>
IntoInternal<Vec<Element>, IndexSet<InternalElement>> for Vec<Element>
where
Element: Into<InternalElement>,
InternalElement: std::hash::Hash + Eq,
{
fn into_internal(self) -> IndexSet<InternalElement> {
self.into_iter().map(Into::into).collect::<IndexSet<_>>()
}
}

// === IdentifiedVec ====
impl<InternalElement, Element>
IntoInternal<Vec<Element>, IdentifiedVecOf<InternalElement>>
for Vec<Element>
where
InternalElement: Debug + PartialEq + Eq + Clone + sargon::Identifiable,
Element: Into<InternalElement>,
{
fn into_internal(self) -> Vec<InternalElement> {
fn into_internal(self) -> IdentifiedVecOf<InternalElement> {
self.into_iter().map(Into::into).collect()
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sargon-uniffi/src/core/types/requested_quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ pub fn requested_quantity_is_fulfilled_by_ids(
) -> bool {
requested_quantity
.into_internal()
.is_fulfilled_by_ids(number_of_ids as usize)
.is_fulfilled_by_quantity(number_of_ids as usize)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;
use sargon::IndexMap;
use sargon::{IndexSet, KeyDerivationRequest as InternalKeyDerivationRequest};
use sargon::KeyDerivationRequest as InternalKeyDerivationRequest;

/// A collection of derivation paths, on a per-factor-source basis.
#[derive(Clone, PartialEq, Eq, uniffi::Record)]
Expand Down Expand Up @@ -61,11 +61,7 @@ impl From<KeyDerivationRequest> for InternalKeyDerivationRequest {
IndexMap::from_iter(value.per_factor_source.into_iter().map(|f| {
(
f.factor_source_id.into_internal(),
IndexSet::from_iter(
f.derivation_paths
.into_iter()
.map(|d| d.into_internal()),
),
f.derivation_paths.into_internal(),
)
})),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::prelude::*;
use sargon::IndexMap;
use sargon::IndexSet;
use sargon::KeyDerivationResponse as InternalKeyDerivationResponse;

/// A collection of `HierarchicalDeterministicFactorInstance`s, on a
Expand Down Expand Up @@ -58,11 +57,7 @@ impl From<KeyDerivationResponse> for InternalKeyDerivationResponse {
value.per_factor_source.into_iter().map(|item| {
(
item.factor_source_id.into_internal(),
IndexSet::from_iter(
item.factor_instances
.into_iter()
.map(|d| d.into_internal()),
),
item.factor_instances.into_internal(),
)
}),
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use std::{
borrow::Borrow,
future::Future,
sync::{Arc, RwLock},
};

use sargon::SecurityShieldBuilder as InternalSecurityShieldBuilder;
use sargon::SelectedFactorSourcesForRoleStatus as InternalSelectedFactorSourcesForRoleStatus;
use sargon::{IndexSet, MatrixBuilder};
use sargon::{
SecurityShieldBuilder as InternalSecurityShieldBuilder,
SelectedFactorSourcesForRoleStatus as InternalSelectedFactorSourcesForRoleStatus,
};

use crate::prelude::*;

Expand Down Expand Up @@ -378,6 +380,23 @@ impl SecurityShieldBuilder {
}
}

use sargon::FactorSource as InternalFactorSource;

#[uniffi::export]
impl SecurityShieldBuilder {
pub fn auto_assign_factors_to_recovery_and_confirmation_based_on_primary(
&self,
all_factors: Vec<FactorSource>,
) -> Result<()> {
let binding = self.wrapped.write().expect("No poison");
let _ = binding
.auto_assign_factors_to_recovery_and_confirmation_based_on_primary(
all_factors.into_internal(),
);
Ok(())
}
}

#[uniffi::export]
impl SecurityShieldBuilder {
pub fn validate(&self) -> Option<SecurityShieldBuilderInvalidReason> {
Expand Down Expand Up @@ -455,6 +474,10 @@ impl FactorSourceID {
Self::new(sargon::FactorSourceID::sample_ledger_other())
}

pub fn sample_trusted_contact() -> Self {
Self::new(sargon::FactorSourceID::sample_trusted_contact())
}

pub fn sample_arculus() -> Self {
Self::new(sargon::FactorSourceID::sample_arculus())
}
Expand All @@ -472,6 +495,48 @@ impl FactorSourceID {
}
}

impl FactorSource {
pub fn new(inner: impl Borrow<sargon::FactorSource>) -> Self {
Self::from(inner.borrow().clone())
}
}

#[cfg(test)]
impl FactorSource {
pub fn id(&self) -> FactorSourceID {
use sargon::BaseBaseIsFactorSource;
self.clone().into_internal().factor_source_id().into()
}

pub fn sample_device() -> Self {
Self::new(sargon::FactorSource::sample_device())
}
pub fn sample_password() -> Self {
Self::new(sargon::FactorSource::sample_password())
}
pub fn sample_trusted_contact_frank() -> Self {
Self::new(sargon::FactorSource::sample_trusted_contact_frank())
}
pub fn sample_device_babylon() -> Self {
Self::new(sargon::FactorSource::sample_device_babylon())
}
pub fn sample_device_babylon_other() -> Self {
Self::new(sargon::FactorSource::sample_device_babylon_other())
}
pub fn sample_ledger() -> Self {
Self::new(sargon::FactorSource::sample_ledger())
}
pub fn sample_arculus() -> Self {
Self::new(sargon::FactorSource::sample_arculus())
}
pub fn sample_arculus_other() -> Self {
Self::new(sargon::FactorSource::sample_arculus_other())
}
pub fn sample_ledger_other() -> Self {
Self::new(sargon::FactorSource::sample_ledger_other())
}
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -713,4 +778,84 @@ mod tests {
vec![FactorSourceID::sample_device()]
);
}

#[test]
fn auto_assign() {
let sut = SUT::new();
let all_factors_in_profile = vec![
FactorSource::sample_password(),
FactorSource::sample_trusted_contact_frank(),
FactorSource::sample_device_babylon(),
FactorSource::sample_device_babylon_other(),
FactorSource::sample_ledger(),
FactorSource::sample_arculus(),
FactorSource::sample_arculus_other(),
FactorSource::sample_ledger_other(),
];
let name = "Auto Built";
let days_to_auto_confirm = 237;
sut.set_name(name.to_owned());
sut.set_number_of_days_until_auto_confirm(days_to_auto_confirm);
sut.set_threshold(2);
sut.add_factor_source_to_primary_threshold(
FactorSource::sample_device_babylon().id(),
);
sut.add_factor_source_to_primary_threshold(
FactorSource::sample_ledger().id(),
);

sut.auto_assign_factors_to_recovery_and_confirmation_based_on_primary(
all_factors_in_profile.clone(),
)
.unwrap();

let shield = sut.build().unwrap();

assert_eq!(shield.metadata.display_name.value, name.to_owned());
let matrix = shield.matrix_of_factors;
assert_eq!(
matrix.number_of_days_until_auto_confirm,
days_to_auto_confirm
);

pretty_assertions::assert_eq!(
matrix.primary_role,
PrimaryRoleWithFactorSourceIDs {
threshold: 2,
threshold_factors: vec![
FactorSourceID::sample_device(),
FactorSourceID::sample_ledger()
],
override_factors: Vec::new()
}
);

pretty_assertions::assert_eq!(
matrix.recovery_role,
RecoveryRoleWithFactorSourceIDs {
threshold: 0,
threshold_factors: Vec::new(),
override_factors: vec![
FactorSourceID::sample_trusted_contact(),
FactorSourceID::sample_ledger(),
FactorSourceID::sample_arculus_other(),
FactorSourceID::sample_ledger_other(),
]
}
);

pretty_assertions::assert_eq!(
matrix.confirmation_role,
ConfirmationRoleWithFactorSourceIDs {
threshold: 0,
threshold_factors: Vec::new(),
override_factors: vec![
FactorSourceID::sample_password(),
FactorSourceID::sample_arculus(),
FactorSourceID::sample_device(),
FactorSourceID::sample_device_other(),
]
}
);
}
}
9 changes: 1 addition & 8 deletions crates/sargon-uniffi/src/signing/neglected_factors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ impl From<InternalNeglectedFactors> for NeglectedFactors {

impl From<NeglectedFactors> for InternalNeglectedFactors {
fn from(value: NeglectedFactors) -> Self {
Self::new(
value.reason.into_internal(),
value
.factors
.into_iter()
.map(|id| id.into_internal())
.collect::<sargon::IndexSet<_>>(),
)
Self::new(value.reason.into_internal(), value.factors.into_internal())
}
}

Expand Down
Loading

0 comments on commit 8faaf3e

Please sign in to comment.