[#684] Improvements for the derivation tool dependency (#689)

- dependency updated and simplified
- tests fixed
This commit is contained in:
Lukas Korba 2023-03-21 11:09:26 +01:00 committed by GitHub
parent 28e95efca9
commit 4a3be9ac0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 87 additions and 81 deletions

View File

@ -239,8 +239,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "45167b8006448c79dda4b7bd604e07a034c15c49",
"version" : "2.48.0"
"revision" : "9b2848d76f5caad08b97e71a04345aa5bdb23a06",
"version" : "2.49.0"
}
},
{
@ -248,8 +248,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "d75ed708d00353acf173ca23018b6bd46f949464",
"version" : "1.17.0"
"revision" : "cc1e5275079380c859417dbea8588531f1a90ec3",
"version" : "1.18.0"
}
},
{
@ -311,8 +311,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swiftui-navigation",
"state" : {
"revision" : "0a0e1b321d70ee6a464ecfe6b0136d9eff77ebfc",
"version" : "0.7.0"
"revision" : "47dd574b900ba5ba679f56ea00d4d282fc7305a6",
"version" : "0.7.1"
}
},
{

View File

@ -9,6 +9,6 @@ import ComposableArchitecture
extension DeeplinkClient: DependencyKey {
static let liveValue = Self(
resolveDeeplinkURL: { try Deeplink().resolveDeeplinkURL($0, isValidZcashAddress: $1.isValidZcashAddress) }
resolveDeeplinkURL: { try Deeplink().resolveDeeplinkURL($0, isValidZcashAddress: $1.isZcashAddress) }
)
}

View File

@ -23,12 +23,15 @@ struct DerivationToolClient {
/// - Returns: the spending keys that correspond to the seed, formatted as Strings.
var deriveSpendingKey: ([UInt8], Int) throws -> UnifiedSpendingKey
/// Checks validity of the transparent address.
var isValidTransparentAddress: (String) throws -> Bool
/// Checks validity of the unified address.
var isUnifiedAddress: (String) -> Bool
/// Checks validity of the shielded address.
var isValidSaplingAddress: (String) throws -> Bool
var isSaplingAddress: (String) -> Bool
/// Checks validity of the transparent address.
var isTransparentAddress: (String) -> Bool
/// Checks if given address is a valid zcash address.
var isValidZcashAddress: (String) throws -> Bool
var isZcashAddress: (String) -> Bool
}

View File

@ -17,13 +17,40 @@ extension DerivationToolClient: DependencyKey {
deriveSpendingKey: { seed, accountIndex in
try derivationTool.deriveUnifiedSpendingKey(seed: seed, accountIndex: accountIndex)
},
isValidTransparentAddress: { tAddress in
derivationTool.isValidTransparentAddress(tAddress)
isUnifiedAddress: { address in
do {
if case .unified = try Recipient(address, network: TargetConstants.zcashNetwork.networkType) {
return true
} else {
return false
}
} catch {
return false
}
},
isValidSaplingAddress: { zAddress in
derivationTool.isValidSaplingAddress(zAddress)
isSaplingAddress: { address in
do {
if case .sapling = try Recipient(address, network: TargetConstants.zcashNetwork.networkType) {
return true
} else {
return false
}
} catch {
return false
}
},
isValidZcashAddress: { address in
isTransparentAddress: { address in
do {
if case .transparent = try Recipient(address, network: TargetConstants.zcashNetwork.networkType) {
return true
} else {
return false
}
} catch {
return false
}
},
isZcashAddress: { address in
do {
_ = try Recipient(address, network: TargetConstants.zcashNetwork.networkType)
return true

View File

@ -12,17 +12,19 @@ import ZcashLightClientKit
extension DerivationToolClient: TestDependencyKey {
static let testValue = Self(
deriveSpendingKey: XCTUnimplemented("\(Self.self).deriveSpendingKey"),
isValidTransparentAddress: XCTUnimplemented("\(Self.self).isValidTransparentAddress", placeholder: false),
isValidSaplingAddress: XCTUnimplemented("\(Self.self).isValidShieldedAddress", placeholder: false),
isValidZcashAddress: XCTUnimplemented("\(Self.self).isValidZcashAddress", placeholder: false)
isUnifiedAddress: XCTUnimplemented("\(Self.self).isUnifiedAddress", placeholder: false),
isSaplingAddress: XCTUnimplemented("\(Self.self).isSaplingAddress", placeholder: false),
isTransparentAddress: XCTUnimplemented("\(Self.self).isTransparentAddress", placeholder: false),
isZcashAddress: XCTUnimplemented("\(Self.self).isZcashAddress", placeholder: false)
)
}
extension DerivationToolClient {
static let noOp = Self(
deriveSpendingKey: { _, _ in throw "NotImplemented" },
isValidTransparentAddress: { _ in return false },
isValidSaplingAddress: { _ in return false },
isValidZcashAddress: { _ in return false }
isUnifiedAddress: { _ in return false },
isSaplingAddress: { _ in return false },
isTransparentAddress: { _ in return false },
isZcashAddress: { _ in return false }
)
}

View File

@ -16,7 +16,7 @@ struct URIParser {
self.derivationTool = derivationTool
}
func isValidURI(_ uri: String) throws -> Bool {
try derivationTool.isValidZcashAddress(uri)
func isValidURI(_ uri: String) -> Bool {
derivationTool.isZcashAddress(uri)
}
}

View File

@ -16,6 +16,6 @@ extension DependencyValues {
}
struct URIParserClient {
var isValidURI: (String) throws -> Bool
var isValidURI: (String) -> Bool
}

View File

@ -12,9 +12,7 @@ extension URIParserClient: DependencyKey {
static func live(uriParser: URIParser = URIParser(derivationTool: .live())) -> Self {
Self(
isValidURI: { uri in
try uriParser.isValidURI(uri)
}
isValidURI: { uri in uriParser.isValidURI(uri) }
)
}
}

View File

@ -24,7 +24,6 @@ struct ScanReducer: ReducerProtocol {
@BindingState var alert: AlertState<ScanReducer.Action>?
var isTorchAvailable = false
var isTorchOn = false
var isValidValue = false
var scanStatus: ScanStatus = .unknown
var scannedValue: String? {
@ -34,6 +33,13 @@ struct ScanReducer: ReducerProtocol {
return scannedValue.data
}
var isValidValue: Bool {
if case .value = scanStatus {
return true
}
return false
}
}
@Dependency(\.captureDevice) var captureDevice
@ -60,7 +66,6 @@ struct ScanReducer: ReducerProtocol {
case .onAppear:
// reset the values
state.scanStatus = .unknown
state.isValidValue = false
state.isTorchOn = false
// check the torch availability
do {
@ -90,22 +95,18 @@ struct ScanReducer: ReducerProtocol {
if let prevCode = state.scannedValue, prevCode == code.data {
return .none
}
state.scanStatus = .value(code)
state.isValidValue = false
do {
if try uriParser.isValidURI(code.data) {
state.isValidValue = true
// once valid URI is scanned we want to start the timer to deliver the code
// any new code cancels the schedule and fires new one
return .concatenate(
EffectTask.cancel(id: CancelId.self),
EffectTask(value: .found(code))
.delay(for: 1.0, scheduler: mainQueue)
.eraseToEffect()
.cancellable(id: CancelId.self, cancelInFlight: true)
)
}
} catch {
if uriParser.isValidURI(code.data) {
state.scanStatus = .value(code)
// once valid URI is scanned we want to start the timer to deliver the code
// any new code cancels the schedule and fires new one
return .concatenate(
EffectTask.cancel(id: CancelId.self),
EffectTask(value: .found(code))
.delay(for: 1.0, scheduler: mainQueue)
.eraseToEffect()
.cancellable(id: CancelId.self, cancelInFlight: true)
)
} else {
state.scanStatus = .failed
}
return .cancel(id: CancelId.self)

View File

@ -224,15 +224,7 @@ struct SendFlowReducer: ReducerProtocol {
// The is valid Zcash address check is already covered in the scan feature
// so we can be sure it's valid and thus `true` value here.
state.transactionAddressInputState.isValidAddress = true
do {
if case .transparent = try Recipient(address.data, network: zcashSDKEnvironment.network.networkType) {
state.transactionAddressInputState.isValidTransparentAddress = true
} else {
state.transactionAddressInputState.isValidTransparentAddress = false
}
} catch {
state.transactionAddressInputState.isValidTransparentAddress = false
}
state.transactionAddressInputState.isValidTransparentAddress = derivationTool.isTransparentAddress(address.data)
audioServices.systemSoundVibrate()
return EffectTask(value: .updateDestination(nil))

View File

@ -38,22 +38,8 @@ struct TransactionAddressTextFieldReducer: ReducerProtocol {
return .none
case .textField(.set(let address)):
do {
state.isValidAddress = try derivationTool.isValidZcashAddress(address.data)
} catch {
state.isValidAddress = false
}
do {
if case .transparent = try Recipient(address.data, network: zcashSDKEnvironment.network.networkType) {
state.isValidTransparentAddress = true
} else {
state.isValidTransparentAddress = false
}
state.isValidTransparentAddress = true
} catch {
state.isValidTransparentAddress = false
}
state.isValidAddress = derivationTool.isZcashAddress(address.data)
state.isValidTransparentAddress = derivationTool.isTransparentAddress(address.data)
return .none
}
}

View File

@ -17,7 +17,6 @@ class ScanTests: XCTestCase {
ScanReducer.State(
isTorchAvailable: true,
isTorchOn: true,
isValidValue: true,
scanStatus: .value("t1gXqfSSQt6WfpwyuCU3Wi7sSVZ66DYQ3Po".redacted)
),
reducer: ScanReducer()
@ -28,7 +27,6 @@ class ScanTests: XCTestCase {
store.send(.onAppear) { state in
state.isTorchAvailable = false
state.isTorchOn = false
state.isValidValue = false
state.scanStatus = .unknown
}
}
@ -71,8 +69,7 @@ class ScanTests: XCTestCase {
let value = "test".redacted
store.send(.scan(value)) { state in
state.scanStatus = .value(value)
state.isValidValue = false
state.scanStatus = .failed
}
}
@ -91,7 +88,6 @@ class ScanTests: XCTestCase {
let address = "t1gXqfSSQt6WfpwyuCU3Wi7sSVZ66DYQ3Po".redacted
store.send(.scan(address)) { state in
state.scanStatus = .value(address)
state.isValidValue = true
}
testScheduler.advance(by: 1.01)

View File

@ -223,7 +223,7 @@ class SendTests: XCTestCase {
)
store.dependencies.derivationTool = .noOp
store.dependencies.derivationTool.isValidZcashAddress = { _ in false }
store.dependencies.derivationTool.isZcashAddress = { _ in false }
let address = "3HRG769ii3HDSJV5vNknQPzXqtL2mTSGnr".redacted
store.send(.transactionAddressInput(.textField(.set(address)))) { state in
@ -246,7 +246,7 @@ class SendTests: XCTestCase {
)
store.dependencies.derivationTool = .noOp
store.dependencies.derivationTool.isValidZcashAddress = { _ in true }
store.dependencies.derivationTool.isZcashAddress = { _ in true }
let address = "t1gXqfSSQt6WfpwyuCU3Wi7sSVZ66DYQ3Po".redacted
store.send(.transactionAddressInput(.textField(.set(address)))) { state in
@ -425,7 +425,7 @@ class SendTests: XCTestCase {
)
store.dependencies.derivationTool = .noOp
store.dependencies.derivationTool.isValidZcashAddress = { _ in true }
store.dependencies.derivationTool.isZcashAddress = { _ in true }
let address = "t1gXqfSSQt6WfpwyuCU3Wi7sSVZ66DYQ3Po".redacted
store.send(.transactionAddressInput(.textField(.set(address)))) { state in
@ -465,7 +465,7 @@ class SendTests: XCTestCase {
)
store.dependencies.derivationTool = .noOp
store.dependencies.derivationTool.isValidZcashAddress = { _ in true }
store.dependencies.derivationTool.isZcashAddress = { _ in true }
let address = "t1gXqfSSQt6WfpwyuCU3Wi7sSVZ66DYQ3Po".redacted
store.send(.transactionAddressInput(.textField(.set(address)))) { state in
@ -544,7 +544,8 @@ class SendTests: XCTestCase {
)
store.dependencies.derivationTool = .noOp
store.dependencies.derivationTool.isValidZcashAddress = { _ in true }
store.dependencies.derivationTool.isZcashAddress = { _ in true }
store.dependencies.derivationTool.isTransparentAddress = { _ in true }
let address = "tmGh6ttAnQRJra81moqYcedFadW9XtUT5Eq".redacted
store.send(.transactionAddressInput(.textField(.set(address)))) { state in