-
Notifications
You must be signed in to change notification settings - Fork 26
Add per-contact deterministic payer keys #690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
src/commonMain/kotlin/fr/acinq/lightning/payment/TrustedContact.kt
This file contains hidden or 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,45 @@ | ||
| 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. | ||
| return PrivateKey(Crypto.sha256("blip42_bolt12_contacts".toByteArray() + paidOffer.offerId.toByteArray())) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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())) } | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.