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