311 lines
12 KiB
Swift
311 lines
12 KiB
Swift
//
|
|
// LightningRepository.swift
|
|
// wallet
|
|
//
|
|
// Created by Jason on 8/30/20.
|
|
// Copyright © 2020 Jason. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class LightningRepository {
|
|
|
|
func createWallet(password: String, onSuccess: @escaping ([String]) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.generateSeed { [weak self] (seed, error) in
|
|
guard let self = self else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
Lightning.shared.createWallet(password: password, cipherSeedMnemonic: seed) { [weak self] (error) in
|
|
guard self != nil else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
onSuccess(seed)
|
|
}
|
|
}
|
|
}
|
|
func closeChannel(channel:Lnrpc_Channel, onSuccess: @escaping (Lnrpc_ClosedChannelsResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.closeChannel(channel: channel) { response, error in
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
debugPrint("👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻 Close Channel error ", response)
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func openChannel(localFunding: Int64, pushSat: Int64, host: String, port: UInt, nodePubKey: NodePublicKey, closeAddress: String?, onSuccess: @escaping (String) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
|
|
Lightning.shared.connectToNode(nodePubkey: nodePubKey, hostAddress: host, hostPort: port) { [weak self] (response, error) in
|
|
if error != nil && error?.localizedDescription.contains("already connected to peer") == false {
|
|
debugPrint("🔆🔆🔆🔆🔆 Connect to node error ", error)
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
onSuccess("Connected to peer")
|
|
|
|
Lightning.shared.openChannel(localFundingAmount: localFunding, pushSat: pushSat, closeAddress: closeAddress, nodePubkey: nodePubKey) { (response, error) in
|
|
guard error == nil else {
|
|
debugPrint("❤️❤️❤️❤️❤️❤️❤️ Open Channel error ", error)
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
guard let update = response.update else {
|
|
return
|
|
}
|
|
|
|
switch update {
|
|
case .chanPending(let pendingUpdate):
|
|
onSuccess("Channel open pending update\nTXID: \(pendingUpdate.txid.base64EncodedString())")
|
|
case .chanOpen(let openUpdate):
|
|
onSuccess("Channel open success update")
|
|
case .psbtFund(let onpsbtFund):
|
|
onSuccess("I don't know why you would get this error")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func listChannels(onSuccess: @escaping (Lnrpc_ListChannelsResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
|
|
Lightning.shared.listChannels { (response, error) in
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
debugPrint("👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻 List Channel error ", response)
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func listClosedChannels(onSuccess: @escaping (Lnrpc_ClosedChannelsResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
|
|
Lightning.shared.listClosedChannels { (response, error) in
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
debugPrint("👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻 List Channel error ", response)
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func listPendingChannels(onSuccess: @escaping (Lnrpc_PendingChannelsResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
|
|
Lightning.shared.listPendingChannels { (response, error) in
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
debugPrint("👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻 List Pending Channel error ", response)
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func getChannelInfo(id: UInt64, onSuccess: @escaping (Lnrpc_ChannelEdge) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
|
|
Lightning.shared.getChanInfo(id: id) { (response, error) in
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
debugPrint("👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻 Get Channel info error ", response)
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func listPeers(onSuccess: @escaping (Lnrpc_ListPeersResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.listPeers { [weak self] (response, error) in
|
|
if error != nil && error?.localizedDescription.contains("already connected to peer") == false {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
debugPrint("Listing peers ", response)
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func listInvoices(onSuccess: @escaping (Lnrpc_ListInvoiceResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
|
|
Lightning.shared.listInvoices { (response, error) in
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
debugPrint("👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻 List Invoices error ", response)
|
|
for payment in response.invoices{
|
|
Lightning.shared.queryRequestFee(key: payment.fallbackAddr) { responseQuery, error in
|
|
debugPrint("Query request fee", responseQuery, error)
|
|
}
|
|
}
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func listPayments(onSuccess: @escaping (Lnrpc_ListPaymentsResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
|
|
Lightning.shared.listPayments { (response, error) in
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
debugPrint("👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻 List Payments error ", response)
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func wipeWallet(onSuccess: @escaping () -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.stop { (error) in
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
Lightning.shared.purge()
|
|
onSuccess()
|
|
|
|
}
|
|
}
|
|
|
|
func getWalletBalance(onSuccess: @escaping (_ total: Int64, _ confirmed: Int64, _ unconfirmed: Int64) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.walletBalance { [weak self] (balanceResponse, error) in
|
|
guard self != nil else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
onSuccess(
|
|
balanceResponse.totalBalance,
|
|
balanceResponse.confirmedBalance,
|
|
balanceResponse.unconfirmedBalance
|
|
)
|
|
}
|
|
}
|
|
|
|
func getNewAddress(onSuccess: @escaping (String) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.newAddress { [weak self] (address, error) in
|
|
guard self != nil else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
onSuccess(address)
|
|
}
|
|
}
|
|
|
|
func getInfo(onSuccess: @escaping (Lnrpc_GetInfoResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.info { [weak self] (info, error) in
|
|
guard self != nil else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
onSuccess(info)
|
|
}
|
|
}
|
|
|
|
func unlockWallet(password: String, onSuccess: @escaping () -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.unlockWalet(password: password) { [weak self] (error) in
|
|
guard self != nil else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
onSuccess()
|
|
}
|
|
}
|
|
func createInvoice(amount: Int, memo: String, onSuccess: @escaping (Lnrpc_AddInvoiceResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.createPayRequest(amount: amount, memo: memo) { [weak self] (response, error) in
|
|
guard self != nil else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func getPaymentInfo(paymentRequest: String, onSuccess: @escaping (Lnrpc_PayReq) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.decodePaymentRequest(paymentRequest) { [weak self] (decodedResponse, error) in
|
|
debugPrint("Decoded response", decodedResponse)
|
|
guard self != nil else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
onSuccess(decodedResponse)
|
|
}
|
|
}
|
|
|
|
func getLightningChannelBalance(onSuccess: @escaping (_ balance: Lnrpc_ChannelBalanceResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.lightningChannelBalance { [weak self] (balanceResponse, error) in
|
|
guard self != nil else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
onSuccess(balanceResponse)
|
|
}
|
|
}
|
|
func estimateFee(address: String, amount: Int64, onSuccess: @escaping (Lnrpc_EstimateFeeResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.estimateFee(address, amount: amount) { response, error in
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
debugPrint("Estimate fee response",response)
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func feeReport(onSuccess: @escaping (Lnrpc_FeeReportResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.feeReport(){ response, error in
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
debugPrint("Fee Report fee response",response)
|
|
onSuccess(response)
|
|
}
|
|
}
|
|
func pay(paymentRequest: String, onSuccess: @escaping (Lnrpc_SendResponse) -> Void, onFailure: @escaping (Error?) -> Void) {
|
|
Lightning.shared.decodePaymentRequest(paymentRequest) { [weak self] (decodedResponse, error) in
|
|
debugPrint("Decoded response", decodedResponse)
|
|
guard self != nil else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
//Requst decoded succesfully so can be used to make the payment
|
|
Lightning.shared.payRequest(paymentRequest) { [weak self] (sendResponse, error) in
|
|
guard self != nil else { return }
|
|
|
|
guard error == nil else {
|
|
onFailure(error)
|
|
return
|
|
}
|
|
|
|
onSuccess(sendResponse)
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|