Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch 20241018 #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import datetime
import time
from urllib.parse import urljoin

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization

from alpha.utils.request import AsyncHttpRequests
from alpha.const import USER_AGENT

Expand All @@ -34,9 +38,10 @@ class HuobiCoinFutureRestAccountAPI:
passphrase: API KEY Passphrase.
"""

def __init__(self, host, access_key, secret_key):
def __init__(self, host, access_key, secret_key,sign):
""" initialize REST API client. """
self._host = host
self._sign = sign
self._access_key = access_key
self._secret_key = secret_key

Expand Down Expand Up @@ -382,10 +387,18 @@ async def request(self, method, uri, params=None, body=None, headers=None, auth=
if auth:
timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
params = params if params else {}
params.update({"AccessKeyId": self._access_key,
if(self._sign=="256"):

params.update({"AccessKeyId": self._access_key,
"SignatureMethod": "HmacSHA256",
"SignatureVersion": "2",
"Timestamp": timestamp})
else:
params.update({"AccessKeyId": self._access_key,
"SignatureMethod": "Ed25519",
"SignatureVersion": "2",
"Timestamp": timestamp})


params["Signature"] = self.generate_signature(method, params, uri)

Expand All @@ -412,18 +425,52 @@ async def request(self, method, uri, params=None, body=None, headers=None, auth=
return result, None

def generate_signature(self, method, params, request_path):
if request_path.startswith("http://") or request_path.startswith("https://"):
host_url = urllib.parse.urlparse(request_path).hostname.lower()
request_path = '/' + '/'.join(request_path.split('/')[3:])
if self._sign=="256":
if request_path.startswith("http://") or request_path.startswith("https://"):
host_url = urllib.parse.urlparse(request_path).hostname.lower()
request_path = '/' + '/'.join(request_path.split('/')[3:])
else:
host_url = urllib.parse.urlparse(self._host).hostname.lower()

sorted_params = sorted(params.items(), key=lambda d: d[0], reverse=False)
encode_params = urllib.parse.urlencode(sorted_params)

payload = [method, host_url, request_path, encode_params]
payload = "\n".join(payload)
payload = payload.encode(encoding="UTF8")
secret_key = self._secret_key.encode(encoding="utf8")
digest = hmac.new(secret_key, payload, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest)
signature = signature.decode()
return signature
else:
host_url = urllib.parse.urlparse(self._host).hostname.lower()
sorted_params = sorted(params.items(), key=lambda d: d[0], reverse=False)
encode_params = urllib.parse.urlencode(sorted_params)
payload = [method, host_url, request_path, encode_params]
payload = "\n".join(payload)
payload = payload.encode(encoding="UTF8")
secret_key = self._secret_key.encode(encoding="utf8")
digest = hmac.new(secret_key, payload, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest)
signature = signature.decode()
return signature
if request_path.startswith("http://") or request_path.startswith("https://"):
host_url = urllib.parse.urlparse(request_path).hostname.lower()
request_path = '/' + '/'.join(request_path.split('/')[3:])
else:
host_url = urllib.parse.urlparse(self._host).hostname.lower()

sorted_params = sorted(params.items(), key=lambda d: d[0], reverse=False)
encode_params = urllib.parse.urlencode(sorted_params)

payload = [method, host_url, request_path, encode_params]
payload = "\n".join(payload)
payload = payload.encode(encoding="UTF-8")

# 从 PEM 格式的私钥加载 Ed25519 密钥
private_key = serialization.load_pem_private_key(
self._secret_key.encode(),
password=None,
backend=default_backend()
)

# 使用 Ed25519 签名
signature = private_key.sign(payload)

# 将签名编码为 Base64
signature_b64 = base64.b64encode(signature).decode()
return signature_b64




Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
Date: 2020/09/10
Email: [email protected]
"""

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
import gzip
import json
import copy
Expand All @@ -18,6 +19,10 @@
import datetime
import time
from urllib.parse import urljoin

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization

from alpha.utils.request import AsyncHttpRequests
from alpha.const import USER_AGENT

