Skip to content

Commit

Permalink
Merge pull request #120 from getAlby/fix/fetch-l402
Browse files Browse the repository at this point in the history
Fix: fetch with l402
  • Loading branch information
rolznz authored Dec 4, 2023
2 parents e671557 + f742e60 commit a2fed96
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
16 changes: 0 additions & 16 deletions src/l402/parse.js

This file was deleted.

40 changes: 40 additions & 0 deletions src/l402/parse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { parseL402 } from "./parse";

const BASE64_MAC =
"AgEEbHNhdAJCAAAClGOZrh7C569Yc7UMk8merfnMdIviyXr1qscW7VgpChNl21LkZ8Jex5QiPp+E1VaabeJDuWmlrh/j583axFpNAAIXc2VydmljZXM9cmFuZG9tbnVtYmVyOjAAAiZyYW5kb21udW1iZXJfY2FwYWJpbGl0aZVzPWFkZCxzdWJ0cmFjdAAABiAvFpzXGyc+8d/I9nMKKvAYP8w7kUlhuxS0eFN2sqmqHQ==";
const HEX_MAC =
"jkse4mpp5q22x8xdwrmpw0t6cww6sey7fn6klnnr5303vj7h44tr3dm2c9y9qdq8f4f5z4qcqzzsxqyz5vqsp5mmhp6cx4xxysc8x";
const HEX_INVOICE =
"lnbc100n1pjkse4mpp5q22x8xdwrmpw0t6cww6sey7fn6klnnr5303vj7h44tr3dm2c9y9qdq8f4f5z4qcqzzsxqyz5vqsp5mmhp6cx4xxysc8xvxaj984eue9pm83lxgezmk3umx6wxr9rrq2ns9qyyssqmmrrwthves6z3d85nafj2ds4z20qju2vpaatep8uwrvxz0xs4kznm99m7f6pmkzax09k2k9saldy34z0p0l8gm0zm5xsmg2g667pnlqp7a0qdz";

describe("parseL402", () => {
test("should correctly parse L402 string", () => {
const testString = `L402 macaroon="${BASE64_MAC}", invoice="${HEX_INVOICE}"`;
const result = parseL402(testString);
expect(result).toEqual({ macaroon: BASE64_MAC, invoice: HEX_INVOICE });
});

test("should correctly parse LSAT string", () => {
const testString = `LSAT macaroon="${BASE64_MAC}", invoice="${HEX_INVOICE}"`;
const result = parseL402(testString);
expect(result).toEqual({ macaroon: BASE64_MAC, invoice: HEX_INVOICE });
});

test("should correctly handle unquoted values", () => {
const testString = `L402 macaroon=${BASE64_MAC}, invoice=${HEX_INVOICE}`;
const result = parseL402(testString);
expect(result).toEqual({ macaroon: BASE64_MAC, invoice: HEX_INVOICE });
});

test("should correctly handle single-quoted values", () => {
const testString = `LSAT macaroon='${BASE64_MAC}', invoice='${HEX_INVOICE}'`;
const result = parseL402(testString);
expect(result).toEqual({ macaroon: BASE64_MAC, invoice: HEX_INVOICE });
});

test("should correctly handle hexadecimal macaroon values", () => {
const testString = `LSAT macaroon='${HEX_MAC}', invoice='${HEX_INVOICE}'`;
const result = parseL402(testString);
expect(result).toEqual({ macaroon: HEX_MAC, invoice: HEX_INVOICE });
});
});
20 changes: 20 additions & 0 deletions src/l402/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const parseL402 = (input: string): Record<string, string> => {
// Remove the L402 and LSAT identifiers
const string = input.replace("L402", "").replace("LSAT", "").trim();

// Initialize an object to store the key-value pairs
const keyValuePairs = {};

// Regular expression to match key and (quoted or unquoted) value
const regex = /(\w+)=("([^"]*)"|'([^']*)'|([^,]*))/g;
let match;

// Use regex to find all key-value pairs
while ((match = regex.exec(string)) !== null) {
// Key is always match[1]
// Value is either match[3] (double-quoted), match[4] (single-quoted), or match[5] (unquoted)
keyValuePairs[match[1]] = match[3] || match[4] || match[5];
}

return keyValuePairs;
};

0 comments on commit a2fed96

Please sign in to comment.