Skip to content
Open
63 changes: 63 additions & 0 deletions packages/core/src/services/LightningService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ import type {
CreateBolt11Response,
GatewayInfo,
JSONObject,
LightningAddressSuccessAction,
LightningAddressVerification,
LightningAddressInvoiceResponse,
LightningGateway,
LnInternalPayState,
LnPayState,
LnReceiveState,
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,
Expand Down Expand Up @@ -304,4 +319,52 @@ export class LightningService {
this.clientName,
)
}

async verifyLightningAddress(
lightningAddress: string,
): Promise<LightningAddressVerification> {
return await this.client.rpcSingle<LightningAddressVerification>(
'ln',
'verify_lightning_address',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method doesn't exist in the fedimint-ln-client as of now, could you please mention the corresponding PR where you have added the required functions?

{
lightning_address: lightningAddress,
},
this.clientName,
)
}

async payLightningAddress(
lightningAddress: string,
amountMsats: number,
options: PayLightningAddressOptions = {},
): Promise<PayLightningAddressResult> {
const gateway = options.gatewayInfo ?? (await this._getDefaultGatewayInfo())

const invoiceResponse =
await this.client.rpcSingle<LightningAddressInvoiceResponse>(
'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 }
4 changes: 4 additions & 0 deletions packages/core/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -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'
54 changes: 54 additions & 0 deletions packages/core/src/types/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, JSONValue>
>

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'
Expand Down Expand Up @@ -268,6 +314,14 @@ export {
FeeToAmount,
OutgoingLightningPayment,
PayType,
LightningAddressMetadata,
LightningAddressMetadataItem,
LightningAddressPayRequest,
LightningAddressPayerData,
LightningAddressSuccessAction,
LightningAddressInvoiceResponse,
LightningAddressErrorResponse,
LightningAddressVerification,
LnPayState,
LnReceiveState,
CreateBolt11Response,
Expand Down
Loading