Add functions for LND subscription (#4062)

This commit is contained in:
benthecarman 2022-02-08 07:20:03 -06:00 committed by GitHub
parent e2b9c458e4
commit 44373f8449
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 1 deletions

View file

@ -56,6 +56,7 @@ import scala.util.{Failure, Success, Try}
class LndRpcClient(val instance: LndInstance, binaryOpt: Option[File] = None)( class LndRpcClient(val instance: LndInstance, binaryOpt: Option[File] = None)(
implicit system: ActorSystem) implicit system: ActorSystem)
extends NativeProcessFactory extends NativeProcessFactory
with LndUtils
with StartStopAsync[LndRpcClient] with StartStopAsync[LndRpcClient]
with Logging { with Logging {
instance match { instance match {
@ -248,6 +249,28 @@ class LndRpcClient(val instance: LndInstance, binaryOpt: Option[File] = None)(
lnd.subscribeInvoices(InvoiceSubscription()) lnd.subscribeInvoices(InvoiceSubscription())
} }
def subscribeTransactions(): Source[TxDetails, NotUsed] = {
lnd
.subscribeTransactions(GetTransactionsRequest())
.map(LndTransactionToTxDetails)
}
def subscribeChannelEvents(): Source[ChannelEventUpdate, NotUsed] = {
lnd.subscribeChannelEvents(ChannelEventSubscription())
}
def subscribePeerEvents(): Source[PeerEvent, NotUsed] = {
lnd.subscribePeerEvents(PeerEventSubscription())
}
def subscribeChannelGraph(): Source[GraphTopologyUpdate, NotUsed] = {
lnd.subscribeChannelGraph(GraphTopologySubscription())
}
def subscribeChannelBackups(): Source[ChanBackupSnapshot, NotUsed] = {
lnd.subscribeChannelBackups(ChannelBackupSubscription())
}
def getNewAddress: Future[BitcoinAddress] = { def getNewAddress: Future[BitcoinAddress] = {
logger.trace("lnd calling newaddress") logger.trace("lnd calling newaddress")

View file

@ -3,8 +3,10 @@ package org.bitcoins.lnd.rpc
import com.google.protobuf.ByteString import com.google.protobuf.ByteString
import lnrpc.ChannelPoint import lnrpc.ChannelPoint
import lnrpc.ChannelPoint.FundingTxid.FundingTxidBytes import lnrpc.ChannelPoint.FundingTxid.FundingTxidBytes
import org.bitcoins.commons.jsonmodels.lnd.TxDetails
import org.bitcoins.core.currency.Satoshis import org.bitcoins.core.currency.Satoshis
import org.bitcoins.core.number.UInt32 import org.bitcoins.core.number.UInt32
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.core.protocol.script.ScriptPubKey import org.bitcoins.core.protocol.script.ScriptPubKey
import org.bitcoins.core.protocol.transaction._ import org.bitcoins.core.protocol.transaction._
import org.bitcoins.crypto._ import org.bitcoins.crypto._
@ -13,7 +15,7 @@ import signrpc.TxOut
import scala.language.implicitConversions import scala.language.implicitConversions
object LndUtils { trait LndUtils {
implicit def byteVecToByteString(byteVector: ByteVector): ByteString = implicit def byteVecToByteString(byteVector: ByteVector): ByteString =
ByteString.copyFrom(byteVector.toArray) ByteString.copyFrom(byteVector.toArray)
@ -50,4 +52,29 @@ object LndUtils {
val txId = FundingTxidBytes(outPoint.txId.bytes) val txId = FundingTxidBytes(outPoint.txId.bytes)
ChannelPoint(txId, outPoint.vout.toInt) ChannelPoint(txId, outPoint.vout.toInt)
} }
implicit def LndTransactionToTxDetails(
details: lnrpc.Transaction): TxDetails = {
val blockHashOpt = if (details.blockHash.isEmpty) {
None
} else Some(DoubleSha256DigestBE(details.blockHash))
val addrs =
details.destAddresses.map(BitcoinAddress.fromString).toVector
TxDetails(
txId = DoubleSha256DigestBE(details.txHash),
amount = Satoshis(details.amount),
numConfirmations = details.numConfirmations,
blockHashOpt = blockHashOpt,
blockHeight = details.blockHeight,
timeStamp = details.timeStamp,
totalFees = Satoshis(details.totalFees),
destAddresses = addrs,
tx = Transaction(details.rawTxHex),
label = details.label
)
}
} }
object LndUtils extends LndUtils