diff --git a/src/cli/cmds/sumsub-cmds/applicant-details.ts b/src/cli/cmds/sumsub-cmds/applicant-details.ts index 9496639..90ce2c3 100644 --- a/src/cli/cmds/sumsub-cmds/applicant-details.ts +++ b/src/cli/cmds/sumsub-cmds/applicant-details.ts @@ -1,6 +1,7 @@ import { Arguments } from 'yargs'; import dotenv from 'dotenv'; -import { sumsubAxios } from '../../..'; + +import { sumsubAxios } from '../../../utils'; dotenv.config(); diff --git a/src/index.ts b/src/index.ts index 6098b5c..923129d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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}; diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..2d5caf6 --- /dev/null +++ b/src/utils.ts @@ -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; +}