Skip to content

Commit

Permalink
PR Comments addressed and code cleaned up
Browse files Browse the repository at this point in the history
- Comments in the code updated and cleaned up
- OfflineTests passes again, those failing has been removed from the bundle and marked to be fixed with a TODO

Fixes of build

- The SDK builds again
  • Loading branch information
LukasKorba committed Dec 5, 2024
1 parent 7a6bd72 commit 27af6f1
Show file tree
Hide file tree
Showing 28 changed files with 359 additions and 615 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Added
- `DerivationTool.deriveArbitraryWalletKey`
- `DerivationTool.deriveArbitraryAccountKey`
- `SDKSynchronizer.listAccounts` Returns a list of the accounts in the wallet.

## Changed
- `zcashlc_propose_transfer`, `zcashlc_propose_transfer_from_uri` and `zcashlc_propose_shielding` no longer accpt a `use_zip317_fees` parameter; ZIP 317 standard fees are now always used and are not configurable.
- The SDK no longer assumes a default account. All business logic with instances of Zip32AccountIndex(<index>) has been refactored.
- `SDKSynchronizer.getAccountBalance -> AccountBalance?` into `SDKSynchronizer.getAccountsBalances -> [AccountUUID: AccountBalance]`

## Removed
- `SDKSynchronizer.sendToAddress`, deperacted in 2.1
- `SDKSynchronizer.shieldFunds`, deperacted in 2.1

