Skip to content

Update invoice creator #185

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Jul 16, 2024
Merged
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 gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ktlint = "11.3.1"
ktor = "2.3.7"
lightsparkCore = "0.6.0"
lightsparkCrypto = "0.6.0"
uma = "1.1.1"
uma = "1.2.1"
mavenPublish = "0.25.2"
mockitoCore = "5.5.0"
taskTree = "2.1.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,54 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.future.future
import me.uma.UmaInvoiceCreator

class LightsparkClientUmaInvoiceCreator(
/**
* Creates UMA invoices using the Lightspark client.
*
* @constructor Creates an instance of [LightsparkClientUmaInvoiceCreator].
*
* @param client The [LightsparkCoroutinesClient] used to create invoices.
* @param nodeId The ID of the node for which to create the invoice.
* @param expirySecs The number of seconds before the invoice expires.
* @param enableUmaAnalytics A flag indicating whether UMA analytics should be enabled. If `true`,
* the receiver identifier will be hashed using a monthly-rotated seed and used for anonymized
* analysis.
* @param signingPrivateKey Optional, the receiver's signing private key. Used to hash the receiver
* identifier if UMA analytics is enabled.
*/
class LightsparkClientUmaInvoiceCreator @JvmOverloads constructor(
private val client: LightsparkCoroutinesClient,
private val nodeId: String,
private val expirySecs: Int,
private val enableUmaAnalytics: Boolean = false,
private val signingPrivateKey: ByteArray? = null,
) : UmaInvoiceCreator {
private val coroutineScope = CoroutineScope(Dispatchers.IO)

constructor(apiClientId: String, apiClientSecret: String, nodeId: String, expirySecs: Int) : this(
@JvmOverloads
constructor(
apiClientId: String,
apiClientSecret: String,
nodeId: String,
expirySecs: Int,
enableUmaAnalytics: Boolean = false,
signingPrivateKey: ByteArray? = null,
) : this(
LightsparkCoroutinesClient(
ClientConfig(
authProvider = AccountApiTokenAuthProvider(apiClientId, apiClientSecret),
),
),
nodeId,
expirySecs,
enableUmaAnalytics,
signingPrivateKey,
)

override fun createUmaInvoice(amountMsats: Long, metadata: String) = coroutineScope.future {
client.createUmaInvoice(nodeId, amountMsats, metadata, expirySecs).data.encodedPaymentRequest
override fun createUmaInvoice(amountMsats: Long, metadata: String, receiverIdentifier: String?) = coroutineScope.future {
if (enableUmaAnalytics && signingPrivateKey != null) {
client.createUmaInvoice(nodeId, amountMsats, metadata, expirySecs, signingPrivateKey, receiverIdentifier)
} else {
client.createUmaInvoice(nodeId, amountMsats, metadata, expirySecs)
}.data.encodedPaymentRequest
}
}
8 changes: 7 additions & 1 deletion umaserverdemo/src/main/kotlin/com/lightspark/Vasp2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,13 @@ class Vasp2(
val response = try {
uma.getPayReqResponse(
query = request,
invoiceCreator = LightsparkClientUmaInvoiceCreator(client, config.nodeID, expirySecs),
invoiceCreator = LightsparkClientUmaInvoiceCreator(
client = client,
nodeId = config.nodeID,
expirySecs = expirySecs,
enableUmaAnalytics = true,
signingPrivateKey = config.umaSigningPrivKey,
),
metadata = getEncodedMetadata(),
receivingCurrencyCode = receivingCurrency.code,
receivingCurrencyDecimals = receivingCurrency.decimals,
Expand Down