Skip to content

Commit

Permalink
additional migrations + abstract storage
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Jan 16, 2025
1 parent f9bb849 commit bce1882
Show file tree
Hide file tree
Showing 30 changed files with 662 additions and 453 deletions.
23 changes: 23 additions & 0 deletions storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as Keychain from 'react-native-keychain';

class Storage {
getItem = async (key: string) => {
const response: any = await Keychain.getInternetCredentials(key);
if (response.password) {
return response.password;
}
return false;
};

setItem = async (key: string, value: any) => {
const response = await Keychain.setInternetCredentials(
key,
key,
JSON.stringify(value)
);
return response;
};
}

const storage = new Storage();
export default storage;
17 changes: 6 additions & 11 deletions stores/ActivityStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { action, observable } from 'mobx';
import EncryptedStorage from 'react-native-encrypted-storage';

// LN
import Payment from './../models/Payment';
Expand All @@ -15,6 +14,8 @@ import TransactionsStore from './TransactionsStore';
import BackendUtils from './../utils/BackendUtils';
import ActivityFilterUtils from '../utils/ActivityFilterUtils';

import Storage from '../storage';

const STORAGE_KEY = 'zeus-activity-filters';

export interface Filter {
Expand Down Expand Up @@ -78,10 +79,7 @@ export default class ActivityStore {
@action
public resetFilters = async () => {
this.filters = DEFAULT_FILTERS;
await EncryptedStorage.setItem(
STORAGE_KEY,
JSON.stringify(this.filters)
);
await Storage.setItem(STORAGE_KEY, this.filters);
this.setFilters(this.filters);
};

Expand All @@ -101,10 +99,7 @@ export default class ActivityStore {
startDate: undefined,
endDate: undefined
};
await EncryptedStorage.setItem(
STORAGE_KEY,
JSON.stringify(this.filters)
);
await Storage.setItem(STORAGE_KEY, this.filters);
};

@action
Expand Down Expand Up @@ -192,7 +187,7 @@ export default class ActivityStore {
public async getFilters() {
this.loading = true;
try {
const filters = await EncryptedStorage.getItem(STORAGE_KEY);
const filters = await Storage.getItem(STORAGE_KEY);
if (filters) {
this.filters = JSON.parse(filters, (key, value) =>
(key === 'startDate' || key === 'endDate') && value
Expand Down Expand Up @@ -224,7 +219,7 @@ export default class ActivityStore {
activity.determineFormattedRemainingTimeUntilExpiry(locale);
}
});
await EncryptedStorage.setItem(STORAGE_KEY, JSON.stringify(filters));
await Storage.setItem(STORAGE_KEY, filters);
this.loading = false;
};

Expand Down
22 changes: 12 additions & 10 deletions stores/ChannelBackupStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { action, observable } from 'mobx';
import ReactNativeBlobUtil from 'react-native-blob-util';
import EncryptedStorage from 'react-native-encrypted-storage';
import * as CryptoJS from 'crypto-js';

import NodeInfoStore from './NodeInfoStore';
Expand All @@ -18,8 +17,16 @@ import { LndMobileEventEmitter } from '../utils/LndMobileUtils';
import Base64Utils from '../utils/Base64Utils';
import { errorToUserFriendly } from '../utils/ErrorUtils';

import Storage from '../storage';

const BACKUPS_HOST = 'https://backups.lnolymp.us';

export const LEGACY_LAST_CHANNEL_BACKUP_STATUS = 'LAST_CHANNEL_BACKUP_STATUS';
export const LEGACY_LAST_CHANNEL_BACKUP_TIME = 'LAST_CHANNEL_BACKUP_TIME';

export const LAST_CHANNEL_BACKUP_STATUS = 'zeus-last-channel-backup-status';
export const LAST_CHANNEL_BACKUP_TIME = 'zeus-last-channel-backup-time';

export default class ChannelBackupStore {
@observable public channelEventsSubscription: any;
@observable public backups: Array<any> = [];
Expand All @@ -44,11 +51,8 @@ export default class ChannelBackupStore {
};

logBackupStatus = async (status: string) => {
await EncryptedStorage.setItem('LAST_CHANNEL_BACKUP_STATUS', status);
await EncryptedStorage.setItem(
'LAST_CHANNEL_BACKUP_TIME',
`${new Date()}`
);
await Storage.setItem('LAST_CHANNEL_BACKUP_STATUS', status);
await Storage.setItem('LAST_CHANNEL_BACKUP_TIME', `${new Date()}`);
};

@action
Expand Down Expand Up @@ -317,10 +321,8 @@ export default class ChannelBackupStore {
public initSubscribeChannelEvents = async () => {
// Check if latest channel backup status is success
// or if it's over three days ago and trigger backup
const status = await EncryptedStorage.getItem(
'LAST_CHANNEL_BACKUP_STATUS'
);
const time = await EncryptedStorage.getItem('LAST_CHANNEL_BACKUP_TIME');
const status = await Storage.getItem(LAST_CHANNEL_BACKUP_STATUS);
const time = await Storage.getItem(LAST_CHANNEL_BACKUP_TIME);
if (status && status === 'ERROR') this.backupChannels();
if (time) {
const ONE_HOUR = 60 * 60 * 1000; /* ms */
Expand Down
43 changes: 15 additions & 28 deletions stores/ContactStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { action, observable } from 'mobx';
import { v4 as uuidv4 } from 'uuid';
import Contact from '../models/Contact';
import * as Keychain from 'react-native-keychain';
import Storage from '../storage';

export const LEGACY_CONTACTS_KEY = 'zeus-contacts';
export const MODERN_CONTACTS_KEY = 'zeus-contacts-v2';
Expand All @@ -16,13 +16,11 @@ export default class ContactStore {
try {
this.loading = true;
console.log('LOADING CONTACTS.....');
const contactsString: any = await Keychain.getInternetCredentials(
const contactsString: any = await Storage.getItem(
MODERN_CONTACTS_KEY
);
if (contactsString.password) {
const allContacts: Contact[] = JSON.parse(
contactsString.password
);
if (contactsString) {
const allContacts: Contact[] = JSON.parse(contactsString);
this.contacts = allContacts;
this.loading = false;
} else {
Expand All @@ -44,11 +42,11 @@ export default class ContactStore {
try {
const compactedContactDetails =
this.compactContactArrays(contactDetails);
const contactsString: any = await Keychain.getInternetCredentials(
const contactsString: any = await Storage.getItem(
MODERN_CONTACTS_KEY
);
const existingContacts: Contact[] = contactsString.password
? JSON.parse(contactsString.password)
const existingContacts: Contact[] = contactsString
? JSON.parse(contactsString)
: [];

if (isEdit && this.prefillContact && !isNostrContact) {
Expand All @@ -62,11 +60,7 @@ export default class ContactStore {
updatedContacts.sort((a, b) => a.name.localeCompare(b.name));

// Save the updated contacts to encrypted storage
await Keychain.setInternetCredentials(
MODERN_CONTACTS_KEY,
MODERN_CONTACTS_KEY,
JSON.stringify(updatedContacts)
);
await Storage.setItem(MODERN_CONTACTS_KEY, updatedContacts);

console.log('Contact updated successfully!', updatedContacts);

Expand All @@ -86,11 +80,7 @@ export default class ContactStore {
);

// Save the updated contacts to encrypted storage
await Keychain.setInternetCredentials(
MODERN_CONTACTS_KEY,
MODERN_CONTACTS_KEY,
JSON.stringify(updatedContacts)
);
await Storage.setItem(MODERN_CONTACTS_KEY, updatedContacts);

console.log('Contact saved successfully!');

Expand Down Expand Up @@ -118,22 +108,19 @@ export default class ContactStore {
public deleteContact = async (navigation: any) => {
if (this.prefillContact) {
try {
const contactsString: any =
await Keychain.getInternetCredentials(MODERN_CONTACTS_KEY);
const existingContacts: Contact[] = contactsString.password
? JSON.parse(contactsString.password)
const contactsString: any = await Storage.getItem(
MODERN_CONTACTS_KEY
);
const existingContacts: Contact[] = contactsString
? JSON.parse(contactsString)
: [];

const updatedContacts = existingContacts.filter(
(contact) =>
contact.contactId !== this.prefillContact.contactId
);

await Keychain.setInternetCredentials(
MODERN_CONTACTS_KEY,
MODERN_CONTACTS_KEY,
JSON.stringify(updatedContacts)
);
await Storage.setItem(MODERN_CONTACTS_KEY, updatedContacts);

console.log('Contact deleted successfully!');

Expand Down
17 changes: 10 additions & 7 deletions stores/InventoryStore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { action, observable } from 'mobx';
import Product from '../models/Product';
import ProductCategory from '../models/ProductCategory';
import EncryptedStorage from 'react-native-encrypted-storage';
import Storage from '../storage';

const CATEGORY_KEY = 'zeus-product-categories';
const PRODUCT_KEY = 'zeus-products';
export const LEGACY_CATEGORY_KEY = 'zeus-product-categories';
export const LEGACY_PRODUCT_KEY = 'zeus-products';

export const CATEGORY_KEY = 'zeus-product-categories-v2';
export const PRODUCT_KEY = 'zeus-products-v2';

export default class InventoryStore {
@observable categories: Array<ProductCategory> = [];
Expand All @@ -16,12 +19,12 @@ export default class InventoryStore {
this.loading = true;
try {
// Retrieve the categories
const categories = await EncryptedStorage.getItem(CATEGORY_KEY);
const categories = await Storage.getItem(CATEGORY_KEY);
if (categories) {
this.categories = JSON.parse(categories) || [];
}
// Retrieve the products
const products = await EncryptedStorage.getItem(PRODUCT_KEY);
const products = await Storage.getItem(PRODUCT_KEY);
if (products) {
this.products = JSON.parse(products) || [];
}
Expand All @@ -40,7 +43,7 @@ export default class InventoryStore {
@action
public async setCategories(categories: string) {
this.loading = true;
await EncryptedStorage.setItem(CATEGORY_KEY, categories);
await Storage.setItem(CATEGORY_KEY, categories);
this.loading = false;
return categories;
}
Expand Down Expand Up @@ -90,7 +93,7 @@ export default class InventoryStore {
@action
public async setProducts(products: string) {
this.loading = true;
await EncryptedStorage.setItem(PRODUCT_KEY, products);
await Storage.setItem(PRODUCT_KEY, products);
this.loading = false;
return products;
}
Expand Down
3 changes: 3 additions & 0 deletions stores/LSPStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import { LndMobileEventEmitter } from '../utils/LndMobileUtils';
import { localeString } from '../utils/LocaleUtils';
import { errorToUserFriendly } from '../utils/ErrorUtils';

export const LEGACY_LSPS1_ORDERS_KEY = 'orderResponses';
export const LSPS1_ORDERS_KEY = 'zeus-lsps1-orders';

export default class LSPStore {
@observable public info: any = {};
@observable public zeroConfFee: number | undefined;
Expand Down
Loading

0 comments on commit bce1882

Please sign in to comment.