diff --git a/ZcashLightClientKit/DAO/UnspentTransactionOutputDAO.swift b/ZcashLightClientKit/DAO/UnspentTransactionOutputDAO.swift new file mode 100644 index 00000000..8d6f4fc7 --- /dev/null +++ b/ZcashLightClientKit/DAO/UnspentTransactionOutputDAO.swift @@ -0,0 +1,22 @@ +// +// UnspentTransactionOutputDAO.swift +// ZcashLightClientKit +// +// Created by Francisco Gindre on 12/9/20. +// + +import Foundation + +struct UTXO: UnspentTransactionOutputEntity { + var address: String + + var txid: Data + + var index: Int32 + + var script: Data + + var valueZat: Int64 + + var height: UInt64 +} diff --git a/ZcashLightClientKit/Entity/UnspentTransactionOutputEntity.swift b/ZcashLightClientKit/Entity/UnspentTransactionOutputEntity.swift new file mode 100644 index 00000000..5562e890 --- /dev/null +++ b/ZcashLightClientKit/Entity/UnspentTransactionOutputEntity.swift @@ -0,0 +1,24 @@ +// +// UnspentTransactionOutputEntity.swift +// ZcashLightClientKit +// +// Created by Francisco Gindre on 12/9/20. +// + +import Foundation + +public protocol UnspentTransactionOutputEntity { + + var address: String { get set } + + var txid: Data {get set} + + var index: Int32 {get set} + + var script: Data {get set} + + var valueZat: Int64 {get set} + + var height: UInt64 {get set} + +} diff --git a/ZcashLightClientKit/Service/LightWalletGRPCService.swift b/ZcashLightClientKit/Service/LightWalletGRPCService.swift index b65ed3b1..a752c0e5 100644 --- a/ZcashLightClientKit/Service/LightWalletGRPCService.swift +++ b/ZcashLightClientKit/Service/LightWalletGRPCService.swift @@ -75,8 +75,7 @@ public class LightWalletGRPCService { requestIDProvider: .autogenerated, requestIDHeader: nil, cacheable: false) - } - + } } extension LightWalletGRPCService: LightWalletService { @@ -218,6 +217,39 @@ extension LightWalletGRPCService: LightWalletService { } return height } + + public func fetchUTXOs(for tAddress: String, result: @escaping (Result<[UnspentTransactionOutputEntity], LightWalletServiceError>) -> Void) { + queue.async { [weak self] in + guard let self = self else { return } + let arg = GetAddressUtxosArg.with { (utxoArgs) in + utxoArgs.address = tAddress + } + var utxos = [UnspentTransactionOutputEntity]() + let response = self.compactTxStreamer.getAddressUtxosStream(arg) { (reply) in + utxos.append( + UTXO(address: tAddress, + txid: reply.txid, + index: reply.index, + script: reply.script, + valueZat: reply.valueZat, + height: UInt64(reply.valueZat) + ) + ) + } + + do { + let status = try response.status.wait() + switch status.code { + case .ok: + result(.success(utxos)) + default: + result(.failure(.mapCode(status))) + } + } catch { + result(.failure(error.mapToServiceError())) + } + } + } } extension Error { diff --git a/ZcashLightClientKit/Service/LightWalletService.swift b/ZcashLightClientKit/Service/LightWalletService.swift index 7a170b28..b901fd4f 100644 --- a/ZcashLightClientKit/Service/LightWalletService.swift +++ b/ZcashLightClientKit/Service/LightWalletService.swift @@ -168,4 +168,7 @@ public protocol LightWalletService { - Returns: LightWalletServiceResponse */ func fetchTransaction(txId: Data, result: @escaping (Result) -> Void) + + + func fetchUTXOs(for tAddress: String, result: @escaping(Result<[UnspentTransactionOutputEntity], LightWalletServiceError>) -> Void) }