Skip to content

Commit fa5ec01

Browse files
committed
Add per-contact deterministic payer keys
When paying people we added as trusted contacts, we may want to use a deterministic payer_key which lets them know the payment came from us. If they have added us to their own contact list, they will be able to match the payment and know it came from us. This is of course optional: whenever we don't want to reveal that we are the source of a payment, we keep using a random payer_key.
1 parent fb0ae19 commit fa5ec01

File tree

6 files changed

+107
-5
lines changed

6 files changed

+107
-5
lines changed

src/commonMain/kotlin/fr/acinq/lightning/NodeParams.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ data class NodeParams(
240240
* This offer will stay valid after restoring the seed on a different device.
241241
* @return the default offer and the private key that will sign invoices for this offer.
242242
*/
243-
fun defaultOffer(trampolineNodeId: PublicKey): Pair<OfferTypes.Offer, PrivateKey> {
243+
fun defaultOffer(trampolineNodeId: PublicKey): OfferTypes.OfferAndKey {
244244
// We generate a deterministic blindingSecret based on:
245245
// - a custom tag indicating that this is used in the Bolt 12 context
246246
// - our trampoline node, which is used as an introduction node for the offer's blinded path

src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ class Peer(
730730
.first()
731731
.let { event -> replyTo.complete(event.address) }
732732
}
733-
peerConnection?.send(DNSAddressRequest(nodeParams.chainHash, nodeParams.defaultOffer(walletParams.trampolineNode.id).first, languageSubtag))
733+
peerConnection?.send(DNSAddressRequest(nodeParams.chainHash, nodeParams.defaultOffer(walletParams.trampolineNode.id).offer, languageSubtag))
734734
return replyTo.await()
735735
}
736736

src/commonMain/kotlin/fr/acinq/lightning/payment/OfferManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class OfferManager(val nodeParams: NodeParams, val walletParams: WalletParams, v
4848
private val localOffers: HashMap<ByteVector32, OfferTypes.Offer> = HashMap()
4949

5050
init {
51-
registerOffer(nodeParams.defaultOffer(walletParams.trampolineNode.id).first, null)
51+
registerOffer(nodeParams.defaultOffer(walletParams.trampolineNode.id).offer, null)
5252
}
5353

5454
fun registerOffer(offer: OfferTypes.Offer, pathId: ByteVector32?) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package fr.acinq.lightning.payment
2+
3+
import fr.acinq.bitcoin.Crypto
4+
import fr.acinq.bitcoin.PrivateKey
5+
import fr.acinq.bitcoin.PublicKey
6+
import fr.acinq.lightning.NodeParams
7+
import fr.acinq.lightning.wire.OfferTypes
8+
import io.ktor.utils.io.core.*
9+
10+
/**
11+
* We may want to reveal our identity when paying Bolt 12 offers from our trusted contacts.
12+
* We also want to be able to identify payments from our trusted contacts if they choose to reveal themselves.
13+
*
14+
* @param offer Bolt 12 offer used by that contact.
15+
* @param payerIds list of payer_id that this contact may use in their [OfferTypes.InvoiceRequest] when they want to reveal their identity.
16+
*/
17+
data class TrustedContact(val offer: OfferTypes.Offer, val payerIds: List<PublicKey>) {
18+
/**
19+
* Derive a deterministic payer_key to pay our trusted contact's offer (used in our [OfferTypes.InvoiceRequest]).
20+
* This payer_key is unique to this contact and only lets them identify us if they know our local offer.
21+
*
22+
* @param localOffer local offer that our contact may have stored in their contact list (see [NodeParams.defaultOffer]).
23+
*/
24+
fun deterministicPayerKey(localOffer: OfferTypes.OfferAndKey): PrivateKey = localOffer.privateKey * deriveTweak(offer)
25+
26+
/** Return true if this payer_id matches this conact. */
27+
fun isPayer(payerId: PublicKey): Boolean = payerIds.contains(payerId)
28+
29+
/** Return true if the [invoiceRequest] comes from this contact. */
30+
fun isPayer(invoiceRequest: OfferTypes.InvoiceRequest): Boolean = isPayer(invoiceRequest.payerId)
31+
32+
companion object {
33+
fun create(localOffer: OfferTypes.OfferAndKey, remoteOffer: OfferTypes.Offer): TrustedContact {
34+
// We derive the payer_ids that this contact may use when paying our local offer.
35+
// If they use one of those payer_ids, we'll be able to identify that the payment came from them.
36+
val payerIds = remoteOffer.contactNodeIds.map { nodeId -> nodeId * deriveTweak(localOffer.offer) }
37+
return TrustedContact(remoteOffer, payerIds)
38+
}
39+
40+
private fun deriveTweak(paidOffer: OfferTypes.Offer): PrivateKey {
41+
// Note that we use a tagged hash to ensure this tweak only applies to the contact feature.
42+
return PrivateKey(Crypto.sha256("blip42_bolt12_contacts".toByteArray() + paidOffer.offerId.toByteArray()))
43+
}
44+
}
45+
}

src/commonMain/kotlin/fr/acinq/lightning/wire/OfferTypes.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,9 @@ object OfferTypes {
709709
}
710710
}
711711

712+
/** A bolt 12 offer and the private key used to sign invoices for that offer. */
713+
data class OfferAndKey(val offer: Offer, val privateKey: PrivateKey)
714+
712715
data class Offer(val records: TlvStream<OfferTlv>) {
713716
val chains: List<BlockHash> = records.get<OfferChains>()?.chains ?: listOf(Block.LivenetGenesisBlock.hash)
714717
val metadata: ByteVector? = records.get<OfferMetadata>()?.data
@@ -789,7 +792,7 @@ object OfferTypes {
789792
blindingSecret: PrivateKey,
790793
additionalTlvs: Set<OfferTlv> = setOf(),
791794
customTlvs: Set<GenericTlv> = setOf()
792-
): Pair<Offer, PrivateKey> {
795+
): OfferAndKey {
793796
if (description == null) require(amount == null) { "an offer description must be provided if the amount isn't null" }
794797
val blindedRouteDetails = OnionMessages.buildRouteToRecipient(blindingSecret, listOf(OnionMessages.IntermediateNode(EncodedNodeId.WithPublicKey.Plain(trampolineNodeId))), OnionMessages.Destination.Recipient(EncodedNodeId.WithPublicKey.Wallet(nodeParams.nodeId), null))
795798
val tlvs: Set<OfferTlv> = setOfNotNull(
@@ -800,7 +803,7 @@ object OfferTypes {
800803
// Note that we don't include an offer_node_id since we're using a blinded path.
801804
OfferPaths(listOf(ContactInfo.BlindedPath(blindedRouteDetails.route))),
802805
)
803-
return Pair(Offer(TlvStream(tlvs + additionalTlvs, customTlvs)), blindedRouteDetails.blindedPrivateKey(nodeParams.nodePrivateKey))
806+
return OfferAndKey(Offer(TlvStream(tlvs + additionalTlvs, customTlvs)), blindedRouteDetails.blindedPrivateKey(nodeParams.nodePrivateKey))
804807
}
805808

806809
fun validate(records: TlvStream<OfferTlv>): Either<InvalidTlvPayload, Offer> {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package fr.acinq.lightning.payment
2+
3+
import fr.acinq.bitcoin.Block
4+
import fr.acinq.lightning.Features
5+
import fr.acinq.lightning.Lightning.randomKey
6+
import fr.acinq.lightning.tests.TestConstants
7+
import fr.acinq.lightning.tests.utils.LightningTestSuite
8+
import fr.acinq.lightning.utils.msat
9+
import fr.acinq.lightning.wire.OfferTypes
10+
import kotlin.test.Test
11+
import kotlin.test.assertFalse
12+
import kotlin.test.assertNotEquals
13+
import kotlin.test.assertTrue
14+
15+
class TrustedContactTestsCommon : LightningTestSuite() {
16+
17+
@Test
18+
fun `identify payments coming from trusted contacts`() {
19+
val alice = TestConstants.Alice.nodeParams.defaultOffer(trampolineNodeId = randomKey().publicKey())
20+
val bob = TestConstants.Alice.nodeParams.defaultOffer(trampolineNodeId = randomKey().publicKey())
21+
val carol = run {
22+
val priv = randomKey()
23+
val offer = OfferTypes.Offer.createNonBlindedOffer(null, null, priv.publicKey(), Features.empty, Block.RegtestGenesisBlock.hash)
24+
OfferTypes.OfferAndKey(offer, priv)
25+
}
26+
27+
// Alice has Bob and Carol in her contacts list.
28+
val aliceContacts = listOf(bob, carol).map { TrustedContact.create(alice, it.offer) }
29+
// Bob's only contact is Alice.
30+
val bobContacts = listOf(alice).map { TrustedContact.create(bob, it.offer) }
31+
// Carol's only contact is Bob.
32+
val carolContacts = listOf(bob).map { TrustedContact.create(carol, it.offer) }
33+
34+
// Alice pays Bob: they are both in each other's contact list.
35+
val alicePayerKeyForBob = aliceContacts.first().deterministicPayerKey(alice)
36+
val aliceInvoiceRequestForBob = OfferTypes.InvoiceRequest(bob.offer, 1105.msat, 1, Features.empty, alicePayerKeyForBob, null, Block.RegtestGenesisBlock.hash)
37+
assertTrue(bobContacts.first().isPayer(aliceInvoiceRequestForBob))
38+
assertTrue(bobContacts.first().isPayer(alicePayerKeyForBob.publicKey()))
39+
40+
// Alice pays Carol: but Carol's only contact is Bob, so she cannot identify the payer.
41+
val alicePayerKeyForCarol = aliceContacts.last().deterministicPayerKey(alice)
42+
assertNotEquals(alicePayerKeyForBob, alicePayerKeyForCarol)
43+
val aliceInvoiceRequestForCarol = OfferTypes.InvoiceRequest(carol.offer, 1729.msat, 1, Features.empty, alicePayerKeyForCarol, null, Block.RegtestGenesisBlock.hash)
44+
carolContacts.forEach { assertFalse(it.isPayer(aliceInvoiceRequestForCarol)) }
45+
carolContacts.forEach { assertFalse(it.isPayer(alicePayerKeyForCarol.publicKey())) }
46+
47+
// Alice pays Bob, with a different payer_key: Bob cannot identify the payer.
48+
val aliceRandomPayerKey = randomKey()
49+
val alicePrivateInvoiceRequestForBob = OfferTypes.InvoiceRequest(bob.offer, 2465.msat, 1, Features.empty, aliceRandomPayerKey, null, Block.RegtestGenesisBlock.hash)
50+
bobContacts.forEach { assertFalse(it.isPayer(alicePrivateInvoiceRequestForBob)) }
51+
bobContacts.forEach { assertFalse(it.isPayer(aliceRandomPayerKey.publicKey())) }
52+
}
53+
54+
}

0 commit comments

Comments
 (0)