Skip to content

Commit

Permalink
Sargon os v1 bootstrap (#197)
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'

* 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

* Change profile holder to keep profile state instead of profile

* Driver emits profile states instead of profile

* Implement new and delete wallet functions

* Remove unused comment

* Implement import functionality

* Propagate CRUD operations on profile to the profile state change driver.

* Remove unnecessary comment

* Fix swift tests

* Fix doc

* Prioritize bdfs save and then save profile in new wallet

* Implement retry read mechanism for profile snapshot.

This currently exists on Android codebase and needs revisiting.

* Bump version

---------

Co-authored-by: Alexander Cyon <[email protected]>
Co-authored-by: Ghenadie Vasiliev-Pusca <[email protected]>
  • Loading branch information
3 people authored Aug 14, 2024
1 parent 5d45750 commit ecd2466
Show file tree
Hide file tree
Showing 40 changed files with 1,309 additions and 735 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.3"
version = "1.1.4"
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 HostOs.ios(version: UIDevice.current.systemVersion)
return HostOs.ios(version: UIDevice.current.systemVersion)
}

nonisolated public func hostDeviceName() async -> String {
Expand Down
17 changes: 0 additions & 17 deletions apple/Sources/Sargon/Drivers/ProfileChange/ProfileChange.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import SargonUniFFI

public typealias ProfileStateChangeEventPublisher = EventPublisher<ProfileState>

extension ProfileStateChangeEventPublisher: ProfileStateChangeDriver {
public static let shared = ProfileStateChangeEventPublisher()

public func handleProfileStateChange(changedProfileState: ProfileState) async {
subject.send(changedProfileState)
}
}

extension ProfileStateChangeDriver where Self == ProfileStateChangeEventPublisher {
public static var shared: Self {
ProfileStateChangeEventPublisher.shared
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extension Drivers {
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: unsafeStorage,
profileChangeDriver: .shared
profileStateChangeDriver: .shared
)
}
}
16 changes: 12 additions & 4 deletions apple/Sources/Sargon/SargonOS/SargonOSProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,28 @@ extension SargonOSProtocol {
// MARK: Extensions
extension SargonOSProtocol {
public var currentNetworkID: NetworkID {
os.currentNetworkId()
get throws {
try os.currentNetworkId()
}
}

public var gateways: SavedGateways {
os.gateways()
get throws {
try os.gateways()
}
}

@available(*, deprecated, message: "Consider using faster `accountsForDisplayOnCurrentNetwork` and follow up with ")
public var accountsOnCurrentNetwork: [Account] {
os.accountsOnCurrentNetwork()
get throws {
try os.accountsOnCurrentNetwork()
}
}

public var accountsForDisplayOnCurrentNetwork: [AccountForDisplay] {
os.accountsForDisplayOnCurrentNetwork()
get throws {
try os.accountsForDisplayOnCurrentNetwork()
}
}

public func accountByAddress(_ address: AccountAddress) throws -> Account {
Expand Down
8 changes: 4 additions & 4 deletions apple/Sources/Sargon/SargonOS/TestOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public final class TestOS {
self.os = os
}

public convenience init(bios: BIOS) async throws {
let os = try await SargonOS.boot(bios: bios)
public convenience init(bios: BIOS) async {
let os = await SargonOS.boot(bios: bios)
self.init(os: os)
}

Expand All @@ -42,8 +42,8 @@ extension TestOS: SargonOSProtocol {}

// MARK: Private
extension TestOS {
private func nextAccountName() -> DisplayName {
let index = accountsForDisplayOnCurrentNetwork.count
private func nextAccountName() throws -> DisplayName {
let index = try accountsForDisplayOnCurrentNetwork.count
return DisplayName(value: "Unnamed \(index)")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class EventBusDriverTests: DriverTest<EventBus> {
}

let bios = BIOS(drivers: .withEventBus(sut))
let os = try await TestOS(bios: bios)
let os = await TestOS(bios: bios)
try await os.os.newWallet()

try await os.createAccount()
let notifications = await task.value
XCTAssertEqual(Set(notifications.map(\.event.kind)), Set(expectedEvents))
Expand Down Expand Up @@ -55,7 +57,7 @@ extension Drivers {
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: .shared,
profileChangeDriver: .shared
profileStateChangeDriver: .shared
)
}

Expand All @@ -69,7 +71,7 @@ extension Drivers {
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: .shared,
profileChangeDriver: .shared
profileStateChangeDriver: .shared

)
}
Expand All @@ -84,7 +86,7 @@ extension Drivers {
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: .shared,
profileChangeDriver: .shared
profileStateChangeDriver: .shared
)
}

Expand All @@ -98,7 +100,7 @@ extension Drivers {
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: .shared,
profileChangeDriver: .shared
profileStateChangeDriver: .shared
)
}

Expand All @@ -112,7 +114,7 @@ extension Drivers {
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: .shared,
profileChangeDriver: .shared
profileStateChangeDriver: .shared
)
}

Expand All @@ -126,7 +128,7 @@ extension Drivers {
eventBus: eventBus,
fileSystem: .shared,
unsafeStorage: .shared,
profileChangeDriver: .shared
profileStateChangeDriver: .shared
)
}

Expand All @@ -140,7 +142,7 @@ extension Drivers {
eventBus: .shared,
fileSystem: fileSystem,
unsafeStorage: .shared,
profileChangeDriver: .shared
profileStateChangeDriver: .shared
)
}

Expand All @@ -154,7 +156,7 @@ extension Drivers {
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: unsafeStorage,
profileChangeDriver: .shared
profileStateChangeDriver: .shared
)
}
}
Expand Down
48 changes: 28 additions & 20 deletions apple/Tests/IntegrationTests/SargonOS/TestOSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import XCTest

