Skip to content

Commit

Permalink
Merge pull request #1652 from kaloudis/lndhub-expiry
Browse files Browse the repository at this point in the history
LNDHub: fix display of expiry time, hash, preimage
  • Loading branch information
kaloudis authored Sep 7, 2023
2 parents 979d667 + b523710 commit 7a1cc64
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions models/Invoice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { observable, computed } from 'mobx';
import BigNumber from 'bignumber.js';

import BaseModel from './BaseModel';
import Base64Utils from './../utils/Base64Utils';
Expand Down Expand Up @@ -46,7 +47,7 @@ export default class Invoice extends BaseModel {
public private: boolean;
public creation_date: string;
public description_hash: string;
public r_preimage: string;
public r_preimage: any;
public cltv_expiry: string;
public htlcs: Array<HTLC>;
// c-lightning, eclair
Expand Down Expand Up @@ -76,7 +77,8 @@ export default class Invoice extends BaseModel {
}

@computed public get getRPreimage(): string {
const preimage = this.r_preimage;
if (!this.r_preimage) return '';
const preimage = this.r_preimage.data || this.r_preimage;
return typeof preimage === 'object'
? Base64Utils.bytesToHexString(preimage)
: typeof preimage === 'string'
Expand All @@ -87,7 +89,8 @@ export default class Invoice extends BaseModel {
}

@computed public get getRHash(): string {
const hash = this.r_hash;
if (!this.r_hash) return '';
const hash = this.r_hash.data || this.r_hash;
return typeof hash === 'object'
? Base64Utils.bytesToHexString(hash)
: typeof hash === 'string'
Expand Down Expand Up @@ -225,10 +228,16 @@ export default class Invoice extends BaseModel {
}

@computed public get expirationDate(): Date | string {
if (this.expiry || this.expire_time) {
const expiration = this.expiry || this.expire_time;
if (expiration == '0') return localeString('models.Invoice.never');
return `${expiration} ${localeString('models.Invoice.seconds')}`;
const expiry = this.expiry || this.expire_time;

// handle LNDHub
if (expiry && new BigNumber(expiry).gte(1600000000)) {
return DateTimeUtils.listFormattedDate(expiry);
}

if (expiry) {
if (expiry == '0') return localeString('models.Invoice.never');
return `${expiry} ${localeString('models.Invoice.seconds')}`;
}

return this.expires_at
Expand All @@ -237,10 +246,19 @@ export default class Invoice extends BaseModel {
}

@computed public get isExpired(): boolean {
if (this.expiry) {
const expiry = this.expiry || this.expire_time;

if (expiry && new BigNumber(expiry).gte(1600000000)) {
return (
new Date().getTime() / 1000 >
DateTimeUtils.listFormattedDate(expiry)
);
}

if (expiry) {
return (
new Date().getTime() / 1000 >
Number(this.creation_date) + Number(this.expiry)
Number(this.creation_date) + Number(expiry)
);
}

Expand Down

0 comments on commit 7a1cc64

Please sign in to comment.