Skip to content

Commit

Permalink
Merge pull request #2518 from myxmaster/avoid-require-cycles
Browse files Browse the repository at this point in the history
Use central storeInstances file
  • Loading branch information
kaloudis authored Nov 11, 2024
2 parents bdd8a1a + 89e55fd commit ab37333
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 44 deletions.
7 changes: 3 additions & 4 deletions backends/CLNRest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import stores from '../stores/Stores';
import { settingsStore, nodeInfoStore } from '../stores/storeInstances';
import TransactionRequest from '../models/TransactionRequest';
import OpenChannelRequest from '../models/OpenChannelRequest';
import VersionUtils from '../utils/VersionUtils';
Expand Down Expand Up @@ -29,7 +29,7 @@ export default class CLNRest {
eosVersion?: string,
minApiVersion?: string
) => {
const { nodeInfo } = stores.nodeInfoStore;
const { nodeInfo } = nodeInfoStore;
const { version, api_version } = nodeInfo;
const { isSupportedVersion } = VersionUtils;
if (minApiVersion) {
Expand Down Expand Up @@ -127,8 +127,7 @@ export default class CLNRest {
};

request = (route: string, method: string, data?: any, params?: any) => {
const { host, port, rune, certVerification, enableTor } =
stores.settingsStore;
const { host, port, rune, certVerification, enableTor } = settingsStore;

if (params) {
route = `${route}?${Object.keys(params)
Expand Down
4 changes: 2 additions & 2 deletions backends/CLightningREST.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import stores from '../stores/Stores';
import { nodeInfoStore } from '../stores/storeInstances';
import LND from './LND';
import TransactionRequest from './../models/TransactionRequest';
import OpenChannelRequest from './../models/OpenChannelRequest';
Expand All @@ -20,7 +20,7 @@ export default class CLightningREST extends LND {
eosVersion?: string,
minApiVersion?: string
) => {
const { nodeInfo } = stores.nodeInfoStore;
const { nodeInfo } = nodeInfoStore;
const { version, api_version } = nodeInfo;
const { isSupportedVersion } = VersionUtils;
if (minApiVersion) {
Expand Down
6 changes: 3 additions & 3 deletions backends/Eclair.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ReactNativeBlobUtil from 'react-native-blob-util';
import querystring from 'querystring-es3';
import stores from '../stores/Stores';
import { settingsStore } from '../stores/storeInstances';
import { doTorRequest, RequestMethod } from '../utils/TorUtils';
import TransactionRequest from './../models/TransactionRequest';
import OpenChannelRequest from './../models/OpenChannelRequest';
Expand All @@ -14,8 +14,8 @@ export default class Eclair {
clearCachedCalls = () => calls.clear();

api = (method: string, params: any = {}) => {
const { password, certVerification, enableTor } = stores.settingsStore;
let { url } = stores.settingsStore;
const { password, certVerification, enableTor } = settingsStore;
let { url } = settingsStore;

const id: string = method + JSON.stringify(params);
if (calls.has(id)) {
Expand Down
14 changes: 7 additions & 7 deletions backends/LND.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ReactNativeBlobUtil from 'react-native-blob-util';
import stores from '../stores/Stores';
import { settingsStore, nodeInfoStore } from '../stores/storeInstances';
import { doTorRequest, RequestMethod } from '../utils/TorUtils';
import OpenChannelRequest from './../models/OpenChannelRequest';
import Base64Utils from './../utils/Base64Utils';
Expand Down Expand Up @@ -106,15 +106,15 @@ export default class LND {
};

supports = (minVersion: string, eosVersion?: string) => {
const { nodeInfo } = stores.nodeInfoStore;
const { nodeInfo } = nodeInfoStore;
const { version } = nodeInfo;
const { isSupportedVersion } = VersionUtils;
return isSupportedVersion(version, minVersion, eosVersion);
};

wsReq = (route: string, method: string, data?: any) => {
const { host, lndhubUrl, port, macaroonHex, accessToken } =
stores.settingsStore;
settingsStore;

const auth = macaroonHex || accessToken;
const headers: any = this.getHeaders(auth, true);
Expand Down Expand Up @@ -199,7 +199,7 @@ export default class LND {
accessToken,
certVerification,
enableTor
} = stores.settingsStore;
} = settingsStore;

if (params) {
route = `${route}?${Object.keys(params)
Expand Down Expand Up @@ -263,7 +263,7 @@ export default class LND {
const method = 'GET';

const { host, lndhubUrl, port, macaroonHex, accessToken } =
stores.settingsStore;
settingsStore;

const auth = macaroonHex || accessToken;
const headers: any = this.getHeaders(auth, true);
Expand Down Expand Up @@ -403,7 +403,7 @@ export default class LND {

// make call
const { host, lndhubUrl, port, macaroonHex, accessToken } =
stores.settingsStore;
settingsStore;

const auth = macaroonHex || accessToken;
const headers: any = this.getHeaders(auth, true);
Expand Down Expand Up @@ -600,7 +600,7 @@ export default class LND {
subscribeTransactions = () => this.getRequest('/v1/transactions/subscribe');
initChanAcceptor = (data?: any) => {
const { host, lndhubUrl, port, macaroonHex, accessToken } =
stores.settingsStore;
settingsStore;

const auth = macaroonHex || accessToken;
const headers: any = this.getHeaders(auth, true);
Expand Down
6 changes: 3 additions & 3 deletions backends/LightningNodeConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NativeModules, NativeEventEmitter } from 'react-native';

import LNC, { lnrpc, walletrpc } from '../zeus_modules/@lightninglabs/lnc-rn';

import stores from '../stores/Stores';
import { settingsStore, nodeInfoStore } from '../stores/storeInstances';
import CredentialStore from './LNC/credentialStore';

import OpenChannelRequest from '../models/OpenChannelRequest';
Expand Down Expand Up @@ -45,7 +45,7 @@ export default class LightningNodeConnect {

initLNC = async () => {
const { pairingPhrase, mailboxServer, customMailboxServer } =
stores.settingsStore;
settingsStore;

this.lnc = new LNC({
credentialStore: new CredentialStore()
Expand Down Expand Up @@ -492,7 +492,7 @@ export default class LightningNodeConnect {
this.lnc.lnd.lightning.subscribeTransactions();

supports = (minVersion: string, eosVersion?: string) => {
const { nodeInfo } = stores.nodeInfoStore;
const { nodeInfo } = nodeInfoStore;
const { version } = nodeInfo;
const { isSupportedVersion } = VersionUtils;
return isSupportedVersion(version, minVersion, eosVersion);
Expand Down
22 changes: 11 additions & 11 deletions backends/LndHub.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bolt11 from 'bolt11';

import stores from '../stores/Stores';
import { settingsStore } from '../stores/storeInstances';

import LND from './LND';
import LoginRequest from './../models/LoginRequest';
Expand Down Expand Up @@ -84,12 +84,12 @@ export default class LndHub extends LND {
.digest();

let signed, signature, key, linkingKeyPair;
switch (stores.settingsStore.settings.lndHubLnAuthMode || 'Alby') {
switch (settingsStore.settings.lndHubLnAuthMode || 'Alby') {
case 'Alby':
key = new sha256Hash()
.update(
Base64Utils.stringToUint8Array(
`lndhub://${stores.settingsStore.username}:${stores.settingsStore.password}`
`lndhub://${settingsStore.username}:${settingsStore.password}`
)
)
.digest();
Expand All @@ -103,7 +103,7 @@ export default class LndHub extends LND {
break;
case 'BlueWallet':
signature = Base64Utils.stringToUint8Array(
`lndhub://${stores.settingsStore.username}:${stores.settingsStore.password}`
`lndhub://${settingsStore.username}:${settingsStore.password}`
);
break;
}
Expand All @@ -119,18 +119,18 @@ export default class LndHub extends LND {
supportsOnchainSends = () => false;
supportsOnchainReceiving = () =>
!(
stores?.settingsStore?.lndhubUrl?.includes('lnbank/api/lndhub') ||
stores?.settingsStore?.lndhubUrl?.includes('lntxbot') ||
settingsStore?.lndhubUrl?.includes('lnbank/api/lndhub') ||
settingsStore?.lndhubUrl?.includes('lntxbot') ||
// Alby
stores?.settingsStore?.lndhubUrl?.includes('ln.getalby.com') ||
stores?.settingsStore?.lndhubUrl?.includes('getalby.com/lndhub') ||
settingsStore?.lndhubUrl?.includes('ln.getalby.com') ||
settingsStore?.lndhubUrl?.includes('getalby.com/lndhub') ||
// LNBits
stores?.settingsStore?.lndhubUrl?.includes('/lndhub/ext/')
settingsStore?.lndhubUrl?.includes('/lndhub/ext/')
);
supportsLightningSends = () => {
return !(
stores?.settingsStore?.lndhubUrl?.includes('/lndhub/ext/') &&
stores.settingsStore.username === 'invoice'
settingsStore?.lndhubUrl?.includes('/lndhub/ext/') &&
settingsStore.username === 'invoice'
);
};
supportsKeysend = () => false;
Expand Down
6 changes: 3 additions & 3 deletions backends/Spark.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ReactNativeBlobUtil from 'react-native-blob-util';
import stores from '../stores/Stores';
import { settingsStore } from '../stores/storeInstances';
import { doTorRequest, RequestMethod } from '../utils/TorUtils';
import TransactionRequest from './../models/TransactionRequest';
import OpenChannelRequest from './../models/OpenChannelRequest';
Expand All @@ -11,8 +11,8 @@ export default class Spark {
clearCachedCalls = () => calls.clear();

rpc = (rpcmethod: string, param = {}, range: any = null) => {
const { accessKey, certVerification, enableTor } = stores.settingsStore;
let { url } = stores.settingsStore;
const { accessKey, certVerification, enableTor } = settingsStore;
let { url } = settingsStore;

const id = rpcmethod + JSON.stringify(param) + JSON.stringify(range);
if (calls.has(id)) {
Expand Down
4 changes: 2 additions & 2 deletions models/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Base64Utils from '../utils/Base64Utils';
import DateTimeUtils from '../utils/DateTimeUtils';
import Bolt11Utils from '../utils/Bolt11Utils';
import { localeString } from '../utils/LocaleUtils';
import stores from '../stores/Stores';
import { notesStore } from '../stores/storeInstances';

interface HopHint {
fee_proportional_millionths: number;
Expand Down Expand Up @@ -394,6 +394,6 @@ export default class Invoice extends BaseModel {
}

@computed public get getNote(): string {
return stores.notesStore.notes[this.getNoteKey] || '';
return notesStore.notes[this.getNoteKey] || '';
}
}
6 changes: 3 additions & 3 deletions models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import moment from 'moment';
import BaseModel from './BaseModel';
import { localeString } from '../utils/LocaleUtils';
import { orderPaymentInfo } from '../stores/PosStore';
import stores from '../stores/Stores';
import { fiatStore } from '../stores/storeInstances';

interface BasePriceMoney {
amount: number;
Expand Down Expand Up @@ -79,7 +79,7 @@ export default class Order extends BaseModel {
}

@computed public get getTotalMoneyDisplay(): string {
return stores.fiatStore.formatAmountForDisplay(this.getTotalMoney);
return fiatStore.formatAmountForDisplay(this.getTotalMoney);
}

@computed public get getTaxMoney(): string {
Expand All @@ -89,6 +89,6 @@ export default class Order extends BaseModel {
}

@computed public get getTaxMoneyDisplay(): string {
return stores.fiatStore.formatAmountForDisplay(this.getTaxMoney);
return fiatStore.formatAmountForDisplay(this.getTaxMoney);
}
}
4 changes: 2 additions & 2 deletions models/Payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { localeString } from '../utils/LocaleUtils';
import Bolt11Utils from '../utils/Bolt11Utils';
import Base64Utils from '../utils/Base64Utils';
import { lnrpc } from '../proto/lightning';
import stores from '../stores/Stores';
import { notesStore } from '../stores/storeInstances';

interface preimageBuffer {
data: Array<number>;
Expand Down Expand Up @@ -315,6 +315,6 @@ export default class Payment extends BaseModel {
}

@computed public get getNote(): string {
return stores.notesStore.notes[this.getNoteKey] || '';
return notesStore.notes[this.getNoteKey] || '';
}
}
4 changes: 2 additions & 2 deletions models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import BigNumber from 'bignumber.js';
import BaseModel from './BaseModel';
import DateTimeUtils from '../utils/DateTimeUtils';
import { localeString } from '../utils/LocaleUtils';
import stores from '../stores/Stores';
import { notesStore } from '../stores/storeInstances';

interface OutputDetail {
address: string;
Expand Down Expand Up @@ -136,6 +136,6 @@ export default class Transaction extends BaseModel {
}

@computed public get getNote(): string {
return stores.notesStore.notes[this.getNoteKey] || '';
return notesStore.notes[this.getNoteKey] || '';
}
}
5 changes: 5 additions & 0 deletions stores/storeInstances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import stores from './Stores';
export const fiatStore = stores.fiatStore;
export const notesStore = stores.notesStore;
export const settingsStore = stores.settingsStore;
export const nodeInfoStore = stores.nodeInfoStore;
4 changes: 2 additions & 2 deletions utils/BackendUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import stores from '../stores/Stores';
import { settingsStore } from '../stores/storeInstances';
// LND
import LND from '../backends/LND';
import LightningNodeConnect from '../backends/LightningNodeConnect';
Expand Down Expand Up @@ -33,7 +33,7 @@ class BackendUtils {
}

getClass = () => {
const { implementation } = stores.settingsStore;
const { implementation } = settingsStore;
switch (implementation) {
case 'lnd':
return this.lnd;
Expand Down

0 comments on commit ab37333

Please sign in to comment.