Key Validation protocol for DerivationTool
This commit is contained in:
parent
231a7c8401
commit
72c682d8a5
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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"))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue