Skip to content

Commit

Permalink
Merge pull request #1467 from Electric-Coin-Company/derivation-tool-c…
Browse files Browse the repository at this point in the history
…leanup
  • Loading branch information
nuttycom authored Jul 19, 2024
2 parents 9dd5eeb + 6f8d32c commit 0523fa0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 76 deletions.
28 changes: 4 additions & 24 deletions Sources/ZcashLightClientKit/Rust/ZcashKeyDerivationBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ struct ZcashKeyDerivationBackend: ZcashKeyDerivationBackendWelding {

// MARK: Address metadata and validation
static func getAddressMetadata(_ address: String) -> AddressMetadata? {
guard !address.containsCStringNullBytesBeforeStringEnding() else {
return nil
}

var networkId: UInt32 = 0
var addrId: UInt32 = 0
guard zcashlc_get_address_metadata(
Expand Down Expand Up @@ -63,14 +67,6 @@ struct ZcashKeyDerivationBackend: ZcashKeyDerivationBackendWelding {
return typecodes
}

func isValidSaplingAddress(_ address: String) -> Bool {
guard !address.containsCStringNullBytesBeforeStringEnding() else {
return false
}

return zcashlc_is_valid_sapling_address([CChar](address.utf8CString), networkType.networkId)
}

func isValidSaplingExtendedFullViewingKey(_ key: String) -> Bool {
guard !key.containsCStringNullBytesBeforeStringEnding() else {
return false
Expand All @@ -87,22 +83,6 @@ struct ZcashKeyDerivationBackend: ZcashKeyDerivationBackendWelding {
return zcashlc_is_valid_sapling_extended_spending_key([CChar](key.utf8CString), networkType.networkId)
}

func isValidTransparentAddress(_ address: String) -> Bool {
guard !address.containsCStringNullBytesBeforeStringEnding() else {
return false
}

return zcashlc_is_valid_transparent_address([CChar](address.utf8CString), networkType.networkId)
}

func isValidUnifiedAddress(_ address: String) -> Bool {
guard !address.containsCStringNullBytesBeforeStringEnding() else {
return false
}

return zcashlc_is_valid_unified_address([CChar](address.utf8CString), networkType.networkId)
}

func isValidUnifiedFullViewingKey(_ key: String) -> Bool {
guard !key.containsCStringNullBytesBeforeStringEnding() else {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ protocol ZcashKeyDerivationBackendWelding {
/// - `rustRustReceiverTypecodesOnUnifiedAddressMalformed` if getting typecodes for unified address fails.
func receiverTypecodesOnUnifiedAddress(_ address: String) throws -> [UInt32]

/// Validates the if the given string is a valid Sapling Address
/// - Parameter address: UTF-8 encoded String to validate
/// - Returns: true when the address is valid. Returns false in any other case
func isValidSaplingAddress(_ address: String) -> Bool

/// Validates the if the given string is a valid Sapling Extended Full Viewing Key
/// - Parameter key: UTF-8 encoded String to validate
/// - Returns: `true` when the Sapling Extended Full Viewing Key is valid. `false` in any other case
Expand All @@ -40,16 +35,6 @@ protocol ZcashKeyDerivationBackendWelding {
/// - parameter key: String encoded Extended Spending Key
func isValidSaplingExtendedSpendingKey(_ key: String) -> Bool

/// Validates the if the given string is a valid Transparent Address
/// - Parameter address: UTF-8 encoded String to validate
/// - Returns: true when the address is valid and transparent. false in any other case
func isValidTransparentAddress(_ address: String) -> Bool

/// validates whether a string encoded address is a valid Unified Address.
/// - Parameter address: UTF-8 encoded String to validate
/// - Returns: true when the address is valid and transparent. false in any other case
func isValidUnifiedAddress(_ address: String) -> Bool

/// verifies that the given string-encoded `UnifiedFullViewingKey` is valid.
/// - Parameter ufvk: UTF-8 encoded String to validate
/// - Returns: true when the encoded string is a valid UFVK. false in any other case
Expand Down
18 changes: 13 additions & 5 deletions Sources/ZcashLightClientKit/Tool/DerivationTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,23 @@ extension DerivationTool: KeyValidation {
}

public func isValidUnifiedAddress(_ unifiedAddress: String) -> Bool {
backend.isValidUnifiedAddress(unifiedAddress)
DerivationTool.getAddressMetadata(unifiedAddress).map {
$0.networkType == backend.networkType && $0.addressType == AddressType.unified
} ?? false
}

public func isValidTransparentAddress(_ tAddress: String) -> Bool {
backend.isValidTransparentAddress(tAddress)
DerivationTool.getAddressMetadata(tAddress).map {
$0.networkType == backend.networkType && (
$0.addressType == AddressType.p2pkh || $0.addressType == AddressType.p2sh
)
} ?? false
}

public func isValidSaplingAddress(_ zAddress: String) -> Bool {
backend.isValidSaplingAddress(zAddress)
DerivationTool.getAddressMetadata(zAddress).map {
$0.networkType == backend.networkType && $0.addressType == AddressType.sapling
} ?? false
}

public func isValidSaplingExtendedSpendingKey(_ extsk: String) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,70 @@ class DerivationToolTestnetTests: XCTestCase {
)
}

func testIsValidTransparentAddressWrongNetwork() {
XCTAssertFalse(
derivationTool.isValidTransparentAddress(
"t1dRJRY7GmyeykJnMH38mdQoaZtFhn1QmGz"
)
)
}

func testIsValidTransparentAddressFalse() {
XCTAssertFalse(
derivationTool.isValidTransparentAddress(
"ztestsapling12k9m98wmpjts2m56wc60qzhgsfvlpxcwah268xk5yz4h942sd58jy3jamqyxjwums6hw7kfa4cc"
)
)
}

func testIsValidTransparentAddressTrue() {
XCTAssertTrue(
derivationTool.isValidTransparentAddress("tmSwpioc7reeoNrYB9SKpWkurJz3yEj3ee7")
)
}

func testIsValidSaplingAddressTrue() {
XCTAssertTrue(
derivationTool.isValidSaplingAddress(
"ztestsapling12k9m98wmpjts2m56wc60qzhgsfvlpxcwah268xk5yz4h942sd58jy3jamqyxjwums6hw7kfa4cc"
)
)
}

func testIsValidSaplingAddressWrongNetwork() {
XCTAssertFalse(
derivationTool.isValidSaplingAddress(
"zs1vp7kvlqr4n9gpehztr76lcn6skkss9p8keqs3nv8avkdtjrcctrvmk9a7u494kluv756jeee5k0"
)
)
}

func testIsValidSaplingAddressFalse() {
XCTAssertFalse(
derivationTool.isValidSaplingAddress("tmSwpioc7reeoNrYB9SKpWkurJz3yEj3ee7")
)
}

func testIsValidUnifiedAddressTrue() {
XCTAssertTrue(
derivationTool.isValidUnifiedAddress(testRecipientAddress.encoding)
)
}

func testIsValidUnifiedAddressWrongNetwork() {
XCTAssertFalse(
derivationTool.isValidUnifiedAddress(
"u1l9f0l4348negsncgr9pxd9d3qaxagmqv3lnexcplmufpq7muffvfaue6ksevfvd7wrz7xrvn95rc5zjtn7ugkmgh5rnxswmcj30y0pw52pn0zjvy38rn2esfgve64rj5pcmazxgpyuj"
)
)
}

func testIsValidUnifiedAddressFalse() {
XCTAssertFalse(
derivationTool.isValidUnifiedAddress("tmSwpioc7reeoNrYB9SKpWkurJz3yEj3ee7")
)
}

func testIsValidViewingKey() throws {
XCTAssertTrue(
ZcashKeyDerivationBackend(networkType: .testnet).isValidSaplingExtendedFullViewingKey(
Expand Down
32 changes: 0 additions & 32 deletions Tests/OfflineTests/ZcashRustBackendTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,38 +64,6 @@ class ZcashRustBackendTests: XCTestCase {
} catch { }
}

func testIsValidTransparentAddressFalse() {
XCTAssertFalse(
ZcashKeyDerivationBackend(networkType: networkType).isValidTransparentAddress(
"ztestsapling12k9m98wmpjts2m56wc60qzhgsfvlpxcwah268xk5yz4h942sd58jy3jamqyxjwums6hw7kfa4cc"
)
)
}

func testIsValidTransparentAddressTrue() {
XCTAssertTrue(
ZcashKeyDerivationBackend(networkType: networkType).isValidTransparentAddress(
"tmSwpioc7reeoNrYB9SKpWkurJz3yEj3ee7"
)
)
}

func testIsValidSaplingAddressTrue() {
XCTAssertTrue(
ZcashKeyDerivationBackend(networkType: networkType).isValidSaplingAddress(
"ztestsapling12k9m98wmpjts2m56wc60qzhgsfvlpxcwah268xk5yz4h942sd58jy3jamqyxjwums6hw7kfa4cc"
)
)
}

func testIsValidSaplingAddressFalse() {
XCTAssertFalse(
ZcashKeyDerivationBackend(networkType: networkType).isValidSaplingAddress(
"tmSwpioc7reeoNrYB9SKpWkurJz3yEj3ee7"
)
)
}

func testListTransparentReceivers() async throws {
let testVector = [TestVector](TestVector.testVectors![0 ... 2])
let tempDBs = TemporaryDbBuilder.build()
Expand Down

0 comments on commit 0523fa0

Please sign in to comment.