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

Rust: SargonOS &Drivers more Profile Logic | Swift: Shared State (working example app)! #131

Merged
merged 141 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
141 commits
Select commit Hold shift + click to select a range
c0cc997
BROKEN WIP
Sajjon May 2, 2024
d23e2c8
BROKEN WIP
Sajjon May 2, 2024
5493e93
WIP
Sajjon May 2, 2024
b9296b5
WIP
Sajjon May 2, 2024
dbf0988
WIP
Sajjon May 2, 2024
b6c60f9
WIP
Sajjon May 3, 2024
8302776
WIP
Sajjon May 3, 2024
d53e213
wip
Sajjon May 3, 2024
e7d6872
nicer boot
CyonAlexRDX May 3, 2024
c17200e
logging driver
CyonAlexRDX May 3, 2024
013d8e8
WIP
Sajjon May 3, 2024
45c216e
WIP
CyonAlexRDX May 3, 2024
0145b86
WIP
Sajjon May 3, 2024
14d9f0a
WIP
Sajjon May 3, 2024
7a6ff10
WIP
Sajjon May 3, 2024
f59d687
WIP
Sajjon May 3, 2024
4dcc68a
WIP
Sajjon May 3, 2024
56b8a7c
WIP
Sajjon May 4, 2024
5d572c0
Merge branch 'main' into ac/system_with_drivers
Sajjon May 4, 2024
a503380
WIP
Sajjon May 4, 2024
7ab28f7
WIP
Sajjon May 4, 2024
4bd1635
WIP
Sajjon May 4, 2024
09c3faf
WIP
Sajjon May 4, 2024
ee7fc6e
WIP
Sajjon May 5, 2024
7f6bf0f
WIP
Sajjon May 5, 2024
eeace21
WIP
Sajjon May 5, 2024
c7138a9
WIP
Sajjon May 5, 2024
ca1a045
WIP
Sajjon May 5, 2024
a5c058f
WIP
Sajjon May 5, 2024
71f982a
WIP
Sajjon May 5, 2024
6a36962
WIP
Sajjon May 5, 2024
836e9ca
WIP
Sajjon May 5, 2024
202edf4
WIP
Sajjon May 5, 2024
a363d0b
WIP
Sajjon May 6, 2024
b7ce86a
WIP
Sajjon May 6, 2024
68548f9
WIP
Sajjon May 6, 2024
a41b65d
WIP
Sajjon May 6, 2024
3a4e390
WIP
Sajjon May 6, 2024
c7b728c
merge and fixes
CyonAlexRDX May 9, 2024
adc4f90
Merge branch 'main' into ac/system_with_drivers
Sajjon May 9, 2024
c202bca
bump version to 1.1.0
Sajjon May 9, 2024
5e7da97
wip rust drivers
CyonAlexRDX May 9, 2024
855fc3a
BROKEN WIP
CyonAlexRDX May 9, 2024
b356ae1
WIP
Sajjon May 9, 2024
8ff484f
WIP
Sajjon May 9, 2024
4c519ff
WIP
Sajjon May 9, 2024
06fa870
WIP
Sajjon May 9, 2024
5e69f2c
sargon os tests
Sajjon May 9, 2024
301ec9a
merge
Sajjon May 9, 2024
f3cede3
fix failing swift test
Sajjon May 9, 2024
f272fba
restructure
Sajjon May 10, 2024
8a66b30
async test util
Sajjon May 10, 2024
5f637f4
make it possible to inject mnemonic into SargonOS. for tests.
Sajjon May 10, 2024
99dc187
more tests
Sajjon May 11, 2024
0d8cc9b
merge
Sajjon May 11, 2024
0277010
more tests
Sajjon May 11, 2024
a59ef1d
more tests
Sajjon May 11, 2024
da3548c
more tests
Sajjon May 11, 2024
8c04c18
make sure timestamp is stable post JSON roundtrip
Sajjon May 12, 2024
e773211
logging
Sajjon May 12, 2024
084d6e3
log
Sajjon May 12, 2024
abb73c1
test rust file system.
Sajjon May 12, 2024
8300417
update account tests
Sajjon May 12, 2024
13b582c
more tests
Sajjon May 12, 2024
9c79f89
more tests
Sajjon May 12, 2024
5701232
WIP more tests
Sajjon May 13, 2024
ee7366f
merge
Sajjon May 13, 2024
017638d
swift tests
Sajjon May 13, 2024
4c75954
cleanup
Sajjon May 13, 2024
14800a1
events
Sajjon May 13, 2024
62dc6a8
Merge branch 'main' into ac/system_with_drivers
Sajjon May 13, 2024
062eac3
more tests
Sajjon May 13, 2024
eef6c72
more tests (factors)
Sajjon May 13, 2024
895ec12
fix swift tests
Sajjon May 13, 2024
a10cb29
more tests
Sajjon May 14, 2024
d70c1a7
claim profile
Sajjon May 14, 2024
130964b
use device_info from host
Sajjon May 14, 2024
ea14243
bump tarpaulin to 0.30.0 in CI (also done locally)
Sajjon May 14, 2024
1dd3bcd
kotlin
Sajjon May 14, 2024
a1219de
error code
CyonAlexRDX May 14, 2024
181b97e
kotlin
CyonAlexRDX May 14, 2024
161b7c6
Merge branch 'main' into ac/system_with_drivers
CyonAlexRDX May 14, 2024
f584c9c
fix kotlin? fix build issue with cast (discriminant of CommonError).
Sajjon May 14, 2024
3c80613
UniFFI export drivers
Sajjon May 14, 2024
0bf4987
remove test
Sajjon May 14, 2024
7c36650
more tests
Sajjon May 14, 2024
80957c9
more tests
Sajjon May 14, 2024
b523b05
more tests
Sajjon May 14, 2024
0ab29d1
remove shared state
Sajjon May 15, 2024
6c6f116
more tests
Sajjon May 15, 2024
9581a7e
WIP
Sajjon May 15, 2024
7d186ba
WIP
Sajjon May 15, 2024
4118bfe
WIP
Sajjon May 15, 2024
d381b93
merge
Sajjon May 15, 2024
b197e2a
wip
Sajjon May 16, 2024
419f143
IMPORTANT Fix broken Rust security-framework bug see https://github.c…
Sajjon May 16, 2024
9e269c7
WIP
Sajjon May 16, 2024
2625153
WIP
Sajjon May 16, 2024
2b4c346
WIP changeGW
CyonAlexRDX May 16, 2024
30f8a85
merge
Sajjon May 16, 2024
6afaf58
fix
Sajjon May 16, 2024
07ec7ed
fixes
Sajjon May 16, 2024
60ae29a
Profile is now created with empty mainnet network
CyonAlexRDX May 16, 2024
129cf57
more tests
Sajjon May 16, 2024
f90c571
tests
Sajjon May 16, 2024
e67c6d2
Add Vendor to DeviceInfo and HostInfoDriver
Sajjon May 16, 2024
b7c1ca5
more tests
Sajjon May 16, 2024
0fd46ef
fix kotlin test
Sajjon May 16, 2024
170677b
emit event when account is updated
Sajjon May 16, 2024
dda3a50
fixes
Sajjon May 17, 2024
48934a5
Ensure SharedReader in swift is created once per value kind, and then…
Sajjon May 17, 2024
4d890d5
add host_vendor to DeviceFactorSourceHitn
CyonAlexRDX May 17, 2024
f8701e4
more tests
CyonAlexRDX May 17, 2024
7aee200
add attention
CyonAlexRDX May 17, 2024
a8dff03
Change designated init of Profile to accept a Mnemonic and DeviceInfo…
Sajjon May 18, 2024
2055fe2
fix device_info_description for old Swift format. Still need to add A…
Sajjon May 20, 2024
c45b015
address some PR comments.
Sajjon May 23, 2024
54eda11
merge
Sajjon May 24, 2024
ec50125
bump to 1.1.0
Sajjon May 24, 2024
526e052
WIP event kind query methods shared between iOS and Android to check …
Sajjon May 24, 2024
274ae2d
merge
CyonAlexRDX May 24, 2024
9789e8c
Add methods to check if a certain EventKind affects certain state, e.…
Sajjon May 25, 2024
4da9c46
Swift: Changing singleton instance creation of BIOS and SargonOS and …
Sajjon May 25, 2024
4cfd24a
fixes
Sajjon May 25, 2024
9bdc364
Clients no longer hold reference to Drivers, since each client respec…
Sajjon May 26, 2024
751f073
cleanup
Sajjon May 26, 2024
50701c6
fixes
Sajjon May 26, 2024
78e1017
documentation
Sajjon May 26, 2024
1847ba9
fix kotlin
Sajjon May 26, 2024
75e7c7a
fixes
Sajjon May 26, 2024
2bfa430
fixes
Sajjon May 26, 2024
d91b83e
documentation and cleanup of SharedState
Sajjon May 26, 2024
40792b5
more tests
Sajjon May 27, 2024
d79e565
add missing asserts to test
CyonAlexRDX May 27, 2024
436c2bc
fixes
CyonAlexRDX May 27, 2024
f47dc2c
rename events
CyonAlexRDX May 27, 2024
ddbc911
fix swift
CyonAlexRDX May 27, 2024
b3d19c7
merge main
Sajjon Jun 28, 2024
dd5514a
fix swift, try fix kotlin tests
CyonAlexRDX Jun 28, 2024
9297cb7
try fix kotlin
CyonAlexRDX Jun 28, 2024
2ce371b
fix kotlin
CyonAlexRDX Jun 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0.2"
version = "1.1.0"
edition = "2021"
build = "build.rs"

