add generate address functionality
This commit is contained in:
parent
43217f327e
commit
c91e90c657
1 changed files with 31 additions and 0 deletions
31
index.ts
31
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<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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue