2019-10-18 11:45:19 -07:00
|
|
|
//
|
|
|
|
// LightWalletService.swift
|
|
|
|
// ZcashLightClientKit
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 12/09/2019.
|
|
|
|
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2020-04-09 15:25:43 -07:00
|
|
|
import GRPC
|
2019-12-03 07:19:44 -08:00
|
|
|
import SwiftProtobuf
|
2020-02-26 08:54:48 -08:00
|
|
|
|
|
|
|
/**
|
2021-09-17 06:49:58 -07:00
|
|
|
Wrapper for errors received from a Lightwalletd endpoint
|
|
|
|
*/
|
2019-10-18 11:45:19 -07:00
|
|
|
public enum LightWalletServiceError: Error {
|
2020-08-10 15:19:59 -07:00
|
|
|
case generalError(message: String)
|
2020-04-09 15:25:43 -07:00
|
|
|
case failed(statusCode: Int, message: String)
|
2019-10-18 11:45:19 -07:00
|
|
|
case invalidBlock
|
2020-08-10 15:19:59 -07:00
|
|
|
case sentFailed(error: Error)
|
2019-12-03 07:19:44 -08:00
|
|
|
case genericError(error: Error)
|
2020-08-10 15:19:59 -07:00
|
|
|
case timeOut
|
|
|
|
case criticalError
|
|
|
|
case userCancelled
|
|
|
|
case unknown
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
extension LightWalletServiceError: Equatable {
|
2021-09-15 05:21:29 -07:00
|
|
|
// swiftlint:disable cyclomatic_complexity
|
2019-10-18 11:45:19 -07:00
|
|
|
public static func == (lhs: Self, rhs: Self) -> Bool {
|
|
|
|
switch lhs {
|
2021-09-17 06:49:58 -07:00
|
|
|
case .generalError(let message):
|
2019-10-18 11:45:19 -07:00
|
|
|
switch rhs {
|
2020-08-10 15:19:59 -07:00
|
|
|
case .generalError(let msg):
|
2021-09-17 06:49:58 -07:00
|
|
|
return message == msg
|
2019-10-18 11:45:19 -07:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
case .failed(let statusCode, _):
|
2019-10-18 11:45:19 -07:00
|
|
|
switch rhs {
|
2020-02-26 08:54:48 -08:00
|
|
|
case .failed(let anotherStatus, _):
|
2019-10-18 11:45:19 -07:00
|
|
|
return statusCode == anotherStatus
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
case .invalidBlock:
|
|
|
|
switch rhs {
|
|
|
|
case .invalidBlock:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2020-09-23 11:39:00 -07:00
|
|
|
case .sentFailed:
|
2019-12-03 07:19:44 -08:00
|
|
|
switch rhs {
|
2020-09-23 11:39:00 -07:00
|
|
|
case .sentFailed:
|
2020-08-10 15:19:59 -07:00
|
|
|
return true
|
2019-12-03 07:19:44 -08:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2019-12-06 04:38:47 -08:00
|
|
|
case .genericError:
|
2019-12-03 07:19:44 -08:00
|
|
|
return false
|
2020-08-10 15:19:59 -07:00
|
|
|
|
|
|
|
case .timeOut:
|
|
|
|
switch rhs {
|
|
|
|
case .timeOut:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case .criticalError:
|
2021-09-15 05:21:29 -07:00
|
|
|
switch rhs {
|
2020-08-10 15:19:59 -07:00
|
|
|
case .criticalError:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case .userCancelled:
|
2021-09-15 05:21:29 -07:00
|
|
|
switch rhs {
|
2020-08-10 15:19:59 -07:00
|
|
|
case .userCancelled:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case .unknown:
|
2021-09-15 05:21:29 -07:00
|
|
|
switch rhs {
|
2020-08-10 15:19:59 -07:00
|
|
|
case .unknown:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 07:19:44 -08:00
|
|
|
public protocol LightWalletServiceResponse {
|
|
|
|
var errorCode: Int32 { get }
|
|
|
|
var errorMessage: String { get }
|
|
|
|
var unknownFields: SwiftProtobuf.UnknownStorage { get }
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SendResponse: LightWalletServiceResponse {}
|
|
|
|
|
2022-10-03 16:05:11 -07:00
|
|
|
public protocol LightWalletService {
|
|
|
|
/// Returns the info for this lightwalletd server
|
|
|
|
func getInfo() async throws -> LightWalletdInfo
|
2021-09-17 06:49:58 -07:00
|
|
|
|
2022-08-25 06:39:59 -07:00
|
|
|
/// Return the latest block height known to the service.
|
2022-10-03 16:05:11 -07:00
|
|
|
/// Blocking API
|
2019-10-18 11:45:19 -07:00
|
|
|
func latestBlockHeight() throws -> BlockHeight
|
2021-09-17 06:49:58 -07:00
|
|
|
|
2022-08-25 06:39:59 -07:00
|
|
|
/// Return the latest block height known to the service.
|
|
|
|
func latestBlockHeightAsync() async throws -> BlockHeight
|
|
|
|
|
|
|
|
/// Return the given range of blocks.
|
|
|
|
/// - Parameter range: the inclusive range to fetch.
|
|
|
|
/// For instance if 1..5 is given, then every block in that will be fetched, including 1 and 5.
|
|
|
|
func blockRange(_ range: CompactBlockRange) -> AsyncThrowingStream<ZcashCompactBlock, Error>
|
|
|
|
|
|
|
|
/// Submits a raw transaction over lightwalletd. Non-Blocking
|
|
|
|
/// - Parameter spendTransaction: data representing the transaction to be sent
|
2022-10-03 16:05:11 -07:00
|
|
|
func submit(spendTransaction: Data) async throws -> LightWalletServiceResponse
|
2022-08-25 06:39:59 -07:00
|
|
|
|
|
|
|
/// Gets a transaction by id
|
|
|
|
/// - Parameter txId: data representing the transaction ID
|
|
|
|
/// - Throws: LightWalletServiceError
|
|
|
|
/// - Returns: LightWalletServiceResponse
|
2022-10-03 16:05:11 -07:00
|
|
|
func fetchTransaction(txId: Data) async throws -> TransactionEntity
|
2022-08-25 06:39:59 -07:00
|
|
|
|
|
|
|
func fetchUTXOs(for tAddress: String, height: BlockHeight) -> AsyncThrowingStream<UnspentTransactionOutputEntity, Error>
|
2021-09-17 06:49:58 -07:00
|
|
|
|
2022-08-25 06:39:59 -07:00
|
|
|
func fetchUTXOs(for tAddresses: [String], height: BlockHeight) -> AsyncThrowingStream<UnspentTransactionOutputEntity, Error>
|
2021-04-21 13:02:23 -07:00
|
|
|
|
2022-08-25 06:39:59 -07:00
|
|
|
func blockStream(
|
|
|
|
startHeight: BlockHeight,
|
|
|
|
endHeight: BlockHeight
|
|
|
|
) -> AsyncThrowingStream<ZcashCompactBlock, Error>
|
|
|
|
|
2021-04-21 13:02:23 -07:00
|
|
|
func closeConnection()
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|