From 46d8c04afc83eacfa8fd53a302ba6956b9ca0ad0 Mon Sep 17 00:00:00 2001 From: Lukas Korba Date: Mon, 3 Oct 2022 12:44:33 +0200 Subject: [PATCH] [#546] TransactionEncoder To Async/Await (#547) - async/await API update - code cleaned up from the unused APIs --- .../PersistentTransactionManager.swift | 4 +- .../Transaction/TransactionEncoder.swift | 50 +------------------ .../WalletTransactionEncoder.swift | 47 +---------------- 3 files changed, 6 insertions(+), 95 deletions(-) diff --git a/Sources/ZcashLightClientKit/Transaction/PersistentTransactionManager.swift b/Sources/ZcashLightClientKit/Transaction/PersistentTransactionManager.swift index b1e5fdab..8cae2b8a 100644 --- a/Sources/ZcashLightClientKit/Transaction/PersistentTransactionManager.swift +++ b/Sources/ZcashLightClientKit/Transaction/PersistentTransactionManager.swift @@ -96,7 +96,7 @@ class PersistentTransactionManager: OutboundTransactionManager { } do { - let encodedTransaction = try self.encoder.createShieldingTransaction( + let encodedTransaction = try await self.encoder.createShieldingTransaction( spendingKey: spendingKey, tSecretKey: tsk, memo: pendingTransaction.memo?.asZcashTransactionMemo(), @@ -126,7 +126,7 @@ class PersistentTransactionManager: OutboundTransactionManager { pendingTransaction: PendingTransactionEntity ) async throws -> PendingTransactionEntity { do { - let encodedTransaction = try self.encoder.createTransaction( + let encodedTransaction = try await self.encoder.createTransaction( spendingKey: spendingKey, zatoshi: pendingTransaction.intValue, to: pendingTransaction.toAddress, diff --git a/Sources/ZcashLightClientKit/Transaction/TransactionEncoder.swift b/Sources/ZcashLightClientKit/Transaction/TransactionEncoder.swift index f8fcdc80..f6d1dc91 100644 --- a/Sources/ZcashLightClientKit/Transaction/TransactionEncoder.swift +++ b/Sources/ZcashLightClientKit/Transaction/TransactionEncoder.swift @@ -39,31 +39,7 @@ protocol TransactionEncoder { to address: String, memo: String?, from accountIndex: Int - ) throws -> EncodedTransaction - - /** - Creates a transaction, throwing an exception whenever things are missing. When the provided wallet implementation - doesn't throw an exception, we wrap the issue into a descriptive exception ourselves (rather than using - double-bangs for things). - Non-blocking - - - Parameters: - - Parameter spendingKey: a string containing the spending key - - Parameter zatoshi: the amount to send in zatoshis - - Parameter to: string containing the recipient address - - Parameter memo: string containing the memo (optional) - - Parameter accountIndex: index of the account that will be used to send the funds - - Parameter result: a non escaping closure that receives a Result containing either an EncodedTransaction or a TransactionEncoderError - */ - // swiftlint:disable:next function_parameter_count - func createTransaction( - spendingKey: String, - zatoshi: Int, - to address: String, - memo: String?, - from accountIndex: Int, - result: @escaping TransactionEncoderResultBlock - ) + ) async throws -> EncodedTransaction /** Creates a transaction that will attempt to shield transparent funds that are present on the cacheDB .throwing an exception whenever things are missing. When the provided wallet implementation doesn't throw an exception, we wrap the issue into a descriptive exception ourselves (rather than using double-bangs for things). @@ -82,29 +58,7 @@ protocol TransactionEncoder { tSecretKey: String, memo: String?, from accountIndex: Int - ) throws -> EncodedTransaction - - /** - Creates a transaction that will attempt to shield transparent funds that are present on the cacheDB .throwing an exception whenever things are missing. When the provided wallet implementation doesn't throw an exception, we wrap the issue into a descriptive exception ourselves (rather than using double-bangs for things). - - Non-Blocking - - - Parameters: - - Parameter spendingKey: a string containing the spending key - - Parameter tSecretKey: transparent secret key to spend the UTXOs - - Parameter memo: string containing the memo (optional) - - Parameter accountIndex: index of the account that will be used to send the funds - - - Returns: a TransactionEncoderResultBlock - */ - - func createShieldingTransaction( - spendingKey: String, - tSecretKey: String, - memo: String?, - from accountIndex: Int, - result: @escaping TransactionEncoderResultBlock - ) + ) async throws -> EncodedTransaction /** Fetch the Transaction Entity from the encoded representation diff --git a/Sources/ZcashLightClientKit/Transaction/WalletTransactionEncoder.swift b/Sources/ZcashLightClientKit/Transaction/WalletTransactionEncoder.swift index 9394d004..d973cf8e 100644 --- a/Sources/ZcashLightClientKit/Transaction/WalletTransactionEncoder.swift +++ b/Sources/ZcashLightClientKit/Transaction/WalletTransactionEncoder.swift @@ -10,7 +10,6 @@ import Foundation class WalletTransactionEncoder: TransactionEncoder { var rustBackend: ZcashRustBackendWelding.Type var repository: TransactionRepository - var queue: DispatchQueue private var outputParamsURL: URL private var spendParamsURL: URL @@ -34,7 +33,6 @@ class WalletTransactionEncoder: TransactionEncoder { self.outputParamsURL = outputParams self.spendParamsURL = spendParams self.networkType = networkType - self.queue = DispatchQueue(label: "wallet.transaction.encoder.serial.queue") } convenience init(initializer: Initializer) { @@ -55,7 +53,7 @@ class WalletTransactionEncoder: TransactionEncoder { to address: String, memo: String?, from accountIndex: Int - ) throws -> EncodedTransaction { + ) async throws -> EncodedTransaction { let txId = try createSpend( spendingKey: spendingKey, zatoshi: zatoshi, @@ -77,35 +75,6 @@ class WalletTransactionEncoder: TransactionEncoder { throw TransactionEncoderError.notFound(transactionId: txId) } } - - // swiftlint:disable:next function_parameter_count - func createTransaction( - spendingKey: String, - zatoshi: Int, - to address: String, - memo: String?, - from accountIndex: Int, - result: @escaping TransactionEncoderResultBlock - ) { - queue.async { [weak self] in - guard let self = self else { return } - do { - result( - .success( - try self.createTransaction( - spendingKey: spendingKey, - zatoshi: zatoshi, - to: address, - memo: memo, - from: accountIndex - ) - ) - ) - } catch { - result(.failure(error)) - } - } - } func createSpend(spendingKey: String, zatoshi: Int, to address: String, memo: String?, from accountIndex: Int) throws -> Int { guard ensureParams(spend: self.spendParamsURL, output: self.spendParamsURL) else { @@ -136,7 +105,7 @@ class WalletTransactionEncoder: TransactionEncoder { tSecretKey: String, memo: String?, from accountIndex: Int - ) throws -> EncodedTransaction { + ) async throws -> EncodedTransaction { let txId = try createShieldingSpend( spendingKey: spendingKey, tsk: tSecretKey, @@ -158,18 +127,6 @@ class WalletTransactionEncoder: TransactionEncoder { } } - func createShieldingTransaction( - spendingKey: String, - tSecretKey: String, - memo: String?, - from accountIndex: Int, - result: @escaping TransactionEncoderResultBlock - ) { - queue.async { - result(.failure(RustWeldingError.genericError(message: "not implemented"))) - } - } - func createShieldingSpend(spendingKey: String, tsk: String, memo: String?, accountIndex: Int) throws -> Int { guard ensureParams(spend: self.spendParamsURL, output: self.spendParamsURL) else { throw TransactionEncoderError.missingParams