diff --git a/models/Invoice.ts b/models/Invoice.ts index 85efaa89c..d8d06620b 100644 --- a/models/Invoice.ts +++ b/models/Invoice.ts @@ -1,4 +1,5 @@ import { observable, computed } from 'mobx'; +import BigNumber from 'bignumber.js'; import BaseModel from './BaseModel'; import Base64Utils from './../utils/Base64Utils'; @@ -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; // c-lightning, eclair @@ -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' @@ -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' @@ -223,10 +226,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 @@ -235,10 +244,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) ); }