diff --git a/server/helpers/auth.ts b/server/helpers/auth.ts index d408bce0..7b9042ed 100644 --- a/server/helpers/auth.ts +++ b/server/helpers/auth.ts @@ -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; }; diff --git a/server/helpers/crypto.ts b/server/helpers/crypto.ts index 5bc7036b..3c64f309 100644 --- a/server/helpers/crypto.ts +++ b/server/helpers/crypto.ts @@ -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); diff --git a/server/helpers/savedLnd.ts b/server/helpers/savedLnd.ts new file mode 100644 index 00000000..299aba86 --- /dev/null +++ b/server/helpers/savedLnd.ts @@ -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; + } +} diff --git a/server/schema/account/resolvers.ts b/server/schema/account/resolvers.ts index 7a533144..20d6f600 100644 --- a/server/schema/account/resolvers.ts +++ b/server/schema/account/resolvers.ts @@ -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;