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

[#1232] Implement createAccount #1237

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
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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")"
}
Expand Down
10 changes: 9 additions & 1 deletion Sources/ZcashLightClientKit/Initializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 8 additions & 2 deletions Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ 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,
seed,
UInt(seed.count),
treeState,
UInt(treeState.count),
recoverUntil != nil ? Int64(recoverUntil!) : -1,
rUntil,
networkType.networkId
)

Expand Down Expand Up @@ -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"))
}
Expand Down
Loading