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

🚀[Release v3.11.1] Merge into Main #212

Merged
merged 19 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ Package.resolved

# Add this line if you want to avoid checking in Xcode SPM integration.
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control

Pods/


# SwiftFormat
BuildTools/.build
Expand All @@ -46,4 +39,6 @@ BuildTools/.swiftpm

# Sensitive Partner API
Modules/litewallet-partner-api-ios
litewallet-partner-api-ios
litewallet-partner-api-ios

/partner-keys.plist
2 changes: 1 addition & 1 deletion Modules/loafwallet-core
96 changes: 68 additions & 28 deletions loafwallet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

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
60 changes: 60 additions & 0 deletions loafwallet/BRAddressExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// BRAddressExtension.swift
// loafwallet
//
// Created by Kerry Washington on 11/4/23.
// Copyright © 2023 Litecoin Foundation. All rights reserved.
//
import BRCore
import Foundation

extension BRAddress: CustomStringConvertible, Hashable {
init?(string: String) {
self.init()
let cStr = [CChar](string.utf8CString)
guard cStr.count <= MemoryLayout<BRAddress>.size else { return nil }
UnsafeMutableRawPointer(mutating: &s).assumingMemoryBound(to: CChar.self).assign(from: cStr,
count: cStr.count)
}

init?(scriptPubKey: [UInt8]) {
self.init()
guard BRAddressFromScriptPubKey(UnsafeMutableRawPointer(mutating: &s).assumingMemoryBound(to: CChar.self),
MemoryLayout<BRAddress>.size, scriptPubKey, scriptPubKey.count) > 0
else { return nil }
}

init?(scriptSig: [UInt8]) {
self.init()
guard BRAddressFromScriptSig(UnsafeMutableRawPointer(mutating: &s).assumingMemoryBound(to: CChar.self),
MemoryLayout<BRAddress>.size, scriptSig, scriptSig.count) > 0 else { return nil }
}

var scriptPubKey: [UInt8]? {
var script = [UInt8](repeating: 0, count: 25)
let count = BRAddressScriptPubKey(&script, script.count,
UnsafeRawPointer([s]).assumingMemoryBound(to: CChar.self))
guard count > 0 else { return nil }
if count < script.count { script.removeSubrange(count...) }
return script
}

var hash160: UInt160? {
var hash = UInt160()
guard BRAddressHash160(&hash, UnsafeRawPointer([s]).assumingMemoryBound(to: CChar.self)) != 0
else { return nil }
return hash
}

public var description: String {
return String(cString: UnsafeRawPointer([s]).assumingMemoryBound(to: CChar.self))
}

public var hashValue: Int {
return BRAddressHash([s])
}

public static func == (l: BRAddress, r: BRAddress) -> Bool {
return BRAddressEq([l.s], [r.s]) != 0
}
}
60 changes: 60 additions & 0 deletions loafwallet/BRCalculationExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import BRCore
import Foundation

extension UInt256: CustomStringConvertible {
public var description: String {
return String(format: "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" +
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
u8.31, u8.30, u8.29, u8.28, u8.27, u8.26, u8.25, u8.24,
u8.23, u8.22, u8.21, u8.20, u8.19, u8.18, u8.17, u8.16,
u8.15, u8.14, u8.13, u8.12, u8.11, u8.10, u8.9, u8.8,
u8.7, u8.6, u8.5, u8.4, u8.3, u8.2, u8.1, u8.0)
}
}

extension UInt128: Equatable {
public static func == (l: UInt128, r: UInt128) -> Bool {
return l.u64 == r.u64
}

public static func != (l: UInt128, r: UInt128) -> Bool {
return l.u64 != r.u64
}
}

extension UInt160: Equatable {
public static func == (l: UInt160, r: UInt160) -> Bool {
return l.u32 == r.u32
}

public static func != (l: UInt160, r: UInt160) -> Bool {
return l.u32 != r.u32
}
}

extension UInt256: Equatable {
public static func == (l: UInt256, r: UInt256) -> Bool {
return l.u64 == r.u64
}

public static func != (l: UInt256, r: UInt256) -> Bool {
return l.u64 != r.u64
}

var hexString: String {
var u = self
return withUnsafePointer(to: &u) { p in
Data(bytes: p, count: MemoryLayout<UInt256>.stride).hexString
}
}
}

extension UInt512: Equatable {
public static func == (l: UInt512, r: UInt512) -> Bool {
return l.u64 == r.u64
}

public static func != (l: UInt512, r: UInt512) -> Bool {
return l.u64 != r.u64
}
}
118 changes: 118 additions & 0 deletions loafwallet/BRKeyExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// BRKeyExtension.swift
// loafwallet
//
// Created by Kerry Washington on 11/4/23.
// Copyright © 2023 Litecoin Foundation. All rights reserved.
//
import BRCore
import Foundation

