Skip to content

Commit

Permalink
FFI lib 0.16.9 update - introduces key-value storage to the wallet li…
Browse files Browse the repository at this point in the history
…brary wrapper class and a corresponding test case. Base node list update. Solution for the startup crash bug re #592 #623.
  • Loading branch information
kukabi committed Oct 28, 2020
1 parent 75c806d commit dcc286e
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 11 deletions.
2 changes: 1 addition & 1 deletion MobileWallet/Backup/ICloudBackup.swift
Expand Up @@ -180,7 +180,7 @@ class ICloudBackup: NSObject {
try? startObserveReachability()

if FileManager.default.ubiquityIdentityToken == nil {
iCloudBackupsIsOn = false
UserDefaults.Key.iCloudBackupsIsOn.set(false)
}
}

Expand Down
1 change: 0 additions & 1 deletion MobileWallet/TariLib/TariLib.swift
Expand Up @@ -289,7 +289,6 @@ class TariLib {

tariWallet = try Wallet(commsConfig: config, loggingFilePath: loggingFilePath)
try setBasenode(syncAfterSetting: false)
//try setBasenode(try BaseNode(TariSettings.shared.defaultBaseNodePool["t-tbn-seoul"]!))

TariEventBus.postToMainThread(.walletServiceStarted)

Expand Down
12 changes: 4 additions & 8 deletions MobileWallet/TariLib/Wrappers/Utils/TariSettings.swift
Expand Up @@ -90,14 +90,10 @@ struct TariSettings {
let bugReportEmail = "bug_reports@tari.com"

let defaultBaseNodePool: [String: String] = [
"faucet": "2e93c460df49d8cfbbf7a06dd9004c25a84f92584f7d0ac5e30bd8e0beee9a43::/onion3/nuuq3e2olck22rudimovhmrdwkmjncxvwdgbvfxhz6myzcnx2j4rssyd:18141",
"t-tbn-nvir": "06e98e9c5eb52bd504836edec1878eccf12eb9f26a5fe5ec0e279423156e657a::/onion3/bsmuof2cn4y2ysz253gzsvg3s72fcgh4f3qcm3hdlxdtcwe6al2dicyd:18141",
"t-tbn-ncal": "3a5081a0c9ff72b2d5cf52f8d78cc5a206d643259cdeb7d934512f519e090e6c::/onion3/gfynjxfm7rcxcekhu6jwuhvyyfdjvmruvvehurmlte565v74oinv2lad:18141",
"t-tbn-oregon": "e6f3c83dc592af45ede5424974f52c776e9e6859e530066e57c1b0dd59d9b61c::/onion3/ixihtmcbvr2q53bj23bsla5hi7ek37odnsxzkapr7znyfneqxzhq7zad:18141",
"t-tbn-london": "ce2254825d0e0294d31a86c6aac18f83c9a7b3d01d9cdb6866b4b2af8fd3fd17::/onion3/gm7kxmr4cyjg5fhcw4onav2ofa3flscrocfxiiohdcys3kshdhcjeuyd:18141",
"t-tbn-stockholm": "461d4d7be657521969896f90e3f611f0c4e902ca33d3b808c03357ad86fd7801::/onion3/4me2aw6auq2ucql34uuvsegtjxmvsmcyk55qprtrpqxvu3whxajvb5ad:18141",
"t-tbn-seoul": "d440b328e69b20dd8ee6c4a61aeb18888939f0f67cf96668840b7f72055d834c::/onion3/j5x7xkcxnrich5lcwibwszd5kylclbf6a5unert5sy6ykio2kphnopad:18141",
"t-tbn-sydney": "b81b4071f72418cc410166d9baf0c6ef7a8c309e64671fafbbed88f7e1ee7709::/onion3/lwwcv4nq7epgem5vdcawom4mquqsw2odbwfcjzv3j6sksx4gr24e52ad:18141"
"t-tbn-nvir": "30c9e74dcf2a3f967457cf10f09b0cb2d0b112b6e98fc76bf64cf27597a6961f::/onion3/zyjmhhwj572dnizdbe672vzzyqfmlzjs4k4mp4bmso6wysn2megndzad:18141",
"t-tbn-oregon": "e856839057aac496b9e25f10821116d02b58f20129e9b9ba681b830568e47c4d::/onion3/exe2zgehnw3tvrbef3ep6taiacr6sdyeb54be2s25fpru357r4skhtad:18141",
"t-tbn-stockholm": "106ca872ec83bc2522bce7e4b35b86c4a598297312cf46ce38caf0b497cf6748::/onion3/hf3n3btfh4tfh2n6afvxs5m6lqkyjlqlj3bm4todjjdcngapa4phhoyd:18141",
"t-tbn-sydney": "ac7fba427913a653a27b69c05549e14d9e87cb7849ea0c740d6c8a5855a3882a::/onion3/avteohvrvvfy4fff7ona2wy6yhb7i4ss4aklp74v64knofvev3vd5yyd:18141"
]

var pushServerApiKey: String?
Expand Down
47 changes: 47 additions & 0 deletions MobileWallet/TariLib/Wrappers/Wallet.swift
Expand Up @@ -600,6 +600,53 @@ class Wallet {
return result
}

func setKeyValue(key: String, value: String) throws -> Bool {
var errorCode: Int32 = -1
let keyPointer = (key as NSString).utf8String
let valuePointer = (value as NSString).utf8String

let result = withUnsafeMutablePointer(to: &errorCode) {
error in
wallet_set_key_value(ptr, keyPointer, valuePointer, error)
}
guard errorCode == 0 else {
throw WalletErrors.generic(errorCode)
}
return result
}

func getKeyValue(key: String) throws -> String {
var errorCode: Int32 = -1
let keyPointer = (key as NSString).utf8String
let resultPtr = withUnsafeMutablePointer(to: &errorCode) {
error in
wallet_get_value(ptr, keyPointer, error)
}
guard errorCode == 0 else {
throw WalletErrors.generic(errorCode)
}

let result = String(cString: resultPtr!)
let mutable = UnsafeMutablePointer<Int8>(mutating: resultPtr!)
string_destroy(mutable)

return result
}

func removeKeyValue(key: String) throws -> Bool {
var errorCode: Int32 = -1
let keyPointer = (key as NSString).utf8String

let result = withUnsafeMutablePointer(to: &errorCode) {
error in
wallet_clear_value(ptr, keyPointer, error)
}
guard errorCode == 0 else {
throw WalletErrors.generic(errorCode)
}
return result
}

deinit {
TariLogger.warn("Wallet destroy")
wallet_destroy(ptr)
Expand Down
74 changes: 74 additions & 0 deletions MobileWallet/TariLib/wallet.h
Expand Up @@ -75,6 +75,12 @@ struct TariSeedWords;

struct EmojiSet;

struct TariExcess;

struct TariExcessPublicNonce;

struct TariExcessSignature;

/// -------------------------------- Transport Types ----------------------------------------------- ///

// Creates a memory transport type
Expand Down Expand Up @@ -237,6 +243,24 @@ bool completed_transaction_is_outbound(struct TariCompletedTransaction *tx,int*
// Frees memory for a TariCompletedTransaction
void completed_transaction_destroy(struct TariCompletedTransaction *transaction);

// Gets the TariExcess of a TariCompletedTransaction
struct TariExcess *completed_transaction_get_excess(struct TariCompletedTransaction *transaction,int* error_out);

// Gets the TariExcessPublicNonce of a TariCompletedTransaction
struct TariExcessPublicNonce *completed_transaction_get_public_nonce(struct TariCompletedTransaction *transaction,int* error_out);

// Gets the TariExcessSignature of a TariCompletedTransaction
struct TariExcessSignature *completed_transaction_get_signature(struct TariCompletedTransaction *transaction,int* error_out);

// Frees memory for a TariExcess
void excess_destroy(struct TariExcess *excess);

// Frees memory for a TariExcessPublicNonce
void nonce_destroy(struct TariExcessPublicNonce *nonce);

// Frees memory for a TariExcessSignature
void signature_destroy(struct TariExcessSignature *signature);

/// -------------------------------- CompletedTransactions ------------------------------------------------------ ///

// Gets number of elements in TariCompletedTransactions
Expand Down Expand Up @@ -515,6 +539,56 @@ void wallet_apply_encryption(struct TariWallet *wallet, const char *passphrase,
// be removed. If it is not encrypted then this function will still succeed to make the operation idempotent
void wallet_remove_encryption(struct TariWallet *wallet, int* error_out);

/// Set a Key Value in the Wallet storage used for Client Key Value store
///
/// ## Arguments
/// `wallet` - The TariWallet pointer.
/// `key` - The pointer to a Utf8 string representing the Key
/// `value` - The pointer to a Utf8 string representing the Value ot be stored
/// `error_out` - Pointer to an int which will be modified to an error code should one occur, may not be null. Functions
/// as an out parameter.
///
/// ## Returns
/// `bool` - Return a boolean value indicating the operation's success or failure. The error_ptr will hold the error
/// code if there was a failure
///
/// # Safety
/// None
bool wallet_set_key_value(struct TariWallet *wallet, const char* key, const char* value, int* error_out);

/// get a stored Value that was previously stored in the Wallet storage used for Client Key Value store
///
/// ## Arguments
/// `wallet` - The TariWallet pointer.
/// `key` - The pointer to a Utf8 string representing the Key
/// `error_out` - Pointer to an int which will be modified to an error code should one occur, may not be null. Functions
/// as an out parameter.
///
/// ## Returns
/// `*mut c_char` - Returns a pointer to a char array of the Value string. Note that it returns an null pointer if an
/// error occured.
///
/// # Safety
/// The ```string_destroy``` method must be called when finished with a string from rust to prevent a memory leak
const char *wallet_get_value(struct TariWallet *wallet, const char* key, int* error_out);

/// Clears a Value for the provided Key Value in the Wallet storage used for Client Key Value store
///
/// ## Arguments
/// `wallet` - The TariWallet pointer.
/// `key` - The pointer to a Utf8 string representing the Key
/// `error_out` - Pointer to an int which will be modified to an error code should one occur, may not be null. Functions
/// as an out parameter.
///
/// ## Returns
/// `bool` - Return a boolean value indicating the operation's success or failure. The error_ptr will hold the error
/// code if there was a failure
///
/// # Safety
/// None
bool wallet_clear_value(struct TariWallet *wallet, const char* key, int* error_out);


// Frees memory for a TariWallet
void wallet_destroy(struct TariWallet *wallet);

Expand Down
16 changes: 16 additions & 0 deletions MobileWalletTests/TariLibWrapperTests.swift
Expand Up @@ -347,6 +347,22 @@ class TariLibWrapperTests: XCTestCase {
XCTAssertThrowsError(try MicroTari(decimalValue: 0.123456789))
}

func testKeyValueStorage() {
TariLogger.info("TEST KEY VALUE STORAGE")
let (wallet, _) = createWallet(privateHex: nil)
// random key
let key = "7SXVVFERUP"
let value = "DQORS7M0EO_⚽🧣👂🤝🏧_X6IZFL5OG3"
// store value
XCTAssert(try wallet.setKeyValue(key: key, value: value))
// get value
XCTAssertEqual(value, try wallet.getKeyValue(key: key))
// clear value
XCTAssert(try wallet.removeKeyValue(key: key))
// value cleared, "get" should throw error
XCTAssertThrowsError(try wallet.getKeyValue(key: key))
}

func restoreWallet(completion: @escaping ((_ wallet: Wallet?, _ error: Error?) -> Void)) {
ICloudBackup.shared.restoreWallet(password: backupPassword, completion: { error in
var commsConfig: CommsConfig?
Expand Down
2 changes: 1 addition & 1 deletion dependencies.env
@@ -1 +1 @@
FFI_VERSION="0.16.4"
FFI_VERSION="0.16.9"

0 comments on commit dcc286e

Please sign in to comment.