Expand All @@ -34,9 +39,10 @@ class HuobiCoinFutureRestMarketAPI:
passphrase: API KEY Passphrase.
"""

def __init__(self, host, access_key, secret_key):
def __init__(self, host, access_key, secret_key,sign):
""" initialize REST API client. """
self._host = host
self._sign = sign
self._access_key = access_key
self._secret_key = secret_key

Expand Down Expand Up @@ -204,10 +210,17 @@ async def request(self, method, uri, params=None, body=None, headers=None, auth=
if auth:
timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
params = params if params else {}
params.update({"AccessKeyId": self._access_key,
"SignatureMethod": "HmacSHA256",
"SignatureVersion": "2",
"Timestamp": timestamp})
if (self._sign == "256"):

params.update({"AccessKeyId": self._access_key,
"SignatureMethod": "HmacSHA256",
"SignatureVersion": "2",
"Timestamp": timestamp})
else:
params.update({"AccessKeyId": self._access_key,
"SignatureMethod": "Ed25519",
"SignatureVersion": "2",
"Timestamp": timestamp})

params["Signature"] = self.generate_signature(method, params, uri)

Expand All @@ -234,18 +247,46 @@ async def request(self, method, uri, params=None, body=None, headers=None, auth=
return result, None

def generate_signature(self, method, params, request_path):
if request_path.startswith("http://") or request_path.startswith("https://"):
host_url = urllib.parse.urlparse(request_path).hostname.lower()
request_path = '/' + '/'.join(request_path.split('/')[3:])
if self._sign == "256":
if request_path.startswith("http://") or request_path.startswith("https://"):
host_url = urllib.parse.urlparse(request_path).hostname.lower()
request_path = '/' + '/'.join(request_path.split('/')[3:])
else:
host_url = urllib.parse.urlparse(self._host).hostname.lower()
sorted_params = sorted(params.items(), key=lambda d: d[0], reverse=False)
encode_params = urllib.parse.urlencode(sorted_params)
payload = [method, host_url, request_path, encode_params]
payload = "\n".join(payload)
payload = payload.encode(encoding="UTF8")
secret_key = self._secret_key.encode(encoding="utf8")
digest = hmac.new(secret_key, payload, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest)
signature = signature.decode()
return signature
else:
host_url = urllib.parse.urlparse(self._host).hostname.lower()
sorted_params = sorted(params.items(), key=lambda d: d[0], reverse=False)
encode_params = urllib.parse.urlencode(sorted_params)
payload = [method, host_url, request_path, encode_params]
payload = "\n".join(payload)
payload = payload.encode(encoding="UTF8")
secret_key = self._secret_key.encode(encoding="utf8")
digest = hmac.new(secret_key, payload, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest)
signature = signature.decode()
return signature
if request_path.startswith("http://") or request_path.startswith("https://"):
host_url = urllib.parse.urlparse(request_path).hostname.lower()
request_path = '/' + '/'.join(request_path.split('/')[3:])
else:
host_url = urllib.parse.urlparse(self._host).hostname.lower()

sorted_params = sorted(params.items(), key=lambda d: d[0], reverse=False)
encode_params = urllib.parse.urlencode(sorted_params)

payload = [method, host_url, request_path, encode_params]
payload = "\n".join(payload)
payload = payload.encode(encoding="UTF-8")

# 从 PEM 格式的私钥加载 Ed25519 密钥
private_key = serialization.load_pem_private_key(
self._secret_key.encode(),
password=None,
backend=default_backend()
)

# 使用 Ed25519 签名
signature = private_key.sign(payload)

# 将签名编码为 Base64
signature_b64 = base64.b64encode(signature).decode()
return signature_b64
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from urllib.parse import urljoin
from alpha.utils.request import AsyncHttpRequests
from alpha.const import USER_AGENT

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
__all__ = ("HuobiCoinFutureRestReferenceAPI",)


Expand All @@ -34,9 +35,10 @@ class HuobiCoinFutureRestReferenceAPI:
passphrase: API KEY Passphrase.
"""

def __init__(self, host, access_key, secret_key):
def __init__(self, host, access_key, secret_key, sign):
""" initialize REST API client. """
self._host = host
self._sign = sign
self._access_key = access_key
self._secret_key = secret_key

Expand Down Expand Up @@ -336,10 +338,17 @@ async def request(self, method, uri, params=None, body=None, headers=None, auth=
if auth:
timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
params = params if params else {}
params.update({"AccessKeyId": self._access_key,
"SignatureMethod": "HmacSHA256",
"SignatureVersion": "2",
"Timestamp": timestamp})
if (self._sign == "256"):

params.update({"AccessKeyId": self._access_key,
"SignatureMethod": "HmacSHA256",
"SignatureVersion": "2",
"Timestamp": timestamp})
else:
params.update({"AccessKeyId": self._access_key,
"SignatureMethod": "Ed25519",
"SignatureVersion": "2",
"Timestamp": timestamp})

params["Signature"] = self.generate_signature(method, params, uri)

Expand All @@ -366,18 +375,46 @@ async def request(self, method, uri, params=None, body=None, headers=None, auth=
return result, None

def generate_signature(self, method, params, request_path):
if request_path.startswith("http://") or request_path.startswith("https://"):
host_url = urllib.parse.urlparse(request_path).hostname.lower()
request_path = '/' + '/'.join(request_path.split('/')[3:])
if self._sign == "256":
if request_path.startswith("http://") or request_path.startswith("https://"):
host_url = urllib.parse.urlparse(request_path).hostname.lower()
request_path = '/' + '/'.join(request_path.split('/')[3:])
else:
host_url = urllib.parse.urlparse(self._host).hostname.lower()
sorted_params = sorted(params.items(), key=lambda d: d[0], reverse=False)
encode_params = urllib.parse.urlencode(sorted_params)
payload = [method, host_url, request_path, encode_params]
payload = "\n".join(payload)
payload = payload.encode(encoding="UTF8")
secret_key = self._secret_key.encode(encoding="utf8")
digest = hmac.new(secret_key, payload, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest)
signature = signature.decode()
return signature
else:
host_url = urllib.parse.urlparse(self._host).hostname.lower()
sorted_params = sorted(params.items(), key=lambda d: d[0], reverse=False)
encode_params = urllib.parse.urlencode(sorted_params)
payload = [method, host_url, request_path, encode_params]
payload = "\n".join(payload)
payload = payload.encode(encoding="UTF8")
secret_key = self._secret_key.encode(encoding="utf8")
digest = hmac.new(secret_key, payload, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest)
signature = signature.decode()
return signature
if request_path.startswith("http://") or request_path.startswith("https://"):
host_url = urllib.parse.urlparse(request_path).hostname.lower()
request_path = '/' + '/'.join(request_path.split('/')[3:])
else:
host_url = urllib.parse.urlparse(self._host).hostname.lower()

sorted_params = sorted(params.items(), key=lambda d: d[0], reverse=False)
encode_params = urllib.parse.urlencode(sorted_params)

payload = [method, host_url, request_path, encode_params]
payload = "\n".join(payload)
payload = payload.encode(encoding="UTF-8")

# 从 PEM 格式的私钥加载 Ed25519 密钥
private_key = serialization.load_pem_private_key(
self._secret_key.encode(),
password=None,
backend=default_backend()
)

# 使用 Ed25519 签名
signature = private_key.sign(payload)

# 将签名编码为 Base64
signature_b64 = base64.b64encode(signature).decode()
return signature_b64
Loading