Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROLA using Signatures Collector #310

Merged
merged 14 commits into from
Dec 19, 2024
Merged
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.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
public class ThrowingHostInteractor: HostInteractor {
public nonisolated(unsafe) static var shared: HostInteractor = ThrowingHostInteractor()

public func signAuth(request: SargonUniFFI.AuthenticationSigningRequest) async throws -> SargonUniFFI.AuthenticationSigningResponse {
public func signAuth(request: SargonUniFFI.SignRequestOfAuthIntent) async throws -> SargonUniFFI.SignWithFactorsOutcomeOfAuthIntentHash {

Check warning on line 5 in apple/Sources/Sargon/SargonOS/ThrowingHostInteractor+Static+Shared.swift

View check run for this annotation

Codecov / codecov/patch

apple/Sources/Sargon/SargonOS/ThrowingHostInteractor+Static+Shared.swift#L5

Added line #L5 was not covered by tests
throw CommonError.SigningRejected
}

Expand Down
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.91"
version = "1.1.92"
edition = "2021"
build = "build.rs"

Expand Down
95 changes: 95 additions & 0 deletions crates/sargon-uniffi/src/signing/authentication/auth_intent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::prelude::*;
use sargon::AuthIntent as InternalAuthIntent;
use std::hash::Hasher;

#[derive(Clone, PartialEq, Eq, uniffi::Record)]
pub struct AuthIntent {
/// The challenge nonce that with some `metadata` values are generating the `RolaChallenge`
/// needed to be signed
pub challenge_nonce: Exactly32Bytes,

/// The `NetworkID` on which the request was made
pub network_id: NetworkID,

/// The origin `Url` of the dApp from which the request was made
pub origin: Url,

/// The dApp's definition address
pub dapp_definition_address: DappDefinitionAddress,

/// The entities needed to be signed.
pub entities_to_sign: Vec<AddressOfAccountOrPersona>,
}

/// Since `AuthIntent` is also acting as a payload in `SignaturesCollector` when signing auth,
/// needs to conform to Hash too. For other `Signable`s like `TransactionIntent` or `Subintent`
/// there are specific compiled versions of them like `CompiledTransactionIntent` and
/// `CompiledSubintent` respectively, which conform to Hash.
impl std::hash::Hash for AuthIntent {
fn hash<H: Hasher>(&self, state: &mut H) {
self.into_internal().hash(state);
}
}

impl AuthIntent {
pub fn into_internal(&self) -> InternalAuthIntent {
self.clone().into()
}
}

impl From<InternalAuthIntent> for AuthIntent {
fn from(value: InternalAuthIntent) -> Self {
Self {
challenge_nonce: value.challenge_nonce.into(),
network_id: value.network_id.into(),
origin: value.origin,
dapp_definition_address: value.dapp_definition_address.into(),
entities_to_sign: value
.entities_to_sign
.into_iter()
.map(Into::into)
.collect(),
}
}
}

impl From<AuthIntent> for InternalAuthIntent {
fn from(value: AuthIntent) -> Self {
Self::new(
value.challenge_nonce.into(),
value.network_id.into(),
value.origin,
value.dapp_definition_address.into(),
value.entities_to_sign.into_iter().map(Into::into).collect(),
)
}
}

#[uniffi::export]
pub fn new_auth_intent_from_request(
challenge_nonce: DappToWalletInteractionAuthChallengeNonce,
metadata: DappToWalletInteractionMetadata,
entities_to_sign: Vec<AddressOfAccountOrPersona>,
) -> Result<AuthIntent> {
InternalAuthIntent::new_from_request(
challenge_nonce.into(),
metadata.into(),
entities_to_sign.into_iter().map(|a| a.into_internal()),
)
.into_result()
}

#[uniffi::export]
pub fn auth_intent_get_hash(auth_intent: AuthIntent) -> AuthIntentHash {
auth_intent.into_internal().auth_intent_hash().into()
}

#[uniffi::export]
pub fn new_auth_intent_sample() -> AuthIntent {
InternalAuthIntent::sample().into()
}

#[uniffi::export]
pub fn new_auth_intent_sample_other() -> AuthIntent {
InternalAuthIntent::sample_other().into()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::prelude::*;
use sargon::AuthIntentHash as InternalAuthIntentHash;

#[derive(
Clone, PartialEq, Eq, std::hash::Hash, InternalConversion, uniffi::Record,
)]
pub struct AuthIntentHash {
pub payload: BagOfBytes,
}

#[uniffi::export]
pub fn new_auth_intent_hash_sample() -> AuthIntentHash {
InternalAuthIntentHash::sample().into()
}

#[uniffi::export]
pub fn new_auth_intent_hash_sample_other() -> AuthIntentHash {
InternalAuthIntentHash::sample_other().into()
}
7 changes: 7 additions & 0 deletions crates/sargon-uniffi/src/signing/authentication/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod auth_intent;
mod auth_intent_hash;
mod signed_auth_intent;

pub use auth_intent::*;
pub use auth_intent_hash::*;
pub use signed_auth_intent::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::prelude::*;
use sargon::AddressOfAccountOrPersona as InternalAddressOfAccountOrPersona;
use sargon::IntentSignature as InternalIntentSignature;
use sargon::SignedAuthIntent as InternalSignedAuthIntent;

#[derive(Clone, PartialEq, Eq, uniffi::Record)]
pub struct SignedAuthIntent {
pub intent: AuthIntent,
pub intent_signatures_per_owner: Vec<IntentSignatureOfOwner>,
}

#[uniffi::export]
pub fn new_signed_auth_intent_sample() -> SignedAuthIntent {
InternalSignedAuthIntent::sample().into()
}

#[uniffi::export]
pub fn new_signed_auth_intent_sample_other() -> SignedAuthIntent {
InternalSignedAuthIntent::sample_other().into()
}

impl SignedAuthIntent {
pub fn into_internal(&self) -> InternalSignedAuthIntent {
self.clone().into()
}
}

impl From<InternalSignedAuthIntent> for SignedAuthIntent {
fn from(value: InternalSignedAuthIntent) -> Self {
SignedAuthIntent {
intent: value.intent.into(),
intent_signatures_per_owner: value
.intent_signatures_per_owner
.iter()
.map(|(owner, signature)| {
IntentSignatureOfOwner::new(
(*owner).into(),
(*signature).into(),
)
})
.collect(),
}
}
}

impl From<SignedAuthIntent> for InternalSignedAuthIntent {
fn from(value: SignedAuthIntent) -> Self {
Self {
intent: value.intent.into(),
intent_signatures_per_owner: value
.intent_signatures_per_owner
.into_iter()
.map(|item| {
(
item.owner.into_internal(),
item.intent_signature.into_internal(),
)
})
.collect::<sargon::IndexMap<
InternalAddressOfAccountOrPersona,
InternalIntentSignature,
>>(),
}
}
}
36 changes: 0 additions & 36 deletions crates/sargon-uniffi/src/signing/authentication_signing_input.rs

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions crates/sargon-uniffi/src/signing/hd_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ macro_rules! decl_hd_signature {

decl_hd_signature!(TransactionIntentHash);
decl_hd_signature!(SubintentHash);
decl_hd_signature!(AuthIntentHash);
1 change: 1 addition & 0 deletions crates/sargon-uniffi/src/signing/hd_signature_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ macro_rules! decl_hd_signature_input {

decl_hd_signature_input!(TransactionIntentHash);
decl_hd_signature_input!(SubintentHash);
decl_hd_signature_input!(AuthIntentHash);
19 changes: 19 additions & 0 deletions crates/sargon-uniffi/src/signing/intent_signature_of_owner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::prelude::*;

#[derive(Clone, PartialEq, Eq, uniffi::Record)]
pub struct IntentSignatureOfOwner {
pub owner: AddressOfAccountOrPersona,
pub intent_signature: IntentSignature,
}

impl IntentSignatureOfOwner {
pub fn new(
owner: AddressOfAccountOrPersona,
intent_signature: IntentSignature,
) -> Self {
Self {
owner,
intent_signature,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ macro_rules! decl_invalid_transaction_if_neglected {

decl_invalid_transaction_if_neglected!(TransactionIntentHash);
decl_invalid_transaction_if_neglected!(SubintentHash);
decl_invalid_transaction_if_neglected!(AuthIntentHash);
12 changes: 4 additions & 8 deletions crates/sargon-uniffi/src/signing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
mod authentication_signing_input;
mod authentication_signing_request;
mod authentication_signing_response;
mod authentication;
mod hd_signature;
mod hd_signature_input;
mod intent_signature_of_owner;
mod invalid_transaction_if_neglected;
mod neglected_factors;
mod rola_challenge;
mod sign_request;
mod sign_response;
mod sign_with_factors_outcome;
mod signatures_per_fractor_source;
mod transaction_sign_request_input;
mod transactions_to_sign_per_factor_source;

pub use authentication_signing_input::*;
pub use authentication_signing_request::*;
pub use authentication_signing_response::*;
pub use authentication::*;
pub use hd_signature::*;
pub use hd_signature_input::*;
pub use intent_signature_of_owner::*;
pub use invalid_transaction_if_neglected::*;
pub use neglected_factors::*;
pub use rola_challenge::*;
pub use sign_request::*;
pub use sign_response::*;
pub use sign_with_factors_outcome::*;
Expand Down
22 changes: 0 additions & 22 deletions crates/sargon-uniffi/src/signing/rola_challenge.rs

This file was deleted.

Loading
Loading