Skip to content

Commit

Permalink
add sync wrapper around ledger getAddress function so it can be more …
Browse files Browse the repository at this point in the history
…easily integrated into existing code
  • Loading branch information
simonmcl committed Aug 12, 2024
1 parent ce9be84 commit a749cc5
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Sources/KukaiCoreSwift/Services/LedgerService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public class LedgerService: NSObject, CBPeripheralDelegate, CBCentralManagerDele
private var bag_connection = Set<AnyCancellable>()
private var bag_writer = Set<AnyCancellable>()
private var bag_apdu = Set<AnyCancellable>()
private var bag_addressFetcher = Set<AnyCancellable>()
private var counter = 0

/// Public shared instace to avoid having multiple copies of the underlying `JSContext` created
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a749cc5

Please sign in to comment.