Skip to content

Commit

Permalink
Merge pull request #1237 from LukasKorba/1232-Implement-createAccount
Browse files Browse the repository at this point in the history
[#1232] Implement createAccount
  • Loading branch information
LukasKorba authored Sep 8, 2023
2 parents 7631810 + b8b0f29 commit 3cf1f23
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
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

0 comments on commit 3cf1f23

Please sign in to comment.