· 5 years ago · Mar 02, 2020, 09:20 AM
1"use strict";
2const crypto = require('crypto');
3const _ = require('lodash');
4const axios = require("axios");
5const API_KEY = '<API_KEY>';
6const API_SECRET = '<SECRET_KEY>';
7class Tcdx{
8
9 constructor(sandbox) {
10 this.key = API_KEY;
11 this.secret = API_SECRET;
12
13 this.baseUrl = `http://147.139.135.182`;
14 }
15
16 createRequestConfig({ key, secret, payload }) {
17
18 const encodedPayload = JSON.stringify(payload);
19
20 const signature = crypto
21 .createHmac(`sha256`, secret)
22 .update(encodedPayload)
23 .digest(`hex`);
24
25 return {
26 headers: {
27 'X-TC-APIKEY': key,
28 'X-TC-SIGNATURE': signature,
29 },
30 };
31 }
32
33 requestPrivate(endpoint, method, params = {}) {
34 if (!this.key || !this.secret) {
35 throw new Error(
36 `API key and secret key required to use authenticated methods`);
37 }
38
39 const requestPath = `/${endpoint}`;
40 const requestUrl = `${this.baseUrl}${requestPath}`;
41 let payload = {
42 timestamp: Date.now()
43 };
44 payload = _.assign(payload, params);
45 const config = this.createRequestConfig({
46 payload,
47 key: this.key,
48 secret: this.secret,
49 });
50
51 if (method === 'post'){
52 return new Promise(function (resolve, reject) {
53 axios.post(requestUrl, {}, config)
54 .then(function (response) {
55 resolve(response.data);
56 }).catch(function (err) {
57 reject(err.response.data.message);
58 });
59 });
60 }else if (method === 'get'){
61 return new Promise(function (resolve, reject) {
62 axios.get(requestUrl, config)
63 .then(function (response) {
64 resolve(response.data);
65 }).catch(function (err) {
66 reject(err.response.data.message);
67 });
68 });
69 }
70
71 }
72
73 async getKyc() {
74 try {
75 let account = await this.requestPrivate('/kyc', 'get',{});
76 return account;
77 } catch (error) {
78 throw error;
79 }
80 }
81}
82
83module.exports = new Tcdx();
84
85async function main(){
86 let kycData= await module.exports.getKyc();
87 console.log(kycData);
88}
89
90main();