Refactor utils

This commit is contained in:
Nabarun 2024-07-27 18:34:21 +05:30
parent be9ec7a252
commit e46462ef20
3 changed files with 36 additions and 35 deletions

View File

@ -1,6 +1,7 @@
import { Arguments } from 'yargs';
import dotenv from 'dotenv';
import { sumsubAxios } from '../../..';
import { sumsubAxios } from '../../../utils';
dotenv.config();

View File

@ -1,47 +1,15 @@
import express from 'express';
import axios, { InternalAxiosRequestConfig } from 'axios';
import dotenv from 'dotenv';
import FormData from 'form-data';
import crypto from 'crypto';
import assert from 'assert';
import cors from 'cors';
import { sumsubAxios } from './utils';
dotenv.config();
const SUMSUB_BASE_URL = 'https://api.sumsub.com';
const DEFAULT_KYC_LEVEL = process.env.KYC_LEVEL || 'basic-kyc-level';
const DEFAULT_TOKEN_TTL_IN_SECS = process.env.TTL_IN_SECS || 600;
const sumsubAxios = axios.create();
sumsubAxios.interceptors.request.use(createSignature, function (error) {
return Promise.reject(error);
})
export { sumsubAxios };
// This function creates signature for the request as described here: https://developers.sumsub.com/api-reference/#app-tokens
function createSignature(config: InternalAxiosRequestConfig) {
assert(config.method);
console.log('Creating a signature for the request...');
var ts = Math.floor(Date.now() / 1000);
const signature = crypto.createHmac('sha256', process.env.SUMSUB_SECRET_KEY || "");
signature.update(ts + config.method.toUpperCase() + config.url);
if (config.data instanceof FormData) {
signature.update(config.data.getBuffer());
} else if (config.data) {
signature.update(config.data);
}
config.headers['X-App-Access-Ts'] = ts;
config.headers['X-App-Access-Sig'] = signature.digest('hex');
return config;
}
// https://developers.sumsub.com/api-reference/#access-tokens-for-sdks
function createAccessToken (externalUserId: string, levelName: string, ttlInSecs: number) {
const config: any = { baseURL: SUMSUB_BASE_URL};

32
src/utils.ts Normal file
View File

@ -0,0 +1,32 @@
import axios, { InternalAxiosRequestConfig } from 'axios';
import FormData from 'form-data';
import crypto from 'crypto';
import assert from 'assert';
const sumsubAxios = axios.create();
sumsubAxios.interceptors.request.use(createSignature, function (error) {
return Promise.reject(error);
})
export { sumsubAxios };
// This function creates signature for the request as described here: https://developers.sumsub.com/api-reference/#app-tokens
function createSignature(config: InternalAxiosRequestConfig) {
assert(config.method);
var ts = Math.floor(Date.now() / 1000);
const signature = crypto.createHmac('sha256', process.env.SUMSUB_SECRET_KEY || "");
signature.update(ts + config.method.toUpperCase() + config.url);
if (config.data instanceof FormData) {
signature.update(config.data.getBuffer());
} else if (config.data) {
signature.update(config.data);
}
config.headers['X-App-Access-Ts'] = ts;
config.headers['X-App-Access-Sig'] = signature.digest('hex');
return config;
}