Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- const crypto = require('crypto');
- const _ = require('lodash');
- const axios = require("axios");
- const API_KEY = '<API_KEY>';
- const API_SECRET = '<SECRET_KEY>';
- class Tokocrypto {
- constructor(sandbox) {
- this.key = API_KEY;
- this.secret = API_SECRET;
- this.baseUrl = `https://api.tokocrypto.com`;
- }
- createRequestConfig({ key, secret, payload }) {
- const encodedPayload = (new Buffer(JSON.stringify(payload)))
- .toString(`base64`);
- const signature = crypto
- .createHmac(`sha384`, secret)
- .update(encodedPayload)
- .digest(`hex`);
- return {
- headers: {
- 'X-TC-APIKEY': key,
- 'X-TC-PAYLOAD': encodedPayload,
- 'X-TC-SIGNATURE': signature,
- },
- };
- }
- requestPrivate(endpoint, method, params = {}) {
- if (!this.key || !this.secret) {
- throw new Error(
- `API key and secret key required to use authenticated methods`);
- }
- const requestPath = `/v1${endpoint}`;
- const requestUrl = `${this.baseUrl}${requestPath}`;
- let payload = {
- nonce: Date.now()
- };
- payload = _.assign(payload, params);
- const config = this.createRequestConfig({
- payload,
- key: this.key,
- secret: this.secret,
- });
- if (method === 'post'){
- return new Promise(function (resolve, reject) {
- axios.post(requestUrl, {}, config)
- .then(function (response) {
- resolve(response.data);
- }).catch(function (err) {
- reject(err.response.data.message);
- });
- });
- }else if (method === 'get'){
- return new Promise(function (resolve, reject) {
- axios.get(requestUrl, config)
- .then(function (response) {
- resolve(response.data);
- }).catch(function (err) {
- reject(err.response.data.message);
- });
- });
- }
- }
- async account() {
- try {
- let account = await this.requestPrivate('/account', 'get',{});
- return account;
- } catch (error) {
- throw error;
- }
- }
- }
- module.exports = new Tokocrypto();
- async function main(){
- let account = await module.exports.account();
- console.log(account);
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement