Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LNDHub: fix display of expiry time, hash, preimage #1652

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand All @@ -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)
);
}

Expand Down