2023-03-16 02:11:18 -07:00
|
|
|
//
|
|
|
|
// CombineSDKSynchronizer.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by Michal Fousek on 16.03.2023.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
/// This is a super thin layer that implements the `CombineSynchronizer` protocol and translates the async API defined in `Synchronizer` to
|
|
|
|
/// Combine-based API. And it doesn't do anything else. It doesn't keep any state. It's here so each client can choose the API that suits its case the
|
|
|
|
/// best. And usage of this can be combined with usage of `ClosureSDKSynchronizer`. So devs can really choose the best SDK API for each part of the
|
|
|
|
/// client app.
|
|
|
|
///
|
|
|
|
/// If you are looking for documentation for a specific method or property look for it in the `Synchronizer` protocol.
|
|
|
|
public struct CombineSDKSynchronizer {
|
|
|
|
private let synchronizer: Synchronizer
|
|
|
|
|
|
|
|
public init(synchronizer: Synchronizer) {
|
|
|
|
self.synchronizer = synchronizer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension CombineSDKSynchronizer: CombineSynchronizer {
|
2023-03-22 05:47:32 -07:00
|
|
|
public var alias: ZcashSynchronizerAlias { synchronizer.alias }
|
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
public var latestState: SynchronizerState { synchronizer.latestState }
|
|
|
|
public var connectionState: ConnectionState { synchronizer.connectionState }
|
|
|
|
|
|
|
|
public var stateStream: AnyPublisher<SynchronizerState, Never> { synchronizer.stateStream }
|
|
|
|
public var eventStream: AnyPublisher<SynchronizerEvent, Never> { synchronizer.eventStream }
|
|
|
|
|
|
|
|
public func prepare(
|
|
|
|
with seed: [UInt8]?,
|
|
|
|
viewingKeys: [UnifiedFullViewingKey],
|
|
|
|
walletBirthday: BlockHeight
|
2023-04-03 08:19:40 -07:00
|
|
|
) -> SinglePublisher<Initializer.InitializationResult, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-16 02:11:18 -07:00
|
|
|
return try await self.synchronizer.prepare(with: seed, viewingKeys: viewingKeys, walletBirthday: walletBirthday)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:19:40 -07:00
|
|
|
public func start(retry: Bool) -> CompletablePublisher<Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-16 02:11:18 -07:00
|
|
|
try await self.synchronizer.start(retry: retry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 11:28:24 -07:00
|
|
|
public func stop() {
|
|
|
|
synchronizer.stop()
|
2023-03-16 02:11:18 -07:00
|
|
|
}
|
|
|
|
|
2023-04-03 08:21:10 -07:00
|
|
|
public func getSaplingAddress(accountIndex: Int) -> SinglePublisher<SaplingAddress, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-30 03:49:28 -07:00
|
|
|
try await self.synchronizer.getSaplingAddress(accountIndex: accountIndex)
|
2023-03-16 02:11:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:21:10 -07:00
|
|
|
public func getUnifiedAddress(accountIndex: Int) -> SinglePublisher<UnifiedAddress, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-30 03:49:28 -07:00
|
|
|
try await self.synchronizer.getUnifiedAddress(accountIndex: accountIndex)
|
2023-03-16 02:11:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:21:10 -07:00
|
|
|
public func getTransparentAddress(accountIndex: Int) -> SinglePublisher<TransparentAddress, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-30 03:49:28 -07:00
|
|
|
try await self.synchronizer.getTransparentAddress(accountIndex: accountIndex)
|
2023-03-16 02:11:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func sendToAddress(
|
|
|
|
spendingKey: UnifiedSpendingKey,
|
|
|
|
zatoshi: Zatoshi,
|
|
|
|
toAddress: Recipient,
|
|
|
|
memo: Memo?
|
2023-05-05 10:30:47 -07:00
|
|
|
) -> SinglePublisher<ZcashTransaction.Overview, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-16 02:11:18 -07:00
|
|
|
try await self.synchronizer.sendToAddress(spendingKey: spendingKey, zatoshi: zatoshi, toAddress: toAddress, memo: memo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func shieldFunds(
|
|
|
|
spendingKey: UnifiedSpendingKey,
|
|
|
|
memo: Memo,
|
|
|
|
shieldingThreshold: Zatoshi
|
2023-05-05 10:30:47 -07:00
|
|
|
) -> SinglePublisher<ZcashTransaction.Overview, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-16 02:11:18 -07:00
|
|
|
try await self.synchronizer.shieldFunds(spendingKey: spendingKey, memo: memo, shieldingThreshold: shieldingThreshold)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 10:30:47 -07:00
|
|
|
public var pendingTransactions: AnyPublisher<[ZcashTransaction.Overview], Never> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeAction() {
|
2023-03-27 07:12:06 -07:00
|
|
|
await self.synchronizer.pendingTransactions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 10:30:47 -07:00
|
|
|
public var allTransactions: SinglePublisher<[ZcashTransaction.Overview], Never> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeAction() {
|
2023-05-05 10:30:47 -07:00
|
|
|
await self.synchronizer.transactions
|
2023-03-27 07:12:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 10:30:47 -07:00
|
|
|
public var sentTransactions: SinglePublisher<[ZcashTransaction.Overview], Never> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeAction() {
|
2023-03-27 07:12:06 -07:00
|
|
|
await self.synchronizer.sentTransactions
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 02:11:18 -07:00
|
|
|
|
2023-05-05 10:30:47 -07:00
|
|
|
public var receivedTransactions: SinglePublisher<[ZcashTransaction.Overview], Never> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeAction() {
|
2023-03-27 07:12:06 -07:00
|
|
|
await self.synchronizer.receivedTransactions
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 02:11:18 -07:00
|
|
|
|
|
|
|
public func paginatedTransactions(of kind: TransactionKind) -> PaginatedTransactionRepository { synchronizer.paginatedTransactions(of: kind) }
|
|
|
|
|
2023-04-03 08:19:40 -07:00
|
|
|
public func getMemos(for transaction: ZcashTransaction.Overview) -> SinglePublisher<[Memo], Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-27 07:12:06 -07:00
|
|
|
try await self.synchronizer.getMemos(for: transaction)
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 02:11:18 -07:00
|
|
|
|
2023-04-03 08:19:40 -07:00
|
|
|
public func getRecipients(for transaction: ZcashTransaction.Overview) -> SinglePublisher<[TransactionRecipient], Never> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeAction() {
|
2023-03-27 07:12:06 -07:00
|
|
|
await self.synchronizer.getRecipients(for: transaction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 10:30:47 -07:00
|
|
|
public func allPendingTransactions() -> AnyPublisher<[ZcashTransaction.Overview], Error> {
|
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
|
|
|
try await self.synchronizer.allPendingTransactions()
|
2023-03-27 07:12:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 10:30:47 -07:00
|
|
|
public func allTransactions(from transaction: ZcashTransaction.Overview, limit: Int) -> SinglePublisher<[ZcashTransaction.Overview], Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-05-05 10:30:47 -07:00
|
|
|
try await self.synchronizer.allTransactions(from: transaction, limit: limit)
|
2023-03-27 07:12:06 -07:00
|
|
|
}
|
2023-03-16 02:11:18 -07:00
|
|
|
}
|
|
|
|
|
2023-04-03 08:19:40 -07:00
|
|
|
public func latestHeight() -> SinglePublisher<BlockHeight, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-16 02:11:18 -07:00
|
|
|
try await self.synchronizer.latestHeight()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:19:40 -07:00
|
|
|
public func refreshUTXOs(address: TransparentAddress, from height: BlockHeight) -> SinglePublisher<RefreshedUTXOs, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-16 02:11:18 -07:00
|
|
|
try await self.synchronizer.refreshUTXOs(address: address, from: height)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:19:40 -07:00
|
|
|
public func getTransparentBalance(accountIndex: Int) -> SinglePublisher<WalletBalance, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-16 02:11:18 -07:00
|
|
|
try await self.synchronizer.getTransparentBalance(accountIndex: accountIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:21:10 -07:00
|
|
|
public func getShieldedBalance(accountIndex: Int = 0) -> SinglePublisher<Zatoshi, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-30 03:49:28 -07:00
|
|
|
try await synchronizer.getShieldedBalance(accountIndex: accountIndex)
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 02:11:18 -07:00
|
|
|
|
2023-04-03 08:21:10 -07:00
|
|
|
public func getShieldedVerifiedBalance(accountIndex: Int = 0) -> SinglePublisher<Zatoshi, Error> {
|
2023-03-31 10:10:35 -07:00
|
|
|
AsyncToCombineGateway.executeThrowingAction() {
|
2023-03-30 03:49:28 -07:00
|
|
|
try await synchronizer.getShieldedVerifiedBalance(accountIndex: accountIndex)
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 02:11:18 -07:00
|
|
|
|
2023-04-03 08:19:40 -07:00
|
|
|
public func rewind(_ policy: RewindPolicy) -> CompletablePublisher<Error> { synchronizer.rewind(policy) }
|
|
|
|
public func wipe() -> CompletablePublisher<Error> { synchronizer.wipe() }
|
2023-03-16 02:11:18 -07:00
|
|
|
}
|