Skip to content

Commit

Permalink
Add invoice payment hash
Browse files Browse the repository at this point in the history
  • Loading branch information
jklein24 committed Sep 18, 2023
1 parent f32b005 commit aa8e76e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.lightspark.sdk.graphql

import com.lightspark.sdk.model.SetInvoicePaymentHashOutput

const val SetInvoicePaymentHashMutation = """
mutation SetInvoicePaymentHash(
${'$'}invoice_id: ID!
${'$'}payment_hash: Hash32!
${'$'}preimage_nonce: Hash32!
) {
set_invoice_payment_hash(input: {
invoice_id: ${'$'}invoice_id
payment_hash: ${'$'}payment_hash
preimage_nonce: ${'$'}preimage_nonce
}) {
...SetInvoicePaymentHashOutputFragment
}
}
${SetInvoicePaymentHashOutput.FRAGMENT}
"""
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.lightspark.sdk.crypto.internal.Network
import com.lightspark.sdk.graphql.DeclineToSignMessagesMutation
import com.lightspark.sdk.graphql.ReleaseChannelPerCommitmentSecretMutation
import com.lightspark.sdk.graphql.ReleasePaymentPreimageMutation
import com.lightspark.sdk.graphql.SetInvoicePaymentHashMutation
import com.lightspark.sdk.graphql.SignInvoiceMutation
import com.lightspark.sdk.graphql.SignMessagesMutation
import com.lightspark.sdk.graphql.UpdateChannelPerCommitmentPointMutation
Expand All @@ -17,6 +18,7 @@ import com.lightspark.sdk.model.DeclineToSignMessagesOutput
import com.lightspark.sdk.model.ReleaseChannelPerCommitmentSecretOutput
import com.lightspark.sdk.model.ReleasePaymentPreimageOutput
import com.lightspark.sdk.model.RemoteSigningSubEventType
import com.lightspark.sdk.model.SetInvoicePaymentHashOutput
import com.lightspark.sdk.model.SignInvoiceOutput
import com.lightspark.sdk.model.SignMessagesOutput
import com.lightspark.sdk.model.UpdateChannelPerCommitmentPointOutput
Expand Down Expand Up @@ -61,7 +63,9 @@ suspend fun handleRemoteSigningEvent(
RemoteSigningSubEventType.SIGN_INVOICE -> handleSignInvoice(client, event, seedBytes)
RemoteSigningSubEventType.DERIVE_KEY_AND_SIGN -> handleDeriveKeyAndSign(client, event, seedBytes)
RemoteSigningSubEventType.RELEASE_PAYMENT_PREIMAGE -> handleReleasePaymentPreimage(client, event, seedBytes)
RemoteSigningSubEventType.REQUEST_INVOICE_PAYMENT_HASH -> TODO()
RemoteSigningSubEventType.REQUEST_INVOICE_PAYMENT_HASH ->
handleRequestInvoicePaymentHash(client, event, seedBytes)

RemoteSigningSubEventType.FUTURE_VALUE -> return "unsupported sub_event_type: $subEventType"
}
}
Expand Down Expand Up @@ -351,6 +355,49 @@ private suspend fun handleReleasePaymentPreimage(
return "released payment preimage for ${result.invoiceId}"
}

private suspend fun handleRequestInvoicePaymentHash(
client: LightsparkCoroutinesClient,
event: WebhookEvent,
seedBytes: ByteArray,
): String {
event.assertSubEventType(RemoteSigningSubEventType.REQUEST_INVOICE_PAYMENT_HASH)
val invoiceId = event.data?.get("invoice_id")?.jsonPrimitive?.content
?: throw RemoteSigningException("Webhook event is missing invoice_id")

val preimageNonce = try {
RemoteSigning.generatePreimageNonce(seedBytes, event.bitcoinNetwork())
} catch (e: Exception) {
throw RemoteSigningException("Error generating preimage nonce", cause = e)
}

val preimageHash = try {
RemoteSigning.generatePreimageHash(seedBytes, event.bitcoinNetwork(), preimageNonce).toHexString()
} catch (e: Exception) {
throw RemoteSigningException("Error generating preimage hash", cause = e)
}

val result = try {
client.executeQuery(
Query(
SetInvoicePaymentHashMutation,
{
add("invoice_id", invoiceId)
add("payment_hash", preimageHash)
add("preimage_nonce", preimageNonce.toHexString())
},
) {
val setInvoicePaymentHashJson =
requireNotNull(it["set_invoice_payment_hash"]) { "Invalid response for invoice payment hash update" }
serializerFormat.decodeFromJsonElement<SetInvoicePaymentHashOutput>(setInvoicePaymentHashJson)
},
)
} catch (e: Exception) {
throw RemoteSigningException("Error setting invoice payment hash", cause = e)
}

return "updated invoice payment hash for ${result.invoiceId}"
}

private fun WebhookEvent.assertSubEventType(expectedSubEventType: RemoteSigningSubEventType) {
if (eventType != WebhookEventType.REMOTE_SIGNING) {
throw RemoteSigningException("Webhook event is not for remote signing: $eventType")
Expand Down

0 comments on commit aa8e76e

Please sign in to comment.