Skip to content

Commit

Permalink
SargonOS v1 integration API (#184)
Browse files Browse the repository at this point in the history
* Rust: `SargonOS` &`Drivers` more Profile Logic | Swift: Shared State  (working example app)! (#131)

Introducing SargonOS

* MFA: Sec Structure of Factor <Source | ID | Instance>s (#150)

MFA Batch 1: SecurityStructureOfFactorSources and MatrixOfFactorSoures and also on FactorSourceID and FactorInstance level. Also add some new FactorSourceKinds at least a POC version of them or stubbed out ones.

* Sargon OS undo Profile JSON format change (#177)

Revert Profile JSON format changes in DeviceInfo

* Merge 'main' into 'sargon_os'

* wip

* Implement HostInfo struct (#189)

* Implement HostInfo struct

* Store host id in secure storage

* Fix doc

* Separate id and date from host info

* Implement HostOS that will encapsulate the different values and constants of each host os

* Expose uniffi method for creating device info from host info

* wip

* Sargon os v1 android (#196)

* Implement sargon os drivers

* Boot sargon os in example app

* Remove active profile id

* Add more unit tests

* Fix apple tests

* pr review

* version bump

---------

Co-authored-by: Alexander Cyon <116169792+CyonAlexRDX@users.noreply.github.com>
Co-authored-by: micbakos-rdx <michael.bakogiannis@rdx.works>
Co-authored-by: micbakos-rdx <125959264+micbakos-rdx@users.noreply.github.com>
  • Loading branch information
4 people committed Aug 12, 2024
1 parent e8cb924 commit 5d45750
Show file tree
Hide file tree
Showing 101 changed files with 3,704 additions and 1,022 deletions.
2 changes: 1 addition & 1 deletion 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "1.1.2"
version = "1.1.3"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import UIKit
extension AppleHostInfoDriver: HostInfoDriver {

public func hostOs() async -> HostOs {
await .ios(version: UIDevice.current.systemVersion)
await HostOs.ios(version: UIDevice.current.systemVersion)
}

nonisolated public func hostDeviceName() async -> String {
Expand Down
17 changes: 17 additions & 0 deletions apple/Sources/Sargon/Drivers/ProfileChange/ProfileChange.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import SargonUniFFI

public typealias ProfileChangeEventPublisher = EventPublisher<Profile>

extension ProfileChangeEventPublisher: ProfileChangeDriver {
public static let shared = ProfileChangeEventPublisher()

public func handleProfileChange(changedProfile: Profile) async {
subject.send(changedProfile)
}
}

extension ProfileChangeDriver where Self == ProfileChangeEventPublisher {
public static var shared: Self {
ProfileChangeEventPublisher.shared
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ extension Drivers {
logging: .shared,
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: unsafeStorage
unsafeStorage: unsafeStorage,
profileChangeDriver: .shared
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,3 @@ import SargonUniFFI
public typealias SargonOS = SargonOs

extension SargonOS: @unchecked Sendable {}

extension SargonOS {

@available(*, deprecated, message: "SHOULD migrate to use more specialized methods on SargonOS instead, e.g. `createAndSaveNewAccount` - SargonOS should be the SOLE object to perform the mutation and persisting.")
public func saveChangedProfile(_ profile: Profile) async throws {
try await deprecatedSaveFfiChangedProfile(profile: profile)
}
}
6 changes: 0 additions & 6 deletions apple/Sources/Sargon/SargonOS/SargonOSProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ extension SargonOSProtocol {

// MARK: Extensions
extension SargonOSProtocol {

@available(*, deprecated, message: "SHOULD migrate to use more specialized access methods on SargonOS instead, e.g. `accountsOnCurrentNetwork`.")
public var profile: Profile {
os.profile()
}

public var currentNetworkID: NetworkID {
os.currentNetworkId()
}
Expand Down
15 changes: 15 additions & 0 deletions apple/Sources/Sargon/Util/EventPublisher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import AsyncExtensions

public final actor EventPublisher<Element: Sendable> {
public typealias Subject = AsyncPassthroughSubject<Element>
public typealias Stream = AsyncThrowingPassthroughSubject<Element, any Error>

let stream = Stream()
let subject = Subject()

public func eventStream() -> AsyncMulticastSequence<Subject, Stream> {
subject
.multicast(stream)
.autoconnect()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ extension Drivers {
logging: .shared,
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: .shared
unsafeStorage: .shared,
profileChangeDriver: .shared
)
}

Expand All @@ -67,8 +68,9 @@ extension Drivers {
logging: .shared,
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: .shared

unsafeStorage: .shared,
profileChangeDriver: .shared

)
}

Expand All @@ -81,7 +83,8 @@ extension Drivers {
logging: .shared,
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: .shared
unsafeStorage: .shared,
profileChangeDriver: .shared
)
}

Expand All @@ -94,7 +97,8 @@ extension Drivers {
logging: .shared,
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: .shared
unsafeStorage: .shared,
profileChangeDriver: .shared
)
}

Expand All @@ -107,7 +111,8 @@ extension Drivers {
logging: logging,
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: .shared
unsafeStorage: .shared,
profileChangeDriver: .shared
)
}

Expand All @@ -120,7 +125,8 @@ extension Drivers {
logging: .shared,
eventBus: eventBus,
fileSystem: .shared,
unsafeStorage: .shared
unsafeStorage: .shared,
profileChangeDriver: .shared
)
}

Expand All @@ -133,7 +139,8 @@ extension Drivers {
logging: .shared,
eventBus: .shared,
fileSystem: fileSystem,
unsafeStorage: .shared
unsafeStorage: .shared,
profileChangeDriver: .shared
)
}

Expand All @@ -146,7 +153,8 @@ extension Drivers {
logging: .shared,
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: unsafeStorage
unsafeStorage: unsafeStorage,
profileChangeDriver: .shared
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class InsecureStorageDriverTests: DriverTest<Insecure︕!TestOnly︕!Ephemer
func test() async throws {
let sut = SUT.init(keychainService: "test")
let data = Data.sampleAced
let key = SUT.Key.activeProfileId
let key = SUT.Key.profileSnapshot
try await sut.saveData(key: key, data: data)
let loaded = try await sut.loadData(key: key)
XCTAssertEqual(loaded, data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class NetworkingDriverTests: DriverTest<URLSession> {
let sut = SUT.shared as NetworkingDriver
let response = try await sut.executeNetworkRequest(
request: .init(
validating: "https://radixdlt.com",
method: .head
validating: "https://stokenet.radixdlt.com/",
method: .get
)
)
XCTAssertEqual(response.statusCode, 200)
Expand Down
13 changes: 7 additions & 6 deletions apple/Tests/IntegrationTests/SargonOS/TestOSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ extension TestOS {
userDefaults: .init(
suiteName: UUID().uuidString
)!
)
),
profileChangeDriver: .shared
)
)
)
Expand Down Expand Up @@ -85,10 +86,10 @@ final class TestOSTests: OSTest {

func test_if_replace_profile_throws() async throws {
let sut = try await TestOS()
var profile = sut.profile
var profile = sut.os.profile()
profile.header.id = ProfileID() // mutate profile
do {
try await sut.os.saveChangedProfile(profile)
try await sut.os.setProfile(profile: profile)
XCTFail("We expected to throw")
} catch {
/* We expected to throw */
Expand All @@ -97,13 +98,13 @@ final class TestOSTests: OSTest {

func test_we_can_mutate_profile_in_swift_and_save_then_profile_is_updated() async throws {
let sut = try await TestOS()
var profile = sut.profile
var profile = sut.os.profile()
let creatingDevice = profile.header.creatingDevice
let newCreatingDevice = DeviceInfo.sampleOther
XCTAssertNotEqual(newCreatingDevice, creatingDevice)
profile.header.creatingDevice = newCreatingDevice // mutate profile
try await sut.os.saveChangedProfile(profile)
XCTAssertEqual(sut.profile.header.creatingDevice, newCreatingDevice) // assert change worked
try await sut.os.setProfile(profile: profile)
XCTAssertEqual(sut.os.profile().header.creatingDevice, newCreatingDevice) // assert change worked
}

func test_batch_create_many_accounts() async throws {
Expand Down
14 changes: 14 additions & 0 deletions examples/android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.hilt)
alias(libs.plugins.ksp)
}

android {
Expand Down Expand Up @@ -38,6 +40,7 @@ android {
jvmTarget = "1.8"
}
buildFeatures {
buildConfig = true
compose = true
}
composeOptions {
Expand All @@ -56,6 +59,17 @@ dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.okhttp)
implementation(libs.okhttp.logging.interceptor)
implementation(libs.hilt.android)
implementation(libs.androidx.datastore.preferences)
implementation(libs.androidx.lifecycle.runtime.compose)
implementation(libs.androidx.lifecycle.viewmodel)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.datastore.preferences)
implementation(libs.androidx.datastore.preferences)
implementation(libs.androidx.activity)
ksp(libs.hilt.compiler)

implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
Expand Down
3 changes: 2 additions & 1 deletion examples/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".ExampleApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand All @@ -17,9 +18,9 @@
android:exported="true"
android:label="@string/title_activity_main"
android:theme="@style/Theme.SargonAndroid">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.radixdlt.sargon.android

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class ExampleApplication: Application()
Loading

0 comments on commit 5d45750

Please sign in to comment.