2019-05-08 07:47:12 -07:00
|
|
|
//
|
|
|
|
// ZcashRustBackend.swift
|
|
|
|
// ZcashLightClientKit
|
|
|
|
//
|
|
|
|
// Created by Jack Grigg on 5/8/19.
|
|
|
|
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2019-09-09 08:30:38 -07:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
class ZcashRustBackend: ZcashRustBackendWelding {
|
|
|
|
|
2019-11-01 12:59:16 -07:00
|
|
|
static func lastError() -> RustWeldingError? {
|
2019-10-18 11:45:19 -07:00
|
|
|
guard let message = getLastError() else { return nil }
|
2020-02-26 08:54:48 -08:00
|
|
|
zcashlc_clear_last_error()
|
2019-11-26 14:32:20 -08:00
|
|
|
if message.contains("couldn't load Sapling spend parameters") {
|
|
|
|
return RustWeldingError.saplingSpendParametersNotFound
|
2020-04-06 08:54:31 -07:00
|
|
|
} else if message.contains("is not empty") {
|
|
|
|
return RustWeldingError.dataDbNotEmpty
|
2019-11-26 14:32:20 -08:00
|
|
|
}
|
2019-10-18 11:45:19 -07:00
|
|
|
return RustWeldingError.genericError(message: message)
|
2019-05-08 07:47:12 -07:00
|
|
|
}
|
2019-10-18 11:45:19 -07:00
|
|
|
|
|
|
|
static func getLastError() -> String? {
|
2019-05-08 07:47:12 -07:00
|
|
|
let errorLen = zcashlc_last_error_length()
|
|
|
|
if errorLen > 0 {
|
|
|
|
let error = UnsafeMutablePointer<Int8>.allocate(capacity: Int(errorLen))
|
|
|
|
zcashlc_error_message_utf8(error, errorLen)
|
|
|
|
zcashlc_clear_last_error()
|
2019-10-18 13:09:13 -07:00
|
|
|
return String(validatingUTF8: error)
|
2019-05-08 07:47:12 -07:00
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 11:45:19 -07:00
|
|
|
|
|
|
|
/**
|
2020-02-26 08:54:48 -08:00
|
|
|
* Sets up the internal structure of the data database.
|
|
|
|
*/
|
2019-11-01 12:59:16 -07:00
|
|
|
static func initDataDb(dbData: URL) throws {
|
2019-10-18 11:45:19 -07:00
|
|
|
let dbData = dbData.osStr()
|
2019-11-01 12:59:16 -07:00
|
|
|
guard zcashlc_init_data_database(dbData.0, dbData.1) != 0 else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw throwDataDbError(error)
|
|
|
|
}
|
|
|
|
throw RustWeldingError.dataDbInitFailed(message: "unknown error")
|
|
|
|
}
|
2019-05-08 07:47:12 -07:00
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
|
|
|
|
static func isValidShieldedAddress(_ address: String) throws -> Bool {
|
2020-06-08 13:07:48 -07:00
|
|
|
guard !address.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-26 08:54:48 -08:00
|
|
|
guard zcashlc_is_valid_shielded_address([CChar](address.utf8CString)) else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
static func isValidTransparentAddress(_ address: String) throws -> Bool {
|
2020-06-08 13:07:48 -07:00
|
|
|
guard !address.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-26 08:54:48 -08:00
|
|
|
guard zcashlc_is_valid_transparent_address([CChar](address.utf8CString)) else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-22 11:43:28 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
static func initAccountsTable(dbData: URL, seed: [UInt8], accounts: Int32) -> [String]? {
|
|
|
|
let dbData = dbData.osStr()
|
2020-10-08 11:35:15 -07:00
|
|
|
var capacity = UInt(0);
|
2020-10-08 10:43:41 -07:00
|
|
|
let extsksCStr = zcashlc_init_accounts_table(dbData.0, dbData.1, seed, UInt(seed.count), accounts, &capacity)
|
2019-05-08 07:47:12 -07:00
|
|
|
if extsksCStr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 13:09:13 -07:00
|
|
|
let extsks = UnsafeBufferPointer(start: extsksCStr, count: Int(accounts)).compactMap({ (cStr) -> String? in
|
|
|
|
guard let str = cStr else { return nil }
|
|
|
|
return String(cString: str)
|
|
|
|
})
|
2020-10-08 10:43:41 -07:00
|
|
|
zcashlc_vec_string_free(extsksCStr, UInt(accounts), capacity)
|
2019-05-08 07:47:12 -07:00
|
|
|
return extsks
|
|
|
|
}
|
2021-04-05 16:12:24 -07:00
|
|
|
|
2021-04-02 15:18:16 -07:00
|
|
|
static func initAccountsTable(dbData: URL, uvks: [UnifiedViewingKey]) throws -> Bool {
|
2020-10-13 09:15:50 -07:00
|
|
|
let dbData = dbData.osStr()
|
|
|
|
|
2021-04-05 16:12:24 -07:00
|
|
|
var ffiUvks = [FFIUnifiedViewingKey]()
|
|
|
|
for uvk in uvks {
|
2021-04-08 10:18:16 -07:00
|
|
|
guard !uvk.extfvk.containsCStringNullBytesBeforeStringEnding() else {
|
2021-04-05 16:12:24 -07:00
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
guard !uvk.extpub.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
|
2021-04-08 10:18:16 -07:00
|
|
|
let extfvkCStr = [CChar](String(uvk.extfvk).utf8CString)
|
2021-04-05 16:12:24 -07:00
|
|
|
|
|
|
|
let extfvkPtr = UnsafeMutablePointer<CChar>.allocate(capacity: extfvkCStr.count)
|
|
|
|
extfvkPtr.initialize(from: extfvkCStr, count: extfvkCStr.count)
|
|
|
|
|
|
|
|
let extpubCStr = [CChar](String(uvk.extpub).utf8CString)
|
|
|
|
let extpubPtr = UnsafeMutablePointer<CChar>.allocate(capacity: extpubCStr.count)
|
|
|
|
extpubPtr.initialize(from:extpubCStr, count: extpubCStr.count)
|
|
|
|
|
|
|
|
|
|
|
|
ffiUvks.append(FFIUnifiedViewingKey(extfvk: extfvkPtr, extpub: extpubPtr))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var result = false
|
|
|
|
ffiUvks.withContiguousMutableStorageIfAvailable { p in
|
|
|
|
let slice = UnsafeMutablePointer<FFIUVKBoxedSlice>.allocate(capacity: 1)
|
|
|
|
slice.initialize(to: FFIUVKBoxedSlice(ptr: p.baseAddress, len: UInt(p.count)))
|
|
|
|
|
|
|
|
result = zcashlc_init_accounts_table_with_keys(dbData.0, dbData.1, slice)
|
|
|
|
slice.deinitialize(count: 1)
|
|
|
|
// slice.deallocate()
|
|
|
|
}
|
|
|
|
|
|
|
|
defer {
|
|
|
|
for uvk in ffiUvks {
|
|
|
|
uvk.extfvk.deallocate()
|
|
|
|
uvk.extpub.deallocate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2020-10-13 09:15:50 -07:00
|
|
|
|
|
|
|
}
|
2021-04-02 15:18:16 -07:00
|
|
|
// static func initAccountsTable(dbData: URL, exfvks: [String]) throws -> Bool {
|
|
|
|
// let dbData = dbData.osStr()
|
|
|
|
// let viewingKeys = exfvks.map { UnsafePointer(strdup($0)) }
|
|
|
|
//
|
|
|
|
// guard exfvks.count > 0 else {
|
|
|
|
// throw RustWeldingError.malformedStringInput
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// let res = zcashlc_init_accounts_table_with_keys(dbData.0, dbData.1, viewingKeys, UInt(viewingKeys.count));
|
|
|
|
//
|
|
|
|
// viewingKeys.compactMap({ UnsafeMutablePointer(mutating: $0) }).forEach({ free($0) })
|
|
|
|
//
|
|
|
|
// guard res else {
|
|
|
|
// if let error = lastError() {
|
|
|
|
// throw error
|
|
|
|
// }
|
|
|
|
// return false
|
|
|
|
// }
|
|
|
|
// return res
|
|
|
|
//
|
|
|
|
// }
|
2020-10-13 09:15:50 -07:00
|
|
|
|
2019-11-01 12:59:16 -07:00
|
|
|
static func initBlocksTable(dbData: URL, height: Int32, hash: String, time: UInt32, saplingTree: String) throws {
|
2019-10-18 11:45:19 -07:00
|
|
|
let dbData = dbData.osStr()
|
2020-06-08 13:07:48 -07:00
|
|
|
|
|
|
|
guard !hash.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
|
|
|
|
guard !saplingTree.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
|
2019-11-01 12:59:16 -07:00
|
|
|
guard zcashlc_init_blocks_table(dbData.0, dbData.1, height, [CChar](hash.utf8CString), time, [CChar](saplingTree.utf8CString)) != 0 else {
|
|
|
|
if let error = lastError() {
|
2020-04-06 08:54:31 -07:00
|
|
|
throw error
|
2019-11-01 12:59:16 -07:00
|
|
|
}
|
|
|
|
throw RustWeldingError.dataDbInitFailed(message: "Unknown Error")
|
|
|
|
}
|
2019-05-08 07:47:12 -07:00
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
static func getAddress(dbData: URL, account: Int32) -> String? {
|
|
|
|
let dbData = dbData.osStr()
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 13:09:13 -07:00
|
|
|
guard let addressCStr = zcashlc_get_address(dbData.0, dbData.1, account) else { return nil }
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 13:09:13 -07:00
|
|
|
let address = String(validatingUTF8: addressCStr)
|
2019-05-08 07:47:12 -07:00
|
|
|
zcashlc_string_free(addressCStr)
|
|
|
|
return address
|
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
static func getBalance(dbData: URL, account: Int32) -> Int64 {
|
|
|
|
let dbData = dbData.osStr()
|
2019-05-08 07:47:12 -07:00
|
|
|
return zcashlc_get_balance(dbData.0, dbData.1, account)
|
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
static func getVerifiedBalance(dbData: URL, account: Int32) -> Int64 {
|
|
|
|
let dbData = dbData.osStr()
|
2019-05-08 07:47:12 -07:00
|
|
|
return zcashlc_get_verified_balance(dbData.0, dbData.1, account)
|
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2021-03-08 10:47:36 -08:00
|
|
|
static func getVerifiedTransparentBalance(dbData: URL, address: String) throws -> Int64 {
|
|
|
|
guard !address.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
|
|
|
|
let dbData = dbData.osStr()
|
|
|
|
|
|
|
|
return zcashlc_get_verified_transparent_balance(dbData.0, dbData.1, [CChar](address.utf8CString))
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getTransparentBalance(dbData: URL, address: String) throws -> Int64 {
|
|
|
|
guard !address.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
|
|
|
|
let dbData = dbData.osStr()
|
|
|
|
return zcashlc_get_total_transparent_balance(dbData.0, dbData.1, [CChar](address.utf8CString))
|
|
|
|
}
|
2021-04-08 10:18:16 -07:00
|
|
|
static func clearUtxos(dbData: URL, address: String, sinceHeight: BlockHeight = ZcashSDK.SAPLING_ACTIVATION_HEIGHT) throws -> Int32 {
|
|
|
|
let dbData = dbData.osStr()
|
|
|
|
|
|
|
|
guard !address.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = zcashlc_clear_utxos(dbData.0, dbData.1, [CChar](address.utf8CString), Int32(sinceHeight))
|
|
|
|
|
|
|
|
guard result > 0 else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2021-03-08 10:47:36 -08:00
|
|
|
|
|
|
|
static func putUnspentTransparentOutput(dbData: URL, address: String, txid: [UInt8], index: Int, script: [UInt8], value: Int64, height: BlockHeight) throws -> Bool {
|
|
|
|
|
|
|
|
let dbData = dbData.osStr()
|
|
|
|
|
|
|
|
guard !address.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
|
|
|
|
guard zcashlc_put_utxo(dbData.0,
|
|
|
|
dbData.1,
|
|
|
|
[CChar](address.utf8CString),
|
|
|
|
txid,
|
|
|
|
UInt(txid.count),
|
|
|
|
Int32(index),
|
|
|
|
script,
|
|
|
|
UInt(script.count),
|
|
|
|
value,
|
|
|
|
Int32(height)) else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
static func downloadedUtxoBalance(dbData: URL, address: String) throws -> WalletBalance {
|
|
|
|
let verified = try getVerifiedTransparentBalance(dbData: dbData, address: address)
|
|
|
|
let total = try getTransparentBalance(dbData: dbData, address: address)
|
|
|
|
return TransparentBalance(verified: verified, total: total, address: address)
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
static func getReceivedMemoAsUTF8(dbData: URL, idNote: Int64) -> String? {
|
|
|
|
let dbData = dbData.osStr()
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 13:09:13 -07:00
|
|
|
guard let memoCStr = zcashlc_get_received_memo_as_utf8(dbData.0, dbData.1, idNote) else { return nil }
|
|
|
|
|
|
|
|
let memo = String(validatingUTF8: memoCStr)
|
2019-05-08 07:47:12 -07:00
|
|
|
zcashlc_string_free(memoCStr)
|
|
|
|
return memo
|
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
static func getSentMemoAsUTF8(dbData: URL, idNote: Int64) -> String? {
|
|
|
|
let dbData = dbData.osStr()
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 13:09:13 -07:00
|
|
|
guard let memoCStr = zcashlc_get_sent_memo_as_utf8(dbData.0, dbData.1, idNote) else { return nil }
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 13:09:13 -07:00
|
|
|
let memo = String(validatingUTF8: memoCStr)
|
2019-05-08 07:47:12 -07:00
|
|
|
zcashlc_string_free(memoCStr)
|
|
|
|
return memo
|
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
static func validateCombinedChain(dbCache: URL, dbData: URL) -> Int32 {
|
|
|
|
let dbCache = dbCache.osStr()
|
|
|
|
let dbData = dbData.osStr()
|
2019-05-08 07:47:12 -07:00
|
|
|
return zcashlc_validate_combined_chain(dbCache.0, dbCache.1, dbData.0, dbData.1)
|
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2021-04-19 10:07:50 -07:00
|
|
|
static func getNearestRewindHeight(dbData: URL, height: Int32) -> Int32 {
|
|
|
|
let dbData = dbData.osStr()
|
|
|
|
return zcashlc_get_nearest_rewind_height(dbData.0, dbData.1, height)
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
static func rewindToHeight(dbData: URL, height: Int32) -> Bool {
|
|
|
|
let dbData = dbData.osStr()
|
2021-04-19 10:07:50 -07:00
|
|
|
return zcashlc_rewind_to_height(dbData.0, dbData.1, height)
|
2019-05-08 07:47:12 -07:00
|
|
|
}
|
2020-02-26 08:54:48 -08:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
static func scanBlocks(dbCache: URL, dbData: URL) -> Bool {
|
|
|
|
let dbCache = dbCache.osStr()
|
|
|
|
let dbData = dbData.osStr()
|
2019-05-08 07:47:12 -07:00
|
|
|
return zcashlc_scan_blocks(dbCache.0, dbCache.1, dbData.0, dbData.1) != 0
|
|
|
|
}
|
2020-03-26 07:24:47 -07:00
|
|
|
|
|
|
|
static func decryptAndStoreTransaction(dbData: URL, tx: [UInt8]) -> Bool {
|
|
|
|
let dbData = dbData.osStr()
|
|
|
|
return zcashlc_decrypt_and_store_transaction(dbData.0, dbData.1, tx, UInt(tx.count)) != 0
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:02:25 -08:00
|
|
|
static func createToAddress(dbData: URL, account: Int32, extsk: String, to: String, value: Int64, memo: String?, spendParamsPath: String, outputParamsPath: String) -> Int64 {
|
2019-10-18 11:45:19 -07:00
|
|
|
let dbData = dbData.osStr()
|
2019-11-26 14:32:20 -08:00
|
|
|
let memoBytes = memo ?? ""
|
|
|
|
|
2020-06-09 17:23:46 -07:00
|
|
|
return zcashlc_create_to_address(dbData.0,
|
|
|
|
dbData.1,
|
|
|
|
account,
|
2021-02-15 16:56:23 -08:00
|
|
|
[CChar](extsk.utf8CString),
|
|
|
|
[CChar](to.utf8CString),
|
2020-06-09 17:23:46 -07:00
|
|
|
value,
|
2021-02-15 16:56:23 -08:00
|
|
|
[CChar](memoBytes.utf8CString),
|
2020-06-09 17:23:46 -07:00
|
|
|
spendParamsPath,
|
|
|
|
UInt(spendParamsPath.lengthOfBytes(using: .utf8)),
|
|
|
|
outputParamsPath,
|
|
|
|
UInt(outputParamsPath.lengthOfBytes(using: .utf8)))
|
2020-12-22 11:44:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static func shieldFunds(dbCache: URL, dbData: URL, account: Int32, tsk: String, extsk: String, memo: String?, spendParamsPath: String, outputParamsPath: String) -> Int64 {
|
|
|
|
let dbData = dbData.osStr()
|
|
|
|
let memoBytes = memo ?? ""
|
|
|
|
|
|
|
|
return zcashlc_shield_funds(dbData.0,
|
|
|
|
dbData.1,
|
|
|
|
account,
|
2021-02-17 15:02:25 -08:00
|
|
|
[CChar](tsk.utf8CString),
|
|
|
|
[CChar](extsk.utf8CString),
|
|
|
|
[CChar](memoBytes.utf8CString),
|
2020-12-22 11:44:12 -08:00
|
|
|
spendParamsPath,
|
|
|
|
UInt(spendParamsPath.lengthOfBytes(using: .utf8)),
|
|
|
|
outputParamsPath,
|
|
|
|
UInt(outputParamsPath.lengthOfBytes(using: .utf8)))
|
2019-05-08 07:47:12 -07:00
|
|
|
}
|
2019-11-01 12:59:16 -07:00
|
|
|
|
2020-02-26 08:54:48 -08:00
|
|
|
static func deriveExtendedFullViewingKey(_ spendingKey: String) throws -> String? {
|
|
|
|
|
2020-06-08 13:07:48 -07:00
|
|
|
guard !spendingKey.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
|
2020-02-26 08:54:48 -08:00
|
|
|
guard let extsk = zcashlc_derive_extended_full_viewing_key([CChar](spendingKey.utf8CString)) else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-06 08:54:31 -07:00
|
|
|
let derived = String(validatingUTF8: extsk)
|
2020-02-26 08:54:48 -08:00
|
|
|
|
|
|
|
zcashlc_string_free(extsk)
|
|
|
|
return derived
|
|
|
|
}
|
|
|
|
|
2020-10-09 13:58:36 -07:00
|
|
|
static func deriveExtendedFullViewingKeys(seed: [UInt8], accounts: Int32) throws -> [String]? {
|
2020-10-08 11:35:15 -07:00
|
|
|
var capacity = UInt(0);
|
2020-10-09 13:58:36 -07:00
|
|
|
guard let extsksCStr = zcashlc_derive_extended_full_viewing_keys(seed, UInt(seed.count), accounts, &capacity) else {
|
2020-02-26 08:54:48 -08:00
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let extsks = UnsafeBufferPointer(start: extsksCStr, count: Int(accounts)).compactMap({ (cStr) -> String? in
|
|
|
|
guard let str = cStr else { return nil }
|
|
|
|
return String(cString: str)
|
|
|
|
})
|
2020-10-08 10:43:41 -07:00
|
|
|
zcashlc_vec_string_free(extsksCStr, UInt(accounts), capacity)
|
2020-02-26 08:54:48 -08:00
|
|
|
return extsks
|
|
|
|
}
|
|
|
|
|
2020-10-09 13:58:36 -07:00
|
|
|
static func deriveExtendedSpendingKeys(seed: [UInt8], accounts: Int32) throws -> [String]? {
|
2020-10-08 11:35:15 -07:00
|
|
|
var capacity = UInt(0);
|
2020-10-09 13:58:36 -07:00
|
|
|
guard let extsksCStr = zcashlc_derive_extended_spending_keys(seed, UInt(seed.count), accounts, &capacity) else {
|
2020-02-26 08:54:48 -08:00
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let extsks = UnsafeBufferPointer(start: extsksCStr, count: Int(accounts)).compactMap({ (cStr) -> String? in
|
|
|
|
guard let str = cStr else { return nil }
|
|
|
|
return String(cString: str)
|
|
|
|
})
|
2020-10-08 10:43:41 -07:00
|
|
|
zcashlc_vec_string_free(extsksCStr, UInt(accounts), capacity)
|
2020-02-26 08:54:48 -08:00
|
|
|
return extsks
|
|
|
|
}
|
2020-06-09 17:23:46 -07:00
|
|
|
|
2021-04-02 15:18:16 -07:00
|
|
|
static func deriveUnifiedViewingKeyFromSeed(_ seed: [UInt8], numberOfAccounts: Int) throws -> [UnifiedViewingKey] {
|
|
|
|
|
|
|
|
guard let uvks_struct = zcashlc_derive_unified_viewing_keys_from_seed(seed, UInt(seed.count), Int32(numberOfAccounts)) else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
throw RustWeldingError.unableToDeriveKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
let uvks_size = uvks_struct.pointee.len
|
|
|
|
guard let uvks_array_pointer = uvks_struct.pointee.ptr, uvks_size > 0 else {
|
|
|
|
throw RustWeldingError.unableToDeriveKeys
|
|
|
|
}
|
|
|
|
var uvks = [UnifiedViewingKey]()
|
|
|
|
|
|
|
|
for i: Int in 0 ..< Int(uvks_size) {
|
|
|
|
let itemPointer = uvks_array_pointer.advanced(by: i)
|
|
|
|
|
|
|
|
guard let extfvk = String(validatingUTF8: itemPointer.pointee.extfvk) else {
|
|
|
|
throw RustWeldingError.unableToDeriveKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let extpub = String(validatingUTF8: itemPointer.pointee.extpub) else {
|
|
|
|
throw RustWeldingError.unableToDeriveKeys
|
|
|
|
}
|
|
|
|
|
2021-04-08 10:18:16 -07:00
|
|
|
uvks.append(UVK(extfvk: extfvk, extpub: extpub))
|
2021-04-02 15:18:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
zcashlc_free_uvk_array(uvks_struct)
|
|
|
|
|
|
|
|
return uvks
|
|
|
|
}
|
|
|
|
|
2020-10-09 13:58:36 -07:00
|
|
|
static func deriveShieldedAddressFromSeed(seed: [UInt8], accountIndex: Int32) throws -> String? {
|
|
|
|
guard let zaddrCStr = zcashlc_derive_shielded_address_from_seed(seed, UInt(seed.count), accountIndex) else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let zAddr = String(validatingUTF8: zaddrCStr)
|
|
|
|
|
|
|
|
zcashlc_string_free(zaddrCStr)
|
|
|
|
|
|
|
|
return zAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
static func deriveShieldedAddressFromViewingKey(_ extfvk: String) throws -> String? {
|
|
|
|
guard !extfvk.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let zaddrCStr = zcashlc_derive_shielded_address_from_viewing_key([CChar](extfvk.utf8CString)) else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let zAddr = String(validatingUTF8: zaddrCStr)
|
|
|
|
|
|
|
|
zcashlc_string_free(zaddrCStr)
|
|
|
|
|
|
|
|
return zAddr
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:02:25 -08:00
|
|
|
static func deriveTransparentAddressFromSeed(seed: [UInt8], account: Int, index: Int) throws -> String? {
|
2020-10-09 13:58:36 -07:00
|
|
|
|
2021-02-17 15:02:25 -08:00
|
|
|
guard let tAddrCStr = zcashlc_derive_transparent_address_from_seed(seed, UInt(seed.count), Int32(account), Int32(index)) else {
|
2020-10-09 13:58:36 -07:00
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let tAddr = String(validatingUTF8: tAddrCStr)
|
|
|
|
|
|
|
|
return tAddr
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:02:25 -08:00
|
|
|
static func deriveTransparentPrivateKeyFromSeed(seed: [UInt8], account: Int, index: Int) throws -> String? {
|
|
|
|
guard let skCStr = zcashlc_derive_transparent_private_key_from_seed(seed, UInt(seed.count), Int32(account), Int32(index)) else {
|
2020-12-11 13:01:02 -08:00
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let sk = String(validatingUTF8: skCStr)
|
|
|
|
|
|
|
|
return sk
|
|
|
|
}
|
|
|
|
|
2021-04-02 15:18:16 -07:00
|
|
|
static func derivedTransparentAddressFromPublicKey(_ pubkey: String) throws -> String {
|
|
|
|
guard !pubkey.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let tAddrCStr = zcashlc_derive_transparent_address_from_public_key([CChar](pubkey.utf8CString)), let tAddr = String(validatingUTF8: tAddrCStr) else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
throw RustWeldingError.unableToDeriveKeys
|
|
|
|
}
|
|
|
|
return tAddr
|
|
|
|
}
|
|
|
|
|
2020-12-23 15:01:09 -08:00
|
|
|
static func deriveTransparentAddressFromSecretKey(_ tsk: String) throws -> String? {
|
|
|
|
|
|
|
|
guard !tsk.containsCStringNullBytesBeforeStringEnding() else {
|
|
|
|
throw RustWeldingError.malformedStringInput
|
|
|
|
}
|
|
|
|
guard let tAddrCStr = zcashlc_derive_transparent_address_from_secret_key([CChar](tsk.utf8CString)) else {
|
|
|
|
if let error = lastError() {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let tAddr = String(validatingUTF8: tAddrCStr)
|
|
|
|
|
|
|
|
return tAddr
|
|
|
|
}
|
|
|
|
|
2020-06-09 17:23:46 -07:00
|
|
|
static func consensusBranchIdFor(height: Int32) throws -> Int32 {
|
|
|
|
let branchId = zcashlc_branch_id_for_height(height)
|
|
|
|
|
|
|
|
guard branchId != -1 else {
|
|
|
|
throw RustWeldingError.noConsensusBranchId(height: height)
|
|
|
|
}
|
|
|
|
|
|
|
|
return branchId
|
|
|
|
}
|
2020-12-23 15:01:09 -08:00
|
|
|
|
2019-11-01 12:59:16 -07:00
|
|
|
}
|
|
|
|
|
2021-04-08 10:18:16 -07:00
|
|
|
private struct UVK: UnifiedViewingKey {
|
|
|
|
var extfvk: ExtendedFullViewingKey
|
|
|
|
var extpub: ExtendedPublicKey
|
|
|
|
}
|
|
|
|
|
2019-11-01 12:59:16 -07:00
|
|
|
private extension ZcashRustBackend {
|
|
|
|
static func throwDataDbError(_ error: RustWeldingError) -> Error {
|
|
|
|
|
|
|
|
if case RustWeldingError.genericError(let message) = error, message.contains("is not empty") {
|
|
|
|
return RustWeldingError.dataDbNotEmpty
|
|
|
|
}
|
|
|
|
return RustWeldingError.dataDbInitFailed(message: error.localizedDescription)
|
|
|
|
}
|
2020-06-09 17:23:46 -07:00
|
|
|
|
2019-05-08 07:47:12 -07:00
|
|
|
}
|
2019-10-18 11:45:19 -07:00
|
|
|
|
|
|
|
private extension URL {
|
|
|
|
|
|
|
|
func osStr() -> (String, UInt) {
|
|
|
|
let path = self.absoluteString
|
|
|
|
return (path, UInt(path.lengthOfBytes(using: .utf8)))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-06-08 13:07:48 -07:00
|
|
|
|
|
|
|
extension String {
|
|
|
|
|
|
|
|
/**
|
|
|
|
Checks whether this string contains null bytes before it's real ending
|
|
|
|
*/
|
|
|
|
func containsCStringNullBytesBeforeStringEnding() -> Bool {
|
|
|
|
self.utf8CString.firstIndex(of: 0) != (self.utf8CString.count - 1)
|
|
|
|
}
|
|
|
|
}
|