Merge pull request #254 from zcash/key-validation

Key validation - New checkpoints
This commit is contained in:
Francisco Gindre 2021-02-26 17:50:00 -03:00 committed by GitHub
commit aadc63a419
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 94 additions and 10 deletions

View File

@ -66,10 +66,10 @@ PODS:
- SwiftNIOFoundationCompat (< 3, >= 2.22.0)
- SwiftNIOTLS (< 3, >= 2.22.0)
- SwiftProtobuf (1.12.0)
- ZcashLightClientKit (0.9.2):
- ZcashLightClientKit (0.9.4):
- gRPC-Swift (= 1.0.0-alpha.19)
- SQLite.swift (~> 0.12.2)
- ZcashLightClientKit/Tests (0.9.2):
- ZcashLightClientKit/Tests (0.9.4):
- gRPC-Swift (= 1.0.0-alpha.19)
- SQLite.swift (~> 0.12.2)
@ -145,8 +145,8 @@ SPEC CHECKSUMS:
SwiftNIOTLS: 46bb3a0ff37d6b52ae6baf5207ec3cd411da327c
SwiftNIOTransportServices: 801923921fbecdcde1e1c1ff38e812167d01ead1
SwiftProtobuf: 4ef85479c18ca85b5482b343df9c319c62bda699
ZcashLightClientKit: 56d5c41ecf19d290a2aa25437775f11285edb4fe
ZcashLightClientKit: 7f144177deece40fb9075bf6024028026255f75b
PODFILE CHECKSUM: 7d5095283dc02470f40ab06564d94076ba16d570
COCOAPODS: 1.10.0
COCOAPODS: 1.10.1

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ZcashLightClientKit'
s.version = '0.9.3'
s.version = '0.9.4'
s.summary = 'Zcash Light Client wallet SDK for iOS'
s.description = <<-DESC

View File

