From a749cc56f841b1b59cd9e86f685360246ec2b293 Mon Sep 17 00:00:00 2001 From: Simon McLoughlin Date: Mon, 12 Aug 2024 15:56:28 +0100 Subject: [PATCH] add sync wrapper around ledger getAddress function so it can be more easily integrated into existing code --- .../Services/LedgerService.swift | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Sources/KukaiCoreSwift/Services/LedgerService.swift b/Sources/KukaiCoreSwift/Services/LedgerService.swift index 03b6fcaa..21beb929 100644 --- a/Sources/KukaiCoreSwift/Services/LedgerService.swift +++ b/Sources/KukaiCoreSwift/Services/LedgerService.swift @@ -143,6 +143,7 @@ public class LedgerService: NSObject, CBPeripheralDelegate, CBCentralManagerDele private var bag_connection = Set() private var bag_writer = Set() private var bag_apdu = Set() + private var bag_addressFetcher = Set() private var counter = 0 /// Public shared instace to avoid having multiple copies of the underlying `JSContext` created @@ -329,6 +330,29 @@ public class LedgerService: NSObject, CBPeripheralDelegate, CBCentralManagerDele }).eraseToAnyPublisher() } + /** + Get a TZ address and public key from the current connected Ledger device + - parameter forDerivationPath: Optional. The derivation path to use to extract the address from the underlying HD wallet + - parameter curve: Optional. The `EllipticalCurve` to use to extract the address + - parameter verify: Whether or not to ask the ledger device to prompt the user to show them what the TZ address should be, to ensure the mobile matches + - returns: An async `Result` object, allowing code to be triggered via while loops more easily + */ + public func getAddress(forDerivationPath derivationPath: String = HD.defaultDerivationPath, curve: EllipticalCurve = .ed25519, verify: Bool) async -> Result<(address: String, publicKey: String), KukaiError> { + return await withCheckedContinuation({ continuation in + var cancellable: AnyCancellable! + cancellable = getAddress(forDerivationPath: derivationPath, curve: curve, verify: verify) + .sink(onError: { error in + continuation.resume(returning: Result.failure(error)) + }, onSuccess: { addressObj in + continuation.resume(returning: Result.success(addressObj)) + }, onComplete: { [weak self] in + self?.bag_addressFetcher.remove(cancellable) + }) + + cancellable.store(in: &bag_addressFetcher) + }) + } + /** Sign an operation payload with the underlying secret key, returning the signature - parameter hex: An operation converted to JSON, forged and watermarked, converted to a hex string. (Note: there are some issues with the ledger app signing batch transactions. May simply return no result at all. Can't run REVEAL and TRANSACTION together for example)