2019-10-18 11:45:19 -07:00
|
|
|
//
|
|
|
|
// Tests+Utils.swift
|
|
|
|
// ZcashLightClientKitTests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 18/09/2019.
|
|
|
|
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2023-03-15 04:17:43 -07:00
|
|
|
import Combine
|
2019-10-18 11:45:19 -07:00
|
|
|
import Foundation
|
2020-04-09 15:25:43 -07:00
|
|
|
import GRPC
|
2019-10-18 11:45:19 -07:00
|
|
|
import XCTest
|
2020-04-09 15:25:43 -07:00
|
|
|
import NIO
|
2022-10-03 11:54:43 -07:00
|
|
|
import NIOTransportServices
|
2023-03-31 10:10:35 -07:00
|
|
|
@testable import ZcashLightClientKit
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2022-02-28 09:03:20 -08:00
|
|
|
enum Environment {
|
|
|
|
static let lightwalletdKey = "LIGHTWALLETD_ADDRESS"
|
2023-02-13 01:34:41 -08:00
|
|
|
static let seedPhrase = """
|
|
|
|
still champion voice habit trend flight survey between bitter process artefact blind carbon truly provide dizzy crush flush breeze blouse charge \
|
|
|
|
solid fish spread
|
|
|
|
"""
|
2023-03-10 03:58:28 -08:00
|
|
|
|
|
|
|
// Seed bytes for `seedPhrase`.
|
|
|
|
static var seedBytes: [UInt8] {
|
|
|
|
let seedString = Data(base64Encoded: "9VDVOZZZOWWHpZtq1Ebridp3Qeux5C+HwiRR0g7Oi7HgnMs8Gfln83+/Q1NnvClcaSwM4ADFL1uZHxypEWlWXg==")!
|
|
|
|
return [UInt8](seedString)
|
|
|
|
}
|
|
|
|
|
2023-02-13 01:34:41 -08:00
|
|
|
static let testRecipientAddress = "zs17mg40levjezevuhdp5pqrd52zere7r7vrjgdwn5sj4xsqtm20euwahv9anxmwr3y3kmwuz8k55a"
|
2023-03-31 10:10:35 -07:00
|
|
|
|
|
|
|
static var uniqueTestTempDirectory: URL {
|
|
|
|
URL(fileURLWithPath: NSString(string: NSTemporaryDirectory())
|
|
|
|
.appendingPathComponent("tmp-\(Int.random(in: 0 ... .max))"))
|
|
|
|
}
|
2023-05-21 09:48:29 -07:00
|
|
|
|
|
|
|
static var uniqueGeneralStorageDirectory: URL {
|
|
|
|
URL(fileURLWithPath: NSString(string: NSTemporaryDirectory())
|
|
|
|
.appendingPathComponent("gens-\(Int.random(in: 0 ... .max))"))
|
|
|
|
}
|
2022-02-28 09:03:20 -08:00
|
|
|
}
|
|
|
|
|
2023-01-18 08:09:04 -08:00
|
|
|
public enum Constants {
|
2022-02-28 09:03:20 -08:00
|
|
|
static let address: String = ProcessInfo.processInfo.environment[Environment.lightwalletdKey] ?? "localhost"
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
enum LightWalletEndpointBuilder {
|
2019-11-26 14:32:20 -08:00
|
|
|
static var `default`: LightWalletEndpoint {
|
2021-06-02 14:32:05 -07:00
|
|
|
LightWalletEndpoint(address: Constants.address, port: 9067, secure: false)
|
2019-11-26 14:32:20 -08:00
|
|
|
}
|
2021-07-28 09:59:10 -07:00
|
|
|
|
|
|
|
static var publicTestnet: LightWalletEndpoint {
|
|
|
|
LightWalletEndpoint(address: "testnet.lightwalletd.com", port: 9067, secure: true)
|
|
|
|
}
|
2021-07-28 15:25:47 -07:00
|
|
|
|
|
|
|
static var eccTestnet: LightWalletEndpoint {
|
|
|
|
LightWalletEndpoint(address: "lightwalletd.testnet.electriccoin.co", port: 9067, secure: true)
|
|
|
|
}
|
2019-11-26 14:32:20 -08:00
|
|
|
}
|
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
class ChannelProvider {
|
2023-03-28 06:30:03 -07:00
|
|
|
func channel(endpoint: LightWalletEndpoint = LightWalletEndpointBuilder.default, secure: Bool = false) -> GRPCChannel {
|
2022-07-08 12:49:24 -07:00
|
|
|
let connectionBuilder = secure ?
|
2022-10-03 11:54:43 -07:00
|
|
|
ClientConnection.usingPlatformAppropriateTLS(for: NIOTSEventLoopGroup(loopCount: 1, defaultQoS: .default)) :
|
|
|
|
ClientConnection.insecure(group: NIOTSEventLoopGroup(loopCount: 1, defaultQoS: .default))
|
2022-07-08 12:49:24 -07:00
|
|
|
|
|
|
|
let channel = connectionBuilder
|
|
|
|
.withKeepalive(
|
|
|
|
ClientConnectionKeepalive(
|
2023-01-18 08:09:04 -08:00
|
|
|
interval: .seconds(15),
|
|
|
|
timeout: .seconds(10)
|
2022-07-08 12:49:24 -07:00
|
|
|
)
|
|
|
|
)
|
|
|
|
.connect(host: endpoint.host, port: endpoint.port)
|
|
|
|
|
|
|
|
return channel
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
enum MockDbInit {
|
|
|
|
@discardableResult
|
|
|
|
static func emptyFile(at path: String) -> Bool {
|
2019-10-18 11:45:19 -07:00
|
|
|
FileManager.default.createFile(atPath: path, contents: Data("".utf8), attributes: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func destroy(at path: String) throws {
|
|
|
|
try FileManager.default.removeItem(atPath: path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func __documentsDirectory() throws -> URL {
|
|
|
|
try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func __dataDbURL() throws -> URL {
|
|
|
|
try __documentsDirectory().appendingPathComponent("data.db", isDirectory: false)
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:32:20 -08:00
|
|
|
func __spendParamsURL() throws -> URL {
|
2020-10-07 16:34:55 -07:00
|
|
|
try __documentsDirectory().appendingPathComponent("sapling-spend.params")
|
2019-11-26 14:32:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func __outputParamsURL() throws -> URL {
|
2020-10-07 16:34:55 -07:00
|
|
|
try __documentsDirectory().appendingPathComponent("sapling-output.params")
|
2019-11-26 14:32:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func copyParametersToDocuments() throws -> (spend: URL, output: URL) {
|
|
|
|
let spendURL = try __documentsDirectory().appendingPathComponent("sapling-spend.params", isDirectory: false)
|
|
|
|
let outputURL = try __documentsDirectory().appendingPathComponent("sapling-output.params", isDirectory: false)
|
|
|
|
try FileManager.default.copyItem(at: try __spendParamsURL(), to: spendURL)
|
|
|
|
try FileManager.default.copyItem(at: try __outputParamsURL(), to: outputURL)
|
|
|
|
|
|
|
|
return (spendURL, outputURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteParametersFromDocuments() throws {
|
|
|
|
let documents = try __documentsDirectory()
|
2021-09-23 06:26:41 -07:00
|
|
|
deleteParamsFrom(
|
|
|
|
spend: documents.appendingPathComponent("sapling-spend.params"),
|
|
|
|
output: documents.appendingPathComponent("sapling-output.params")
|
|
|
|
)
|
2019-11-26 14:32:20 -08:00
|
|
|
}
|
2022-05-31 05:27:24 -07:00
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
func deleteParamsFrom(spend: URL, output: URL) {
|
2019-11-26 14:32:20 -08:00
|
|
|
try? FileManager.default.removeItem(at: spend)
|
|
|
|
try? FileManager.default.removeItem(at: output)
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
func parametersReady() -> Bool {
|
2021-09-23 06:26:41 -07:00
|
|
|
guard
|
|
|
|
let output = try? __outputParamsURL(),
|
|
|
|
let spend = try? __spendParamsURL(),
|
|
|
|
FileManager.default.isReadableFile(atPath: output.absoluteString),
|
|
|
|
FileManager.default.isReadableFile(atPath: spend.absoluteString)
|
|
|
|
else {
|
|
|
|
return false
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
return true
|
|
|
|
}
|
2023-03-31 10:10:35 -07:00
|
|
|
|
|
|
|
extension ZcashRustBackend {
|
|
|
|
static func makeForTests(
|
|
|
|
dbData: URL = try! __dataDbURL(),
|
|
|
|
fsBlockDbRoot: URL,
|
|
|
|
spendParamsPath: URL = SaplingParamsSourceURL.default.spendParamFileURL,
|
|
|
|
outputParamsPath: URL = SaplingParamsSourceURL.default.outputParamFileURL,
|
|
|
|
networkType: NetworkType
|
|
|
|
) -> ZcashRustBackendWelding {
|
|
|
|
ZcashRustBackend(
|
|
|
|
dbData: dbData,
|
|
|
|
fsBlockDbRoot: fsBlockDbRoot,
|
|
|
|
spendParamsPath: spendParamsPath,
|
|
|
|
outputParamsPath: outputParamsPath,
|
|
|
|
networkType: networkType
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 13:13:29 -07:00
|
|
|
|
|
|
|
extension Zatoshi: CustomDebugStringConvertible {
|
|
|
|
public var debugDescription: String {
|
|
|
|
"Zatoshi(\(self.amount))"
|
|
|
|
}
|
|
|
|
}
|