Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#207 resolve core update issue missing labels #211

Merged
merged 9 commits into from
Nov 4, 2023
1 change: 0 additions & 1 deletion loafwallet/BRAPIClient+Wallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ extension BRAPIClient {
func exchangeRates(isFallback: Bool = false, _ handler: @escaping (_ rates: [Rate], _ error: String?) -> Void)
{
let request = isFallback ? URLRequest(url: URL(string: fallbackRatesURL)!) : URLRequest(url: URL(string: APIServer.baseUrl + "v1/rates")!)
print("::: request: \(request.debugDescription)")
_ = dataTaskWithRequest(request) { data, _, error in
if error == nil, let data = data,
let parsedData = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)
Expand Down
3 changes: 2 additions & 1 deletion loafwallet/TransactionCellViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class TransactionCellViewModel: ObservableObject {

feeText = transaction.amountDetails(isLtcSwapped: isLtcSwapped, rate: rate, rates: [rate], maxDigits: maxDigits)

addressText = String(format: transaction.direction.addressTextFormat, transaction.toAddress ?? "")
addressText = String(format: transaction.direction.addressTextFormat, transaction.toAddress ?? "DEBUG-UPDATED-ADDRESS")
print("::: toAddress: \(transaction.toAddress) raw : \(transaction.rawTransaction)")

if transaction.direction == .sent {
directionImageText = "arrowtriangle.up.circle.fill"
Expand Down
56 changes: 53 additions & 3 deletions loafwallet/src/BRCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ private func secureReallocate(ptr: UnsafeMutableRawPointer?, newsize: CFIndex, h
return newptr
}

/// Converts CChar to Int8 and String
/// - Parameter tuple: CChar
/// - Returns: String
private func charInt8ToString(tuple: (CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar)) -> String {
var tuplesArray = [unichar(tuple.0), unichar(tuple.1), unichar(tuple.2), unichar(tuple.3),
unichar(tuple.4), unichar(tuple.5), unichar(tuple.6), unichar(tuple.7),
unichar(tuple.8), unichar(tuple.9), unichar(tuple.10), unichar(tuple.11),
unichar(tuple.12), unichar(tuple.13), unichar(tuple.14), unichar(tuple.15),
unichar(tuple.16), unichar(tuple.17), unichar(tuple.18), unichar(tuple.19),
unichar(tuple.20), unichar(tuple.21), unichar(tuple.22), unichar(tuple.23),
unichar(tuple.23), unichar(tuple.24), unichar(tuple.25), unichar(tuple.26),
unichar(tuple.27), unichar(tuple.28), unichar(tuple.29), unichar(tuple.30),
unichar(tuple.31), unichar(tuple.32), unichar(tuple.33), unichar(tuple.34),
unichar(tuple.35), unichar(tuple.36), unichar(tuple.37), unichar(tuple.38),
unichar(tuple.39), unichar(tuple.40), unichar(tuple.41), unichar(tuple.42),
unichar(tuple.43), unichar(tuple.44), unichar(tuple.45), unichar(tuple.46),
unichar(tuple.47), unichar(tuple.48), unichar(tuple.49), unichar(tuple.50),
unichar(tuple.51), unichar(tuple.52), unichar(tuple.53), unichar(tuple.54),
unichar(tuple.55), unichar(tuple.56), unichar(tuple.57), unichar(tuple.58),
unichar(tuple.59), unichar(tuple.60), unichar(tuple.61), unichar(tuple.62),
unichar(tuple.63), unichar(tuple.64), unichar(tuple.65), unichar(tuple.66),
unichar(tuple.67), unichar(tuple.68), unichar(tuple.69), unichar(tuple.70),
unichar(tuple.71), unichar(tuple.72), unichar(tuple.73), unichar(tuple.74)]

let length = tuplesArray.reduce(0) { $1 != 0 ? $0 + 1 : $0 }
return String(NSString(characters: tuplesArray, length: length))
}

// since iOS does not page memory to disk, all we need to do is cleanse allocated memory prior to deallocation
public let secureAllocator: CFAllocator = {
var context = CFAllocatorContext()
Expand Down Expand Up @@ -210,6 +238,14 @@ extension BRTxInput {
set { BRTxInputSetAddress(&self, newValue) }
}

var updatedSwiftAddress: String {
get {
print(":::Receved: \(charInt8ToString(tuple: address))")
return charInt8ToString(tuple: address)
}
set { BRTxInputSetAddress(&self, newValue) }
}

var swiftScript: [UInt8] {
get { return [UInt8](UnsafeBufferPointer(start: script, count: scriptLen)) }
set { BRTxInputSetScript(&self, newValue, newValue.count) }
Expand All @@ -227,6 +263,15 @@ extension BRTxOutput {
set { BRTxOutputSetAddress(&self, newValue) }
}

var updatedSwiftAddress: String {
get {
print(":::Output: \(charInt8ToString(tuple: address))")

return charInt8ToString(tuple: address)
}
set { BRTxOutputSetAddress(&self, newValue) }
}

var swiftScript: [UInt8] {
get { return [UInt8](UnsafeBufferPointer(start: script, count: scriptLen)) }
set { BRTxOutputSetScript(&self, newValue, newValue.count) }
Expand Down Expand Up @@ -511,15 +556,20 @@ class BRPeerManager {
let cPtr: OpaquePointer
let listener: BRPeerManagerListener
let mainNetParams = [BRMainNetParams]
var falsePositiveRate: Double

init?(wallet: BRWallet, earliestKeyTime: TimeInterval, blocks: [BRBlockRef?], peers: [BRPeer],
listener: BRPeerManagerListener)
init?(wallet: BRWallet,
earliestKeyTime: TimeInterval,
blocks: [BRBlockRef?], peers: [BRPeer],
listener: BRPeerManagerListener,
fpRate: Double)
{
var blockRefs = blocks
guard let cPtr = BRPeerManagerNew(mainNetParams, wallet.cPtr, UInt32(earliestKeyTime + NSTimeIntervalSince1970),
&blockRefs, blockRefs.count, peers, peers.count) else { return nil }
&blockRefs, blockRefs.count, peers, peers.count, fpRate) else { return nil }
self.listener = listener
self.cPtr = cPtr
falsePositiveRate = fpRate

BRPeerManagerSetCallbacks(cPtr, Unmanaged.passUnretained(self).toOpaque(),
{ info in // syncStarted
Expand Down
13 changes: 13 additions & 0 deletions loafwallet/src/Constants/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,16 @@ struct AppVersion {
static let versionNumber = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
static let string = "v" + versionNumber! + " (\(buildNumber!))"
}

/// False Positive Rates
/// The rate at which the requested numner of false
/// addresses are sent to the syncing node. The more
/// fp sent the less likely that the node cannot
/// identify the Litewallet user. Used when deploying the
/// Bloom Filter. The 3 options are from testing ideal
/// rates.
enum FalsePositiveRates: Double {
case lowPrivacy = 0.00005
case semiPrivate = 0.00008
case anonymous = 0.0005
}
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ class SendViewController: UIViewController, Subscriber, ModalPresentable, Tracka
guard let firstOutput = protoReq.details.outputs.first else { return }
guard let wallet = walletManager.wallet else { return }

let address = firstOutput.swiftAddress
let address = firstOutput.updatedSwiftAddress
let isValid = protoReq.isValid()
var isOutputTooSmall = false

Expand Down
40 changes: 30 additions & 10 deletions loafwallet/src/ViewModels/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,40 @@ class Transaction {
lazy var toAddress: String? = {
switch self.direction {
case .sent:
guard let output = self.tx.outputs.filter({ output in
!self.wallet.containsAddress(output.swiftAddress)
}).first else { return nil }
return output.swiftAddress

guard let output = self
.tx.outputs.filter({ output in
!self.wallet.containsAddress(output.updatedSwiftAddress)
})
.first

else {
LWAnalytics.logEventWithParameters(itemName: ._20200112_ERR)
return nil
}

return output.updatedSwiftAddress

case .received:
guard let output = self.tx.outputs.filter({ output in
self.wallet.containsAddress(output.swiftAddress)
}).first else { return nil }
return output.swiftAddress

guard let output = self
.tx
.outputs
.filter({ output in
self.wallet.containsAddress(output.updatedSwiftAddress)
}).first

else {
LWAnalytics.logEventWithParameters(itemName: ._20200112_ERR)
return nil
}
return output.updatedSwiftAddress

case .moved:
guard let output = self.tx.outputs.filter({ output in
self.wallet.containsAddress(output.swiftAddress)
self.wallet.containsAddress(output.updatedSwiftAddress)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@losh11 here is where the problem happens

}).first else { return nil }
return output.swiftAddress
return output.updatedSwiftAddress
}
}()

Expand Down
12 changes: 10 additions & 2 deletions loafwallet/src/WalletManager+Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ extension WalletManager: WalletAuthenticator {
let mpkData: Data? = try keychainItem(key: KeychainKey.masterPubKey)
guard let masterPubKey = mpkData?.masterPubKey
else {
try self.init(masterPubKey: BRMasterPubKey(), earliestKeyTime: 0, dbPath: dbPath, store: store)
try self.init(masterPubKey: BRMasterPubKey(),
earliestKeyTime: 0,
dbPath: dbPath,
store: store,
fpRate: FalsePositiveRates.semiPrivate.rawValue)
return
}

Expand All @@ -69,7 +73,11 @@ extension WalletManager: WalletAuthenticator {
creationTime.withUnsafeBytes { earliestKeyTime = $0.pointee }
}

try self.init(masterPubKey: masterPubKey, earliestKeyTime: earliestKeyTime, dbPath: dbPath, store: store)
try self.init(masterPubKey: masterPubKey,
earliestKeyTime: earliestKeyTime,
dbPath: dbPath,
store: store,
fpRate: FalsePositiveRates.semiPrivate.rawValue)
}

// true if keychain is available and we know that no wallet exists on it
Expand Down
22 changes: 17 additions & 5 deletions loafwallet/src/WalletManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class WalletManager: BRWalletListener, BRPeerManagerListener {
var masterPubKey = BRMasterPubKey()
var earliestKeyTime: TimeInterval = 0

var userPreferredfpRate: Double = FalsePositiveRates.semiPrivate.rawValue

static let sharedInstance: WalletManager = {
var instance: WalletManager?
do {
Expand Down Expand Up @@ -64,13 +66,17 @@ class WalletManager: BRWalletListener, BRPeerManagerListener {
return lazyPeerManager
}

// TODO: Pass the fpRate from User Preferences
internal lazy var lazyPeerManager: BRPeerManager? = {
guard let wallet = self.wallet else { return nil }
return BRPeerManager(wallet: wallet, earliestKeyTime: self.earliestKeyTime,
blocks: self.loadBlocks(), peers: self.loadPeers(), listener: self)
if let wallet = self.wallet {
return BRPeerManager(wallet: wallet, earliestKeyTime: self.earliestKeyTime, blocks: self.loadBlocks(), peers: self.loadPeers(), listener: self, fpRate: FalsePositiveRates.semiPrivate.rawValue)
} else {
return nil
}
}()

internal lazy var lazyWallet: BRWallet? = BRWallet(transactions: self.loadTransactions(), masterPubKey: self.masterPubKey,
internal lazy var lazyWallet: BRWallet? = BRWallet(transactions: self.loadTransactions(),
masterPubKey: self.masterPubKey,
listener: self)

private lazy var lazyAPIClient: BRAPIClient? = {
Expand Down Expand Up @@ -115,14 +121,20 @@ class WalletManager: BRWalletListener, BRPeerManagerListener {
return wordList.map { $0.utf8String }
}

init(masterPubKey: BRMasterPubKey, earliestKeyTime: TimeInterval, dbPath: String? = nil, store: Store) throws
init(masterPubKey: BRMasterPubKey,
earliestKeyTime: TimeInterval,
dbPath: String? = nil,
store: Store,
fpRate: Double) throws
{
self.masterPubKey = masterPubKey
self.earliestKeyTime = earliestKeyTime
self.dbPath = try dbPath ??
FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil,
create: false).appendingPathComponent("BreadWallet.sqlite").path
self.store = store
userPreferredfpRate = fpRate

// open sqlite database
if sqlite3_open_v2(self.dbPath, &db,
SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, nil) != SQLITE_OK
Expand Down