Skip to content

Commit

Permalink
Merge pull request #1223 from getAlby/feature/keysend-meta-tag
Browse files Browse the repository at this point in the history
feat: support keysend in lightning meta tag
  • Loading branch information
bumi authored Aug 2, 2022
2 parents 47ecd18 + 2ee0c18 commit de007af
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 78 deletions.
27 changes: 22 additions & 5 deletions src/app/screens/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,28 @@ function Home() {
description: lnData[0].description,
icon: lnData[0].icon,
};
navigate(
`/lnurlPay?lnurl=${
lnData[0].recipient
}&origin=${encodeURIComponent(JSON.stringify(origin))}`
);
if (lnData[0].method === "lnurl") {
navigate(
`/lnurlPay?lnurl=${
lnData[0].address
}&origin=${encodeURIComponent(JSON.stringify(origin))}`
);
} else if (lnData[0].method === "keysend") {
const params = new URLSearchParams({
destination: lnData[0].address,
origin: encodeURIComponent(JSON.stringify(origin)),
});
if (lnData[0].customKey && lnData[0].customValue) {
const customRecords = {
[lnData[0].customKey]: lnData[0].customValue,
};
params.set(
"customRecords",
JSON.stringify(customRecords) // encodeURIComponent() ??
);
}
navigate(`/keysend?${params.toString()}`);
}
} catch (e) {
if (e instanceof Error) toast.error(e.message);
} finally {
Expand Down
4 changes: 3 additions & 1 deletion src/app/screens/Keysend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ function Keysend(props: Props) {
const navigate = useNavigate();
const auth = useAccount();
const [amount, setAmount] = useState(props.valueSat || "");
const [customRecords] = useState(props.customRecords || {});
const [customRecords] = useState(
props.customRecords || JSON.parse(searchParams.get("customRecords") || "{}")
);
const [destination] = useState(
props.destination || searchParams.get("destination")
);
Expand Down
4 changes: 2 additions & 2 deletions src/extension/content-script/batteries/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function handleProfilePage() {
setLightningData([
{
method: "lnurl",
recipient: address,
address: address,
...getOriginData(),
description: shortBioElement?.innerText ?? "",
name:
Expand All @@ -72,7 +72,7 @@ function handleRepositoryPage(username: string) {
setLightningData([
{
method: "lnurl",
recipient: address,
address: address,
...getOriginData(),
description:
document.querySelector<HTMLHeadingElement>("article")?.innerText ??
Expand Down
2 changes: 1 addition & 1 deletion src/extension/content-script/batteries/Medium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const battery = (): void => {
setLightningData([
{
method: "lnurl",
recipient: match[2],
address: match[2],
...getOriginData(),
description: shortBio,
name:
Expand Down
35 changes: 32 additions & 3 deletions src/extension/content-script/batteries/Monetization.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,51 @@
import { BatteryMetaTagRecipient } from "~/types";

import getOriginData from "../originData";
import setLightningData from "../setLightningData";

const urlMatcher = /^https?:\/\/.*/i;

const parseRecipient = (content: string): BatteryMetaTagRecipient => {
const tokens = content
.split(";")
.map((e) => e.trim())
.filter((e) => !!e);

const recipient = tokens.reduce((obj, tkn) => {
const keyAndValue = tkn.split("=");
const keyAndValueTrimmed = keyAndValue.map((e) => e.trim());
return { ...obj, [keyAndValueTrimmed[0]]: keyAndValueTrimmed[1] };
}, {} as BatteryMetaTagRecipient);

return recipient;
};

const battery = (): void => {
const monetizationTag = document.querySelector<HTMLMetaElement>(
'head > meta[name="lightning"]'
);
if (!monetizationTag) {
return;
}
const recipient = monetizationTag.content.replace(/lnurlp:/i, "");
const content = monetizationTag.content;

let recipient: BatteryMetaTagRecipient;
// check for backwards compatibility: supports directly a lightning address or lnurlp:xxx
if (content.match(/^lnurlp:/) || content.indexOf("=") === -1) {
const lnAddress = monetizationTag.content.replace(/lnurlp:/i, "");
recipient = {
method: "lnurl",
address: lnAddress,
};
} else {
recipient = parseRecipient(content);
}

const metaData = getOriginData();

setLightningData([
{
method: "lnurlp",
recipient: recipient,
...recipient,
...metaData,
},
]);
Expand Down
2 changes: 1 addition & 1 deletion src/extension/content-script/batteries/Peertube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const battery = (): void => {
{
...metaData,
method: "lnurlp",
recipient: recipient,
address: recipient,
name: channelName,
icon: icon,
description: channelDescription,
Expand Down
58 changes: 0 additions & 58 deletions src/extension/content-script/batteries/Podcastindex.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/extension/content-script/batteries/Reddit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function battery(): void {
setLightningData([
{
method: "lnurl",
recipient,
address: recipient,
...getOriginData(),
icon: imageUrl,
name: document.title,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/content-script/batteries/StackOverflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function handleProfilePage() {
setLightningData([
{
method: "lnurl",
recipient: match[2],
address: match[2],
...getOriginData(),
description: aboutElement?.innerText ?? "",
icon:
Expand Down
2 changes: 1 addition & 1 deletion src/extension/content-script/batteries/Twitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function battery(): void {
setLightningData([
{
method: "lnurl",
recipient,
address: recipient,
...getOriginData(),
icon: userData.imageUrl,
name: userData.name,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/content-script/batteries/VimeoVideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const battery = (): void => {
setLightningData([
{
method: "lnurl",
recipient,
address: recipient,
...getOriginData(),
name,
icon: imageUrl,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/content-script/batteries/YouTubeChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const battery = (): void => {
setLightningData([
{
method: "lnurl",
recipient: lnurl[2],
address: lnurl[2],
...getOriginData(),
name: name,
icon: imageUrl,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/content-script/batteries/YouTubeVideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const battery = (): void => {
setLightningData([
{
method: "lnurl",
recipient,
address: recipient,
...getOriginData(),
name,
icon: imageUrl,
Expand Down
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,19 @@ export interface OriginDataInternal {

export interface Battery extends OriginData {
method: string;
recipient: string;
address: string;
customKey?: string;
customValue?: string;
suggested?: string;
name: string;
icon: string;
}

export type BatteryMetaTagRecipient = Pick<
Battery,
"address" | "customKey" | "customValue" | "method"
>;

/**
* @deprecated Use MessageDefault instead
*/
Expand Down

0 comments on commit de007af

Please sign in to comment.