It's Bithumb api, and I'm curious about the reason for some codes.

Asked 2 years ago, Updated 2 years ago, 130 views

I'm going to use API on Bithumb, a cryptocurrency exchange.
There are many things I don't know because I'm still a beginner, so I'm looking around to find out what functions work, but there are parts that I can't interpret well about why.

The code is long, but it's not complicated, so I think people who know can read it all at once. Please don't go too far and help me. The full code is at the bottom.

It's a question.

        data = endpoint + chr(0) + str_data + chr(0) + nonce;
        utf8_data = data.encode('utf-8');

        key = self.api_secret;
        utf8_key = key.encode('utf-8');

        h = hmac.new(bytes(utf8_key), utf8_data, hashlib.sha512);
        hex_output = h.hexdigest();
        utf8_hex_output = hex_output.encode('utf-8');

        api_sign = base64.b64encode(utf8_hex_output);
        utf8_api_sign = api_sign.decode('utf-8');

Below is the full code.

#
# # XCoin API-call related functions
#
# # @author   btckorea
# # @date 2017-04-12
#
# # Compatible with python3 version.

import sys
import time
import math
import base64
import hmac, hashlib
import urllib.parse
import pycurl
import json


class XCoinAPI:
    api_url = "https://api.bithumb.com";
    api_key = "";
    api_secret = "";

    def __init__(self, api_key, api_secret):
        self.api_key = api_key;
        self.api_secret = api_secret;

    def body_callback(self, buf):
        self.contents = buf;

    def microtime(self, get_as_float = False):
        if get_as_float:
            return time.time()
        else:
            return '%f %d' % math.modf(time.time())

    def usecTime(self) :
        mt = self.microtime(False)
        mt_array = mt.split(" ")[:2];
        return mt_array[1] + mt_array[0][2:5];

    def xcoinApiCall(self, endpoint, rgParams):
        # # 1. Api-Sign and Api-Nonce information generation.
        # # 2. Request related information from the Bithumb API server.
        #
        # # - nonce: it is an arbitrary number that may only be used once.
        # # - api_sign: API signature information created in various combinations values.

        endpoint_item_array = {
            "endpoint" : endpoint
        };

        uri_array = dict(endpoint_item_array, **rgParams); # Concatenate the two arrays.

        str_data = urllib.parse.urlencode(uri_array);

        nonce = self.usecTime();

        data = endpoint + chr(0) + str_data + chr(0) + nonce;
        utf8_data = data.encode('utf-8');

        key = self.api_secret;
        utf8_key = key.encode('utf-8');

        h = hmac.new(bytes(utf8_key), utf8_data, hashlib.sha512);
        hex_output = h.hexdigest();
        utf8_hex_output = hex_output.encode('utf-8');

        api_sign = base64.b64encode(utf8_hex_output);
        utf8_api_sign = api_sign.decode('utf-8');


        curl_handle = pycurl.Curl();
        curl_handle.setopt(pycurl.POST, 1);
        #curl_handle.setopt(pycurl.VERBOSE, 1); # vervose mode :: 1 => True, 0 => False
        curl_handle.setopt(pycurl.POSTFIELDS, str_data);

        url = self.api_url + endpoint;
        curl_handle.setopt(curl_handle.URL, url);
        curl_handle.setopt(curl_handle.HTTPHEADER, ['Api-Key: ' + self.api_key, 'Api-Sign: ' + utf8_api_sign, 'Api-Nonce: ' + nonce]);
        curl_handle.setopt(curl_handle.WRITEFUNCTION, self.body_callback);
        curl_handle.perform();

        #response_code = curl_handle.getinfo(pycurl.RESPONSE_CODE); # Get http response status code.

        curl_handle.close();

        return (json.loads(self.contents));

api encoding python encryption

2022-09-21 14:45

1 Answers

If chr(0), the ascii code value would be NULL.

It is assumed that null is used to distinguish endpoint, str_data, and nonce.

I think the utf-8 conversion is caused by using the UTF-8 encoding format where it is received. HMAC I think is Hash based Message Authentication Code.

Communication will need encryption, and it seems to have keys and signs based on HASH.


2022-09-21 14:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.