편집 기록

편집 기록
  • 프로필 알 수 없는 사용자님의 편집
    날짜2018.06.21

    빗썸 api인데요, 몇몇 코드의 이유가 궁금합니다.


    가상화폐 거래소인 빗썸에서 api를 사용해보려 합니다.
    아직 초보인지라 모르는 부분이 많아, 여기저기 찾아가며 어떤 함수가 무슨 작동을 하는지 찾아보고는 있지만, 그 이유에 대해서 잘 해석이 안되는 부분이 있습니다.

    코드가 길지만 복잡한 내용이 아니라, 아시는 분들은 한번에 쭉 읽으실 수 있을 것 같은데요.. 부디 지나치지 마시고 도움좀 부탁드립니다. 전체 코드는 맨 아래에 있습니다.

    질문입니다.

    1. xcoinApiCall함수 안에서, data라는 변수를 만드는데 거기서 chr(0)이 하는 역할이 뭔지 궁금합니다.
    2. 그 아래를 쭉 보시면 다음과 같은 코드들이 있습니다. 왜 이런 작업들을 해주는지 잘 모르겠는데 이 부분 자세한 설명 부탁드립니다 ㅠㅠ 뭐 하나 할 때마다 자꾸 utf-8로 바꿔주고.. 인코딩하고 디코딩하고 왜 이런 복잡한 작업이 필요한지 잘 모르겠습니다.
            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');
    

    아래는 전체 코드입니다.

    #
    # 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));