Skip to content

Commit

Permalink
Add tests from spec
Browse files Browse the repository at this point in the history
  • Loading branch information
thomash-acinq committed Sep 27, 2021
1 parent 0024ae4 commit 8ed1323
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ object OnionCodecs {

private val blindingKey: Codec[Blinding] = variableSizeBytesLong(varintoverflow, "blinding" | publicKey).as[Blinding]

val messageRelayNextCodec: Codec[MessageRelayNext] = TlvCodecs.lengthPrefixedTlvStream[OnionTlv](
val messageRelayNextCodec: Codec[MessageRelayNext] = TlvCodecs.tlvStream[OnionTlv](
discriminated[OnionTlv].by(varint)
.typecase(UInt64(4), outgoingNodeId)
.typecase(UInt64(12), blindingKey)).complete
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 ACINQ SAS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package fr.acinq.eclair.wire.protocol

import fr.acinq.bitcoin.Crypto.{PrivateKey, PublicKey}
import fr.acinq.bitcoin.{ByteVector32, Crypto}
import fr.acinq.eclair.crypto.Sphinx
import org.scalatest.funsuite.AnyFunSuite
import scodec.bits.{ByteVector, HexStringSyntax}
import scodec.{Attempt, DecodeResult}

import scala.util.{Failure, Success}

/**
* Created by thomash on 23/09/2021.
*/

class OnionMessagesSpec extends AnyFunSuite {

test("Simple enctlv for Alice, next is Bob") {
val nodePrivateKey = PrivateKey(hex"414141414141414141414141414141414141414141414141414141414141414101")
val nodeId = PublicKey(hex"02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619")
assert(nodePrivateKey.publicKey == nodeId)
val blindingSecret = PrivateKey(hex"050505050505050505050505050505050505050505050505050505050505050501")
val blindingKey = PublicKey(hex"0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f7")
assert(blindingSecret.publicKey == blindingKey)
val sharedSecret = ByteVector32(hex"2e83e9bc7821d3f6cec7301fa8493aee407557624fb5745bede9084852430e3f")
assert(Sphinx.computeSharedSecret(nodeId, blindingSecret) == sharedSecret)
assert(Sphinx.computeSharedSecret(blindingKey, nodePrivateKey) == sharedSecret)
assert(Sphinx.mac(ByteVector("blinded_node_id".getBytes), sharedSecret) == ByteVector32(hex"7d846b3445621d49a665e5698c52141e9dda8fa2fe0c3da7e0f9008ccc588a38"))
val blindedNodeId = PublicKey(hex"02004b5662061e9db495a6ad112b6c4eba228a079e8e304d9df50d61043acbc014")
val nextNodeId = PublicKey(hex"0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c")
val encmsg = hex"04210324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c"
val Sphinx.RouteBlinding.BlindedRoute(_, blindedHops) = Sphinx.RouteBlinding.create(blindingSecret, nodeId :: Nil, encmsg :: Nil)
assert(blindedHops.head.blindedPublicKey == blindedNodeId)
assert(Crypto.sha256(blindingKey.value ++ sharedSecret.bytes) == ByteVector32(hex"bae3d9ea2b06efd1b7b9b49b6cdcaad0e789474a6939ffa54ff5ec9224d5b76c"))
val enctlv = hex"6970e870b473ddbc27e3098bfa45bb1aa54f1f637f803d957e6271d8ffeba89da2665d62123763d9b634e30714144a1c165ac9"
assert(blindedHops.head.encryptedPayload == enctlv)
OnionCodecs.messageRelayNextCodec.decode(encmsg.bits) match {
case Attempt.Successful(DecodeResult(relayNext, _)) => assert(relayNext.nextNodeId == nextNodeId)
case Attempt.Failure(err) => fail(err.toString)
}
Sphinx.RouteBlinding.decryptPayload(nodePrivateKey, blindingKey, enctlv) match {
case Success((decrypted, nextBlindingKey)) => assert(decrypted == encmsg)
case Failure(err) => fail(err.toString)
}
}

}

0 comments on commit 8ed1323

Please sign in to comment.