@@ -3,8 +3,9 @@ import GRPC
33import RxSwift
44
55public 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
195228public 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}
0 commit comments