## Checkpoints

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class SendViewController: UIViewController {
closureSynchronizer.prepare(
with: DemoAppConfig.defaultSeed,
walletBirthday: DemoAppConfig.defaultBirthdayHeight,
for: .existingWallet
for: .existingWallet,
name: "",
keySource: nil
) { result in
loggerProxy.debug("Prepare result: \(result)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class SyncBlocksListViewController: UIViewController {
_ = try! await synchronizer.prepare(
with: synchronizerData.seed,
walletBirthday: synchronizerData.birthday,
for: .existingWallet
for: .existingWallet,
name: "",
keySource: nil
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ class SyncBlocksViewController: UIViewController {
_ = try await synchronizer.prepare(
with: DemoAppConfig.defaultSeed,
walletBirthday: DemoAppConfig.defaultBirthdayHeight,
for: .existingWallet
for: .existingWallet,
name: "",
keySource: nil
)
} catch {
loggerProxy.error(error.toZcashError().message)
Expand Down
8 changes: 5 additions & 3 deletions Sources/ZcashLightClientKit/ClosureSynchronizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public protocol ClosureSynchronizer {
with seed: [UInt8]?,
walletBirthday: BlockHeight,
for walletMode: WalletInitMode,
name: String,
keySource: String?,
completion: @escaping (Result<Initializer.InitializationResult, Error>) -> Void
)

Expand All @@ -38,7 +40,7 @@ public protocol ClosureSynchronizer {

/// Creates a proposal for transferring funds to the given recipient.
///
/// - Parameter accountIndex: the ZIP 32 index of the account from which to transfer funds.
/// - Parameter accountUUID: the account from which to transfer funds.
/// - Parameter recipient: the recipient's address.
/// - Parameter amount: the amount to send in Zatoshi.
/// - Parameter memo: an optional memo to include as part of the proposal's transactions. Use `nil` when sending to transparent receivers otherwise the function will throw an error.
Expand All @@ -55,7 +57,7 @@ public protocol ClosureSynchronizer {

/// Creates a proposal for shielding any transparent funds received by the given account.
///
/// - Parameter accountIndex: the ZIP 32 index of the account from which to shield funds.
/// - Parameter accountUUID: the account from which to shield funds.
/// - Parameter shieldingThreshold: the minimum transparent balance required before a proposal will be created.
/// - Parameter memo: an optional memo to include as part of the proposal's transactions.
/// - Parameter transparentReceiver: a specific transparent receiver within the account
Expand Down Expand Up @@ -112,7 +114,7 @@ public protocol ClosureSynchronizer {

func refreshUTXOs(address: TransparentAddress, from height: BlockHeight, completion: @escaping (Result<RefreshedUTXOs, Error>) -> Void)

func getAccountsBalances(completion: @escaping (Result<[AccountUUID: AccountBalance], Error>) -> Void)
func getAccountsBalances(_ completion: @escaping (Result<[AccountUUID: AccountBalance], Error>) -> Void)

func refreshExchangeRateUSD()

Expand Down
8 changes: 5 additions & 3 deletions Sources/ZcashLightClientKit/CombineSynchronizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public protocol CombineSynchronizer {
func prepare(
with seed: [UInt8]?,
walletBirthday: BlockHeight,
for walletMode: WalletInitMode
for walletMode: WalletInitMode,
name: String,
keySource: String?
) -> SinglePublisher<Initializer.InitializationResult, Error>

func start(retry: Bool) -> CompletablePublisher<Error>
Expand All @@ -37,7 +39,7 @@ public protocol CombineSynchronizer {

/// Creates a proposal for transferring funds to the given recipient.
///
/// - Parameter accountIndex: the ZIP 32 index of the account from which to transfer funds.
/// - Parameter accountUUID: the account from which to transfer funds.
/// - Parameter recipient: the recipient's address.
/// - Parameter amount: the amount to send in Zatoshi.
/// - Parameter memo: an optional memo to include as part of the proposal's transactions. Use `nil` when sending to transparent receivers otherwise the function will throw an error.
Expand All @@ -53,7 +55,7 @@ public protocol CombineSynchronizer {

/// Creates a proposal for shielding any transparent funds received by the given account.
///
/// - Parameter accountIndex: the ZIP 32 index of the account from which to shield funds.
/// - Parameter accountUUID: the account from which to shield funds.
/// - Parameter shieldingThreshold: the minimum transparent balance required before a proposal will be created.
/// - Parameter memo: an optional memo to include as part of the proposal's transactions.
/// - Parameter transparentReceiver: a specific transparent receiver within the account
Expand Down
12 changes: 9 additions & 3 deletions Sources/ZcashLightClientKit/Initializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,13 @@ public class Initializer {
/// - Parameter seed: ZIP-32 Seed bytes for the wallet that will be initialized
/// - Throws: `InitializerError.dataDbInitFailed` if the creation of the dataDb fails
/// `InitializerError.accountInitFailed` if the account table can't be initialized.
func initialize(with seed: [UInt8]?, walletBirthday: BlockHeight, for walletMode: WalletInitMode) async throws -> InitializationResult {
func initialize(
with seed: [UInt8]?,
walletBirthday: BlockHeight,
for walletMode: WalletInitMode,
name: String,
keySource: String? = nil
) async throws -> InitializationResult {
try await storage.create()

if case .seedRequired = try await rustBackend.initDataDb(seed: seed) {
Expand All @@ -431,8 +437,8 @@ public class Initializer {
seed: seed,
treeState: checkpoint.treeState(),
recoverUntil: chainTip,
name: "",
keySource: ""
name: name,
keySource: keySource
)
}

Expand Down
4 changes: 0 additions & 4 deletions Sources/ZcashLightClientKit/Model/WalletTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public protocol StringEncoded {
public struct UnifiedSpendingKey: Equatable, Undescribable {
let network: NetworkType
let bytes: [UInt8]
//public let accountUUID: AccountUUID
}

/// Sapling Extended Spending Key
Expand Down Expand Up @@ -45,18 +44,15 @@ public struct TransparentAccountPrivKey: Equatable, Undescribable {
/// A ZIP 316 Unified Full Viewing Key.
public struct UnifiedFullViewingKey: Equatable, StringEncoded, Undescribable {
let encoding: String
//public let accountUUID: AccountUUID

public var stringEncoded: String { encoding }

/// Initializes a new UnifiedFullViewingKey (UFVK) from the provided string encoding
/// - Parameters:
/// - parameter encoding: String encoding of unified full viewing key
/// - parameter accountIndex: the ZIP32 account Index of the given UFVK
/// - parameter network: `NetworkType` corresponding to the encoding (Mainnet or Testnet)
/// - Throws: `unifiedFullViewingKeyInvalidInput`when the provided encoding is found to be invalid
public init(encoding: String, network: NetworkType) throws {
// public init(encoding: String, accountUUID: AccountUUID, network: NetworkType) throws {
guard DerivationTool(networkType: network).isValidUnifiedFullViewingKey(encoding) else {
throw ZcashError.unifiedFullViewingKeyInvalidInput
}
Expand Down
22 changes: 17 additions & 5 deletions Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,22 @@ struct ZcashRustBackend: ZcashRustBackendWelding {
recoverUntil: UInt32?,
purpose: AccountPurpose,
name: String,
keySource: String
keySource: String?
) async throws -> AccountUUID {
var rUntil: Int64 = -1

if let recoverUntil {
rUntil = Int64(recoverUntil)
}

let treeStateBytes = try treeState.serializedData(partial: false).bytes

var kSource: [CChar]? = nil

if let keySource {
kSource = [CChar](keySource.utf8CString)
}

let uuidPtr = zcashlc_import_account_ufvk(
dbData.0,
dbData.1,
Expand All @@ -122,7 +128,7 @@ struct ZcashRustBackend: ZcashRustBackendWelding {
networkType.networkId,
purpose.rawValue,
[CChar](name.utf8CString),
[CChar](keySource.utf8CString)
kSource
)

guard let uuidPtr else {
Expand All @@ -140,7 +146,7 @@ struct ZcashRustBackend: ZcashRustBackendWelding {
treeState: TreeState,
recoverUntil: UInt32?,
name: String,
keySource: String
keySource: String?
) async throws -> UnifiedSpendingKey {
var rUntil: Int64 = -1

Expand All @@ -150,6 +156,12 @@ struct ZcashRustBackend: ZcashRustBackendWelding {

let treeStateBytes = try treeState.serializedData(partial: false).bytes

var kSource: [CChar]? = nil

if let keySource {
kSource = [CChar](keySource.utf8CString)
}

let ffiBinaryKeyPtr = zcashlc_create_account(
dbData.0,
dbData.1,
Expand All @@ -160,7 +172,7 @@ struct ZcashRustBackend: ZcashRustBackendWelding {
rUntil,
networkType.networkId,
[CChar](name.utf8CString),
[CChar](keySource.utf8CString)
kSource
)

guard let ffiBinaryKeyPtr else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public enum RewindResult {
}

protocol ZcashRustBackendWelding {
/// Returns a list of the ZIP 32 indices of accounts in the wallet.
/// Returns a list of the accounts in the wallet.
func listAccounts() async throws -> [AccountUUID]

func importAccount(
Expand All @@ -44,7 +44,7 @@ protocol ZcashRustBackendWelding {
recoverUntil: UInt32?,
purpose: AccountPurpose,
name: String,
keySource: String
keySource: String?
) async throws -> AccountUUID

/// Adds the next available account-level spend authority, given the current set of [ZIP 316]
Expand All @@ -71,7 +71,7 @@ protocol ZcashRustBackendWelding {
treeState: TreeState,
recoverUntil: UInt32?,
name: String,
keySource: String
keySource: String?
) async throws -> UnifiedSpendingKey

/// Checks whether the given seed is relevant to any of the derived accounts in the wallet.
Expand Down
18 changes: 10 additions & 8 deletions Sources/ZcashLightClientKit/Synchronizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ public protocol Synchronizer: AnyObject {
func prepare(
with seed: [UInt8]?,
walletBirthday: BlockHeight,
for walletMode: WalletInitMode
for walletMode: WalletInitMode,
name: String,
keySource: String?
) async throws -> Initializer.InitializationResult

/// Starts this synchronizer within the given scope.
Expand All @@ -144,23 +146,23 @@ public protocol Synchronizer: AnyObject {
func stop()

/// Gets the sapling shielded address for the given account.
/// - Parameter accountIndex: the ZIP 32 index of the account whose address is of interest.
/// - Parameter accountUUID: the account whose address is of interest.
/// - Returns the address or nil if account index is incorrect
func getSaplingAddress(accountUUID: AccountUUID) async throws -> SaplingAddress

/// Gets the unified address for the given account.
/// - Parameter accountIndex: the ZIP 32 index of the account whose address is of interest.
/// - Parameter accountUUID: the account whose address is of interest.
/// - Returns the address or nil if account index is incorrect
func getUnifiedAddress(accountUUID: AccountUUID) async throws -> UnifiedAddress

/// Gets the transparent address for the given account.
/// - Parameter accountIndex: the ZIP 32 index of the account whose address is of interest. By default, the first account is used.
/// - Parameter accountUUID: the account whose address is of interest. By default, the first account is used.
/// - Returns the address or nil if account index is incorrect
func getTransparentAddress(accountUUID: AccountUUID) async throws -> TransparentAddress

/// Creates a proposal for transferring funds to the given recipient.
///
/// - Parameter accountIndex: the ZIP 32 index of the account from which to transfer funds.
/// - Parameter accountUUID: the account from which to transfer funds.
/// - Parameter recipient: the recipient's address.
/// - Parameter amount: the amount to send in Zatoshi.
/// - Parameter memo: an optional memo to include as part of the proposal's transactions. Use `nil` when sending to transparent receivers otherwise the function will throw an error.
Expand All @@ -176,7 +178,7 @@ public protocol Synchronizer: AnyObject {

/// Creates a proposal for shielding any transparent funds received by the given account.
///
/// - Parameter accountIndex: the ZIP 32 index of the account for which to shield funds.
/// - Parameter accountUUID: the account for which to shield funds.
/// - Parameter shieldingThreshold: the minimum transparent balance required before a proposal will be created.
/// - Parameter memo: an optional memo to include as part of the proposal's transactions.
/// - Parameter transparentReceiver: a specific transparent receiver within the account
Expand Down Expand Up @@ -214,7 +216,7 @@ public protocol Synchronizer: AnyObject {

/// Attempts to propose fulfilling a [ZIP-321](https://zips.z.cash/zip-0321) payment URI by spending from the ZIP 32 account with the given index.
/// - Parameter uri: a valid ZIP-321 payment URI
/// - Parameter accountIndex: the ZIP 32 index of the account providing spend authority.
/// - Parameter accountUUID: the account providing spend authority.
///
/// - NOTE: If `prepare()` hasn't already been called since creating of synchronizer instance or since the last wipe then this method throws
/// `SynchronizerErrors.notPrepared`.
Expand Down Expand Up @@ -284,7 +286,7 @@ public protocol Synchronizer: AnyObject {
/// Fetches the latest ZEC-USD exchange rate and updates `exchangeRateUSDSubject`.
func refreshExchangeRateUSD()

///
/// Returns a list of the accounts in the wallet.
func listAccounts() async throws -> [AccountUUID]

/// Rescans the known blocks with the current keys.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ extension ClosureSDKSynchronizer: ClosureSynchronizer {
with seed: [UInt8]?,
walletBirthday: BlockHeight,
for walletMode: WalletInitMode,
name: String,
keySource: String?,
completion: @escaping (Result<Initializer.InitializationResult, Error>) -> Void
) {
AsyncToClosureGateway.executeThrowingAction(completion) {
return try await self.synchronizer.prepare(with: seed, walletBirthday: walletBirthday, for: walletMode)
return try await self.synchronizer.prepare(
with: seed,
walletBirthday: walletBirthday,
for: walletMode,
name: name,
keySource: keySource
)
}
}

Expand Down Expand Up @@ -169,7 +177,7 @@ extension ClosureSDKSynchronizer: ClosureSynchronizer {
}
}

public func getAccountsBalances(completion: @escaping (Result<[AccountUUID: AccountBalance], Error>) -> Void) {
public func getAccountsBalances(_ completion: @escaping (Result<[AccountUUID: AccountBalance], Error>) -> Void) {
AsyncToClosureGateway.executeThrowingAction(completion) {
try await self.synchronizer.getAccountsBalances()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ extension CombineSDKSynchronizer: CombineSynchronizer {
public func prepare(
with seed: [UInt8]?,
walletBirthday: BlockHeight,
for walletMode: WalletInitMode
for walletMode: WalletInitMode,
name: String,
keySource: String?
) -> SinglePublisher<Initializer.InitializationResult, Error> {
AsyncToCombineGateway.executeThrowingAction() {
return try await self.synchronizer.prepare(with: seed, walletBirthday: walletBirthday, for: walletMode)
return try await self.synchronizer.prepare(
with: seed,
walletBirthday: walletBirthday,
for: walletMode,
name: name,
keySource: keySource
)
}
}

Expand Down
Loading

0 comments on commit 27af6f1

Please sign in to comment.