Expand Down
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ let package = Package(
.target(name: "Sargon"),
.product(name: "CustomDump", package: "swift-custom-dump"),
],
path: "apple/Tests",
swiftSettings: swiftSettings
path: "apple/Tests"
),
]
)
6 changes: 6 additions & 0 deletions apple/Sources/Sargon/Drivers/EventBus/EventBus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ public final actor EventBus {
/// A stream we multicast on.
private let stream = AsyncThrowingPassthroughSubject<Element, any Error>()
private let subject: Subject
#if DEBUG
public init() {
subject = .init()
}
#else
private init() {
subject = .init()
}
#endif
}

extension EventBus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension EventProfileModified {
extension Event {
public var profileModified: EventProfileModified? {
switch self {
case let .modifiedProfile(change): return change
case let .profileModified(change): return change
default: return nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
) throws -> T {
let url = URL(file: path)
guard url.startAccessingSecurityScopedResource() else {
// FIXME CYON change to specific error
throw CommonError.Unknown
throw CommonError.NotPermissionToAccessFile(path: path)

Check warning on line 48 in apple/Sources/Sargon/Drivers/FileSystem/FileSystemDriver+Data+ContentsOf+URL.swift

View check run for this annotation

Codecov / codecov/patch

apple/Sources/Sargon/Drivers/FileSystem/FileSystemDriver+Data+ContentsOf+URL.swift#L48

Added line #L48 was not covered by tests
}
defer { url.stopAccessingSecurityScopedResource() }
return try io(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import Foundation
import SargonUniFFI

#if DEBUG
extension SecureStorageDriver where Self == Insecure︕!TestOnly︕!Ephemeral︕!SecureStorage {
public init(keychainService: String) {
self.init(keychainService: keychainService)
}
}

/// ‼️ NEVER USE IN PRODUCTION ‼️
/// An INSECURE ephemeral storage conforming to `SecureStorageDriver` meant
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-05-24.
//

import Foundation
import SargonUniFFI

extension EventKind {
public static let allCases: [Self] = eventKindAll()

public var affectsCurrentAccounts: Bool {
eventKindAffectsCurrentAccounts(eventKind: self)
}

public var affectsCurrentNetwork: Bool {
eventKindAffectsCurrentNetwork(eventKind: self)
}

public var affectsSavedGateways: Bool {
eventKindAffectsSavedGateways(eventKind: self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,6 @@ import SargonUniFFI
public typealias BIOS = Bios
extension BIOS: @unchecked Sendable {}


extension Drivers {
public convenience init(
bundle: Bundle,
userDefaultsSuite: String,
secureStorageDriver: SecureStorageDriver
) {
self.init(
appVersion: (bundle.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "Unknown",
userDefaultsSuite: userDefaultsSuite,
secureStorageDriver: secureStorageDriver
)
}

public convenience init(
appVersion: String,
userDefaultsSuite: String,
secureStorageDriver: SecureStorageDriver
) {
self.init(
secureStorage: secureStorageDriver,
hostInfo: HostInfo(appVersion: appVersion),
unsafeStorage: UnsafeStorage(
userDefaults: .init(suiteName: userDefaultsSuite)!
)
)
}
}

extension BIOS {

public convenience init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,36 @@ import SargonUniFFI


extension Drivers: @unchecked Sendable {}


extension Drivers {
public convenience init(
bundle: Bundle,
userDefaultsSuite: String,
secureStorageDriver: SecureStorageDriver
) {
self.init(
appVersion: (bundle.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "Unknown",
userDefaultsSuite: userDefaultsSuite,
secureStorageDriver: secureStorageDriver
)
}

public convenience init(
appVersion: String,
userDefaultsSuite: String,
secureStorageDriver: SecureStorageDriver
) {
self.init(
secureStorage: secureStorageDriver,
hostInfo: HostInfo(appVersion: appVersion),
unsafeStorage: UnsafeStorage(
userDefaults: .init(suiteName: userDefaultsSuite)!
)
)
}
}

extension Drivers {

public convenience init(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-05-24.
//

import Foundation
import SargonUniFFI

extension EventKind: SargonModel {
public static let sample: Self = .accountAdded
public static let sampleOther: Self = .accountUpdated
}
extension EventKind: CaseIterable {}

32 changes: 20 additions & 12 deletions apple/Sources/Sargon/SargonOS/BIOS+Static+Shared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,42 @@
import SargonUniFFI

extension BIOS {
private nonisolated(unsafe) static var _shared: BIOS!

public nonisolated(unsafe) static var shared: BIOS {
guard let shared = Self._shared else {
fatalError("BIOS not created, create it with `BIOS.createShared:bundle:keychainService:userDefaultsSuite`")
fatalError("BIOS not created, create it with `BIOS.creatingShared:drivers`")

Check warning on line 15 in apple/Sources/Sargon/SargonOS/BIOS+Static+Shared.swift

View check run for this annotation

Codecov / codecov/patch

apple/Sources/Sargon/SargonOS/BIOS+Static+Shared.swift#L15

Added line #L15 was not covered by tests
}
return shared
}

/// Can be access later with `OS.shared`
@discardableResult
public static func createdShared(
bundle: Bundle,
userDefaultsSuite: String,
secureStorageDriver: SecureStorageDriver
public static func creatingShared(
drivers: Drivers
) -> BIOS {
Self._creatingShared(drivers: drivers, isEmulatingFreshInstall: false)
}

}

extension BIOS {
private nonisolated(unsafe) static var _shared: BIOS!

/// Can be access later with `OS.shared`
@discardableResult
public static func _creatingShared(
drivers: Drivers,
isEmulatingFreshInstall: Bool
) -> BIOS {
Self.settingShared(
shared: BIOS(
bundle: bundle,
userDefaultsSuite: userDefaultsSuite,
secureStorageDriver: secureStorageDriver
)
shared: BIOS(drivers: drivers),
isEmulatingFreshInstall: isEmulatingFreshInstall
)
}

/// Can be access later with `OS.shared`
@discardableResult
public static func settingShared(
internal static func settingShared(
shared: BIOS,
isEmulatingFreshInstall: Bool = false
) -> BIOS {
Expand Down
28 changes: 23 additions & 5 deletions apple/Sources/Sargon/SargonOS/SargonOS+Static+Shared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,34 @@
}

extension SargonOS {
internal nonisolated(unsafe) static var _shared: SargonOS!

public nonisolated(unsafe) static var shared: SargonOS {
guard let shared = Self._shared else {
fatalError("OS not created, create it with `SargonOS.createdSharedBootingWith:bios`")
fatalError("`SargonOS.shared` not created, create it with `SargonOS.creatingShared:bootingWith` and pass it a `BIOS`.")

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

View check run for this annotation

Codecov / codecov/patch

apple/Sources/Sargon/SargonOS/SargonOS+Static+Shared.swift#L21

Added line #L21 was not covered by tests
}
return shared
}

/// Can be access later with `OS.shared`
@discardableResult
public static func createdSharedBootingWith(
bios: BIOS,
isEmulatingFreshInstall: Bool = false
public static func creatingShared(
bootingWith bios: BIOS
) async throws -> SargonOS {
try await _creatingShared(
bootingWith: bios,
isEmulatingFreshInstall: false
)
}

}

extension SargonOS {

/// Can be access later with `OS.shared`
@discardableResult
internal static func _creatingShared(
bootingWith bios: BIOS,
isEmulatingFreshInstall: Bool
) async throws -> SargonOS {
if !isEmulatingFreshInstall, _shared != nil {
throw SargonOSAlreadyBooted()
Expand All @@ -37,4 +51,8 @@
Self._shared = shared
return shared
}

internal nonisolated(unsafe) static var _shared: SargonOS!

}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class EventBusDriverTests: DriverTest<EventBus> {

func test() async throws {
let sut = SUT()


let task = Task {
var notifications = Set<EventNotification>()
Expand All @@ -22,7 +21,7 @@ class EventBusDriverTests: DriverTest<EventBus> {
let os = try await TestOS(bios: bios)
try await os.createAccount()
let notifications = await task.value
XCTAssertEqual(Set(notifications.map(\.event.kind)), Set([.booted, .profileSaved, .addedAccount]))
XCTAssertEqual(Set(notifications.map(\.event.kind)), Set([.booted, .profileSaved, .accountAdded]))
}

}
Expand Down Expand Up @@ -72,7 +71,7 @@ extension Drivers {
)
}

static func withSecureStorage(_ entropyProvider: some EntropyProviderDriver) -> Drivers {
static func withEntropyProvider(_ entropyProvider: some EntropyProviderDriver) -> Drivers {
Drivers(
networking: .shared,
secureStorage: .shared,
Expand Down Expand Up @@ -124,7 +123,7 @@ extension Drivers {
)
}

static func withEventBus(_ fileSystem: some FileSystemDriver) -> Drivers {
static func withFileSystem(_ fileSystem: some FileSystemDriver) -> Drivers {
Drivers(
networking: .shared,
secureStorage: .shared,
Expand All @@ -137,7 +136,7 @@ extension Drivers {
)
}

static func withEventBus(_ unsafeStorage: some UnsafeStorageDriver) -> Drivers {
static func withUnsafeStorage(_ unsafeStorage: some UnsafeStorageDriver) -> Drivers {
Drivers(
networking: .shared,
secureStorage: .shared,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class FileSystemDriverTests: DriverTest<FileSystem> {
try await sut.saveToFile(path: path, data: data)
let loaded = try await sut.loadFromFile(path: path)
XCTAssertEqual(loaded, data)
try await sut.deleteFile(path: path)

XCTAssertTrue(FileManager.default.fileExists(atPath: path))
try await sut.deleteFile(path: path)
XCTAssertFalse(FileManager.default.fileExists(atPath: path))
}
}
26 changes: 23 additions & 3 deletions apple/Tests/IntegrationTests/SargonOS/BIOSTests.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
import CustomDump
import Foundation
import Sargon
@testable import Sargon
import SargonUniFFI
import XCTest

extension BIOS {
static func creatingShared(
bundle: Bundle = .main,
userDefaultsSuite: String = "test",
secureStorageDriver: SecureStorageDriver = Insecure︕!TestOnly︕!Ephemeral︕!SecureStorage.init(
keychainService: "test"
)
) -> BIOS {
Self.creatingShared(drivers: .init(bundle: bundle, userDefaultsSuite: userDefaultsSuite, secureStorageDriver: secureStorageDriver))
}
}

final class BIOSTests: OSTest {
typealias SUT = BIOS

func test_set_shared() {
let sut = SUT.createdShared(bundle: .main, userDefaultsSuite: "test", secureStorageDriver: Insecure︕!TestOnly︕!Ephemeral︕!SecureStorage(keychainService: "test"))
let sut = SUT.creatingShared()

XCTAssertTrue(SUT.shared === sut)
let new = SUT.settingShared(shared: .test(secureStorageDriver: Insecure︕!TestOnly︕!Ephemeral︕!SecureStorage.init(keychainService: "other")), isEmulatingFreshInstall: true)
let new = SUT.settingShared(
shared: .test(
secureStorageDriver: Insecure︕!TestOnly︕!Ephemeral︕!SecureStorage.init(
keychainService: "other"
)
),
isEmulatingFreshInstall: true
)

XCTAssertFalse(sut === new)
XCTAssertTrue(SUT.shared === new)
Expand Down
Loading
Loading