Skip to content
New issue

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

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

Already on GitHub? # to your account

Type safe address api #491

Merged
merged 4 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand All @@ -15,7 +15,6 @@
0D756A94236C761E009B041B /* GetAddressViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D756A93236C761E009B041B /* GetAddressViewController.swift */; };
0D76121926B1D66D001CA417 /* Testnet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D76121826B1D66D001CA417 /* Testnet.swift */; };
0D7A4A83236CCD88001F4DD8 /* SyncBlocksViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D7A4A82236CCD88001F4DD8 /* SyncBlocksViewController.swift */; };
0D7C85E523AD5A9B006878FC /* SampleStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D7C85E423AD5A9B006878FC /* SampleStorage.swift */; };
0D907F162322CC5900D641FE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D907F152322CC5900D641FE /* AppDelegate.swift */; };
0D907F182322CC5900D641FE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D907F172322CC5900D641FE /* ViewController.swift */; };
0D907F1B2322CC5900D641FE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0D907F192322CC5900D641FE /* Main.storyboard */; };
Expand All @@ -35,7 +34,6 @@
0DA1C4C727D11D9500E5006E /* SaplingParametersViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D6CE8BC252E3C4A0005D707 /* SaplingParametersViewController.swift */; };
0DA1C4C827D11D9500E5006E /* GetUTXOsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D1BE47E2581937100F78BE3 /* GetUTXOsViewController.swift */; };
0DA1C4C927D11D9500E5006E /* DerivationToolViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D1BE4502581585C00F78BE3 /* DerivationToolViewController.swift */; };
0DA1C4CA27D11D9500E5006E /* SampleStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D7C85E423AD5A9B006878FC /* SampleStorage.swift */; };
0DA1C4CB27D11D9500E5006E /* SampleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D49A18B241698A800CC0649 /* SampleLogger.swift */; };
0DA1C4CC27D11D9500E5006E /* TransactionsDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DA58B952397F2CB004596EA /* TransactionsDataSource.swift */; };
0DA1C4CD27D11D9500E5006E /* PaginatedTransactionsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DF53E6623A438F100D7249C /* PaginatedTransactionsViewController.swift */; };
Expand Down Expand Up @@ -83,7 +81,6 @@
0D76121526B1D5ED001CA417 /* Mainnet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mainnet.swift; sourceTree = "<group>"; };
0D76121826B1D66D001CA417 /* Testnet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Testnet.swift; sourceTree = "<group>"; };
0D7A4A82236CCD88001F4DD8 /* SyncBlocksViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncBlocksViewController.swift; sourceTree = "<group>"; };
0D7C85E423AD5A9B006878FC /* SampleStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleStorage.swift; sourceTree = "<group>"; };
0D907F122322CC5900D641FE /* ZcashLightClientSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ZcashLightClientSample.app; sourceTree = BUILT_PRODUCTS_DIR; };
0D907F152322CC5900D641FE /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
0D907F172322CC5900D641FE /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -248,7 +245,6 @@
0D907F212322CC5B00D641FE /* Info.plist */,
F94912622790D7C4004BB3DE /* ZcashLightClientSample-Mainnet-Info.plist */,
0DDFB33D236B844900AED892 /* DemoAppConfig.swift */,
0D7C85E423AD5A9B006878FC /* SampleStorage.swift */,
0D49A18B241698A800CC0649 /* SampleLogger.swift */,
);
path = ZcashLightClientSample;
Expand Down Expand Up @@ -498,6 +494,7 @@
};
0DCB58A4237B5B580040096C /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -534,7 +531,6 @@
0D6CE8BD252E3C4A0005D707 /* SaplingParametersViewController.swift in Sources */,
0D1BE47F2581937100F78BE3 /* GetUTXOsViewController.swift in Sources */,
0D1BE4512581585C00F78BE3 /* DerivationToolViewController.swift in Sources */,
0D7C85E523AD5A9B006878FC /* SampleStorage.swift in Sources */,
0D49A18C241698A800CC0649 /* SampleLogger.swift in Sources */,
0DA58B962397F2CB004596EA /* TransactionsDataSource.swift in Sources */,
0DF53E6723A438F100D7249C /* PaginatedTransactionsViewController.swift in Sources */,
Expand Down Expand Up @@ -567,7 +563,6 @@
0DA1C4C727D11D9500E5006E /* SaplingParametersViewController.swift in Sources */,
0DA1C4C827D11D9500E5006E /* GetUTXOsViewController.swift in Sources */,
0DA1C4C927D11D9500E5006E /* DerivationToolViewController.swift in Sources */,
0DA1C4CA27D11D9500E5006E /* SampleStorage.swift in Sources */,
0DA1C4CB27D11D9500E5006E /* SampleLogger.swift in Sources */,
0DA1C4CC27D11D9500E5006E /* TransactionsDataSource.swift in Sources */,
0DA1C4CD27D11D9500E5006E /* PaginatedTransactionsViewController.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@
},
{
"package": "libzcashlc",
"repositoryURL": "https://github.com/nuttycom/zcash-light-client-ffi.git",
"repositoryURL": "https://github.com/zcash-hackworks/zcash-light-client-ffi",
"state": {
"branch": "bin/librustzcash_0_7-migrations",
"revision": "dbf7ac51a7de0bbc2c1088973236d68ceb752361",
"branch": "bin/librustzcash_0_7",
"revision": "823f864a7952073fb9718cf75610691756e34d59",
"version": null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
)

_ = try! wallet.initialize(with: DemoAppConfig.seed)
var storage = SampleStorage.shared
storage!.seed = Data(DemoAppConfig.seed)
storage!.privateKey = try! DerivationTool(networkType: kZcashNetwork.networkType)
.deriveSpendingKeys(seed: DemoAppConfig.seed, numberOfAccounts: 1)[0]

self.wallet = wallet
return wallet
}
Expand Down Expand Up @@ -141,10 +138,6 @@ extension AppDelegate {
} catch {
loggerProxy.error("error clearing data db: \(error)")
}

var storage = SampleStorage.shared
storage!.seed = nil
storage!.privateKey = nil
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class DerivationToolViewController: UIViewController {
enum DerivationErrors: Error {
case couldNotDeriveSpendingKeys(underlyingError: Error)
case couldNotDeriveViewingKeys(underlyingError: Error)
case couldNotDeriveShieldedAddress(underlyingError: Error)
case couldNotDeriveTransparentAddress(underlyingError: Error)
case unknown
}

Expand Down Expand Up @@ -113,15 +111,15 @@ class DerivationToolViewController: UIViewController {
throw DerivationErrors.couldNotDeriveViewingKeys(underlyingError: DerivationErrors.unknown)
}

let unifiedAddress = try derivationTool.deriveUnifiedAddress(viewingKey: viewingKey.encoding)
let unifiedAddress = try derivationTool.deriveUnifiedAddress(from: viewingKey)

let transparentAddress = try derivationTool.deriveTransparentAddress(seed: seedBytes)

updateLabels(
spendingKey: spendingKey,
viewingKey: viewingKey.encoding,
shieldedAddress: unifiedAddress,
transaparentAddress: transparentAddress
spendingKey: spendingKey.stringEncoded,
viewingKey: viewingKey.stringEncoded,
shieldedAddress: unifiedAddress.stringEncoded,
transaparentAddress: transparentAddress.stringEncoded
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ class GetAddressViewController: UIViewController {
@IBOutlet weak var unifiedAddressLabel: UILabel!
@IBOutlet weak var tAddressLabel: UILabel!
@IBOutlet weak var spendingKeyLabel: UILabel! // THIS SHOULD BE SUPER SECRET!!!!!


// THIS SHOULD NEVER BE STORED IN MEMORY
// swiftlint:disable:next implicitly_unwrapped_optional
var spendingKey: SaplingExtendedSpendingKey!

override func viewDidLoad() {
super.viewDidLoad()
let derivationTool = DerivationTool(networkType: kZcashNetwork.networkType)

// swiftlint:disable:next force_try force_unwrapping
self.spendingKey = try! derivationTool.deriveSpendingKeys(seed: DemoAppConfig.seed, numberOfAccounts: 1).first!

// Do any additional setup after loading the view.

unifiedAddressLabel.text = (try? derivationTool.deriveUnifiedAddress(seed: DemoAppConfig.seed, accountIndex: 0)) ?? "No Addresses found"
tAddressLabel.text = (try? derivationTool.deriveTransparentAddress(seed: DemoAppConfig.seed)) ?? "could not derive t-address"
spendingKeyLabel.text = SampleStorage.shared.privateKey ?? "No Spending Key found"
// swiftlint:disable:next line_length
unifiedAddressLabel.text = (try? derivationTool.deriveUnifiedAddress(seed: DemoAppConfig.seed, accountIndex: 0))?.stringEncoded ?? "No Addresses found"
tAddressLabel.text = (try? derivationTool.deriveTransparentAddress(seed: DemoAppConfig.seed))?.stringEncoded ?? "could not derive t-address"
spendingKeyLabel.text = self.spendingKey.stringEncoded
unifiedAddressLabel.addGestureRecognizer(
UITapGestureRecognizer(
target: self,
Expand All @@ -44,20 +52,12 @@ class GetAddressViewController: UIViewController {
)
spendingKeyLabel.isUserInteractionEnabled = true
loggerProxy.info("Address: \(String(describing: Initializer.shared.getAddress()))")
// NOTE: NEVER LOG YOUR PRIVATE KEYS IN REAL LIFE
// swiftlint:disable:next print_function_usage
print("Spending Key: \(SampleStorage.shared.privateKey ?? "No Spending Key found")")
}

@IBAction func spendingKeyTapped(_ gesture: UIGestureRecognizer) {
guard let key = SampleStorage.shared.privateKey else {
loggerProxy.warn("nothing to copy")
return
}

loggerProxy.event("copied to clipboard")

UIPasteboard.general.string = key
UIPasteboard.general.string = self.spendingKey.stringEncoded
let alert = UIAlertController(
title: "",
message: "Spending Key Copied to clipboard",
Expand Down Expand Up @@ -85,7 +85,7 @@ class GetAddressViewController: UIViewController {
@IBAction func tAddressTapped(_ gesture: UIGestureRecognizer) {
loggerProxy.event("copied to clipboard")
UIPasteboard.general.string = try? DerivationTool(networkType: kZcashNetwork.networkType)
.deriveTransparentAddress(seed: DemoAppConfig.seed)
.deriveTransparentAddress(seed: DemoAppConfig.seed).stringEncoded

let alert = UIAlertController(
title: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GetUTXOsViewController: UIViewController {
let tAddress = try! DerivationTool(networkType: kZcashNetwork.networkType)
.deriveTransparentAddress(seed: DemoAppConfig.seed)

self.transparentAddressLabel.text = tAddress
self.transparentAddressLabel.text = tAddress.stringEncoded

// swiftlint:disable:next force_try
let balance = try! AppDelegate.shared.sharedSynchronizer.getTransparentBalance(accountIndex: 0)
Expand All @@ -40,14 +40,12 @@ class GetUTXOsViewController: UIViewController {
do {
let seed = DemoAppConfig.seed
let derivationTool = DerivationTool(networkType: kZcashNetwork.networkType)
// swiftlint:disable:next force_unwrapping
let spendingKey = try derivationTool.deriveSpendingKeys(seed: seed, numberOfAccounts: 1).first!

let transparentSecretKey = try derivationTool.deriveTransparentAccountPrivateKey(seed: seed, account: 0)

KRProgressHUD.showMessage("🛡 Shielding 🛡")

AppDelegate.shared.sharedSynchronizer.shieldFunds(
spendingKey: spendingKey,
transparentAccountPrivateKey: transparentSecretKey,
memo: "shielding is fun!",
from: 0,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class SendViewController: UIViewController {
guard let addr = self.addressTextField.text else {
return false
}
return wallet.isValidShieldedAddress(addr) || wallet.isValidTransparentAddress(addr)
return wallet.isValidSaplingAddress(addr) || wallet.isValidTransparentAddress(addr)
}

@IBAction func maxFundsValueChanged(_ sender: Any) {
Expand Down Expand Up @@ -206,18 +206,19 @@ class SendViewController: UIViewController {
loggerProxy.warn("WARNING: Form is invalid")
return
}

guard let address = SampleStorage.shared.privateKey else {
loggerProxy.error("NO ADDRESS")
// swiftlint:disable:next line_length force_try
guard let spendingKey = try! DerivationTool(networkType: kZcashNetwork.networkType).deriveSpendingKeys(seed: DemoAppConfig.seed, numberOfAccounts: 1).first else {
loggerProxy.error("NO SPENDING KEY")
pacu marked this conversation as resolved.
Show resolved Hide resolved
return
}

KRProgressHUD.show()

synchronizer.sendToAddress(
spendingKey: address,
spendingKey: spendingKey,
zatoshi: zec,
toAddress: recipient,
// swiftlint:disable:next force_try
toAddress: try! Recipient(recipient, network: kZcashNetwork.networkType),
memo: !self.memoField.text.isEmpty ? self.memoField.text : nil,
from: 0
) { [weak self] result in
Expand Down
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@
},
{
"package": "libzcashlc",
"repositoryURL": "https://github.com/zcash-hackworks/zcash-light-client-ffi.git",
"repositoryURL": "https://github.com/zcash-hackworks/zcash-light-client-ffi",
"state": {
"branch": "librustzcash_0_7_build_test",
"revision": "5a48541c1da48eb082defaf0fa127657f4f99094",
"branch": "bin/librustzcash_0_7",
"revision": "823f864a7952073fb9718cf75610691756e34d59",
"version": null
}
}
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let package = Package(
name: "ZcashLightClientKit",
platforms: [
.iOS(.v13),
.macOS(.v10_12)
.macOS(.v10_15)
],
products: [
.library(
Expand All @@ -16,7 +16,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.8.0"),
.package(url: "https://github.com/stephencelis/SQLite.swift.git", from: "0.13.0"),
.package(name:"libzcashlc", url: "https://github.com/zcash-hackworks/zcash-light-client-ffi.git", branch: "librustzcash_0_7_build_test"),
.package(name:"libzcashlc", url: "https://github.com/zcash-hackworks/zcash-light-client-ffi", branch: "bin/librustzcash_0_7"),
],
targets: [
.target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1260,18 +1260,27 @@ extension CompactBlockProcessor {

extension CompactBlockProcessor {
public func getUnifiedAddres(accountIndex: Int) -> UnifiedAddress? {
guard let account = try? accountRepository.findBy(account: accountIndex) else {
// TODO: perform migrations on the account table to accommodate Unified Address or UFVK to to derive from.
guard let address = try? accountRepository.findBy(account: accountIndex)?.address else {
return nil
}
return UnifiedAddressShim(account: account)

return try? UnifiedAddress(encoding: address, network: self.config.network.networkType)
}

public func getShieldedAddress(accountIndex: Int) -> SaplingShieldedAddress? {
try? accountRepository.findBy(account: accountIndex)?.address
public func getSaplingAddress(accountIndex: Int) -> SaplingAddress? {
guard let zAddress = try? accountRepository.findBy(account: accountIndex)?.address else {
return nil
}

return try? SaplingAddress(encoding: zAddress, network: self.config.network.networkType)
}

public func getTransparentAddress(accountIndex: Int) -> TransparentAddress? {
try? accountRepository.findBy(account: accountIndex)?.transparentAddress
guard let tAddress = try? accountRepository.findBy(account: accountIndex)?.transparentAddress else { return nil }

return TransparentAddress(validatedEncoding: tAddress)

}

public func getTransparentBalance(accountIndex: Int) throws -> WalletBalance {
Expand All @@ -1282,20 +1291,6 @@ extension CompactBlockProcessor {
}
}

private struct UnifiedAddressShim {
let account: AccountEntity
}

extension UnifiedAddressShim: UnifiedAddress {
var encoding: String {
account.transparentAddress
}

var zAddress: SaplingShieldedAddress {
account.address
}
}

extension CompactBlockProcessor {
func refreshUTXOs(tAddress: String, startHeight: BlockHeight, result: @escaping (Result<RefreshedUTXOs, Error>) -> Void) {
let dataDb = self.config.dataDb
Expand Down
Loading