2020-04-12 18:27:01 +02:00
|
|
|
import { authenticatedLndGrpc } from 'ln-service';
|
2020-04-14 08:25:58 +02:00
|
|
|
import getConfig from 'next/config';
|
2020-05-19 07:50:16 +02:00
|
|
|
import {
|
|
|
|
SSO_ACCOUNT,
|
|
|
|
SERVER_ACCOUNT,
|
|
|
|
AuthType,
|
|
|
|
CLIENT_ACCOUNT,
|
|
|
|
} from 'src/context/AccountContext';
|
|
|
|
import { ContextType } from 'api/types/apiTypes';
|
2020-05-15 19:48:00 +02:00
|
|
|
import { logger } from './logger';
|
2020-04-14 08:25:58 +02:00
|
|
|
|
|
|
|
const { serverRuntimeConfig } = getConfig();
|
|
|
|
const { nodeEnv } = serverRuntimeConfig;
|
2020-04-12 18:27:01 +02:00
|
|
|
|
2020-05-19 07:50:16 +02:00
|
|
|
type LndAuthType = {
|
|
|
|
cert: string;
|
|
|
|
macaroon: string;
|
|
|
|
host: string;
|
|
|
|
};
|
|
|
|
|
2020-04-12 18:27:01 +02:00
|
|
|
export const getIp = (req: any) => {
|
|
|
|
if (!req || !req.headers) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
const forwarded = req.headers['x-forwarded-for'];
|
|
|
|
const before = forwarded
|
|
|
|
? forwarded.split(/, /)[0]
|
|
|
|
: req.connection.remoteAddress;
|
2020-04-14 08:25:58 +02:00
|
|
|
const ip = nodeEnv === 'development' ? '1.2.3.4' : before;
|
2020-04-12 18:27:01 +02:00
|
|
|
return ip;
|
|
|
|
};
|
|
|
|
|
2020-05-19 07:50:16 +02:00
|
|
|
export const getCorrectAuth = (
|
|
|
|
auth: AuthType,
|
|
|
|
context: ContextType
|
|
|
|
): LndAuthType => {
|
|
|
|
if (auth.type === SERVER_ACCOUNT) {
|
|
|
|
const { account } = context;
|
|
|
|
if (!account || account.id !== auth.id)
|
|
|
|
throw new Error('This account is not authenticated');
|
|
|
|
|
|
|
|
const foundAccount = context.accounts.find(a => a.id === account.id);
|
|
|
|
if (!foundAccount) throw new Error('This account does not exist');
|
|
|
|
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
if (auth.type === SSO_ACCOUNT) {
|
|
|
|
if (!context.ssoVerified)
|
|
|
|
throw new Error('This account is not authenticated');
|
|
|
|
return { ...context.sso };
|
|
|
|
}
|
|
|
|
if (auth.type === CLIENT_ACCOUNT) {
|
|
|
|
const { host, macaroon, cert } = auth;
|
|
|
|
return { host, macaroon, cert };
|
|
|
|
}
|
|
|
|
throw new Error('This account type does not exist');
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getAuthLnd = (auth: LndAuthType) => {
|
2020-05-03 10:03:36 +02:00
|
|
|
const cert = auth.cert || '';
|
|
|
|
const macaroon = auth.macaroon || '';
|
2020-04-12 18:27:01 +02:00
|
|
|
const socket = auth.host || '';
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
macaroon,
|
|
|
|
socket,
|
2020-05-03 10:03:36 +02:00
|
|
|
...(cert !== '' ? { cert } : {}),
|
2020-04-12 18:27:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const { lnd } = authenticatedLndGrpc(params);
|
|
|
|
|
|
|
|
return lnd;
|
|
|
|
};
|
|
|
|
|
2020-05-01 14:08:30 +02:00
|
|
|
export const getErrorMsg = (error: any[] | string): string => {
|
|
|
|
if (typeof error === 'string') {
|
|
|
|
return error;
|
2020-04-12 18:27:01 +02:00
|
|
|
}
|
2020-05-01 14:08:30 +02:00
|
|
|
if (error.length >= 2) {
|
|
|
|
return error[1];
|
|
|
|
}
|
|
|
|
// if (error.length > 2) {
|
|
|
|
// return error[2].err?.message || 'Error';
|
|
|
|
// }
|
2020-04-12 18:27:01 +02:00
|
|
|
|
2020-05-01 14:08:30 +02:00
|
|
|
return 'Error';
|
2020-04-12 18:27:01 +02:00
|
|
|
};
|
2020-05-03 11:14:36 +02:00
|
|
|
|
|
|
|
export const to = promise => {
|
|
|
|
return promise
|
|
|
|
.then(data => {
|
|
|
|
return data;
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('%o', err);
|
|
|
|
throw new Error(getErrorMsg(err));
|
|
|
|
});
|
|
|
|
};
|