From c91e90c65761947501e0f3d02a4e52396b7a1467 Mon Sep 17 00:00:00 2001 From: Danny Morabito Date: Wed, 9 Jul 2025 13:35:23 +0200 Subject: [PATCH] add generate address functionality --- index.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/index.ts b/index.ts index 12dae2f..f09afe0 100644 --- a/index.ts +++ b/index.ts @@ -5,6 +5,7 @@ import initBreez, { type InputType, type LogEntry, type Payment, + type PaymentMethod, type PreparePayOnchainRequest, type PrepareReceiveRequest, type PrepareSendRequest, @@ -495,4 +496,34 @@ export default class PortalBtcWallet { ); } } + + /** + * Generates a new address for a payment method + * + * @param paymentMethod - The payment method to generate an address for + * @returns Promise - The new address + */ + async generateAddress(paymentMethod: PaymentMethod) { + if (!this.breezSDK) throw new Error("Breez SDK not initialized"); + const prep = await this.breezSDK.prepareReceivePayment({ paymentMethod }); + const res = await this.breezSDK.receivePayment({ prepareResponse: prep }); + const rawDestination = res.destination; + + if (rawDestination === undefined) { + throw new Error("Failed to generate address: destination is undefined."); + } + + let destination = rawDestination; + + const colonIndex = destination.indexOf(":"); + if (colonIndex !== -1) { + destination = destination.substring(colonIndex + 1); + } + + const questionMarkIndex = destination.indexOf("?"); + if (questionMarkIndex !== -1) { + destination = destination.substring(0, questionMarkIndex); + } + return destination; + } }