Skip to content

Commit ba3f04a

Browse files
committed
fixup! sphinx_test: add test for blinded route processing
1 parent e0210d7 commit ba3f04a

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

path_test.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func TestOnionMessageRouteBlinding(t *testing.T) {
385385
require.NoError(t, err)
386386

387387
// Once we have the raw file, we'll unpack it into our
388-
// blindingJsonTestCase struct defined above.
388+
// onionMessageJsonTestCase struct defined above.
389389
testCase := &onionMessageJsonTestCase{}
390390
require.NoError(t, json.Unmarshal(jsonBytes, testCase))
391391

@@ -402,9 +402,7 @@ func TestOnionMessageRouteBlinding(t *testing.T) {
402402
peelOnion := func(key *btcec.PrivateKey,
403403
blindingPoint *btcec.PublicKey) *ProcessedPacket {
404404

405-
r := NewRouter(
406-
&PrivKeyECDH{PrivKey: key}, NewMemoryReplayLog(),
407-
)
405+
r := NewRouter(&PrivKeyECDH{PrivKey: key}, NewMemoryReplayLog())
408406

409407
require.NoError(t, r.Start())
410408
defer r.Stop()
@@ -429,22 +427,30 @@ func TestOnionMessageRouteBlinding(t *testing.T) {
429427
// on layers outside the scope of this library, we provide handle these
430428
// cases manually for the sake of the test.
431429
var (
432-
firstBlinding = pubKeyFromString(testCase.Route.FirstPathKey)
433-
430+
firstBlinding = pubKeyFromString(testCase.Route.FirstPathKey)
434431
concatIndex = 1
435-
blindingOverride = pubKeyFromString(hops[0].EncodedOnionMessageTLVs.NextPathKeyOverride)
432+
blindingOverride = pubKeyFromString(
433+
hops[0].EncodedOnionMessageTLVs.NextPathKeyOverride,
434+
)
436435
)
437436

438-
var blindingPoint *btcec.PublicKey
437+
// Onion message routes are always entirely blinded, so
438+
// the first hop will always use the first blinding
439+
// point.
440+
blindingPoint := firstBlinding
441+
currentOnionPacket := onionPacket
439442
for i, hop := range testCase.Decrypt.Hops {
443+
// We encode the onion message packet to a buffer at each hop to
444+
// compare it to the onion message packet in the test vector.
440445
buff := bytes.NewBuffer(nil)
441-
require.NoError(t, onionPacket.Encode(buff))
446+
require.NoError(t, currentOnionPacket.Encode(buff))
442447

443448
// hop.OnionMessage contains the onion_message hex string. This
444449
// contains the type 513 (two bytes), the path_key (33 bytes)
445450
// and the length of the onion_message_packet (two bytes). We
446451
// are only interested in the onion_message_packet so we only
447-
// check that part.
452+
// check that part. 2 + 33 + 2 = 37 bytes, so we skip the first
453+
// 37 bytes, which equals 74 hex characters.
448454
const onionMessageHexHeaderLen = 74
449455

450456
require.Equal(
@@ -454,24 +460,30 @@ func TestOnionMessageRouteBlinding(t *testing.T) {
454460

455461
priv := privKeyFromString(hop.PrivKey)
456462

457-
switch i {
458-
case 0:
459-
// Onion message routes are always entirely blinded, so
460-
// the first hop will always use the first blinding
461-
// point.
462-
blindingPoint = firstBlinding
463-
case concatIndex:
463+
if i == concatIndex {
464464
blindingPoint = blindingOverride
465465
}
466466

467+
// With peelOnion we call into ProcessOnionPacket (with the
468+
// functional option WithBlindingPoint) and we expect that the
469+
// onion message packet for this hop is processed without error,
470+
// otherwise peelOnion fails the test.
467471
processedPkt := peelOnion(priv, blindingPoint)
468472

473+
// We derive the next blinding point from the current blinding
474+
// point and the private key of the current hop. The new
475+
// blindingPoint will be used to peel the next hop's onion
476+
// unless it is overridden by a blinding override.
469477
blindingPoint, err = NextEphemeral(
470478
&PrivKeyECDH{priv}, blindingPoint,
471479
)
472480
require.NoError(t, err)
473481

474-
onionPacket = processedPkt.NextPacket
482+
// We set the current onion packet to the next packet in the
483+
// processed packet. This is the packet that the next hop will
484+
// process. During the next iteration we will run all the above
485+
// checks on this packet.
486+
currentOnionPacket = processedPkt.NextPacket
475487
}
476488
}
477489

0 commit comments

Comments
 (0)