extension BRKey {
// privKey must be wallet import format (WIF), mini private key format, or hex string
init?(privKey: String) {
self.init()
guard BRKeySetPrivKey(&self, privKey) != 0 else { return nil }
}

// decrypts a BIP38 key using the given passphrase and returns nil if passphrase is incorrect
init?(bip38Key: String, passphrase: String) {
self.init()
guard let nfcPhrase = CFStringCreateMutableCopy(secureAllocator, 0, passphrase as CFString) else { return nil }
CFStringNormalize(nfcPhrase, .C) // NFC unicode normalization
guard BRKeySetBIP38Key(&self, bip38Key, nfcPhrase as String) != 0 else { return nil }
}

// pubKey must be a DER encoded public key
init?(pubKey: [UInt8]) {
self.init()
guard BRKeySetPubKey(&self, pubKey, pubKey.count) != 0 else { return nil }
}

init?(secret: UnsafePointer<UInt256>, compact: Bool) {
self.init()
guard BRKeySetSecret(&self, secret, compact ? 1 : 0) != 0 else { return nil }
}

// recover a pubKey from a compact signature
init?(md: UInt256, compactSig: [UInt8]) {
self.init()
guard BRKeyRecoverPubKey(&self, md, compactSig, compactSig.count) != 0 else { return nil }
}

// WIF private key
mutating func privKey() -> String? {
return autoreleasepool
{ // wrapping in autoreleasepool ensures sensitive memory is wiped and freed immediately
let count = BRKeyPrivKey(&self, nil, 0)
var data = CFDataCreateMutable(secureAllocator, count) as Data
data.count = count
guard data.withUnsafeMutableBytes({ BRKeyPrivKey(&self, $0, count) }) != 0 else { return nil }
return CFStringCreateFromExternalRepresentation(secureAllocator, data as CFData,
CFStringBuiltInEncodings.UTF8.rawValue) as String
}
}

// encrypts key with passphrase
mutating func bip38Key(passphrase: String) -> String? {
return autoreleasepool {
guard let nfcPhrase = CFStringCreateMutableCopy(secureAllocator, 0, passphrase as CFString)
else { return nil }
CFStringNormalize(nfcPhrase, .C) // NFC unicode normalization
let count = BRKeyBIP38Key(&self, nil, 0, nfcPhrase as String)
var data = CFDataCreateMutable(secureAllocator, count) as Data
data.count = count
guard data.withUnsafeMutableBytes({ BRKeyBIP38Key(&self, $0, count, nfcPhrase as String) }) != 0
else { return nil }
return CFStringCreateFromExternalRepresentation(secureAllocator, data as CFData,
CFStringBuiltInEncodings.UTF8.rawValue) as String
}
}

// DER encoded public key
mutating func pubKey() -> [UInt8]? {
var pubKey = [UInt8](repeating: 0, count: BRKeyPubKey(&self, nil, 0))
guard !pubKey.isEmpty, BRKeyPubKey(&self, &pubKey, pubKey.count) == pubKey.count else { return nil }
return pubKey
}

// ripemd160 hash of the sha256 hash of the public key
mutating func hash160() -> UInt160? {
let hash = BRKeyHash160(&self)
guard hash != UInt160() else { return nil }
return hash
}

// pay-to-pubkey-hash litecoin address
mutating func address() -> String? {
var addr = [CChar](repeating: 0, count: MemoryLayout<BRAddress>.size)
guard BRKeyAddress(&self, &addr, addr.count) > 0 else { return nil }
return String(cString: addr)
}

mutating func sign(md: UInt256) -> [UInt8]? {
var sig = [UInt8](repeating: 0, count: 73)
let count = BRKeySign(&self, &sig, sig.count, md)
guard count > 0 else { return nil }
if count < sig.count { sig.removeSubrange(sig.count...) }
return sig
}

mutating func verify(md: UInt256, sig: [UInt8]) -> Bool {
var sig = sig
return BRKeyVerify(&self, md, &sig, sig.count) != 0
}

// wipes key material
mutating func clean() {
BRKeyClean(&self)
}

// Pieter Wuille's compact signature encoding used for bitcoin message signing
// to verify a compact signature, recover a public key from the sig and verify that it matches the signer's pubkey
mutating func compactSign(md: UInt256) -> [UInt8]? {
var sig = [UInt8](repeating: 0, count: 65)
guard BRKeyCompactSign(&self, &sig, sig.count, md) == sig.count else { return nil }
return sig
}
}
12 changes: 12 additions & 0 deletions loafwallet/BRMasterKeyExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import BRCore
import Foundation

extension BRMasterPubKey: Equatable {
public static func == (l: BRMasterPubKey, r: BRMasterPubKey) -> Bool {
return l.fingerPrint == r.fingerPrint && l.chainCode == r.chainCode && l.pubKey == r.pubKey
}

public static func != (l: BRMasterPubKey, r: BRMasterPubKey) -> Bool {
return l.fingerPrint != r.fingerPrint || l.chainCode != r.chainCode || l.pubKey != r.pubKey
}
}
Loading