-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose MakeInvoice and LookupInvoice as iOS AppIntents
- Loading branch information
Vinny Fiano
committed
Jan 10, 2025
1 parent
75788d4
commit b9b1f5e
Showing
5 changed files
with
122 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import ExpoAppIntents from "expo-ios-app-intents"; | ||
import { useAppStore } from "../state/appStore"; | ||
import { Nip47Transaction } from "@getalby/sdk/dist/NWCClient"; | ||
|
||
export interface MakeInvoiceParameters { | ||
_Amount: number; | ||
_Description?: string; | ||
} | ||
|
||
export interface LookupInvoiceParameters { | ||
_Invoice: string; | ||
} | ||
|
||
// Define the proper IntentEventPayload type | ||
interface IntentEventPayload { | ||
id: string; | ||
name: string; | ||
parameters: Record<string, any>; | ||
} | ||
|
||
async function handleIntent(event: IntentEventPayload) { | ||
const { name, parameters, id } = event; | ||
const nwcClient = useAppStore.getState().nwcClient; | ||
|
||
if (!nwcClient) { | ||
ExpoAppIntents.failIntent(id, "NWC client not connected"); | ||
return; | ||
} | ||
|
||
try { | ||
if (name === "MakeInvoice") { | ||
const { _Amount, _Description } = parameters as MakeInvoiceParameters; | ||
const response = (await nwcClient.makeInvoice({ | ||
amount: _Amount, | ||
...(_Description ? { description: _Description } : {}), | ||
})) as Nip47Transaction; | ||
// Return [invoice, paymentHash] as string array | ||
ExpoAppIntents.completeIntent(id, { value: JSON.stringify(response) }); | ||
} else if (name === "LookupInvoice") { | ||
const { _Invoice } = parameters as LookupInvoiceParameters; | ||
const response = (await nwcClient.lookupInvoice({ | ||
invoice: _Invoice, | ||
})) as Nip47Transaction; | ||
|
||
// Return [paid, preimage, amount, description] as string array | ||
// Determine paid status from presence of settled_at timestamp | ||
ExpoAppIntents.completeIntent(id, { value: JSON.stringify(response) }); | ||
} | ||
} catch (error) { | ||
console.error("App Intent error:", error); | ||
ExpoAppIntents.failIntent( | ||
id, | ||
error instanceof Error ? error.message : "Unknown error", | ||
); | ||
} | ||
} | ||
|
||
export const setupLightningIntentHandlers = () => { | ||
ExpoAppIntents.addListener("onIntent", (event: IntentEventPayload) => { | ||
handleIntent(event); | ||
}); | ||
}; |
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,16 @@ | ||
export interface MakeInvoiceResponse { | ||
invoice: string; | ||
paymentHash: string; | ||
} | ||
|
||
export interface LookupInvoiceResponse { | ||
paid: boolean; | ||
preimage?: string; | ||
amount: number; | ||
description?: string; | ||
} | ||
|
||
export interface NWCInvoiceError { | ||
error: string; | ||
message: string; | ||
} |
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