swap check + provider relay

This commit is contained in:
boufni95 2025-12-18 15:39:47 +00:00
parent c9305c34e8
commit 81229b3385
2 changed files with 27 additions and 7 deletions

View file

@ -496,6 +496,10 @@ export default class {
if (!txSwap) { if (!txSwap) {
throw new Error("swap quote not found") throw new Error("swap quote not found")
} }
const info = await this.lnd.GetInfo()
if (info.blockHeight >= txSwap.timeout_block_height) {
throw new Error("swap timeout")
}
const keys = this.swaps.GetKeys(txSwap.ephemeral_private_key) const keys = this.swaps.GetKeys(txSwap.ephemeral_private_key)
const data: TransactionSwapData = { const data: TransactionSwapData = {
createdResponse: { createdResponse: {
@ -526,8 +530,8 @@ export default class {
const fees = this.GetFees() const fees = this.GetFees()
let payment: Types.PayInvoiceResponse let payment: Types.PayInvoiceResponse
try { try {
payment = await this.PayInvoice(ctx.user_id, { payment = await this.PayInvoice(ctx.user_id, {
amount: 0, amount: 0,
invoice: txSwap.invoice, invoice: txSwap.invoice,
expected_fees: { expected_fees: {
outboundFeeFloor: fees.outboundFeeFloor, outboundFeeFloor: fees.outboundFeeFloor,

View file

@ -142,14 +142,14 @@ export class NostrPool {
async Send(initiator: SendInitiator, data: SendData, relays?: string[]) { async Send(initiator: SendInitiator, data: SendData, relays?: string[]) {
try { try {
const keys = this.getSendKeys(initiator) const keys = this.getSendKeys(initiator)
const r = this.getRelays(initiator, relays)
const privateKey = Buffer.from(keys.privateKey, 'hex') const privateKey = Buffer.from(keys.privateKey, 'hex')
const toSign = await this.handleSend(data, keys) const toSign = await this.handleSend(data, keys)
await Promise.all(toSign.map(ue => this.sendEvent(ue, keys, relays))) await Promise.all(toSign.map(ue => this.sendEvent(ue, keys, r)))
} catch (e: any) { } catch (e: any) {
this.log(ERROR, "failed to send event", e.message || e) this.log(ERROR, "failed to send event", e.message || e)
throw e throw e
} }
} }
private async handleSend(data: SendData, keys: { name: string, privateKey: string, publicKey: string }): Promise<UnsignedEvent[]> { private async handleSend(data: SendData, keys: { name: string, privateKey: string, publicKey: string }): Promise<UnsignedEvent[]> {
@ -192,13 +192,17 @@ export class NostrPool {
return Object.values(this.relays).filter(r => r.isServiceRelay()).map(r => r.GetUrl()) return Object.values(this.relays).filter(r => r.isServiceRelay()).map(r => r.GetUrl())
} }
private async sendEvent(event: UnsignedEvent, keys: { name: string, privateKey: string }, relays?: string[]) { private getProviderRelays() {
return Object.values(this.relays).filter(r => r.isProviderRelay()).map(r => r.GetUrl())
}
private async sendEvent(event: UnsignedEvent, keys: { name: string, privateKey: string }, relays: string[]) {
const signed = finalizeEvent(event, Buffer.from(keys.privateKey, 'hex')) const signed = finalizeEvent(event, Buffer.from(keys.privateKey, 'hex'))
let sent = false let sent = false
const log = getLogger({ appName: keys.name }) const log = getLogger({ appName: keys.name })
const r = relays ? relays : this.getServiceRelays() // const r = relays ? relays : this.getServiceRelays()
const pool = new SimplePool() const pool = new SimplePool()
await Promise.all(pool.publish(r, signed).map(async p => { await Promise.all(pool.publish(relays, signed).map(async p => {
try { try {
await p await p
sent = true sent = true
@ -214,6 +218,18 @@ export class NostrPool {
} }
} }
private getRelays(initiator: SendInitiator, requestRelays?: string[]) {
if (requestRelays) {
return requestRelays
}
if (initiator.type === 'app') {
return this.getServiceRelays()
} else if (initiator.type === 'client') {
return this.getProviderRelays()
}
throw new Error("unkown initiator type")
}
private getSendKeys(initiator: SendInitiator) { private getSendKeys(initiator: SendInitiator) {
if (initiator.type === 'app') { if (initiator.type === 'app') {
const { appId } = initiator const { appId } = initiator