Skip to content

Commit a8f6914

Browse files
committed
Make local LND node initialization methods dynamic
1 parent 0050220 commit a8f6914

File tree

2 files changed

+56
-47
lines changed

2 files changed

+56
-47
lines changed

LightningKit/Kit.swift

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import GRPC
33
import RxSwift
44

55
public class Kit {
6-
enum ArgumentError: Error {
6+
public enum KitErrors: Error {
77
case wrongChannelPoint
8+
case cannotInitRemoteNode
89
}
910

1011
private let lndNode: ILndNode
@@ -164,7 +165,7 @@ public class Kit {
164165
let channelPointParts = channelPoint.split(separator: ":")
165166

166167
guard channelPointParts.count == 0, let outputIndex = UInt32(String(channelPointParts[1])) else {
167-
throw ArgumentError.wrongChannelPoint
168+
throw KitErrors.wrongChannelPoint
168169
}
169170

170171
var channelPoint = Lnrpc_ChannelPoint()
@@ -179,6 +180,38 @@ public class Kit {
179180
return try lndNode.closeChannelSingle(request: request)
180181
}
181182

183+
// LocalLnd methods
184+
185+
public func start(password: String) -> Single<Void> {
186+
guard let localNode = lndNode as? LocalLnd else {
187+
return Single.just(Void())
188+
}
189+
190+
return localNode.startAndUnlock(password: password)
191+
}
192+
193+
public func create(password: String) -> Single<[String]> {
194+
guard let localNode = lndNode as? LocalLnd else {
195+
return Single.error(KitErrors.cannotInitRemoteNode)
196+
}
197+
198+
return localNode.start().flatMap {
199+
localNode.createWalletSingle(password: password)
200+
}
201+
}
202+
203+
public func restore(words: [String], password: String) -> Single<Void> {
204+
guard let localNode = lndNode as? LocalLnd else {
205+
return Single.error(KitErrors.cannotInitRemoteNode)
206+
}
207+
208+
return localNode.start().flatMap {
209+
localNode.restoreWalletSingle(words: words, password: password)
210+
}
211+
}
212+
213+
// Private methods
214+
182215
private func retryWhenStatusIsSyncingOrRunning<T>(_ observable: Observable<T>) -> Observable<T> {
183216
observable.retryWhen { [weak self] errorObservable -> Observable<(Error, NodeStatus)> in
184217
guard let kit = self else {
@@ -193,8 +226,6 @@ public class Kit {
193226
}
194227

195228
public extension Kit {
196-
static var lightningKitLocalLnd: Kit? = nil
197-
198229
static func validateRemoteConnection(rpcCredentials: RpcCredentials) -> Single<Void> {
199230
do {
200231
let remoteLndNode = try RemoteLnd(rpcCredentials: rpcCredentials)
@@ -205,42 +236,16 @@ public extension Kit {
205236
}
206237
}
207238

208-
static func local(credentials: LocalNodeCredentials) -> Kit {
209-
if let kit = lightningKitLocalLnd {
210-
return kit
211-
}
212-
213-
let localLnd = LocalLnd(filesDir: credentials.lndDirPath)
214-
localLnd.startAndUnlock(password: credentials.password)
215-
216-
let kit = Kit(lndNode: localLnd)
217-
lightningKitLocalLnd = kit
218-
219-
return kit
220-
}
221-
222-
static func createLocal(credentials: LocalNodeCredentials) -> Single<[String]> {
223-
let localLnd = LocalLnd(filesDir: credentials.lndDirPath)
224-
lightningKitLocalLnd = Kit(lndNode: localLnd)
225-
226-
return localLnd.start().flatMap {
227-
localLnd.createWalletSingle(password: credentials.password)
228-
}
229-
}
230-
231-
static func restoreLocal(words: [String], credentials: LocalNodeCredentials) -> Single<Void> {
232-
let localLnd = LocalLnd(filesDir: credentials.lndDirPath)
233-
lightningKitLocalLnd = Kit(lndNode: localLnd)
234-
235-
return localLnd.start().flatMap {
236-
localLnd.restoreWalletSingle(words: words, password: credentials.password)
237-
}
238-
}
239-
240239
static func remote(rpcCredentials: RpcCredentials) throws -> Kit {
241240
let remoteLndNode = try RemoteLnd(rpcCredentials: rpcCredentials)
242241
remoteLndNode.scheduleStatusUpdates()
243242

244243
return Kit(lndNode: remoteLndNode)
245244
}
245+
246+
static func local() throws -> Kit {
247+
let localLnd = LocalLnd(filesDir: try FileManager.default.walletDirectory().path)
248+
249+
return Kit(lndNode: localLnd)
250+
}
246251
}

LightningKit/Local/LocalLnd.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,18 @@ class LocalLnd: ILndNode {
216216
}
217217
}
218218

219-
func startAndUnlock(password: String) {
219+
func startAndUnlock(password: String) -> Single<Void> {
220220
start()
221221
.flatMap { [weak self] _ in
222222
var request = Lnrpc_UnlockWalletRequest()
223223
request.walletPassword = Data(Array(password.utf8))
224224

225225
return self?.unlockWalletSingle(request: request) ?? Single.error(NodeNotRetained())
226226
}
227-
.subscribe(
228-
onSuccess: { [weak self] in
229-
self?.scheduleStatusUpdates()
230-
},
231-
onError: { [weak self] in self?.status = .error($0) }
227+
.do(
228+
onSuccess: { [weak self] _ in self?.scheduleStatusUpdates() },
229+
onError: { [weak self] in self?.status = .error($0) }
232230
)
233-
.disposed(by: disposeBag)
234231
}
235232

236233
func createWalletSingle(password: String) -> Single<[String]> {
@@ -240,8 +237,11 @@ class LocalLnd: ILndNode {
240237
return Single<[String]>.error(NodeNotRetained())
241238
}
242239

243-
return node.initWalletSingle(mnemonicWords: genSeedResponse.cipherSeedMnemonic, password: password)
244-
.do(onSuccess: { [weak self] _ in self?.scheduleStatusUpdates() })
240+
return node.initWalletSingle(mnemonicWords: genSeedResponse.cipherSeedMnemonic, password: password, recoveryWindow: 0)
241+
.do(
242+
onSuccess: { [weak self] _ in self?.scheduleStatusUpdates() },
243+
onError: { [weak self] in self?.status = .error($0) }
244+
)
245245
.map { _ in
246246
genSeedResponse.cipherSeedMnemonic
247247
}
@@ -250,14 +250,18 @@ class LocalLnd: ILndNode {
250250

251251
func restoreWalletSingle(words: [String], password: String) -> Single<Void> {
252252
initWalletSingle(mnemonicWords: words, password: password)
253-
.do(onSuccess: { [weak self] _ in self?.scheduleStatusUpdates() })
253+
.do(
254+
onSuccess: { [weak self] _ in self?.scheduleStatusUpdates() },
255+
onError: { [weak self] in self?.status = .error($0) }
256+
)
254257
}
255258

256-
func initWalletSingle(mnemonicWords: [String], password: String) -> Single<Void> {
259+
func initWalletSingle(mnemonicWords: [String], password: String, recoveryWindow: Int32 = 100) -> Single<Void> {
257260
Single.create { emitter in
258261
var msg = Lnrpc_InitWalletRequest()
259262
msg.cipherSeedMnemonic = mnemonicWords
260263
msg.walletPassword = Data(Array(password.utf8))
264+
msg.recoveryWindow = recoveryWindow
261265

262266
LndmobileInitWallet(try! msg.serializedData(), VoidResponseCallback(emitter: emitter))
263267

0 commit comments

Comments
 (0)