add generate address functionality

This commit is contained in:
Danny Morabito 2025-07-09 13:35:23 +02:00
parent 43217f327e
commit c91e90c657
Signed by: dannym
GPG key ID: 7CC8056A5A04557E

View file

@ -5,6 +5,7 @@ import initBreez, {
type InputType, type InputType,
type LogEntry, type LogEntry,
type Payment, type Payment,
type PaymentMethod,
type PreparePayOnchainRequest, type PreparePayOnchainRequest,
type PrepareReceiveRequest, type PrepareReceiveRequest,
type PrepareSendRequest, 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<string> - 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;
}
} }