From b8b0f2927b7a95627c02cb173dda621a64f46e10 Mon Sep 17 00:00:00 2001 From: Lukas Korba Date: Thu, 7 Sep 2023 20:53:43 +0200 Subject: [PATCH] [#1232] Implement createAccount - create account draft [#1232] Implement createAccount - TODO for the recoverUntil parameter [#1232] Implement createAccount - force unwrap solved [#1232] Implement createAccount - ; removed --- .../ZcashLightClientSample/DemoAppConfig.swift | 2 +- .../Send/SendViewController.swift | 11 +++++++++-- Sources/ZcashLightClientKit/Initializer.swift | 10 +++++++++- .../ZcashLightClientKit/Rust/ZcashRustBackend.swift | 10 ++++++++-- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Example/ZcashLightClientSample/ZcashLightClientSample/DemoAppConfig.swift b/Example/ZcashLightClientSample/ZcashLightClientSample/DemoAppConfig.swift index 6780982dc..c6501e5fc 100644 --- a/Example/ZcashLightClientSample/ZcashLightClientSample/DemoAppConfig.swift +++ b/Example/ZcashLightClientSample/ZcashLightClientSample/DemoAppConfig.swift @@ -28,7 +28,7 @@ enum DemoAppConfig { static let defaultBirthdayHeight: BlockHeight = ZcashSDK.isMainnet ? 1935000 : 2170000 static let defaultSeed = try! Mnemonic.deterministicSeedBytes(from: """ - kitchen renew wide common vague fold vacuum tilt amazing pear square gossip jewel month tree shock scan alpha just spot fluid toilet view dinner + wish puppy smile loan doll curve hole maze file ginger hair nose key relax knife witness cannon grab despair throw review deal slush frame """) static let otherSynchronizers: [SynchronizerInitData] = [ diff --git a/Example/ZcashLightClientSample/ZcashLightClientSample/Send/SendViewController.swift b/Example/ZcashLightClientSample/ZcashLightClientSample/Send/SendViewController.swift index 7eff58bb2..4e38926a5 100644 --- a/Example/ZcashLightClientSample/ZcashLightClientSample/Send/SendViewController.swift +++ b/Example/ZcashLightClientSample/ZcashLightClientSample/Send/SendViewController.swift @@ -79,8 +79,7 @@ class SendViewController: UIViewController { func setUp() { Task { @MainActor in - balanceLabel.text = format(balance: (try? await synchronizer.getShieldedBalance(accountIndex: 0)) ?? .zero) - verifiedBalanceLabel.text = format(balance: (try? await synchronizer.getShieldedVerifiedBalance(accountIndex: 0)) ?? .zero) + await updateBalance() await toggleSendButton() } memoField.text = "" @@ -93,12 +92,20 @@ class SendViewController: UIViewController { .throttle(for: .seconds(0.2), scheduler: DispatchQueue.main, latest: true) .sink( receiveValue: { [weak self] state in + Task { @MainActor in + await self?.updateBalance() + } self?.synchronizerStatusLabel.text = SDKSynchronizer.textFor(state: state.syncStatus) } ) .store(in: &cancellables) } + func updateBalance() async { + balanceLabel.text = format(balance: (try? await synchronizer.getShieldedBalance(accountIndex: 0)) ?? .zero) + verifiedBalanceLabel.text = format(balance: (try? await synchronizer.getShieldedVerifiedBalance(accountIndex: 0)) ?? .zero) + } + func format(balance: Zatoshi = Zatoshi()) -> String { "Zec \(balance.formattedString ?? "0.0")" } diff --git a/Sources/ZcashLightClientKit/Initializer.swift b/Sources/ZcashLightClientKit/Initializer.swift index 6c1fa9b3f..dc42b5622 100644 --- a/Sources/ZcashLightClientKit/Initializer.swift +++ b/Sources/ZcashLightClientKit/Initializer.swift @@ -420,7 +420,15 @@ public class Initializer { self.walletBirthday = checkpoint.height - // TODO: Initialize accounts if desired. + // If there are no accounts it must be created, the default amount of accounts is 1 + if let seed, try accountRepository.getAll().isEmpty { + // TODO: [#1236] set the recoverUntil properly, https://github.com/zcash/ZcashLightClientKit/issues/1236 + _ = try await rustBackend.createAccount( + seed: seed, + treeState: try checkpoint.treeState().serializedData(partial: false).bytes, + recoverUntil: nil + ) + } return .success } diff --git a/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift b/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift index 8165f5eaa..3824e6c6f 100644 --- a/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift +++ b/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift @@ -48,6 +48,12 @@ actor ZcashRustBackend: ZcashRustBackendWelding { } func createAccount(seed: [UInt8], treeState: [UInt8], recoverUntil: UInt32?) async throws -> UnifiedSpendingKey { + var rUntil: Int64 = -1 + + if let recoverUntil { + rUntil = Int64(recoverUntil) + } + let ffiBinaryKeyPtr = zcashlc_create_account( dbData.0, dbData.1, @@ -55,7 +61,7 @@ actor ZcashRustBackend: ZcashRustBackendWelding { UInt(seed.count), treeState, UInt(treeState.count), - recoverUntil != nil ? Int64(recoverUntil!) : -1, + rUntil, networkType.networkId ) @@ -521,7 +527,7 @@ actor ZcashRustBackend: ZcashRustBackendWelding { if result.denominator == 0 { switch result.numerator { case 0: - return nil; + return nil default: throw ZcashError.rustGetScanProgress(lastErrorMessage(fallback: "`getScanProgress` failed with unknown error")) }