Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/commands/parse-bip21.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Command } from "commander";
import { parseBip21 } from "../tools/lightning/parse_bip21.js";
import { handleError, output } from "../utils.js";

export function registerParseBip21Command(program: Command) {
program
.command("parse-bip21")
.description("Parse a BIP21 bitcoin payment URI")
.requiredOption("-u, --uri <uri>", "BIP21 URI to parse (bitcoin:...)")
.addHelpText(
"after",
"\nExample:\n" +
' $ npx @getalby/cli parse-bip21 --uri "bitcoin:bc1q...?amount=0.001&lightning=lnbc..."\n',
)
.action(async (options) => {
await handleError(async () => {
const result = parseBip21({ uri: options.uri });
output(result);
});
});
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { registerSignMessageCommand } from "./commands/sign-message.js";
import { registerFiatToSatsCommand } from "./commands/fiat-to-sats.js";
import { registerSatsToFiatCommand } from "./commands/sats-to-fiat.js";
import { registerParseInvoiceCommand } from "./commands/parse-invoice.js";
import { registerParseBip21Command } from "./commands/parse-bip21.js";
import { registerVerifyPreimageCommand } from "./commands/verify-preimage.js";
import { registerRequestInvoiceFromLightningAddressCommand } from "./commands/request-invoice-from-lightning-address.js";
import { registerFetch402Command } from "./commands/fetch.js";
Expand Down Expand Up @@ -100,6 +101,7 @@ program.commandsGroup("Lightning Tools (no wallet connection required):");
registerFiatToSatsCommand(program);
registerSatsToFiatCommand(program);
registerParseInvoiceCommand(program);
registerParseBip21Command(program);
registerVerifyPreimageCommand(program);
registerRequestInvoiceFromLightningAddressCommand(program);

Expand Down
18 changes: 18 additions & 0 deletions src/tools/lightning/parse_bip21.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
parseBip21 as parseBip21Lib,
type Bip21,
} from "@getalby/lightning-tools/bip21";

Check failure on line 4 in src/tools/lightning/parse_bip21.ts

View workflow job for this annotation

GitHub Actions / Unit tests

Cannot find module '@getalby/lightning-tools/bip21' or its corresponding type declarations.

export interface ParseBip21Params {
uri: string;
}

export function parseBip21(params: ParseBip21Params): Bip21 {
const result = parseBip21Lib(params.uri);
if (!result) {
throw new Error(
`Not a valid BIP21 URI (must start with "bitcoin:"): ${params.uri}`,
);
}
return result;
}
Loading