Merge pull request #554 from zcash/fix_UFVK_validation

Fix ufvk validation
This commit is contained in:
Francisco Gindre 2022-10-04 14:45:59 -07:00 committed by GitHub
commit d36c42da8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 114 deletions

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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,

View File

@ -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 {

View File

@ -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 {