Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get transaction's confirmation information (including block timestamp) #513

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.acinq.lightning.blockchain.electrum

import fr.acinq.bitcoin.BlockHeader
import fr.acinq.bitcoin.ByteVector32
import fr.acinq.bitcoin.Satoshi
import fr.acinq.bitcoin.Transaction
Expand All @@ -9,7 +10,48 @@ import fr.acinq.lightning.channel.LocalFundingStatus
import fr.acinq.lightning.transactions.Transactions
import fr.acinq.lightning.utils.MDCLogger
import fr.acinq.lightning.utils.sat
import kotlinx.coroutines.CompletableDeferred

data class ConfirmationStatus(
val minedAtBlockHeight: Int,
val currentConfirmationCount: Int,
val firstBlock: BlockHeader // includes timestamp of when block was mined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
val firstBlock: BlockHeader // includes timestamp of when block was mined
val includedInBlock: BlockHeader // includes timestamp of when block was mined

)

suspend fun IElectrumClient.getConfirmationStatus(txId: ByteVector32): ConfirmationStatus? {
val tx = kotlin.runCatching { getTx(txId) }.getOrNull()
return tx?.let { getConfirmationStatus(tx) }
}

/**
* Returns information about when the tx was confirmed
* (including the time of the first mined block)
* if the tx has 1 or more confirmations. Returns null otherwise.
*/
suspend fun IElectrumClient.getConfirmationStatus(tx: Transaction): ConfirmationStatus? {
val scriptHash = ElectrumClient.computeScriptHash(tx.txOut.first().publicKeyScript)
val scriptHashHistory = getScriptHashHistory(scriptHash)
return scriptHashHistory.find { it.txid == tx.txid }?.let { item ->
if (item.blockHeight <= 0) {
null
} else {
val replyTo = CompletableDeferred<ElectrumResponse>()
send(GetHeader(item.blockHeight), replyTo)
when (val res = replyTo.await()) {
is GetHeaderResponse -> {
val currentBlockHeight = startHeaderSubscription().blockHeight
val currentConfirmationCount = currentBlockHeight - item.blockHeight + 1
ConfirmationStatus(
minedAtBlockHeight = item.blockHeight,
currentConfirmationCount = currentConfirmationCount,
firstBlock = res.header
)
}
else -> null
}
}
}
}

suspend fun IElectrumClient.getConfirmations(txId: ByteVector32): Int? {
val tx = kotlin.runCatching { getTx(txId) }.getOrNull()
Expand Down