swap check + provider relay
This commit is contained in:
parent
c9305c34e8
commit
81229b3385
2 changed files with 27 additions and 7 deletions
|
|
@ -496,6 +496,10 @@ export default class {
|
|||
if (!txSwap) {
|
||||
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 data: TransactionSwapData = {
|
||||
createdResponse: {
|
||||
|
|
|
|||
|
|
@ -142,14 +142,14 @@ export class NostrPool {
|
|||
async Send(initiator: SendInitiator, data: SendData, relays?: string[]) {
|
||||
try {
|
||||
const keys = this.getSendKeys(initiator)
|
||||
const r = this.getRelays(initiator, relays)
|
||||
const privateKey = Buffer.from(keys.privateKey, 'hex')
|
||||
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) {
|
||||
this.log(ERROR, "failed to send event", e.message || e)
|
||||
throw e
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
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'))
|
||||
let sent = false
|
||||
const log = getLogger({ appName: keys.name })
|
||||
const r = relays ? relays : this.getServiceRelays()
|
||||
// const r = relays ? relays : this.getServiceRelays()
|
||||
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 {
|
||||
await p
|
||||
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) {
|
||||
if (initiator.type === 'app') {
|
||||
const { appId } = initiator
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue