Skip to content

Commit

Permalink
feat: verify ownership of lightning address
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Dec 7, 2024
1 parent 3a29c76 commit 4343e49
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/(app)/settings/wallets/[id]/lightning-address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LightningAddress } from "../../../../../pages/settings/wallets/LightningAddress";
import { SetLightningAddress } from "../../../../../pages/settings/wallets/LightningAddress";

export default function Page() {
return <LightningAddress />;
return <SetLightningAddress />;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
62 changes: 53 additions & 9 deletions pages/settings/wallets/LightningAddress.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<DismissableKeyboardView>
<View className="flex-1 flex flex-col">
Expand All @@ -41,15 +90,10 @@ export function LightningAddress() {
<View className="p-6">
<Button
size="lg"
onPress={() => {
useAppStore.getState().updateCurrentWallet({ lightningAddress });
Toast.show({
type: "success",
text1: "Lightning address updated",
});
router.back();
}}
className="flex flex-row gap-2"
onPress={updateLightningAddress}
>
{isLoading && <Loading className="text-primary-foreground" />}
<Text>Save</Text>
</Button>
</View>
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4343e49

Please sign in to comment.