diff --git a/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift b/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift index ccfb2dbf..26f60e1f 100644 --- a/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift +++ b/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift @@ -361,7 +361,7 @@ struct ZcashRustBackend: ZcashRustBackendWelding { func extractAndStoreTxFromPCZT( pcztWithProofs: Data, pcztWithSigs: Data - ) async throws -> Data { + ) async throws -> [Data] { let pcztPtr: UnsafeMutablePointer? = pcztWithProofs.withUnsafeBytes { pcztWithProofsBuffer in guard let pcztWithProofsBufferPtr = pcztWithProofsBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else { return nil @@ -394,10 +394,10 @@ struct ZcashRustBackend: ZcashRustBackendWelding { defer { zcashlc_free_boxed_slice(pcztPtr) } - return Data( + return [Data( bytes: pcztPtr.pointee.ptr, count: Int(pcztPtr.pointee.len) - ) + )] } @DBActor diff --git a/Sources/ZcashLightClientKit/Rust/ZcashRustBackendWelding.swift b/Sources/ZcashLightClientKit/Rust/ZcashRustBackendWelding.swift index ee21c96e..7b34752a 100644 --- a/Sources/ZcashLightClientKit/Rust/ZcashRustBackendWelding.swift +++ b/Sources/ZcashLightClientKit/Rust/ZcashRustBackendWelding.swift @@ -296,7 +296,7 @@ protocol ZcashRustBackendWelding { func extractAndStoreTxFromPCZT( pcztWithProofs: Data, pcztWithSigs: Data - ) async throws -> Data + ) async throws -> [Data] /// Gets the consensus branch id for the given height /// - Parameter height: the height you what to know the branch id for diff --git a/Sources/ZcashLightClientKit/Synchronizer.swift b/Sources/ZcashLightClientKit/Synchronizer.swift index 0df0b981..9f8e2956 100644 --- a/Sources/ZcashLightClientKit/Synchronizer.swift +++ b/Sources/ZcashLightClientKit/Synchronizer.swift @@ -236,10 +236,10 @@ public protocol Synchronizer: AnyObject { pczt: Data ) async throws -> Data - func extractAndStoreTxFromPCZT( + func createTransactionFromPCZT( pcztWithProofs: Data, pcztWithSigs: Data - ) async throws -> Data + ) async throws -> AsyncThrowingStream /// all the transactions that are on the blockchain var transactions: [ZcashTransaction.Overview] { get async } diff --git a/Sources/ZcashLightClientKit/Synchronizer/SDKSynchronizer.swift b/Sources/ZcashLightClientKit/Synchronizer/SDKSynchronizer.swift index 06199793..212e3c79 100644 --- a/Sources/ZcashLightClientKit/Synchronizer/SDKSynchronizer.swift +++ b/Sources/ZcashLightClientKit/Synchronizer/SDKSynchronizer.swift @@ -376,6 +376,11 @@ public class SDKSynchronizer: Synchronizer { proposal: proposal, spendingKey: spendingKey ) + + return submitTransactions(transactions) + } + + func submitTransactions(_ transactions: [ZcashTransaction.Overview]) -> AsyncThrowingStream { var iterator = transactions.makeIterator() var submitFailed = false @@ -419,14 +424,28 @@ public class SDKSynchronizer: Synchronizer { ) } - public func extractAndStoreTxFromPCZT( + public func createTransactionFromPCZT( pcztWithProofs: Data, pcztWithSigs: Data - ) async throws -> Data { - try await initializer.rustBackend.extractAndStoreTxFromPCZT( + ) async throws -> AsyncThrowingStream { + try throwIfUnprepared() + + try await SaplingParameterDownloader.downloadParamsIfnotPresent( + spendURL: initializer.spendParamsURL, + spendSourceURL: initializer.saplingParamsSourceURL.spendParamFileURL, + outputURL: initializer.outputParamsURL, + outputSourceURL: initializer.saplingParamsSourceURL.outputParamFileURL, + logger: logger + ) + + let txIds = try await initializer.rustBackend.extractAndStoreTxFromPCZT( pcztWithProofs: pcztWithProofs, pcztWithSigs: pcztWithSigs ) + + let transactions = try await transactionEncoder.createTransactionsFromTxIds(txIds) + + return submitTransactions(transactions) } public func allReceivedTransactions() async throws -> [ZcashTransaction.Overview] { diff --git a/Sources/ZcashLightClientKit/Transaction/TransactionEncoder.swift b/Sources/ZcashLightClientKit/Transaction/TransactionEncoder.swift index d9f9d719..4521ad53 100644 --- a/Sources/ZcashLightClientKit/Transaction/TransactionEncoder.swift +++ b/Sources/ZcashLightClientKit/Transaction/TransactionEncoder.swift @@ -91,5 +91,7 @@ protocol TransactionEncoder { /// - Parameter transaction: a transaction overview func submit(transaction: EncodedTransaction) async throws + func createTransactionsFromTxIds(_ txIds: [Data]) async throws -> [ZcashTransaction.Overview] + func closeDBConnection() } diff --git a/Sources/ZcashLightClientKit/Transaction/WalletTransactionEncoder.swift b/Sources/ZcashLightClientKit/Transaction/WalletTransactionEncoder.swift index e20131ef..95767e88 100644 --- a/Sources/ZcashLightClientKit/Transaction/WalletTransactionEncoder.swift +++ b/Sources/ZcashLightClientKit/Transaction/WalletTransactionEncoder.swift @@ -111,6 +111,24 @@ class WalletTransactionEncoder: TransactionEncoder { usk: spendingKey ) + return try await createTransactionsFromTxIds(txIds) + +// logger.debug("transaction ids: \(txIds)") +// +// var txs: [ZcashTransaction.Overview] = [] +// +// for txId in txIds { +// txs.append(try await repository.find(rawID: txId)) +// } +// +// return txs + } + + func createTransactionsFromTxIds(_ txIds: [Data]) async throws -> [ZcashTransaction.Overview] { + guard ensureParams(spend: self.spendParamsURL, output: self.outputParamsURL) else { + throw ZcashError.walletTransEncoderCreateTransactionMissingSaplingParams + } + logger.debug("transaction ids: \(txIds)") var txs: [ZcashTransaction.Overview] = [] @@ -121,7 +139,7 @@ class WalletTransactionEncoder: TransactionEncoder { return txs } - + func submit( transaction: EncodedTransaction ) async throws { diff --git a/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift b/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift index 44bad01b..a6ac6204 100644 --- a/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift +++ b/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift @@ -1620,27 +1620,27 @@ class SynchronizerMock: Synchronizer { } } - // MARK: - extractAndStoreTxFromPCZT + // MARK: - createTransactionFromPCZT - var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsThrowableError: Error? - var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCallsCount = 0 - var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCalled: Bool { - return extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCallsCount > 0 + var createTransactionFromPCZTPcztWithProofsPcztWithSigsThrowableError: Error? + var createTransactionFromPCZTPcztWithProofsPcztWithSigsCallsCount = 0 + var createTransactionFromPCZTPcztWithProofsPcztWithSigsCalled: Bool { + return createTransactionFromPCZTPcztWithProofsPcztWithSigsCallsCount > 0 } - var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReceivedArguments: (pcztWithProofs: Data, pcztWithSigs: Data)? - var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReturnValue: Data! - var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsClosure: ((Data, Data) async throws -> Data)? + var createTransactionFromPCZTPcztWithProofsPcztWithSigsReceivedArguments: (pcztWithProofs: Data, pcztWithSigs: Data)? + var createTransactionFromPCZTPcztWithProofsPcztWithSigsReturnValue: AsyncThrowingStream! + var createTransactionFromPCZTPcztWithProofsPcztWithSigsClosure: ((Data, Data) async throws -> AsyncThrowingStream)? - func extractAndStoreTxFromPCZT(pcztWithProofs: Data, pcztWithSigs: Data) async throws -> Data { - if let error = extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsThrowableError { + func createTransactionFromPCZT(pcztWithProofs: Data, pcztWithSigs: Data) async throws -> AsyncThrowingStream { + if let error = createTransactionFromPCZTPcztWithProofsPcztWithSigsThrowableError { throw error } - extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCallsCount += 1 - extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReceivedArguments = (pcztWithProofs: pcztWithProofs, pcztWithSigs: pcztWithSigs) - if let closure = extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsClosure { + createTransactionFromPCZTPcztWithProofsPcztWithSigsCallsCount += 1 + createTransactionFromPCZTPcztWithProofsPcztWithSigsReceivedArguments = (pcztWithProofs: pcztWithProofs, pcztWithSigs: pcztWithSigs) + if let closure = createTransactionFromPCZTPcztWithProofsPcztWithSigsClosure { return try await closure(pcztWithProofs, pcztWithSigs) } else { - return extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReturnValue + return createTransactionFromPCZTPcztWithProofsPcztWithSigsReturnValue } }