extension TestOS {

public convenience init() async throws {
try await self.init(
public convenience init() async {
await self.init(
bios: BIOS(
drivers: Drivers(
networking: .shared,
Expand All @@ -24,7 +24,7 @@ extension TestOS {
suiteName: UUID().uuidString
)!
),
profileChangeDriver: .shared
profileStateChangeDriver: .shared
)
)
)
Expand All @@ -36,7 +36,8 @@ final class TestOSTests: OSTest {
func test_create_single_account_many_times() async throws {
let bus = EventBus()
let drivers = Drivers.withEventBus(bus)
let sut = try await TestOS(bios: .init(drivers: drivers))
let sut = await TestOS(bios: .init(drivers: drivers))
try await sut.os.newWallet()
let n = 3

let task = Task {
Expand All @@ -53,40 +54,45 @@ final class TestOSTests: OSTest {
.createAccount()

let events = await task.value
XCTAssertEqual(sut.accountsForDisplayOnCurrentNetwork.map(\.displayName), ["Unnamed 0", "Foo", "Unnamed 2"])
XCTAssertEqual(sut.accountsForDisplayOnCurrentNetwork.map(\.address), events.compactMap(\.addressOfNewAccount))
XCTAssertEqual(try sut.accountsForDisplayOnCurrentNetwork.map(\.displayName), ["Unnamed 0", "Foo", "Unnamed 2"])
XCTAssertEqual(try sut.accountsForDisplayOnCurrentNetwork.map(\.address), events.compactMap(\.addressOfNewAccount))
}

func test_create_account_returned() async throws {
let sut = try await TestOS()
let sut = await TestOS()
try await sut.os.newWallet()
let displayName: DisplayName = "New"
let account = try await sut.createAccount(named: displayName)
XCTAssertEqual(account.displayName, displayName)
XCTAssertEqual(sut.accountsForDisplayOnCurrentNetwork, [AccountForDisplay(account)])
XCTAssertEqual(try sut.accountsForDisplayOnCurrentNetwork, [AccountForDisplay(account)])
}

func test_create_account_returned_can_be_looked_up() async throws {
let sut = try await TestOS()
let sut = await TestOS()
try await sut.os.newWallet()
let displayName: DisplayName = "New"
let account = try await sut.createAccount(named: displayName)
let lookedUp = try sut.accountByAddress(account.address)
XCTAssertEqual(lookedUp, account)
}

func test_lookup_throws_for_unknown_accounts() async throws {
let sut = try await TestOS()
let sut = await TestOS()
try await sut.os.newWallet()
XCTAssertThrowsError(try sut.accountByAddress(.sample))
}

func test_new_profile_has_preset_gateways() async throws {
let sut = try await TestOS()
let gateways = sut.gateways
let sut = await TestOS()
try await sut.os.newWallet()
let gateways = try sut.gateways
XCTAssertEqual(gateways, .preset)
}

func test_if_replace_profile_throws() async throws {
let sut = try await TestOS()
var profile = sut.os.profile()
let sut = await TestOS()
try await sut.os.newWallet()
var profile = try sut.os.profile()
profile.header.id = ProfileID() // mutate profile
do {
try await sut.os.setProfile(profile: profile)
Expand All @@ -97,25 +103,27 @@ 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.os.profile()
let sut = await TestOS()
try await sut.os.newWallet()
var profile = try 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.setProfile(profile: profile)
XCTAssertEqual(sut.os.profile().header.creatingDevice, newCreatingDevice) // assert change worked
XCTAssertEqual(try sut.os.profile().header.creatingDevice, newCreatingDevice) // assert change worked
}

func test_batch_create_many_accounts() async throws {
let sut = try await TestOS()
let sut = await TestOS()
try await sut.os.newWallet()
let n: UInt16 = 4
try await sut.batchCreateAccounts(count: n, namePrefix: "Unnamed")
XCTAssertEqual(sut.accountsOnCurrentNetwork.map(\.displayName.value), (0..<n).map { "Unnamed \($0)" })
XCTAssertEqual(try sut.accountsOnCurrentNetwork.map(\.displayName.value), (0..<n).map { "Unnamed \($0)" })
}

func test_log_at_each_level() async throws {
let _ = try await TestOS()
let _ = await TestOS()
logSystemDiagnosis()
}
}
1 change: 0 additions & 1 deletion examples/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/title_activity_main"
android:theme="@style/Theme.SargonAndroid">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Loading

0 comments on commit ecd2466

Please sign in to comment.