diff --git a/packages/core/src/services/LightningService.ts b/packages/core/src/services/LightningService.ts index 55990960..d56b0c98 100644 --- a/packages/core/src/services/LightningService.ts +++ b/packages/core/src/services/LightningService.ts @@ -3,6 +3,9 @@ import type { CreateBolt11Response, GatewayInfo, JSONObject, + LightningAddressSuccessAction, + LightningAddressVerification, + LightningAddressInvoiceResponse, LightningGateway, LnInternalPayState, LnPayState, @@ -10,6 +13,18 @@ import type { OutgoingLightningPayment, } from '../types' +type PayLightningAddressOptions = { + comment?: string + extraMeta?: JSONObject + gatewayInfo?: GatewayInfo +} + +type PayLightningAddressResult = { + invoice: string + payment: OutgoingLightningPayment + successAction?: LightningAddressSuccessAction +} + export class LightningService { constructor( private client: TransportClient, @@ -304,4 +319,52 @@ export class LightningService { this.clientName, ) } + + async verifyLightningAddress( + lightningAddress: string, + ): Promise { + return await this.client.rpcSingle( + 'ln', + 'verify_lightning_address', + { + lightning_address: lightningAddress, + }, + this.clientName, + ) + } + + async payLightningAddress( + lightningAddress: string, + amountMsats: number, + options: PayLightningAddressOptions = {}, + ): Promise { + const gateway = options.gatewayInfo ?? (await this._getDefaultGatewayInfo()) + + const invoiceResponse = + await this.client.rpcSingle( + 'ln', + 'get_invoice', + { + lightning_address: lightningAddress, + amount_msats: amountMsats, + comment: options.comment ?? null, + extra_meta: options.extraMeta ?? {}, + }, + this.clientName, + ) + + const payment = await this.payInvoice( + invoiceResponse.pr, + gateway, + options.extraMeta, + ) + + return { + invoice: invoiceResponse.pr, + payment, + successAction: invoiceResponse.successAction, + } + } } + +export type { PayLightningAddressOptions, PayLightningAddressResult } diff --git a/packages/core/src/services/index.ts b/packages/core/src/services/index.ts index dc8e16f3..ff38d34b 100644 --- a/packages/core/src/services/index.ts +++ b/packages/core/src/services/index.ts @@ -1,6 +1,10 @@ export { MintService } from './MintService' export { BalanceService } from './BalanceService' export { LightningService } from './LightningService' +export type { + PayLightningAddressOptions, + PayLightningAddressResult, +} from './LightningService' export { RecoveryService } from './RecoveryService' export { FederationService } from './FederationService' export { WalletService } from './WalletService' diff --git a/packages/core/src/types/wallet.ts b/packages/core/src/types/wallet.ts index 6ec707ca..49a01691 100644 --- a/packages/core/src/types/wallet.ts +++ b/packages/core/src/types/wallet.ts @@ -36,6 +36,52 @@ type OutgoingLightningPayment = { type PayType = { lightning: string } | { internal: string } +type LightningAddressMetadataItem = [string, string] +type LightningAddressMetadata = LightningAddressMetadataItem[] + +type LightningAddressPayerData = Record< + string, + { mandatory?: boolean } & Record +> + +type LightningAddressPayRequest = { + tag: 'payRequest' + callback: string + maxSendable: number + minSendable: number + metadata: string + commentAllowed?: number + payerData?: LightningAddressPayerData + allowsNostr?: boolean + nostrPubkey?: string + disposable?: boolean +} + +type LightningAddressSuccessAction = + | { tag: 'message'; message: string } + | { tag: 'url'; description: string; url: string } + | { tag: 'aes'; description: string; ciphertext: string; iv: string } + +type LightningAddressInvoiceResponse = { + pr: string + routes?: unknown[] + successAction?: LightningAddressSuccessAction + disposable?: boolean +} + +type LightningAddressErrorResponse = { + status: 'ERROR' + reason?: string +} + +type LightningAddressVerification = JSONObject & { + address: string + username: string + domain: string + payRequest: LightningAddressPayRequest + metadata: LightningAddressMetadata +} + type LnPayState = | 'created' | 'canceled' @@ -268,6 +314,14 @@ export { FeeToAmount, OutgoingLightningPayment, PayType, + LightningAddressMetadata, + LightningAddressMetadataItem, + LightningAddressPayRequest, + LightningAddressPayerData, + LightningAddressSuccessAction, + LightningAddressInvoiceResponse, + LightningAddressErrorResponse, + LightningAddressVerification, LnPayState, LnReceiveState, CreateBolt11Response,