Skip to content
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

Add custom offers #678

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/fr/acinq/lightning/NodeParams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,6 @@ data class NodeParams(
val blindingSecret = PrivateKey(Crypto.sha256("bolt 12 default offer".toByteArray(Charsets.UTF_8).byteVector() + trampolineNodeId.value + nodePrivateKey.value).byteVector32())
// We don't use our currently activated features, otherwise the offer would change when we add support for new features.
// If we add a new feature that we would like to use by default, we will need to explicitly create a new offer.
return OfferTypes.Offer.createBlindedOffer(amount = null, description = null, this, trampolineNodeId, Features.empty, blindingSecret)
return OfferTypes.Offer.createBlindedOffer(amount = null, description = null, this, trampolineNodeId, Features.empty, blindingSecret, pathId = null)
}
}
11 changes: 11 additions & 0 deletions src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,17 @@ class Peer(
return incomingPaymentHandler.createInvoice(paymentPreimage, amount, description, extraHops, expirySeconds)
}

/** Creates a custom offer and register it with the `offerManager`.
* @param secret A random private key for creating the blinded path of the offer. Must be unique to this offer.
* The offer returned is deterministic, if you need to persist you just need to save the parameters used to create it.
*/
fun createOffer(secret: PrivateKey, amount: MilliSatoshi?, description: String?): OfferTypes.Offer {
val pathId = secret.value
val (offer, _) = OfferTypes.Offer.createBlindedOffer(amount, description, nodeParams, remoteNodeId, Features.empty, secret, pathId)
offerManager.registerOffer(offer, pathId)
return offer
}

// The (node_id, fcm_token) tuple only needs to be registered once.
// And after that, only if the tuple changes (e.g. different fcm_token).
fun registerFcmToken(token: String?) {
Expand Down
4 changes: 3 additions & 1 deletion src/commonMain/kotlin/fr/acinq/lightning/wire/OfferTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ object OfferTypes {
* @param trampolineNodeId our trampoline node.
* @param features features that should be advertised in the offer.
* @param blindingSecret session key used for the blinded path included in the offer.
* @param pathId path id for this offer's blinded path.
*/
fun createBlindedOffer(
amount: MilliSatoshi?,
Expand All @@ -787,11 +788,12 @@ object OfferTypes {
trampolineNodeId: PublicKey,
features: Features,
blindingSecret: PrivateKey,
pathId: ByteVector32?,
additionalTlvs: Set<OfferTlv> = setOf(),
customTlvs: Set<GenericTlv> = setOf()
): Pair<Offer, PrivateKey> {
if (description == null) require(amount == null) { "an offer description must be provided if the amount isn't null" }
val blindedRouteDetails = OnionMessages.buildRouteToRecipient(blindingSecret, listOf(OnionMessages.IntermediateNode(EncodedNodeId.WithPublicKey.Plain(trampolineNodeId))), OnionMessages.Destination.Recipient(EncodedNodeId.WithPublicKey.Wallet(nodeParams.nodeId), null))
val blindedRouteDetails = OnionMessages.buildRouteToRecipient(blindingSecret, listOf(OnionMessages.IntermediateNode(EncodedNodeId.WithPublicKey.Plain(trampolineNodeId))), OnionMessages.Destination.Recipient(EncodedNodeId.WithPublicKey.Wallet(nodeParams.nodeId), pathId))
val tlvs: Set<OfferTlv> = setOfNotNull(
if (nodeParams.chainHash != Block.LivenetGenesisBlock.hash) OfferChains(listOf(nodeParams.chainHash)) else null,
amount?.let { OfferAmount(it) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fr.acinq.bitcoin.ByteVector
import fr.acinq.bitcoin.PrivateKey
import fr.acinq.bitcoin.utils.Either
import fr.acinq.lightning.*
import fr.acinq.lightning.Lightning.randomBytes32
import fr.acinq.lightning.Lightning.randomKey
import fr.acinq.lightning.crypto.RouteBlinding
import fr.acinq.lightning.crypto.sphinx.DecryptedPacket
Expand Down Expand Up @@ -51,15 +52,17 @@ class OfferManagerTestsCommon : LightningTestSuite() {

private fun createOffer(offerManager: OfferManager, amount: MilliSatoshi? = null): OfferTypes.Offer {
val blindingSecret = randomKey()
val pathId = randomBytes32()
val (offer, _) = OfferTypes.Offer.createBlindedOffer(
amount,
"Blockaccino",
offerManager.nodeParams,
offerManager.walletParams.trampolineNode.id,
offerManager.nodeParams.features,
blindingSecret,
pathId,
)
offerManager.registerOffer(offer, null)
offerManager.registerOffer(offer, pathId)
return offer
}

Expand Down
Loading