Skip to content

Commit

Permalink
Add decoding LNURL with 'lightning:' as protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolu89 committed Aug 31, 2022
1 parent 72ab0af commit fad853a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zerologin/lnurl",
"version": "0.1.0",
"version": "0.1.1",
"description": "LNURL library written in TS",
"private": false,
"type": "module",
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ interface LnurlDecoded {
}

const decode = (lnurl: string): LnurlDecoded => {
const protocol = 'lightning:'
if (lnurl.toLowerCase().includes(protocol)) {
lnurl = lnurl.toLowerCase().split('lightning:')[1]
}

const decoded = bech32.decode(lnurl, limit);
const decodedString = Buffer.from(bech32.fromWords(decoded.words)).toString("utf8");

Expand Down
34 changes: 33 additions & 1 deletion tests/lnurl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test("can encode", ({ expect }) => {
expect(encoded).toEqual(expected);
});

test("can decode", ({ expect }) => {
test("can decode without protocol", ({ expect }) => {
const encoded =
"lnurl1dp68gurn8ghj7um9wfmxjcm99e3k7mf0d3h82unv9ash2arg8a6xzeead3hkw6twye4nz0t9xy6r2wtyx3jnzcejv56ngdtp8qukgwryve3rwef4vdnrvcmp8ycxzvny8pnrvetyxgcnwdejxfjk2vejx5uxxcnzvvmryvpkxgurzfngd4skx0trv9jnxcm98qmrwv35xfnrxvnpxuensctrxejrqcekxpjk2etxvscnserxxqmrzwrrxe3nqctzxajkydry8qenxeryvs6nsdrpxymrzajxakz";
const decoded = decode(encoded);
Expand All @@ -26,3 +26,35 @@ test("can decode", ({ expect }) => {
};
expect(decoded).toEqual(expected);
});

test("can decode with protocol lowercase", ({ expect }) => {
const encoded =
"lightning:lnurl1dp68gurn8ghj7um9wfmxjcm99e3k7mf0d3h82unv9ash2arg8a6xzeead3hkw6twye4nz0t9xy6r2wtyx3jnzcejv56ngdtp8qukgwryve3rwef4vdnrvcmp8ycxzvny8pnrvetyxgcnwdejxfjk2vejx5uxxcnzvvmryvpkxgurzfngd4skx0trv9jnxcm98qmrwv35xfnrxvnpxuensctrxejrqcekxpjk2etxvscnserxxqmrzwrrxe3nqctzxajkydry8qenxeryvs6nsdrpxymrzajxakz";
const decoded = decode(encoded);

const expected =
{
decoded: "https://service.com/lnurl/auth?tag=login&k1=e1459d4e1c2e545a89d8dfb7e5cf6ca90a2d8f6ed217722ee3258cbbc6206281&hmac=cae3ce867242f32a738ac6d0c60eeefd18df0618c6c0ab7eb4d833ddd584a161",
domain: "service.com",
action: "",
k1: "e1459d4e1c2e545a89d8dfb7e5cf6ca90a2d8f6ed217722ee3258cbbc6206281",
tag: "login"
};
expect(decoded).toEqual(expected);
});

test("can decode with protocol UPPERCASE", ({ expect }) => {
const encoded =
"LIGHTNING:LNURL1DP68GURN8GHJ7UM9WFMXJCM99E3K7MF0D3H82UNV9ASH2ARG8A6XZEEAD3HKW6TWYE4NZ0T9XY6R2WTYX3JNZCEJV56NGDTP8QUKGWRYVE3RWEF4VDNRVCMP8YCXZVNY8PNRVETYXGCNWDEJXFJK2VEJX5UXXCNZVVMRYVPKXGURZFNGD4SKX0TRV9JNXCM98QMRWV35XFNRXVNPXUENSCTRXEJRQCEKXPJK2ETXVSCNSERXXQMRZWRRXE3NQCTZXAJKYDRY8QENXERYVS6NSDRPXYMRZAJXAKZ";
const decoded = decode(encoded);

const expected =
{
decoded: "https://service.com/lnurl/auth?tag=login&k1=e1459d4e1c2e545a89d8dfb7e5cf6ca90a2d8f6ed217722ee3258cbbc6206281&hmac=cae3ce867242f32a738ac6d0c60eeefd18df0618c6c0ab7eb4d833ddd584a161",
domain: "service.com",
action: "",
k1: "e1459d4e1c2e545a89d8dfb7e5cf6ca90a2d8f6ed217722ee3258cbbc6206281",
tag: "login"
};
expect(decoded).toEqual(expected);
});

0 comments on commit fad853a

Please sign in to comment.