Skip to content

Commit e68343b

Browse files
example: WalletManager helpers to access FFI wallet and managed wallet
- Add getFFIWallet(for:) to fetch SwiftDashSDK.Wallet - Add getManagedWallet(for:) to create a ManagedWallet for UTXO operations
1 parent e731fb4 commit e68343b

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Wallet/WalletManager.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,43 @@ class WalletManager: ObservableObject {
763763
}
764764

765765
// MARK: - Public Utility Methods
766-
766+
767767
func reloadWallets() async {
768768
await loadWallets()
769769
}
770-
770+
771+
/// Get the FFI wallet object for transaction operations
772+
/// - Parameter wallet: The HDWallet to get the FFI wallet for
773+
/// - Returns: The SwiftDashSDK.Wallet instance
774+
func getFFIWallet(for wallet: HDWallet) async throws -> SwiftDashSDK.Wallet {
775+
guard let walletId = wallet.walletId else {
776+
throw WalletError.walletError("Wallet ID not available")
777+
}
778+
779+
let network = wallet.dashNetwork.toKeyWalletNetwork()
780+
781+
guard let ffiWallet = try? sdkWalletManager.getWallet(id: walletId, network: network) else {
782+
throw WalletError.walletError("Unable to retrieve FFI wallet from SDK")
783+
}
784+
785+
return ffiWallet
786+
}
787+
788+
/// Get the managed wallet for transaction building
789+
/// - Parameter wallet: The HDWallet to get the managed wallet for
790+
/// - Returns: The ManagedWallet instance with UTXO information
791+
func getManagedWallet(for wallet: HDWallet) async throws -> SwiftDashSDK.ManagedWallet {
792+
// Get the FFI wallet first
793+
let ffiWallet = try await getFFIWallet(for: wallet)
794+
795+
// Create a managed wallet from it
796+
// Note: This creates a new instance each time. In a production app,
797+
// you might want to cache these and keep them in sync with UTXO updates
798+
let managedWallet = try SwiftDashSDK.ManagedWallet(wallet: ffiWallet)
799+
800+
return managedWallet
801+
}
802+
771803
// MARK: - Private Methods
772804

773805
private func loadWallets() async {

0 commit comments

Comments
 (0)