-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
6 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/commonMain/kotlin/fr/acinq/lightning/payment/TrustedContact.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package fr.acinq.lightning.payment | ||
|
||
import fr.acinq.bitcoin.Crypto | ||
import fr.acinq.bitcoin.PrivateKey | ||
import fr.acinq.bitcoin.PublicKey | ||
import fr.acinq.lightning.NodeParams | ||
import fr.acinq.lightning.wire.OfferTypes | ||
import io.ktor.utils.io.core.* | ||
|
||
/** | ||
* We may want to reveal our identity when paying Bolt 12 offers from our trusted contacts. | ||
* We also want to be able to identify payments from our trusted contacts if they choose to reveal themselves. | ||
* | ||
* @param offer Bolt 12 offer used by that contact. | ||
* @param payerIds list of payer_id that this contact may use in their [OfferTypes.InvoiceRequest] when they want to reveal their identity. | ||
*/ | ||
data class TrustedContact(val offer: OfferTypes.Offer, val payerIds: List<PublicKey>) { | ||
/** | ||
* Derive a deterministic payer_key to pay our trusted contact's offer (used in our [OfferTypes.InvoiceRequest]). | ||
* This payer_key is unique to this contact and only lets them identify us if they know our local offer. | ||
* | ||
* @param localOffer local offer that our contact may have stored in their contact list (see [NodeParams.defaultOffer]). | ||
*/ | ||
fun deterministicPayerKey(localOffer: OfferTypes.OfferAndKey): PrivateKey = localOffer.privateKey * deriveTweak(offer) | ||
|
||
/** Return true if this payer_id matches this conact. */ | ||
fun isPayer(payerId: PublicKey): Boolean = payerIds.contains(payerId) | ||
|
||
/** Return true if the [invoiceRequest] comes from this contact. */ | ||
fun isPayer(invoiceRequest: OfferTypes.InvoiceRequest): Boolean = isPayer(invoiceRequest.payerId) | ||
|
||
companion object { | ||
fun create(localOffer: OfferTypes.OfferAndKey, remoteOffer: OfferTypes.Offer): TrustedContact { | ||
// We derive the payer_ids that this contact may use when paying our local offer. | ||
// If they use one of those payer_ids, we'll be able to identify that the payment came from them. | ||
val payerIds = remoteOffer.contactNodeIds.map { nodeId -> nodeId * deriveTweak(localOffer.offer) } | ||
return TrustedContact(remoteOffer, payerIds) | ||
} | ||
|
||
private fun deriveTweak(paidOffer: OfferTypes.Offer): PrivateKey { | ||
// Note that we use a tagged hash to ensure this tweak only applies to the contact feature. | ||
// TODO: prefix with blip # once written? | ||
return PrivateKey(Crypto.sha256("bolt12_contact_key".toByteArray() + paidOffer.offerId.toByteArray())) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/commonTest/kotlin/fr/acinq/lightning/payment/TrustedContactTestsCommon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package fr.acinq.lightning.payment | ||
|
||
import fr.acinq.bitcoin.Block | ||
import fr.acinq.lightning.Features | ||
import fr.acinq.lightning.Lightning.randomKey | ||
import fr.acinq.lightning.tests.TestConstants | ||
import fr.acinq.lightning.tests.utils.LightningTestSuite | ||
import fr.acinq.lightning.utils.msat | ||
import fr.acinq.lightning.wire.OfferTypes | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNotEquals | ||
import kotlin.test.assertTrue | ||
|
||
class TrustedContactTestsCommon : LightningTestSuite() { | ||
|
||
@Test | ||
fun `identify payments coming from trusted contacts`() { | ||
val alice = TestConstants.Alice.nodeParams.defaultOffer(trampolineNodeId = randomKey().publicKey()) | ||
val bob = TestConstants.Alice.nodeParams.defaultOffer(trampolineNodeId = randomKey().publicKey()) | ||
val carol = run { | ||
val priv = randomKey() | ||
val offer = OfferTypes.Offer.createNonBlindedOffer(null, null, priv.publicKey(), Features.empty, Block.RegtestGenesisBlock.hash) | ||
OfferTypes.OfferAndKey(offer, priv) | ||
} | ||
|
||
// Alice has Bob and Carol in her contacts list. | ||
val aliceContacts = listOf(bob, carol).map { TrustedContact.create(alice, it.offer) } | ||
// Bob's only contact is Alice. | ||
val bobContacts = listOf(alice).map { TrustedContact.create(bob, it.offer) } | ||
// Carol's only contact is Bob. | ||
val carolContacts = listOf(bob).map { TrustedContact.create(carol, it.offer) } | ||
|
||
// Alice pays Bob: they are both in each other's contact list. | ||
val alicePayerKeyForBob = aliceContacts.first().deterministicPayerKey(alice) | ||
val aliceInvoiceRequestForBob = OfferTypes.InvoiceRequest(bob.offer, 1105.msat, 1, Features.empty, alicePayerKeyForBob, null, Block.RegtestGenesisBlock.hash) | ||
assertTrue(bobContacts.first().isPayer(aliceInvoiceRequestForBob)) | ||
assertTrue(bobContacts.first().isPayer(alicePayerKeyForBob.publicKey())) | ||
|
||
// Alice pays Carol: but Carol's only contact is Bob, so she cannot identify the payer. | ||
val alicePayerKeyForCarol = aliceContacts.last().deterministicPayerKey(alice) | ||
assertNotEquals(alicePayerKeyForBob, alicePayerKeyForCarol) | ||
val aliceInvoiceRequestForCarol = OfferTypes.InvoiceRequest(carol.offer, 1729.msat, 1, Features.empty, alicePayerKeyForCarol, null, Block.RegtestGenesisBlock.hash) | ||
carolContacts.forEach { assertFalse(it.isPayer(aliceInvoiceRequestForCarol)) } | ||
carolContacts.forEach { assertFalse(it.isPayer(alicePayerKeyForCarol.publicKey())) } | ||
|
||
// Alice pays Bob, with a different payer_key: Bob cannot identify the payer. | ||
val aliceRandomPayerKey = randomKey() | ||
val alicePrivateInvoiceRequestForBob = OfferTypes.InvoiceRequest(bob.offer, 2465.msat, 1, Features.empty, aliceRandomPayerKey, null, Block.RegtestGenesisBlock.hash) | ||
bobContacts.forEach { assertFalse(it.isPayer(alicePrivateInvoiceRequestForBob)) } | ||
bobContacts.forEach { assertFalse(it.isPayer(aliceRandomPayerKey.publicKey())) } | ||
} | ||
|
||
} |