mirror of
https://github.com/apotdevin/thunderhub.git
synced 2025-02-22 14:22:33 +01:00
fix: ๐ reuse lnd connection
This commit is contained in:
parent
ad480e1740
commit
f2eff6c2e7
4 changed files with 52 additions and 4 deletions
|
@ -2,8 +2,10 @@ import { authenticatedLndGrpc } from 'ln-service';
|
|||
import { SSOType } from 'server/types/apiTypes';
|
||||
import { LndObject } from 'server/types/ln-service.types';
|
||||
import { v5 as uuidv5 } from 'uuid';
|
||||
import { getSHA256Hash } from './crypto';
|
||||
import { ParsedAccount } from './fileHelpers';
|
||||
import { logger } from './logger';
|
||||
import { SavedLnd } from './savedLnd';
|
||||
|
||||
type LndAuthType = {
|
||||
cert: string | null;
|
||||
|
@ -13,6 +15,8 @@ type LndAuthType = {
|
|||
|
||||
const THUNDERHUB_NAMESPACE = '00000000-0000-0000-0000-000000000000';
|
||||
|
||||
export const saved = new SavedLnd();
|
||||
|
||||
export const getUUID = (text: string): string =>
|
||||
uuidv5(text, THUNDERHUB_NAMESPACE);
|
||||
|
||||
|
@ -21,6 +25,13 @@ export const getAuthLnd = (
|
|||
sso: SSOType | null,
|
||||
accounts: ParsedAccount[]
|
||||
): LndObject | null => {
|
||||
const hash = getSHA256Hash(JSON.stringify({ id, sso, accounts }));
|
||||
|
||||
if (saved.isSame(hash)) {
|
||||
logger.silly('Using recycled LND Object');
|
||||
return saved.lnd;
|
||||
}
|
||||
|
||||
if (!id) {
|
||||
logger.silly('Account not authenticated');
|
||||
return null;
|
||||
|
@ -56,6 +67,10 @@ export const getAuthLnd = (
|
|||
authDetails = verifiedAccount;
|
||||
}
|
||||
|
||||
logger.debug('Creating a new LND object');
|
||||
const { lnd } = authenticatedLndGrpc(authDetails);
|
||||
|
||||
saved.save(hash, lnd);
|
||||
|
||||
return lnd;
|
||||
};
|
||||
|
|
|
@ -9,14 +9,16 @@ import { logger } from './logger';
|
|||
|
||||
export const getPreimageAndHash = () => {
|
||||
const preimage = randomBytes(32);
|
||||
const preimageHash = createHash('sha256')
|
||||
.update(preimage)
|
||||
.digest()
|
||||
.toString('hex');
|
||||
const preimageHash = getSHA256Hash(preimage);
|
||||
|
||||
return { preimage, hash: preimageHash };
|
||||
};
|
||||
|
||||
export const getSHA256Hash = (
|
||||
str: string | Buffer,
|
||||
encoding: 'hex' | 'base64' = 'hex'
|
||||
) => createHash('sha256').update(str).digest().toString(encoding);
|
||||
|
||||
export const getPrivateAndPublicKey = () => {
|
||||
const secretKey = bip39.generateMnemonic();
|
||||
const base58 = bip39.mnemonicToSeedSync(secretKey);
|
||||
|
|
28
server/helpers/savedLnd.ts
Normal file
28
server/helpers/savedLnd.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { LndObject } from 'server/types/ln-service.types';
|
||||
|
||||
export class SavedLnd {
|
||||
hash: string | null;
|
||||
lnd: LndObject | null;
|
||||
|
||||
constructor() {
|
||||
this.hash = null;
|
||||
this.lnd = null;
|
||||
}
|
||||
|
||||
save(hash: string, lnd: LndObject) {
|
||||
this.hash = hash;
|
||||
this.lnd = lnd;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.hash = null;
|
||||
this.lnd = null;
|
||||
}
|
||||
|
||||
isSame(hash: string): boolean {
|
||||
if (hash === this.hash && this.lnd) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import { ContextType } from 'server/types/apiTypes';
|
||||
import { logger } from 'server/helpers/logger';
|
||||
import { requestLimiter } from 'server/helpers/rateLimiter';
|
||||
import { saved } from 'server/helpers/auth';
|
||||
|
||||
export const accountResolvers = {
|
||||
Query: {
|
||||
|
@ -39,6 +40,8 @@ export const accountResolvers = {
|
|||
const { ip, accounts, id, sso } = context;
|
||||
await requestLimiter(ip, 'getServerAccounts');
|
||||
|
||||
saved.reset();
|
||||
|
||||
let ssoAccount = null;
|
||||
if (id === 'sso' && sso) {
|
||||
const { cert, socket } = sso;
|
||||
|
|
Loadingโฆ
Add table
Reference in a new issue