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

Use TlvStream for encrypted channel backup #260

Merged
merged 2 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
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
Expand Up @@ -146,9 +146,8 @@ public suspend fun CoroutineScope.newPeer(
nextLocalCommitmentNumber = state.commitments.localCommit.index + 1,
nextRemoteRevocationNumber = state.commitments.remoteCommit.index,
yourLastCommitmentSecret = PrivateKey(yourLastPerCommitmentSecret),
myCurrentPerCommitmentPoint = myCurrentPerCommitmentPoint,
state.commitments.remoteChannelData
)
myCurrentPerCommitmentPoint = myCurrentPerCommitmentPoint
).withChannelData(state.commitments.remoteChannelData)

val msg = LightningMessage.encode(channelReestablish)
peer.send(BytesReceived(msg))
Expand Down
20 changes: 9 additions & 11 deletions src/commonMain/kotlin/fr/acinq/lightning/channel/Channel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ sealed class ChannelState {
private fun updateActions(actions: List<ChannelAction>): List<ChannelAction> = when {
this is ChannelStateWithCommitments && this.isZeroReserve -> actions.map {
when {
it is ChannelAction.Message.Send && it.message is FundingSigned -> it.copy(message = it.message.copy(channelData = Serialization.encrypt(privateKey.value, this)))
it is ChannelAction.Message.Send && it.message is CommitSig -> it.copy(message = it.message.copy(channelData = Serialization.encrypt(privateKey.value, this)))
it is ChannelAction.Message.Send && it.message is RevokeAndAck -> it.copy(message = it.message.copy(channelData = Serialization.encrypt(privateKey.value, this)))
it is ChannelAction.Message.Send && it.message is ClosingSigned -> it.copy(message = it.message.copy(channelData = Serialization.encrypt(privateKey.value, this)))
it is ChannelAction.Message.Send && it.message is FundingSigned -> it.copy(message = it.message.withChannelData(Serialization.encrypt(privateKey.value, this)))
it is ChannelAction.Message.Send && it.message is CommitSig -> it.copy(message = it.message.withChannelData(Serialization.encrypt(privateKey.value, this)))
it is ChannelAction.Message.Send && it.message is RevokeAndAck -> it.copy(message = it.message.withChannelData(Serialization.encrypt(privateKey.value, this)))
it is ChannelAction.Message.Send && it.message is Shutdown -> it.copy(message = it.message.withChannelData(Serialization.encrypt(privateKey.value, this)))
it is ChannelAction.Message.Send && it.message is ClosingSigned -> it.copy(message = it.message.withChannelData(Serialization.encrypt(privateKey.value, this)))
else -> it

}
}
else -> actions
Expand Down Expand Up @@ -738,9 +738,8 @@ data class Offline(val state: ChannelStateWithCommitments) : ChannelState() {
nextLocalCommitmentNumber = state.commitments.localCommit.index + 1,
nextRemoteRevocationNumber = state.commitments.remoteCommit.index,
yourLastCommitmentSecret = PrivateKey(yourLastPerCommitmentSecret),
myCurrentPerCommitmentPoint = myCurrentPerCommitmentPoint,
state.commitments.remoteChannelData
)
myCurrentPerCommitmentPoint = myCurrentPerCommitmentPoint
).withChannelData(state.commitments.remoteChannelData)
logger.info { "c:${state.channelId} syncing ${state::class}" }
val nextState = state.updateCommitments(state.commitments.updateFeatures(event.localInit, event.remoteInit))
Pair(Syncing(nextState, false), listOf(ChannelAction.Message.Send(channelReestablish)))
Expand Down Expand Up @@ -869,9 +868,8 @@ data class Syncing(val state: ChannelStateWithCommitments, val waitForTheirReest
nextLocalCommitmentNumber = nextState.commitments.localCommit.index + 1,
nextRemoteRevocationNumber = nextState.commitments.remoteCommit.index,
yourLastCommitmentSecret = PrivateKey(yourLastPerCommitmentSecret),
myCurrentPerCommitmentPoint = myCurrentPerCommitmentPoint,
nextState.commitments.remoteChannelData
)
myCurrentPerCommitmentPoint = myCurrentPerCommitmentPoint
).withChannelData(nextState.commitments.remoteChannelData)
val actions = listOf<ChannelAction>(ChannelAction.Message.Send(channelReestablish))
// now apply their reestablish message to the restored state
val (nextState1, actions1) = Syncing(nextState, waitForTheirReestablishMessage = false).processInternal(event)
Expand Down
85 changes: 85 additions & 0 deletions src/commonMain/kotlin/fr/acinq/lightning/wire/ChannelTlv.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fr.acinq.lightning.channel.ChannelOrigin
import fr.acinq.lightning.channel.ChannelVersion
import fr.acinq.lightning.serialization.ByteVectorKSerializer
import fr.acinq.lightning.utils.BitField
import fr.acinq.lightning.utils.toByteVector
import kotlinx.serialization.Serializable

