ZcashLightClientKit/Example/ZcashLightClientSample/ZcashLightClientSample/Get UTXOs/GetUTXOsViewController.swift

76 lines
2.5 KiB
Swift
Raw Normal View History

2020-12-09 15:58:33 -08:00
//
// GetUTXOsViewController.swift
// ZcashLightClientSample
//
// Created by Francisco Gindre on 12/9/20.
// Copyright © 2020 Electric Coin Company. All rights reserved.
//
import UIKit
import ZcashLightClientKit
import KRProgressHUD
class GetUTXOsViewController: UIViewController {
2021-04-02 15:18:16 -07:00
@IBOutlet weak var transparentAddressLabel: UILabel!
@IBOutlet weak var verifiedBalanceLabel: UILabel!
@IBOutlet weak var totalBalanceLabel: UILabel!
2020-12-09 15:58:33 -08:00
@IBOutlet weak var messageLabel: UILabel!
2021-04-02 15:18:16 -07:00
@IBOutlet weak var shieldFundsButton: UIButton!
2021-09-17 06:49:58 -07:00
2020-12-09 15:58:33 -08:00
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
func updateUI() {
let synchronizer = SDKSynchronizer.shared
let tAddress = synchronizer.getTransparentAddress(accountIndex: 0)?.stringEncoded ?? "no t-address found"
2021-09-17 06:49:58 -07:00
self.transparentAddressLabel.text = tAddress
2021-09-17 06:49:58 -07:00
Task { @MainActor in
// swiftlint:disable:next force_try
let balance = try! await AppDelegate.shared.sharedSynchronizer.getTransparentBalance(accountIndex: 0)
self.totalBalanceLabel.text = NumberFormatter.zcashNumberFormatter.string(from: NSNumber(value: balance.total.amount))
self.verifiedBalanceLabel.text = NumberFormatter.zcashNumberFormatter.string(from: NSNumber(value: balance.verified.amount))
}
2020-12-09 15:58:33 -08:00
}
2020-12-23 15:01:55 -08:00
@IBAction func shieldFunds(_ sender: Any) {
do {
2021-09-15 05:21:29 -07:00
let derivationTool = DerivationTool(networkType: kZcashNetwork.networkType)
[#461] Adopt a Type-Safe Keys and Addresses API This PR creates data types for Addresses and Keys so that they are not represented by Strings anymore. This avoids mistakenly use the wrong keys because they are all alike for the type system. New Protocols: ============= StringEncoded -> Protocol that makes a type can be expressed in an string-encoded fashion either for UI or Interchange purposes. Undescribable -> A protocol that implements methods that override default decriptions used by debuggers, loggers and event trackers to avoid types conforming to it to be leaked to logs. Deleted Protocols: ================== UnifiedFullViewingKey --> turned into a struct. UnifiedAddress --> turned into a struct new Error Type: ================ ```` enum KeyEncodingError: Error { case invalidEncoding } ```` This error is thrown when an Address or Key type (addresses are public keys in the end) can be decoded from their String representation, typically upon initialization from a User input. New Types: ========= SaplingExtendedSpendingKey -> Type for Sapling Extended Full Viewing Keys this type will be replaced with Unified Spending Keys soon. SaplingExtendedFullViewingKey -> Extended Full Viewing Key for Sapling. Maintains existing funcionality. Will be probably deprecated in favor of UFVK. TransparentAccountPrivKey -> Private key for transparent account. Used only for shielding operations. Note: this will probably be deprecated soon. UnifiedFullViewingKey -> Replaces the protocol that had the same name. TransparentAddress -> Replaces a type alias with a struct SaplingAddress --> Represents a Sapling receiver address. Comonly called zAddress. This address corresponds to the Zcash Sapling shielded pool. Although this it is fully functional, we encourage developers to choose `UnifiedAddress` before Sapling or Transparent ones. UnifiedAddress -> Represents a UA. String-encodable and Equatable. Use of UAs must be favored instead of individual receivers for different pools. This type can't be decomposed into their Receiver types yet. Recipient -> This represents all valid receiver types to be used as inputs for outgoing transactions. ```` public enum Recipient: Equatable, StringEncoded { case transparent(TransparentAddress) case sapling(SaplingAddress) case unified(UnifiedAddress) ```` The wrapped concrete receiver is a valid receiver type. Deleted Type Aliases: ===================== The following aliases were deleted and turned into types ```` public typealias TransparentAddress = String public typealias SaplingShieldedAddress = String ```` Changes to Derivation Tool ========================== DerivationTool has been changed to accomodate this new types and remove Strings whenever possible. Changes to Synchronizer and CompactBlockProcessor ================================================= Accordingly these to components have been modified to accept the new types intead of strings when possible. Changes to Demo App =================== The demo App has been patch to compile and work with the new types. Developers must consider that the use (and abuse) of forced_try and forced unwrapping is a "license" that maintainers are using for the sake of brevity. We consider that clients of this SDK do know how to handle Errors and Optional and it is not the objective of the demo code to show good practices on those matters. Closes #461
2022-08-20 15:10:22 -07:00
let usk = try derivationTool.deriveUnifiedSpendingKey(seed: DemoAppConfig.seed, accountIndex: 0)
2021-09-17 06:49:58 -07:00
2020-12-23 15:01:55 -08:00
KRProgressHUD.showMessage("🛡 Shielding 🛡")
2021-09-17 06:49:58 -07:00
Task { @MainActor in
let transaction = try await AppDelegate.shared.sharedSynchronizer.shieldFunds(
2022-10-04 08:32:32 -07:00
spendingKey: usk,
memo: try Memo(string: "shielding is fun!")
)
KRProgressHUD.dismiss()
self.messageLabel.text = "funds shielded \(transaction)"
}
2020-12-23 15:01:55 -08:00
} catch {
self.messageLabel.text = "Shielding failed \(error)"
2020-12-23 15:01:55 -08:00
}
}
2020-12-09 15:58:33 -08:00
}
extension GetUTXOsViewController: UITextFieldDelegate {
2021-09-17 06:49:58 -07:00
func textField(
_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String
) -> Bool {
2020-12-09 15:58:33 -08:00
updateUI()
return true
}
2021-09-17 06:49:58 -07:00
func textFieldDidEndEditing(_ textField: UITextField) {
2020-12-09 15:58:33 -08:00
updateUI()
}
}