Skip to content

Commit

Permalink
[ABW-3899] Create subintent API (#255)
Browse files Browse the repository at this point in the history
* logic

* version bump

* tests

* Update README.md

* Update README.md

* Add more extensions

* updates after feedback

---------

Co-authored-by: micbakos-rdx <[email protected]>
  • Loading branch information
matiasbzurovski and micbakos-rdx authored Nov 7, 2024
1 parent 4694350 commit e5e07b5
Show file tree
Hide file tree
Showing 27 changed files with 438 additions and 13 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.

10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ xcode-select --install

Or install `Xcode` from App Store

After installing, you should run the following command to verify the SDK path is set to the Xcode app under `Applications`.
```sh
$ xcrun --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
```
If it doesn't point to the expected path (and instead points to something like `/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk`), make sure you have the PATH exported on your profile (e.g. `.zshrc`):
```
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
```

### Kotlin

```sh
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.45"
version = "1.1.46"
edition = "2021"
build = "build.rs"

Expand Down
3 changes: 3 additions & 0 deletions crates/sargon-uniffi/src/core/error/common_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,9 @@ pub enum CommonError {

#[error("Failed to decompile bytes into Subintent")]
FailedToDecompileBytesIntoSubintent = 10205,

#[error("Subintent has already expired")]
SubintentExpired = 10206,
}

#[uniffi::export]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub struct DappToWalletInteractionSubintentRequestItem {

pub message: Option<String>,

pub expiration: Option<DappToWalletInteractionSubintentExpiration>,
pub expiration: DappToWalletInteractionSubintentExpiration,
}
1 change: 1 addition & 0 deletions crates/sargon-uniffi/src/system/sargon_os/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod pre_authorization;
mod profile_state_holder;
mod sargon_os;
mod sargon_os_accounts;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod sargon_os_create_subintent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::prelude::*;

// ==================
// Create Subintent
// ==================
#[uniffi::export]
impl SargonOS {
/// Creates a Subintent given its discriminator, manifest and expiration.
pub async fn create_subintent(
&self,
intent_discriminator: IntentDiscriminator,
subintent_manifest: SubintentManifest,
expiration: DappToWalletInteractionSubintentExpiration,
message: Option<String>,
) -> Result<Subintent> {
self.wrapped
.create_subintent(
intent_discriminator.into_internal(),
subintent_manifest.into_internal(),
expiration.into_internal(),
message,
)
.await
.into_result()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ pub fn subintent_manifest_string(manifest: &SubintentManifest) -> String {
manifest.into_internal().manifest_string()
}

#[uniffi::export]
pub fn subintent_manifest_summary(
manifest: &SubintentManifest,
) -> ManifestSummary {
manifest.into_internal().summary().unwrap().into()
}

#[uniffi::export]
pub fn subintent_manifest_involved_resource_addresses(
manifest: &SubintentManifest,
Expand Down
2 changes: 1 addition & 1 deletion crates/sargon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon"
# Don't forget to update version in crates/sargon-uniffi/Cargo.toml
version = "1.1.45"
version = "1.1.46"
edition = "2021"
build = "build.rs"

Expand Down
3 changes: 3 additions & 0 deletions crates/sargon/src/core/error/common_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,9 @@ pub enum CommonError {

#[error("Failed to decompile bytes into Subintent")]
FailedToDecompileBytesIntoSubintent = 10205,

#[error("Subintent has already expired")]
SubintentExpired = 10206,
}

impl CommonError {
Expand Down
10 changes: 10 additions & 0 deletions crates/sargon/src/core/types/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ impl Epoch {
pub fn window_end_from_start(start: Self) -> Self {
Self::new(start.0 + Self::DEFAULT_EPOCH_WINDOW_SIZE)
}

pub fn adding(&self, amount: u64) -> Self {
Self::new(self.0 + amount)
}
}

impl From<u64> for Epoch {
Expand Down Expand Up @@ -97,4 +101,10 @@ mod tests {
test(2);
test(1337);
}

#[test]
fn adding() {
let sut = Epoch(10);
assert_eq!(sut.adding(5), Epoch(15));
}
}
21 changes: 21 additions & 0 deletions crates/sargon/src/core/types/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ impl From<ScryptoInstant> for Instant {
}
}

impl From<Timestamp> for Instant {
fn from(value: Timestamp) -> Self {
let seconds_since_unix_epoch =
value.duration_since(Timestamp::UNIX_EPOCH).as_seconds_f64() as i64;
Self {
seconds_since_unix_epoch,
}
}
}

impl HasSampleValues for Instant {
fn sample() -> Self {
Self::from(0)
Expand Down Expand Up @@ -98,4 +108,15 @@ mod tests {
test(2);
test(1337);
}

#[test]
fn from_timestamp() {
let timestamp = Timestamp::now_utc();
let seconds_since_unix_epoch = timestamp
.duration_since(Timestamp::UNIX_EPOCH)
.as_seconds_f64() as i64;
let instant = Instant::from(timestamp);

assert_eq!(instant.seconds_since_unix_epoch, seconds_since_unix_epoch);
}
}
9 changes: 9 additions & 0 deletions crates/sargon/src/core/utils/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 5 minutes
pub const EPOCH_DURATION_IN_SECONDS: u64 = 300;

// As per the transaction validation configuration, epoch diff should be less than 1 month.
pub const MAX_EPOCH_DIFF: u64 = 30 * 24 * 60 * 60 / EPOCH_DURATION_IN_SECONDS;

// 1 epoch for the fact that it's min_inclusive and max_exclusive;
// 1 more for the fact that we might be very close to the end of the epoch already
pub const MIN_EPOCH_DIFF: u64 = 2;
2 changes: 2 additions & 0 deletions crates/sargon/src/core/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod constants;
mod factory;
mod image_url_utils;
mod logged_panic;
mod serialization;
mod string_utils;

pub use constants::*;
pub use factory::*;
pub use image_url_utils::*;
pub use logged_panic::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ pub struct DappToWalletInteractionSubintentRequestItem {
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub expiration: Option<DappToWalletInteractionSubintentExpiration>,
pub expiration: DappToWalletInteractionSubintentExpiration,
}

impl DappToWalletInteractionSubintentRequestItem {
pub fn new(
version: impl Into<SubintentVersion>,
unvalidated_manifest: impl Into<UnvalidatedSubintentManifest>,
message: impl Into<Option<String>>,
expiration: impl Into<Option<DappToWalletInteractionSubintentExpiration>>,
expiration: impl Into<DappToWalletInteractionSubintentExpiration>,
) -> Self {
Self {
version: version.into(),
Expand Down
2 changes: 2 additions & 0 deletions crates/sargon/src/system/sargon_os/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod pre_authorization;
mod profile_state_holder;
mod sargon_os;
mod sargon_os_accounts;
Expand All @@ -7,6 +8,7 @@ mod sargon_os_profile;
mod sargon_os_security_structures;
mod transactions;

pub use pre_authorization::*;
pub use profile_state_holder::*;
pub use sargon_os::*;
pub use sargon_os_accounts::*;
Expand Down
3 changes: 3 additions & 0 deletions crates/sargon/src/system/sargon_os/pre_authorization/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod sargon_os_create_subintent;

pub use sargon_os_create_subintent::*;
Loading

0 comments on commit e5e07b5

Please sign in to comment.