Skip to content
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 @@ -90,22 +90,25 @@ object Encryption {
}

private fun fromEncryptedPeerStorageCurrent(key: PrivateKey, encryptedPeerStorage: EncryptedPeerStorage): Result<Serialization.PeerStorageDeserializationResult> {
// data is prefixed by 1 byte of serialization meta-info
return runCatching { decrypt(key.value, encryptedPeerStorage.data.drop(1).toByteArray()) }
.map { Serialization.deserializePeerStorage(encryptedPeerStorage.data[0], it) }
// Data should be prefixed by 1 byte of serialization meta-info, if this is a recent peer storage.
return runCatching {
val decrypted = decrypt(key.value, encryptedPeerStorage.data.drop(1).toByteArray())
Serialization.deserializePeerStorage(encryptedPeerStorage.data[0], decrypted)
}
}

/**
* Backward compatibility for an older backup mechanism where each channel data was stored individually, and restored from channel_reestablish messages.
* Backwards-compatibility for an older backup mechanism where each channel data was stored individually, and restored from channel_reestablish messages.
*/
private fun fromEncryptedPeerStorageLegacyChannelData(key: PrivateKey, encryptedPeerStorage: EncryptedPeerStorage): Result<Serialization.PeerStorageDeserializationResult> {
// The blob is a list of individually encrypted channel data (because each of them was previously stored separately)
// The blob should be a list of individually encrypted channel data (because each of them was previously stored separately).
val input = ByteArrayInput(encryptedPeerStorage.data.toByteArray())
val encryptedChannelDatas = input.readCollection { input.readDelimitedByteArray() }.toList()
val res: List<Result<Serialization.DeserializationResult>> = encryptedChannelDatas.map { data ->
val encryptedChannelDatas = runCatching { input.readCollection { input.readDelimitedByteArray() }.toList() }
.getOrDefault(listOf())
val res = encryptedChannelDatas.map { data ->
runCatching { decrypt(key.value, data.drop(2).toByteArray()) }
.recoverCatching { decrypt(key.value, data) }
.map { Serialization.deserialize(it) }
.mapCatching { Serialization.deserialize(it) }
}

val states = res.mapNotNull { it.getOrNull() }.filterIsInstance<Serialization.DeserializationResult.Success>().map { it.state }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package fr.acinq.lightning.io.peer

import fr.acinq.bitcoin.Block
import fr.acinq.bitcoin.ByteVector
import fr.acinq.bitcoin.ByteVector32
import fr.acinq.bitcoin.PrivateKey
import fr.acinq.bitcoin.Script
import fr.acinq.bitcoin.byteVector
import fr.acinq.bitcoin.utils.Either
import fr.acinq.lightning.*
import fr.acinq.lightning.Lightning.randomBytes
import fr.acinq.lightning.Lightning.randomBytes32
import fr.acinq.lightning.Lightning.randomKey
import fr.acinq.lightning.blockchain.WatchConfirmed
Expand Down Expand Up @@ -609,6 +612,10 @@ class PeerTest : LightningTestSuite() {
val backup = PersistedChannelState.fromEncryptedPeerStorage(TestConstants.Bob.nodeParams.nodePrivateKey, peerStorage.eps, null).getOrThrow()
assertIs<PeerStorageDeserializationResult.Success>(backup)
assertEquals(listOf(normal), backup.states) // the backup contains only the Normal channel
val emptyBackup = PersistedChannelState.fromEncryptedPeerStorage(TestConstants.Bob.nodeParams.nodePrivateKey, EncryptedPeerStorage(ByteVector.empty), null)
assertTrue(emptyBackup.isFailure)
val corruptedBackup = PersistedChannelState.fromEncryptedPeerStorage(TestConstants.Bob.nodeParams.nodePrivateKey, EncryptedPeerStorage(randomBytes(731).byteVector()), null)
assertTrue(corruptedBackup.isFailure)

// usePeerStorage = false
Peer.updatePeerStorage(TestConstants.Bob.nodeParams.copy(usePeerStorage = false), mapOf(normal.channelId to normal, closed.channelId to closed), peerConnection, Features(Feature.ProvideStorage to FeatureSupport.Optional), null)
Expand Down