From 4343e49a4e2c6bba0dc05bd4d0f66860f13ce0c2 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Sat, 7 Dec 2024 16:46:54 +0700 Subject: [PATCH] feat: verify ownership of lightning address --- .../wallets/[id]/lightning-address.js | 4 +- package.json | 2 +- pages/settings/wallets/LightningAddress.tsx | 62 ++++++++++++++++--- yarn.lock | 8 +-- 4 files changed, 60 insertions(+), 16 deletions(-) diff --git a/app/(app)/settings/wallets/[id]/lightning-address.js b/app/(app)/settings/wallets/[id]/lightning-address.js index c021a78..1886418 100644 --- a/app/(app)/settings/wallets/[id]/lightning-address.js +++ b/app/(app)/settings/wallets/[id]/lightning-address.js @@ -1,5 +1,5 @@ -import { LightningAddress } from "../../../../../pages/settings/wallets/LightningAddress"; +import { SetLightningAddress } from "../../../../../pages/settings/wallets/LightningAddress"; export default function Page() { - return ; + return ; } diff --git a/package.json b/package.json index 779b10e..bae0245 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "test:ci": "jest" }, "dependencies": { - "@getalby/lightning-tools": "^5.1.0", + "@getalby/lightning-tools": "^5.1.1", "@getalby/sdk": "^3.8.1", "@react-native-async-storage/async-storage": "1.23.1", "@rn-primitives/dialog": "^1.0.3", diff --git a/pages/settings/wallets/LightningAddress.tsx b/pages/settings/wallets/LightningAddress.tsx index b51200a..360e57a 100644 --- a/pages/settings/wallets/LightningAddress.tsx +++ b/pages/settings/wallets/LightningAddress.tsx @@ -1,21 +1,70 @@ +import { LightningAddress } from "@getalby/lightning-tools"; import { router } from "expo-router"; import React from "react"; import { View } from "react-native"; import Toast from "react-native-toast-message"; import DismissableKeyboardView from "~/components/DismissableKeyboardView"; +import Loading from "~/components/Loading"; import Screen from "~/components/Screen"; import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; import { Text } from "~/components/ui/text"; +import { errorToast } from "~/lib/errorToast"; import { useAppStore } from "~/lib/state/appStore"; -export function LightningAddress() { +export function SetLightningAddress() { const selectedWalletId = useAppStore((store) => store.selectedWalletId); const wallets = useAppStore((store) => store.wallets); const [lightningAddress, setLightningAddress] = React.useState(""); React.useEffect(() => { setLightningAddress(wallets[selectedWalletId].lightningAddress || ""); }, [wallets, selectedWalletId]); + const [isLoading, setLoading] = React.useState(false); + + const updateLightningAddress = async () => { + setLoading(true); + try { + if (lightningAddress) { + const nwcClient = useAppStore.getState().nwcClient; + if (!nwcClient) { + throw new Error("NWC client not connected"); + } + + // by generating an invoice from the lightning address and checking + // we own it via lookup_invoice, we can prove we own the lightning address + const _lightningAddress = new LightningAddress(lightningAddress); + await _lightningAddress.fetch(); + const invoiceFromLightningAddress = + await _lightningAddress.requestInvoice({ satoshi: 1 }); + let found = false; + try { + const transaction = await nwcClient.lookupInvoice({ + payment_hash: invoiceFromLightningAddress.paymentHash, + }); + found = + transaction?.invoice === invoiceFromLightningAddress.paymentRequest; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + } catch (_ /* transaction is not found */) {} + + if (!found) { + throw new Error( + "Could not verify you are the owner of this lightning address.", + ); + } + } + + useAppStore.getState().updateCurrentWallet({ lightningAddress }); + Toast.show({ + type: "success", + text1: "Lightning address updated", + }); + router.back(); + } catch (error) { + errorToast(error); + } + setLoading(false); + }; + return ( @@ -41,15 +90,10 @@ export function LightningAddress() { diff --git a/yarn.lock b/yarn.lock index 4ba85b0..a8705e3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1252,10 +1252,10 @@ find-up "^5.0.0" js-yaml "^4.1.0" -"@getalby/lightning-tools@^5.1.0": - version "5.1.0" - resolved "https://registry.yarnpkg.com/@getalby/lightning-tools/-/lightning-tools-5.1.0.tgz#85fae6c6501e59e3fc1ae836e365a35df00bab41" - integrity sha512-KhLnKu6hboNYxVmmegRi06ufW3J0o/5gZ5vMe4e94tKC1o8glN/fDn1fTL4hRIqgZjfzvDPPmV1A3YUkNtQttA== +"@getalby/lightning-tools@^5.1.1": + version "5.1.1" + resolved "https://registry.yarnpkg.com/@getalby/lightning-tools/-/lightning-tools-5.1.1.tgz#51125b2c58ef9372ae9efa93d0808d2205914b91" + integrity sha512-qiGWY7AMnQXywNlpEUTm/2u7Qx0C0qV0i3vlAV5ip8xV2quo4hkesHuAh6dBg/p3VC7t1fa9YUe9677hvQ3fVA== "@getalby/sdk@^3.8.1": version "3.8.1"