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

ZEUS PAY optimizations #1888

Merged
merged 6 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions backends/EmbeddedLND.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ export default class EmbeddedLND extends LND {
getNetworkInfo = async () => await getNetworkInfo();
getInvoices = async () => await listInvoices();
createInvoice = async (data: any) =>
await addInvoice(
Number(data.value),
data.memo,
data.expiry,
data.is_amp,
data.private,
data.preimage
);
await addInvoice({
amount: data.value ? Number(data.value) : undefined,
amount_msat: data.value_msat ? Number(data.value_msat) : undefined,
memo: data.memo,
expiry: data.expiry,
is_amp: data.is_amp,
is_private: data.private,
preimage: data.preimage
});
getPayments = async () => await listPayments();
getNewAddress = async (data: any) => await newAddress(data.type);
openChannel = async (data: OpenChannelRequest) =>
Expand Down
2 changes: 1 addition & 1 deletion backends/LND.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export default class LND {
createInvoice = (data: any) =>
this.postRequest('/v1/invoices', {
memo: data.memo,
value_msat: Number(data.value) * 1000,
value_msat: data.value_msat || Number(data.value) * 1000,
expiry: data.expiry,
is_amp: data.is_amp,
private: data.private,
Expand Down
2 changes: 1 addition & 1 deletion backends/LightningNodeConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default class LightningNodeConnect {
await this.lnc.lnd.lightning
.addInvoice({
memo: data.memo,
value_msat: Number(data.value) * 1000,
value_msat: data.value_msat || Number(data.value) * 1000,
expiry: data.expiry,
is_amp: data.is_amp,
private: data.private,
Expand Down
74 changes: 74 additions & 0 deletions components/AttestationButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React, { useState } from 'react';
import { TouchableOpacity } from 'react-native';

import stores from '../stores/Stores';

import { themeColor } from '../utils/ThemeUtils';

import LoadingIndicator from '../components/LoadingIndicator';
import NostrichNotLoaded from '../assets/images/SVG/Nostrich_not-loaded.svg';
import NostrichValid from '../assets/images/SVG/Nostrich_valid.svg';
import NostrichInvalid from '../assets/images/SVG/Nostrich_invalid.svg';
import NostrichNotFound from '../assets/images/SVG/Nostrich_not-found.svg';

export default function AttestationButton(props: any) {
const { navigation, hash, amount_msat } = props;
const [attestationStatus, setAttestationStatus] = useState('neutral');
const [loading, setLoading] = useState(false);
const [attestations, setAttestations] = useState([]);

return (
<TouchableOpacity
onPress={() => {
if (attestationStatus === 'neutral') {
setLoading(true);
stores.lightningAddressStore
.lookupAttestations(hash, amount_msat)
.then(({ attestations, status }) => {
setAttestations(attestations);
setAttestationStatus(status || '');
setLoading(false);
})
.catch(() => {
setLoading(false);
});
} else {
if (attestationStatus === 'success') {
navigation.navigate('Attestation', {
attestation: attestations[0]
});
} else {
navigation.navigate('Attestations', {
attestations
});
}
}
}}
style={{ marginRight: 15 }}
>
{loading ? (
<LoadingIndicator size={32} />
) : attestationStatus === 'warning' ? (
<NostrichNotFound fill="#FFC300" width={32} height={32} />
) : attestationStatus === 'neutral' ? (
<NostrichNotLoaded
fill={themeColor('text')}
width={32}
height={32}
/>
) : attestationStatus === 'success' ? (
<NostrichValid
fill={themeColor(attestationStatus)}
width={32}
height={32}
/>
) : (
<NostrichInvalid
fill={themeColor(attestationStatus)}
width={32}
height={32}
/>
)}
</TouchableOpacity>
);
}
25 changes: 17 additions & 8 deletions lndmobile/LndMobileInjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,23 @@ export interface ILndMobileInjections {
TEMP_moveLndToApplicationSupport: () => Promise<boolean>;
excludeLndICloudBackup: () => Promise<boolean>;

addInvoice: (
amount: number,
memo: string,
expiry?: number,
is_amp?: boolean,
is_private?: boolean,
r_preimage?: string
) => Promise<lnrpc.AddInvoiceResponse>;
addInvoice: ({
amount,
amount_msat,
memo,
expiry,
is_amp,
is_private,
preimage
}: {
amount?: number;
amount_msat?: number;
memo: string;
expiry?: number;
is_amp?: boolean;
is_private?: boolean;
preimage?: string;
}) => Promise<lnrpc.AddInvoiceResponse>;
cancelInvoice: (
paymentHash: string
) => Promise<invoicesrpc.CancelInvoiceResp>;
Expand Down
28 changes: 19 additions & 9 deletions lndmobile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,23 @@ export const decodePaymentStatus = (data: string): routerrpc.PaymentStatus => {
/**
* @throws
*/
export const addInvoice = async (
amount: number,
memo: string,
expiry: number = 3600,
is_amp?: boolean,
is_private?: boolean,
preimage?: string
): Promise<lnrpc.AddInvoiceResponse> => {
export const addInvoice = async ({
amount,
amount_msat,
memo,
expiry = 3600,
is_amp,
is_private,
preimage
}: {
amount?: number;
amount_msat?: number;
memo: string;
expiry: number;
is_amp?: boolean;
is_private?: boolean;
preimage?: string;
}): Promise<lnrpc.AddInvoiceResponse> => {
const response = await sendCommand<
lnrpc.IInvoice,
lnrpc.Invoice,
Expand All @@ -515,7 +524,8 @@ export const addInvoice = async (
response: lnrpc.AddInvoiceResponse,
method: 'AddInvoice',
options: {
value: Long.fromValue(amount),
value: amount ? Long.fromValue(amount) : undefined,
value_msat: amount_msat ? Long.fromValue(amount_msat) : undefined,
memo,
expiry: Long.fromValue(expiry),
private: is_private,
Expand Down
3 changes: 1 addition & 2 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,7 @@
"views.Settings.LightningAddress.changeNostrKeys": "Change Nostr keys",
"views.Settings.LightningAddress.chooseHandle": "Choose a handle",
"views.Settings.LightningAddress.zaplockerVerification": "Zaplocker verification",
"views.Settings.LightningAddress.noOpenPayments": "No open payments",
"views.Settings.LightningAddress.noSettledPayments": "No settled payments",
"views.Settings.LightningAddress.noPaymentsToRedeem": "No payments to redeem. Check back later.",
"views.Settings.LightningAddress.statusExplainer1": "This is the number of payments that can be made to you while you're offline.",
"views.Settings.LightningAddress.statusExplainer2": "More payment hashes will be sent to the server automatically when required, so that you can continue to receive payments.",
"views.Settings.LightningAddress.generatingPreimages": "Generating preimages",
Expand Down
5 changes: 5 additions & 0 deletions models/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,9 @@ export default class Invoice extends BaseModel {

return '';
}

@computed public get isZeusPay(): boolean {
if (this.getMemo?.startsWith('ZEUS PAY')) return true;
return false;
}
}
3 changes: 3 additions & 0 deletions stores/ActivityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface Filter {
received: boolean;
unpaid: boolean;
inTransit: boolean;
zeusPay: boolean;
minimumAmount: number;
startDate?: Date;
endDate?: Date;
Expand All @@ -37,6 +38,7 @@ export const DEFAULT_FILTERS = {
received: true,
unpaid: true,
inTransit: false,
zeusPay: true,
minimumAmount: 0,
startDate: undefined,
endDate: undefined
Expand Down Expand Up @@ -86,6 +88,7 @@ export default class ActivityStore {
received: true,
unpaid: false,
inTransit: false,
zeusPay: true,
minimumAmount: 0,
startDate: undefined,
endDate: undefined
Expand Down
29 changes: 17 additions & 12 deletions stores/LightningAddressStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default class LightningAddressStore {
@observable public error_msg: string = '';
@observable public availableHashes: number = 0;
@observable public paid: any = [];
@observable public settled: any = [];
@observable public preimageMap: any = {};
@observable public fees: any = {};
@observable public minimumSats: number;
Expand Down Expand Up @@ -100,6 +99,20 @@ export default class LightningAddressStore {
this.lightningAddress = `${handle}@${domain}`;
};

deleteHash = async (hash: string) => {
const hashesString =
(await EncryptedStorage.getItem(HASHES_STORAGE_STRING)) || '{}';

const oldHashes = JSON.parse(hashesString);
delete oldHashes[hash];
const newHashes = oldHashes;

return await EncryptedStorage.setItem(
HASHES_STORAGE_STRING,
JSON.stringify(newHashes)
);
};

@action
public generatePreimages = async () => {
this.error = false;
Expand Down Expand Up @@ -541,7 +554,6 @@ export default class LightningAddressStore {
results,
success,
paid,
settled,
fees,
minimumSats,
handle,
Expand All @@ -557,8 +569,6 @@ export default class LightningAddressStore {
this.availableHashes = results || 0;
this.paid =
this.enhanceWithFee(paid);
this.settled =
this.enhanceWithFee(settled);
this.fees = fees;
this.minimumSats = minimumSats;
this.lightningAddressHandle =
Expand Down Expand Up @@ -674,7 +684,7 @@ export default class LightningAddressStore {

if (status === 200 && success) {
this.redeeming = false;

await this.deleteHash(hash);
resolve({
success
});
Expand Down Expand Up @@ -933,7 +943,7 @@ export default class LightningAddressStore {
BackendUtils.createInvoice({
// 24 hrs
expiry: '86400',
value,
value_msat: amount_msat,
memo: comment ? `ZEUS PAY: ${comment}` : 'ZEUS PAY',
preimage,
private:
Expand Down Expand Up @@ -1053,11 +1063,7 @@ export default class LightningAddressStore {
});

this.socket.on('paid', (data: any) => {
const { hash, req, amount_msat, comment } = data;

console.log('hash', hash);
console.log('req', req);
console.log('amount_msat', amount_msat);
const { hash, amount_msat, comment } = data;

const attestationLevel = this.settingsStore?.settings
?.lightningAddress
Expand Down Expand Up @@ -1124,7 +1130,6 @@ export default class LightningAddressStore {
this.error_msg = '';
this.availableHashes = 0;
this.paid = [];
this.settled = [];
this.preimageMap = {};
this.socket = undefined;
this.lightningAddress = '';
Expand Down
11 changes: 11 additions & 0 deletions utils/ActivityFilterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ class ActivityFilterUtils {
);
}

if (filter.zeusPay == false) {
filteredActivity = filteredActivity.filter(
(activity: any) =>
!(
activity.model ===
localeString('views.Invoice.title') &&
activity.isZeusPay
)
);
}

if (filter.minimumAmount > 0) {
filteredActivity = filteredActivity.filter(
(activity: any) =>
Expand Down
Loading