@ -539,7 +539,7 @@ public class CompactBlockProcessor {
NotificationCenter.default.post(name: Notification.Name.blockProcessorUpdated,
object: self,
userInfo: [ CompactBlockProcessorNotificationKey.progress : progress,
CompactBlockProcessorNotificationKey.progressHeight : self.latestBlockHeight])
CompactBlockProcessorNotificationKey.progressHeight : completedRange.upperBound])
}
func notifyTransactions(_ txs: [ConfirmedTransactionEntity], in range: BlockRange) {

View File

@ -159,7 +159,9 @@ public class Initializer {
let derivationTool = DerivationTool()
for vk in viewingKeys {
do {
try derivationTool.validateViewingKey(viewingKey: vk)
guard try derivationTool.isValidExtendedViewingKey(vk) else {
throw InitializerError.invalidViewingKey(key: vk)
}
} catch {
throw InitializerError.invalidViewingKey(key: vk)
}

View File

@ -74,6 +74,20 @@ class ZcashRustBackend: ZcashRustBackendWelding {
return true
}
static func isValidExtendedFullViewingKey(_ key: String) throws -> Bool {
guard !key.containsCStringNullBytesBeforeStringEnding() else {
return false
}
guard zcashlc_is_valid_viewing_key([CChar](key.utf8CString)) else {
if let error = lastError() {
throw error
}
return false
}
return true
}
static func initAccountsTable(dbData: URL, seed: [UInt8], accounts: Int32) -> [String]? {
let dbData = dbData.osStr()
var capacity = UInt(0);

View File

@ -48,6 +48,13 @@ public protocol ZcashRustBackendWelding {
*/
static func isValidTransparentAddress(_ address: String) throws -> Bool
/**
- Returns: true when the address is valid and transparent. false in any other case
- Throws: Error when there's another problem not related to validity of the string in question
*/
static func isValidExtendedFullViewingKey(_ key: String) throws -> Bool
/**
initialize the accounts table from a given seed and a number of accounts
- Parameters:

View File

@ -7,6 +7,16 @@
import Foundation
public protocol KeyValidation {
func isValidExtendedViewingKey(_ extvk: String) throws -> Bool
func isValidTransparentAddress(_ tAddress: String) throws -> Bool
func isValidShieldedAddress(_ zAddress: String) throws -> Bool
}
public protocol KeyDeriving {
/**
Given a seed and a number of accounts, return the associated viewing keys.
@ -62,7 +72,6 @@ public protocol KeyDeriving {
// - the underlying implementation needs to be split out into a few lower-level calls
func deriveTransparentAddress(seed: [UInt8]) throws -> String
func validateViewingKey(viewingKey: String) throws
}
public enum KeyDerivationErrors: Error {
@ -200,8 +209,33 @@ public class DerivationTool: KeyDeriving {
}
}
public func validateViewingKey(viewingKey: String) throws {
// TODO
}
extension DerivationTool: KeyValidation {
public func isValidExtendedViewingKey(_ extvk: String) throws -> Bool {
do {
return try rustwelding.isValidExtendedFullViewingKey(extvk)
} catch {
throw KeyDerivationErrors.derivationError(underlyingError: error)
}
}
public func isValidTransparentAddress(_ tAddress: String) throws -> Bool {
do {
return try rustwelding.isValidTransparentAddress(tAddress)
} catch {
throw KeyDerivationErrors.derivationError(underlyingError: error)
}
}
public func isValidShieldedAddress(_ zAddress: String) throws -> Bool {
do {
return try rustwelding.isValidShieldedAddress(zAddress)
} catch {
throw KeyDerivationErrors.derivationError(underlyingError: error)
}
}
}

View File

@ -190,6 +190,8 @@ bool zcashlc_is_valid_shielded_address(const char *address);
*/
bool zcashlc_is_valid_transparent_address(const char *address);
bool zcashlc_is_valid_viewing_key(const char *key);
/**
* Returns the length of the last error message to be logged.
*/

View File

@ -67,4 +67,10 @@ class DerivationToolTests: XCTestCase {
XCTAssertEqual(try DerivationTool.default.deriveTransparentAddress(seed: [UInt8](seedData)), expectedTransparentAddress)
}
func testIsValidViewingKey() throws {
XCTAssertTrue(try DerivationTool.default.isValidExtendedViewingKey("zxviews1q0dm7hkzqqqqpqplzv3f50rl4vay8uy5zg9e92f62lqg6gzu63rljety32xy5tcyenzuu3n386ws772nm6tp4sads8n37gff6nxmyz8dn9keehmapk0spc6pzx5uxepgu52xnwzxxnuja5tv465t9asppnj3eqncu3s7g3gzg5x8ss4ypkw08xwwyj7ky5skvnd9ldwj2u8fz2ry94s5q8p9lyp3j96yckudmp087d2jr2rnfuvjp7f56v78vpe658vljjddj7s645q399jd7"))
XCTAssertFalse(try DerivationTool.default.isValidExtendedViewingKey("zxviews1q0dm7hkzky5skvnd9ldwj2u8fz2ry94s5q8p9lyp3j96yckudmp087d2jr2rnfuvjp7f56v78vpe658vljjddj7s645q399jd7"))
}
}

View File

@ -77,6 +77,10 @@ extension LightWalletServiceMockResponse {
}
class MockRustBackend: ZcashRustBackendWelding {
static func isValidExtendedFullViewingKey(_ key: String) throws -> Bool {
false
}
static func initAccountsTable(dbData: URL, exfvks: [String]) throws -> Bool {
false
}

View File

@ -483,6 +483,21 @@ pub unsafe extern "C" fn zcashlc_is_valid_transparent_address(address: *const c_
unwrap_exc_or(res, false)
}
#[no_mangle]
pub unsafe extern "C" fn zcashlc_is_valid_viewing_key(key: *const c_char) -> bool {
let res = catch_panic(|| {
let vkstr = CStr::from_ptr(key).to_str()?;
match decode_extended_full_viewing_key(HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, &vkstr) {
Ok(s) => match s {
None => Ok(false),
_ => Ok(true),
},
Err(_) => Ok(false),
}
});
unwrap_exc_or(res, false)
}
/// Returns the balance for the account, including all unspent notes that we know about.
#[no_mangle]
pub extern "C" fn zcashlc_get_balance(db_data: *const u8, db_data_len: usize, account: i32) -> i64 {