Skip to content

Commit

Permalink
feat: lud-16 text/email parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
matjaz committed Aug 5, 2024
1 parent 939b5ee commit 2b9df05
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type LnUrlPayResponse = {
metadata: Array<Array<string>>;
metadataHash: string;
identifier: string;
email: string;
description: string;
image: string;
commentAllowed?: number;
Expand Down
55 changes: 55 additions & 0 deletions src/utils/lnurl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,60 @@ describe("parseLnUrlPayResponse", () => {
expect(parsed.min).toBe(1000);
expect(parsed.max).toBe(11000000000);
});
test("fixed amount", async () => {
const response = {
status: "OK",
tag: "payRequest",
callback: "https://getalby.com/lnurlp/hello/callback",
metadata:
'[["text/identifier","hello@getalby.com"],["text/plain","Sats for Alby"]]',
minSendable: 1000,
maxSendable: 1000,
};
const parsed = await parseLnUrlPayResponse(response);
expect(parsed.fixed).toBe(true);
});
test("exception on invalid callback URL", async () => {
const response = {
status: "OK",
tag: "payRequest",
callback: "//getalby.com/lnurlp/hello/callback",
metadata:
'[["text/identifier","hello@getalby.com"],["text/plain","Sats for Alby"]]',
minSendable: 1000,
maxSendable: 11000000000,
};
expect(parseLnUrlPayResponse(response)).rejects.toThrow(
"Callback must be a valid url",
);
});
test("identifier must be set", async () => {
const response = {
status: "OK",
tag: "payRequest",
callback: "https://getalby.com/lnurlp/hello/callback",
metadata:
'[["text/identifier","hello@getalby.com"],["text/plain","Sats for Alby"]]',
minSendable: 1000,
maxSendable: 11000000000,
};
const parsed = await parseLnUrlPayResponse(response);
expect(parsed.identifier).toBe("hello@getalby.com");
expect(parsed.email).toBe("");
});
test("email must be set", async () => {
const response = {
status: "OK",
tag: "payRequest",
callback: "https://getalby.com/lnurlp/hello/callback",
metadata:
'[["text/email","hello@getalby.com"],["text/plain","Sats for Alby"]]',
minSendable: 1000,
maxSendable: 11000000000,
};
const parsed = await parseLnUrlPayResponse(response);
expect(parsed.identifier).toBe("");
expect(parsed.email).toBe("hello@getalby.com");
});
// TODO: add more tests
});
5 changes: 5 additions & 0 deletions src/utils/lnurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const parseLnUrlPayResponse = async (
metadataHash = await sha256("[]");
}

let email = "";
let image = "";
let description = "";
let identifier = "";
Expand All @@ -63,6 +64,9 @@ export const parseLnUrlPayResponse = async (
case "text/identifier":
identifier = v;
break;
case "text/email":
email = v;
break;
case "image/png;base64":
case "image/jpeg;base64":
image = "data:" + k + "," + v;
Expand All @@ -87,6 +91,7 @@ export const parseLnUrlPayResponse = async (
metadata,
metadataHash,
identifier,
email,
description,
image,
payerData,
Expand Down

0 comments on commit 2b9df05

Please sign in to comment.