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; + } }