Skip to content

Commit

Permalink
Fix serialization of onion messages (#643)
Browse files Browse the repository at this point in the history
The onion routing packet's size is 66 bytes more than its payload's size. We were taking this into account in the `read` function but not in the `write` one.
  • Loading branch information
thomash-acinq authored May 21, 2024
1 parent 66c1038 commit e63b537
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ data class OnionMessage(

override fun write(out: Output) {
LightningCodecs.writeBytes(blindingKey.value, out)
LightningCodecs.writeU16(onionRoutingPacket.payload.size(), out)
LightningCodecs.writeU16(onionRoutingPacket.payload.size() + 66, out)
OnionRoutingPacketSerializer(onionRoutingPacket.payload.size()).write(onionRoutingPacket, out)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import fr.acinq.lightning.blockchain.fee.FeeratePerByte
import fr.acinq.lightning.blockchain.fee.FeeratePerKw
import fr.acinq.lightning.channel.*
import fr.acinq.lightning.crypto.assertArrayEquals
import fr.acinq.lightning.message.OnionMessages
import fr.acinq.lightning.tests.utils.LightningTestSuite
import fr.acinq.lightning.utils.msat
import fr.acinq.lightning.utils.sat
Expand Down Expand Up @@ -864,4 +865,10 @@ class LightningCodecsTestsCommon : LightningTestSuite() {
assertEquals(decoded, LightningCodecs.encodedNodeId(ByteArrayInput(encoded.toByteArray())))
}
}

@Test
fun `encode and decode onion message`() {
val onionMessage = OnionMessages.buildMessage(randomKey(), randomKey(), listOf(), OnionMessages.Destination.Recipient(randomKey().publicKey(), null), TlvStream.empty()).right!!
assertEquals(onionMessage, OnionMessage.read(onionMessage.write()))
}
}

0 comments on commit e63b537

Please sign in to comment.