Skip to content

Commit

Permalink
Merge branch 'master' into swift-wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Nov 17, 2023
2 parents 3bbaf98 + eaaefca commit 92978fa
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 21 deletions.
12 changes: 6 additions & 6 deletions Examples/Swift/Extension/ActionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import TesseractService
class ActionViewController: UIViewController, TestSigningServiceDelegate {
@IBOutlet weak var textView: UILabel!

var continuation: UnsafeContinuation<Result<Bool, TesseractError>, Never>?
var continuation: UnsafeContinuation<Bool, Error>?

var tesseract: Tesseract!

Expand All @@ -28,25 +28,25 @@ class ActionViewController: UIViewController, TestSigningServiceDelegate {
}

@MainActor
func acceptTx(tx: String) async -> Result<Bool, TesseractError> {
return await withUnsafeContinuation { cont in
func acceptTx(tx: String) async throws -> Bool {
try await withUnsafeThrowingContinuation { cont in
self.continuation = cont
self.textView.text = tx
}
}

@IBAction func allow() {
self.continuation?.resume(returning: .success(true))
self.continuation?.resume(returning: true)
self.continuation = nil
}

@IBAction func reject() {
self.continuation?.resume(returning: .success(false))
self.continuation?.resume(returning: false)
self.continuation = nil
}

@IBAction func cancel() {
self.continuation?.resume(returning: .failure(.cancelled))
self.continuation?.resume(throwing: TesseractError.cancelled)
self.continuation = nil
}
}
11 changes: 6 additions & 5 deletions Examples/Swift/Extension/TestSigningService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import TesseractService

protocol TestSigningServiceDelegate: AnyObject {
func acceptTx(tx: String) async -> Result<Bool, TesseractError>
func acceptTx(tx: String) async throws -> Bool
}

class TestSigningService: TestService {
Expand All @@ -21,12 +21,13 @@ class TestSigningService: TestService {
self.signature = signature
}

func signTransation(req: String) async -> Result<String, TesseractError> {
func signTransation(req: String) async throws -> String {
guard let delegate = self.delegate else {
return .failure(.null(reason: "TestSigningService delegate is empty"))
throw TesseractError.null(TestSigningService.self)
}
return await delegate.acceptTx(tx: req).flatMap {
$0 ? .success(req + self.signature) : .failure(.cancelled)
guard try await delegate.acceptTx(tx: req) else {
throw TesseractError.cancelled
}
return req + signature
}
}
10 changes: 6 additions & 4 deletions Sources/TesseractService/SubstrateService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ extension CTesseract.SubstrateService: CoreService {
}
}

public protocol SubstrateService: TesseractShared.SubstrateService, Service
public protocol SubstrateServiceResult: TesseractShared.SubstrateServiceResult, Service
where Core == CTesseract.SubstrateService {}

public extension SubstrateService {
public protocol SubstrateService: SubstrateServiceResult, TesseractShared.SubstrateService {}

public extension SubstrateServiceResult {
func toCore() -> Core {
var value = Core(value: self)
value.get_account = substrate_service_get_account
Expand All @@ -37,7 +39,7 @@ private func substrate_service_get_account(
accountType: CTesseract.SubstrateAccountType
) -> CFuture_SubstrateGetAccountResponse {
CFuture_SubstrateGetAccountResponse {
await this.unowned((any SubstrateService).self).castError().asyncFlatMap {
await this.unowned((any SubstrateServiceResult).self).castError().asyncFlatMap {
await $0.getAccount(
type: TesseractShared.SubstrateAccountType(cvalue: accountType)
)
Expand All @@ -55,7 +57,7 @@ private func substrate_service_sign(
let metadata = metadata.copied()
let types = types.copied()
return CFutureData {
await this.unowned((any SubstrateService).self).castError().asyncFlatMap {
await this.unowned((any SubstrateServiceResult).self).castError().asyncFlatMap {
await $0.signTransation(type: TesseractShared.SubstrateAccountType(cvalue: type),
path: path, extrinsic: extrinsic,
metadata: metadata, types: types)
Expand Down
6 changes: 4 additions & 2 deletions Sources/TesseractService/TestService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ extension CTesseract.TestService: CoreService {
}
}

public protocol TestService: TesseractShared.TestService, Service
public protocol TestServiceResult: TesseractShared.TestServiceResult, Service
where Core == CTesseract.TestService {}

public protocol TestService: TestServiceResult, TesseractShared.TestService {}

public extension TestService {
func toCore() -> Core {
var value = Core(value: self)
Expand All @@ -33,7 +35,7 @@ private func test_service_sign(this: UnsafePointer<CTesseract.TestService>!,
{
let req = req.copied()
return CFutureString {
await this.unowned((any TestService).self).castError().asyncFlatMap {
await this.unowned((any TestServiceResult).self).castError().asyncFlatMap {
await $0.signTransation(req: req)
}
}
Expand Down
33 changes: 32 additions & 1 deletion Sources/TesseractShared/SubstrateService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct SubstrateGetAccountResponse {
}
}

public protocol SubstrateService {
public protocol SubstrateServiceResult {
func getAccount(
type: SubstrateAccountType
) async -> Result<SubstrateGetAccountResponse, TesseractError>
Expand All @@ -38,6 +38,37 @@ public protocol SubstrateService {
) async -> Result<Data, TesseractError>
}

public protocol SubstrateService: SubstrateServiceResult {
func getAccount(
type: SubstrateAccountType
) async throws -> SubstrateGetAccountResponse

func signTransation(
type: SubstrateAccountType, path: String,
extrinsic: Data, metadata: Data, types: Data
) async throws -> Data
}

public extension SubstrateService {
func getAccount(
type: SubstrateAccountType
) async -> Result<SubstrateGetAccountResponse, TesseractError> {
await Result { try await getAccount(type: type) }
}

func signTransation(
type: SubstrateAccountType, path: String,
extrinsic: Data, metadata: Data, types: Data
) async -> Result<Data, TesseractError> {
await Result {
try await signTransation(type: type, path: path,
extrinsic: extrinsic,
metadata: metadata,
types: types)
}
}
}

extension CTesseract.SubstrateAccountType: CType {
public init() { self.init(0) }
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/TesseractShared/TestService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import Foundation
import TesseractTransportsShared
#endif

public protocol TestService {
public protocol TestServiceResult {
func signTransation(req: String) async -> Result<String, TesseractError>
}

public protocol TestService: TestServiceResult {
func signTransation(req: String) async throws -> String
}

public extension TestService {
func signTransation(req: String) async -> Result<String, TesseractError> {
await Result { try await signTransation(req: req) }
}
}
28 changes: 26 additions & 2 deletions Sources/TesseractTransportsShared/TesseractError+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public extension Result where Failure: TesseractErrorConvertible {
}

public extension Result where Failure == TesseractError {
init(tesseract fn: () async throws -> Success) async {
do {
self = await .success(try fn())
} catch let err as TesseractErrorConvertible {
self = .failure(err.tesseract)
} catch let err as CErrorConvertible {
self = .failure(TesseractError(cError: err.cError))
} catch {
self = .failure(TesseractError(parsing: error as NSError))
}
}

func castError<E: TesseractErrorInitializable>() -> Result<Success, E> {
mapError { E(tesseract: $0) }
}
Expand All @@ -36,8 +48,20 @@ public extension Result where Failure == TesseractError {
}

public extension TesseractError {
init(parsing error: NSError) {
self.init(cError: InteropError(parsing: error))
@inlinable init(parsing error: NSError) {
self.init(cError: .init(parsing: error))
}

@inlinable static func null<T>(_ type: T.Type) -> Self {
.init(cError: .null(type))
}

@inlinable static func panic(reason: String) -> Self {
.init(cError: .panic(reason: reason))
}

@inlinable static func cast<F, T>(from: F.Type, to: T.Type) -> Self {
.init(cError: .cast(from: from, to: to))
}
}

Expand Down

0 comments on commit 92978fa

Please sign in to comment.