diff --git a/src/commands/parse-bip21.ts b/src/commands/parse-bip21.ts new file mode 100644 index 0000000..bcab10f --- /dev/null +++ b/src/commands/parse-bip21.ts @@ -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 ", "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); + }); + }); +} diff --git a/src/index.ts b/src/index.ts index e8aae48..bab6245 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; @@ -100,6 +101,7 @@ program.commandsGroup("Lightning Tools (no wallet connection required):"); registerFiatToSatsCommand(program); registerSatsToFiatCommand(program); registerParseInvoiceCommand(program); +registerParseBip21Command(program); registerVerifyPreimageCommand(program); registerRequestInvoiceFromLightningAddressCommand(program); diff --git a/src/tools/lightning/parse_bip21.ts b/src/tools/lightning/parse_bip21.ts new file mode 100644 index 0000000..543157b --- /dev/null +++ b/src/tools/lightning/parse_bip21.ts @@ -0,0 +1,18 @@ +import { + parseBip21 as parseBip21Lib, + type Bip21, +} from "@getalby/lightning-tools/bip21"; + +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; +}