diff --git a/Sources/ZcashLightClientKit/Model/WalletTypes.swift b/Sources/ZcashLightClientKit/Model/WalletTypes.swift index 89e0d216..e50c76d8 100644 --- a/Sources/ZcashLightClientKit/Model/WalletTypes.swift +++ b/Sources/ZcashLightClientKit/Model/WalletTypes.swift @@ -35,7 +35,7 @@ public struct SaplingExtendedSpendingKey: Equatable, StringEncoded, Undescribabl /// - Throws: `KeyEncodingError.invalidEncoding`when the provided encoding is /// found to be invalid public init(encoding: String, network: NetworkType) throws { - guard let valid = try? DerivationTool(networkType: network).isValidSaplingExtendedSpendingKey(encoding), valid else { + guard DerivationTool(networkType: network).isValidSaplingExtendedSpendingKey(encoding) else { throw KeyEncodingError.invalidEncoding } self.encoding = encoding @@ -65,7 +65,7 @@ public struct UnifiedFullViewingKey: Equatable, StringEncoded, Undescribable { /// - Throws: `KeyEncodingError.invalidEncoding`when the provided encoding is /// found to be invalid public init(encoding: String, account: UInt32, network: NetworkType) throws { - guard let valid = try? DerivationTool(networkType: network).isValidExtendedViewingKey(encoding), valid else { + guard DerivationTool.rustwelding.isValidUnifiedFullViewingKey(encoding, networkType: network) else { throw KeyEncodingError.invalidEncoding } @@ -87,7 +87,7 @@ public struct SaplingExtendedFullViewingKey: Equatable, StringEncoded, Undescrib /// - Throws: `KeyEncodingError.invalidEncoding`when the provided encoding is /// found to be invalid public init(encoding: String, network: NetworkType) throws { - guard let valid = try? DerivationTool(networkType: network).isValidExtendedViewingKey(encoding), valid else { + guard DerivationTool.rustwelding.isValidSaplingExtendedFullViewingKey(encoding, networkType: network) else { throw KeyEncodingError.invalidEncoding } self.encoding = encoding @@ -110,7 +110,7 @@ public struct TransparentAddress: Equatable, StringEncoded { /// - Throws: `KeyEncodingError.invalidEncoding`when the provided encoding is /// found to be invalid public init(encoding: String, network: NetworkType) throws { - guard let valid = try? DerivationTool(networkType: network).isValidTransparentAddress(encoding), valid else { + guard DerivationTool(networkType: network).isValidTransparentAddress(encoding) else { throw KeyEncodingError.invalidEncoding } @@ -135,7 +135,7 @@ public struct SaplingAddress: Equatable, StringEncoded { /// - Throws: `KeyEncodingError.invalidEncoding`when the provided encoding is /// found to be invalid public init(encoding: String, network: NetworkType) throws { - guard let valid = try? DerivationTool(networkType: network).isValidSaplingAddress(encoding), valid else { + guard DerivationTool(networkType: network).isValidSaplingAddress(encoding) else { throw KeyEncodingError.invalidEncoding } @@ -182,7 +182,7 @@ public struct UnifiedAddress: Equatable, StringEncoded { /// - Throws: `KeyEncodingError.invalidEncoding`when the provided encoding is /// found to be invalid public init(encoding: String, network: NetworkType) throws { - guard let valid = try? DerivationTool(networkType: network).isValidUnifiedAddress(encoding), valid else { + guard DerivationTool(networkType: network).isValidUnifiedAddress(encoding) else { throw KeyEncodingError.invalidEncoding } diff --git a/Sources/ZcashLightClientKit/Tool/DerivationTool.swift b/Sources/ZcashLightClientKit/Tool/DerivationTool.swift index 1cf2e5b6..cb0b3ae4 100644 --- a/Sources/ZcashLightClientKit/Tool/DerivationTool.swift +++ b/Sources/ZcashLightClientKit/Tool/DerivationTool.swift @@ -8,15 +8,15 @@ import Foundation public protocol KeyValidation { - func isValidExtendedViewingKey(_ extvk: String) throws -> Bool + func isValidUnifiedFullViewingKey(_ ufvk: String) -> Bool - func isValidTransparentAddress(_ tAddress: String) throws -> Bool + func isValidTransparentAddress(_ tAddress: String) -> Bool - func isValidSaplingAddress(_ zAddress: String) throws -> Bool + func isValidSaplingAddress(_ zAddress: String) -> Bool - func isValidSaplingExtendedSpendingKey(_ extsk: String) throws -> Bool + func isValidSaplingExtendedSpendingKey(_ extsk: String) -> Bool - func isValidUnifiedAddress(_ unifiedAddress: String) throws -> Bool + func isValidUnifiedAddress(_ unifiedAddress: String) -> Bool } public protocol KeyDeriving { @@ -107,44 +107,24 @@ public class DerivationTool: KeyDeriving { } extension DerivationTool: KeyValidation { - public func isValidUnifiedAddress(_ unifiedAddress: String) throws -> Bool { - do { - return try DerivationTool.rustwelding.isValidUnifiedAddress(unifiedAddress, networkType: networkType) - } catch { - throw KeyDerivationErrors.derivationError(underlyingError: error) - } + public func isValidUnifiedFullViewingKey(_ ufvk: String) -> Bool { + DerivationTool.rustwelding.isValidUnifiedFullViewingKey(ufvk, networkType: networkType) } - public func isValidExtendedViewingKey(_ extvk: String) throws -> Bool { - do { - return try DerivationTool.rustwelding.isValidSaplingExtendedFullViewingKey(extvk, networkType: networkType) - } catch { - throw KeyDerivationErrors.derivationError(underlyingError: error) - } + public func isValidUnifiedAddress(_ unifiedAddress: String) -> Bool { + DerivationTool.rustwelding.isValidUnifiedAddress(unifiedAddress, networkType: networkType) } - public func isValidTransparentAddress(_ tAddress: String) throws -> Bool { - do { - return try DerivationTool.rustwelding.isValidTransparentAddress(tAddress, networkType: networkType) - } catch { - throw KeyDerivationErrors.derivationError(underlyingError: error) - } + public func isValidTransparentAddress(_ tAddress: String) -> Bool { + DerivationTool.rustwelding.isValidTransparentAddress(tAddress, networkType: networkType) } - public func isValidSaplingAddress(_ zAddress: String) throws -> Bool { - do { - return try DerivationTool.rustwelding.isValidSaplingAddress(zAddress, networkType: networkType) - } catch { - throw KeyDerivationErrors.derivationError(underlyingError: error) - } + public func isValidSaplingAddress(_ zAddress: String) -> Bool { + DerivationTool.rustwelding.isValidSaplingAddress(zAddress, networkType: networkType) } - public func isValidSaplingExtendedSpendingKey(_ extsk: String) throws -> Bool { - do { - return try DerivationTool.rustwelding.isValidSaplingExtendedSpendingKey(extsk, networkType: networkType) - } catch { - throw KeyDerivationErrors.derivationError(underlyingError: error) - } + public func isValidSaplingExtendedSpendingKey(_ extsk: String) -> Bool { + DerivationTool.rustwelding.isValidSaplingExtendedSpendingKey(extsk, networkType: networkType) } } diff --git a/Tests/NetworkTests/ZcashRustBackendTests.swift b/Tests/NetworkTests/ZcashRustBackendTests.swift index 0673fb54..84a3e35d 100644 --- a/Tests/NetworkTests/ZcashRustBackendTests.swift +++ b/Tests/NetworkTests/ZcashRustBackendTests.swift @@ -92,79 +92,39 @@ class ZcashRustBackendTests: XCTestCase { } func testIsValidTransparentAddressFalse() { - var isValid: Bool? - - XCTAssertNoThrow( - try { - isValid = try ZcashRustBackend.isValidTransparentAddress( - "ztestsapling12k9m98wmpjts2m56wc60qzhgsfvlpxcwah268xk5yz4h942sd58jy3jamqyxjwums6hw7kfa4cc", - networkType: networkType - ) - }() + XCTAssertFalse( + ZcashRustBackend.isValidTransparentAddress( + "ztestsapling12k9m98wmpjts2m56wc60qzhgsfvlpxcwah268xk5yz4h942sd58jy3jamqyxjwums6hw7kfa4cc", + networkType: networkType + ) ) - - if let valid = isValid { - XCTAssertFalse(valid) - } else { - XCTFail("Failed as invalid") - } } func testIsValidTransparentAddressTrue() { - var isValid: Bool? - - XCTAssertNoThrow( - try { - isValid = try ZcashRustBackend.isValidTransparentAddress( - "tmSwpioc7reeoNrYB9SKpWkurJz3yEj3ee7", - networkType: networkType - ) - }() + XCTAssertTrue( + ZcashRustBackend.isValidTransparentAddress( + "tmSwpioc7reeoNrYB9SKpWkurJz3yEj3ee7", + networkType: networkType + ) ) - - if let valid = isValid { - XCTAssertTrue(valid) - } else { - XCTFail("Failed as invalid") - } } func testIsValidSaplingAddressTrue() { - var isValid: Bool? - - XCTAssertNoThrow( - try { - isValid = try ZcashRustBackend.isValidSaplingAddress( - "ztestsapling12k9m98wmpjts2m56wc60qzhgsfvlpxcwah268xk5yz4h942sd58jy3jamqyxjwums6hw7kfa4cc", - networkType: networkType - ) - }() + XCTAssertTrue( + ZcashRustBackend.isValidSaplingAddress( + "ztestsapling12k9m98wmpjts2m56wc60qzhgsfvlpxcwah268xk5yz4h942sd58jy3jamqyxjwums6hw7kfa4cc", + networkType: networkType + ) ) - - if let valid = isValid { - XCTAssertTrue(valid) - } else { - XCTFail("Failed as invalid") - } } func testIsValidSaplingAddressFalse() { - var isValid: Bool? - - XCTAssertNoThrow( - try { - isValid = try ZcashRustBackend.isValidSaplingAddress( - "tmSwpioc7reeoNrYB9SKpWkurJz3yEj3ee7", - networkType: networkType - ) - }() + XCTAssertFalse( + ZcashRustBackend.isValidSaplingAddress( + "tmSwpioc7reeoNrYB9SKpWkurJz3yEj3ee7", + networkType: networkType + ) ) - - if let valid = isValid { - XCTAssertFalse(valid) - } else { - XCTFail("Failed as invalid") - } } func testListTransparentReceivers() throws { @@ -172,7 +132,6 @@ class ZcashRustBackendTests: XCTestCase { let network = NetworkType.mainnet let tempDBs = TemporaryDbBuilder.build() let seed = testVector[0].root_seed! - let ufvk = try DerivationTool(networkType: network).deriveUnifiedSpendingKey(seed: seed, accountIndex: Int(testVector[0].account)).deriveFullViewingKey() XCTAssertEqual( try ZcashRustBackend.initDataDb( @@ -183,13 +142,6 @@ class ZcashRustBackendTests: XCTestCase { .success ) -// XCTAssertTrue( -// try ZcashRustBackend.initAccountsTable( -// dbData: tempDBs.dataDB, -// ufvks: [ufvk], -// networkType: network -// ) -// ) XCTAssertNoThrow( try ZcashRustBackend.createAccount( dbData: tempDBs.dataDB, diff --git a/Tests/OfflineTests/DerivationToolTests/DerivationToolMainnetTests.swift b/Tests/OfflineTests/DerivationToolTests/DerivationToolMainnetTests.swift index 4d4205f8..d2211956 100644 --- a/Tests/OfflineTests/DerivationToolTests/DerivationToolMainnetTests.swift +++ b/Tests/OfflineTests/DerivationToolTests/DerivationToolMainnetTests.swift @@ -67,10 +67,10 @@ class DerivationToolMainnetTests: XCTestCase { ) } - func testIsValidViewingKey() throws { - XCTAssertTrue(try derivationTool.isValidExtendedViewingKey("zxviews1q0dm7hkzqqqqpqplzv3f50rl4vay8uy5zg9e92f62lqg6gzu63rljety32xy5tcyenzuu3n386ws772nm6tp4sads8n37gff6nxmyz8dn9keehmapk0spc6pzx5uxepgu52xnwzxxnuja5tv465t9asppnj3eqncu3s7g3gzg5x8ss4ypkw08xwwyj7ky5skvnd9ldwj2u8fz2ry94s5q8p9lyp3j96yckudmp087d2jr2rnfuvjp7f56v78vpe658vljjddj7s645q399jd7")) + func testIsValidViewingKey() { + XCTAssertTrue( DerivationTool.rustwelding.isValidSaplingExtendedFullViewingKey("zxviews1q0dm7hkzqqqqpqplzv3f50rl4vay8uy5zg9e92f62lqg6gzu63rljety32xy5tcyenzuu3n386ws772nm6tp4sads8n37gff6nxmyz8dn9keehmapk0spc6pzx5uxepgu52xnwzxxnuja5tv465t9asppnj3eqncu3s7g3gzg5x8ss4ypkw08xwwyj7ky5skvnd9ldwj2u8fz2ry94s5q8p9lyp3j96yckudmp087d2jr2rnfuvjp7f56v78vpe658vljjddj7s645q399jd7", networkType: .mainnet)) - XCTAssertFalse(try derivationTool.isValidExtendedViewingKey("zxviews1q0dm7hkzky5skvnd9ldwj2u8fz2ry94s5q8p9lyp3j96yckudmp087d2jr2rnfuvjp7f56v78vpe658vljjddj7s645q399jd7")) + XCTAssertFalse( DerivationTool.rustwelding.isValidSaplingExtendedFullViewingKey("zxviews1q0dm7hkzky5skvnd9ldwj2u8fz2ry94s5q8p9lyp3j96yckudmp087d2jr2rnfuvjp7f56v78vpe658vljjddj7s645q399jd7", networkType: .mainnet)) } func testDeriveQuiteALotOfUnifiedKeysFromSeed() throws { diff --git a/Tests/OfflineTests/DerivationToolTests/DerivationToolTestnetTests.swift b/Tests/OfflineTests/DerivationToolTests/DerivationToolTestnetTests.swift index 5aee51c6..06799b64 100644 --- a/Tests/OfflineTests/DerivationToolTests/DerivationToolTestnetTests.swift +++ b/Tests/OfflineTests/DerivationToolTests/DerivationToolTestnetTests.swift @@ -70,9 +70,9 @@ class DerivationToolTestnetTests: XCTestCase { } func testIsValidViewingKey() throws { - XCTAssertTrue(try derivationTool.isValidExtendedViewingKey("zxviewtestsapling1qdxykmuaqqqqpqqg3x5c02p4rhw0rtszr8ln4xl7g6wg6qzsqgn445qsu3cq4vd6l5smlqrckkl2x5rnrauzc4gp665q3zyw0qf2sfdsx5wpp832htfavqk72uchuuvq2dpmgk8jfaza5t5l56u66fpx0sr8ewp9s3wj2txavmhhlazn5rj8mshh470fkrmzg4xarhrqlygg8f486307ujhndwhsw2h7ddzf89k3534aeu0ypz2tjgrzlcqtat380vhe8awm03f58cqgegsaj")) + XCTAssertTrue( DerivationTool.rustwelding.isValidSaplingExtendedFullViewingKey("zxviewtestsapling1qdxykmuaqqqqpqqg3x5c02p4rhw0rtszr8ln4xl7g6wg6qzsqgn445qsu3cq4vd6l5smlqrckkl2x5rnrauzc4gp665q3zyw0qf2sfdsx5wpp832htfavqk72uchuuvq2dpmgk8jfaza5t5l56u66fpx0sr8ewp9s3wj2txavmhhlazn5rj8mshh470fkrmzg4xarhrqlygg8f486307ujhndwhsw2h7ddzf89k3534aeu0ypz2tjgrzlcqtat380vhe8awm03f58cqgegsaj",networkType: .testnet)) - XCTAssertFalse(try derivationTool.isValidExtendedViewingKey("zxviews1q0dm7hkzky5skvnd9ldwj2u8fz2ry94s5q8p9lyp3j96yckudmp087d2jr2rnfuvjp7f56v78vpe658vljjddj7s645q399jd7")) + XCTAssertFalse( DerivationTool.rustwelding.isValidSaplingExtendedFullViewingKey("zxviews1q0dm7hkzky5skvnd9ldwj2u8fz2ry94s5q8p9lyp3j96yckudmp087d2jr2rnfuvjp7f56v78vpe658vljjddj7s645q399jd7", networkType: .testnet)) } func testDeriveQuiteALotOfUnifiedKeysFromSeed() throws {