@OptIn(ExperimentalUnsignedTypes::class)
Expand Down Expand Up @@ -99,3 +100,87 @@ sealed class ChannelTlv : Tlv {
}
}
}

@Serializable
sealed class FundingSignedTlv : Tlv {
@Serializable
data class ChannelData(val ecb: EncryptedChannelData) : FundingSignedTlv() {
override val tag: Long get() = ChannelData.tag
override fun write(out: Output) = LightningCodecs.writeBytes(ecb.data, out)

companion object : TlvValueReader<ChannelData> {
const val tag: Long = 0x47010000
override fun read(input: Input): ChannelData = ChannelData(EncryptedChannelData(LightningCodecs.bytes(input, input.availableBytes).toByteVector()))
}
}
}

@Serializable
sealed class CommitSigTlv : Tlv {
@Serializable
data class ChannelData(val ecb: EncryptedChannelData) : CommitSigTlv() {
override val tag: Long get() = ChannelData.tag
override fun write(out: Output) = LightningCodecs.writeBytes(ecb.data, out)

companion object : TlvValueReader<ChannelData> {
const val tag: Long = 0x47010000
override fun read(input: Input): ChannelData = ChannelData(EncryptedChannelData(LightningCodecs.bytes(input, input.availableBytes).toByteVector()))
}
}
}

@Serializable
sealed class RevokeAndAckTlv : Tlv {
@Serializable
data class ChannelData(val ecb: EncryptedChannelData) : RevokeAndAckTlv() {
override val tag: Long get() = ChannelData.tag
override fun write(out: Output) = LightningCodecs.writeBytes(ecb.data, out)

companion object : TlvValueReader<ChannelData> {
const val tag: Long = 0x47010000
override fun read(input: Input): ChannelData = ChannelData(EncryptedChannelData(LightningCodecs.bytes(input, input.availableBytes).toByteVector()))
}
}
}

@Serializable
sealed class ChannelReestablishTlv : Tlv {
@Serializable
data class ChannelData(val ecb: EncryptedChannelData) : ChannelReestablishTlv() {
override val tag: Long get() = ChannelData.tag
override fun write(out: Output) = LightningCodecs.writeBytes(ecb.data, out)

companion object : TlvValueReader<ChannelData> {
const val tag: Long = 0x47010000
override fun read(input: Input): ChannelData = ChannelData(EncryptedChannelData(LightningCodecs.bytes(input, input.availableBytes).toByteVector()))
}
}
}

@Serializable
sealed class ShutdownTlv : Tlv {
@Serializable
data class ChannelData(val ecb: EncryptedChannelData) : ShutdownTlv() {
override val tag: Long get() = ChannelData.tag
override fun write(out: Output) = LightningCodecs.writeBytes(ecb.data, out)

companion object : TlvValueReader<ChannelData> {
const val tag: Long = 0x47010000
override fun read(input: Input): ChannelData = ChannelData(EncryptedChannelData(LightningCodecs.bytes(input, input.availableBytes).toByteVector()))
}
}
}

@Serializable
sealed class ClosingSignedTlv : Tlv {
@Serializable
data class ChannelData(val ecb: EncryptedChannelData) : ClosingSignedTlv() {
override val tag: Long get() = ChannelData.tag
override fun write(out: Output) = LightningCodecs.writeBytes(ecb.data, out)

companion object : TlvValueReader<ChannelData> {
const val tag: Long = 0x47010000
override fun read(input: Input): ChannelData = ChannelData(EncryptedChannelData(LightningCodecs.bytes(input, input.availableBytes).toByteVector()))
}
}
}
22 changes: 0 additions & 22 deletions src/commonMain/kotlin/fr/acinq/lightning/wire/LightningCodecs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import fr.acinq.bitcoin.io.ByteArrayOutput
import fr.acinq.bitcoin.io.Input
import fr.acinq.bitcoin.io.Output
import fr.acinq.lightning.utils.leftPaddedCopyOf
import fr.acinq.lightning.utils.toByteVector
import fr.acinq.secp256k1.Hex
import kotlin.jvm.JvmStatic

@OptIn(ExperimentalUnsignedTypes::class)
Expand Down Expand Up @@ -202,24 +200,4 @@ object LightningCodecs {
return bytes(input, length)
}

private val channelDataMagic = Hex.decode("fe 47010000").toByteVector()

fun writeChannelData(msg: ByteArray, out: Output) {
if (msg.isNotEmpty()) {
out.write(channelDataMagic.toByteArray())
writeBigSize(msg.size.toLong(), out)
writeBytes(msg, out)
}
}

fun writeChannelData(msg: EncryptedChannelData, out: Output) = writeChannelData(msg.data.toByteArray(), out)

fun channelData(input: Input): EncryptedChannelData {
if (input.availableBytes <= 5) return EncryptedChannelData.empty
val magic = bytes(input, 5)
if (!channelDataMagic.contentEquals(magic)) return EncryptedChannelData.empty
val length = bigSize(input)
return EncryptedChannelData(bytes(input, length).toByteVector())
}

}
Loading