bittrade-kraken-rest


Namebittrade-kraken-rest JSON
Version 0.13.15 PyPI version JSON
download
home_pagehttps://github.com/TechSpaceAsia/bittrade-kraken-rest
SummaryKraken REST library
upload_time2024-08-02 07:41:31
maintainerNone
docs_urlNone
authorMatt Kho
requires_python<4.0,>=3.10
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ELM Bittrade's Kraken REST package & optional CLI

## Install

`pip install bittrade-kraken-rest` or `poetry add bittrade-kraken-rest`

Not all Kraken endpoints are implemented yet.

# Public endpoints

```python
from bittrade_kraken_rest import get_server_time

server_time = get_server_time().run()
print(server_time) # GetServerTimeResult(unixtime=1673053481, rfc1123='Sat, 07 Jan 23 01:04:41 +0000')
```

*The above example is complete, it should run as is*

Bring Your Own ~~Credentials~~ Signature (Private endpoints)
---

TLDR; Don't agree to pass your API secret to third-party code; instead sign the requests yourself, with your own code. It's safer.

This library doesn't want to ever access your Kraken secret keys.

Most libraries expect you to provide your api key and secret. I'm not comfortable doing that with third-party code, even open sourced.

Here instead, the library prepares the request, which you then sign using your own code and the library finishes the job. It has NO access to your secret.

Thankfully this is quite straightforward: you need to implement a `sign(x: tuple[PreparedRequest, str, dict[str, Any]]) -> PreparedRequest` method which sets the correct headers. Below is an example of such a signature function:

```python
from os import getenv
import urllib, hmac, base64, hashlib
from pathlib import Path

# Taken from https://docs.kraken.com/rest/#section/Authentication/Headers-and-Signature
def generate_kraken_signature(urlpath, data, secret):
    post_data = urllib.parse.urlencode(data)
    encoded = (str(data["nonce"]) + post_data).encode()
    message = urlpath.encode() + hashlib.sha256(encoded).digest()
    mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
    signature_digest = base64.b64encode(mac.digest())
    return signature_digest.decode()

# Here the key/secret are loaded from a .gitignored folder, but you can use environment variables or other method of configuration
def sign(x: tuple[PreparedRequest, str, dict[str, Any]]):
    request, url, data = x
    request.headers["API-Key"] = Path("./config_local/key").read_text()
    request.headers["API-Sign"] = generate_kraken_signature(
        url, data, Path("./config_local/secret").read_text()
    )
    return request

```

With that in place, a observable pipe will get you the result you need:


```python
from bittrade_kraken_rest import get_websockets_token_request, get_websockets_token_result
from reactivex import operators

result = get_websockets_token_request().pipe(
    operators.map(sign),
    get_websockets_token_result()
).run()
```

### Full example with signature function

```python
from os import getenv
import urllib, hmac, base64, hashlib
from pathlib import Path
from bittrade_kraken_rest import get_websockets_token_request, get_websockets_token_result
from reactivex import operators

# Taken from https://docs.kraken.com/rest/#section/Authentication/Headers-and-Signature
def generate_kraken_signature(urlpath, data, secret):
    post_data = urllib.parse.urlencode(data)
    encoded = (str(data["nonce"]) + post_data).encode()
    message = urlpath.encode() + hashlib.sha256(encoded).digest()
    mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
    signature_digest = base64.b64encode(mac.digest())
    return signature_digest.decode()

# Here the key/secret are loaded from a .gitignored folder, but you can use environment variables or other method of configuration
def sign(x: tuple[PreparedRequest, str, dict[str, Any]]):
    request, url, data = x
    request.headers["API-Key"] = Path("./config_local/key").read_text()
    request.headers["API-Sign"] = generate_kraken_signature(
        url, data, Path("./config_local/secret").read_text()
    )
    return request

result = get_websockets_token_request().pipe(
    operators.map(sign),
    get_websockets_token_result()
).run()

```

*The above example is complete, it should run as is*

### Observables

The above examples use `.run()` to trigger the observable subscription but Observables make it very easy to create pipes, retries and more. All operators can be found on the [RxPy read the docs](https://rxpy.readthedocs.io/en/latest/).

## Tests

```
pytest
```

Note that integration tests require a valid key/secret pair saved as `key` and `secret` files in a `.config_local` folder placed at the root of the repo.

## CLI


The CLI has been moved to [its own repo](https://github.com/TechSpaceAsia/bittrade-kraken-cli)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/TechSpaceAsia/bittrade-kraken-rest",
    "name": "bittrade-kraken-rest",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Matt Kho",
    "author_email": "matt@techspace.asia",
    "download_url": "https://files.pythonhosted.org/packages/a3/3d/6ef26676948952f71a3682384109f88a233d6423510b1de15705a020f059/bittrade_kraken_rest-0.13.15.tar.gz",
    "platform": null,
    "description": "# ELM Bittrade's Kraken REST package & optional CLI\n\n## Install\n\n`pip install bittrade-kraken-rest` or `poetry add bittrade-kraken-rest`\n\nNot all Kraken endpoints are implemented yet.\n\n# Public endpoints\n\n```python\nfrom bittrade_kraken_rest import get_server_time\n\nserver_time = get_server_time().run()\nprint(server_time) # GetServerTimeResult(unixtime=1673053481, rfc1123='Sat, 07 Jan 23 01:04:41 +0000')\n```\n\n*The above example is complete, it should run as is*\n\nBring Your Own ~~Credentials~~ Signature (Private endpoints)\n---\n\nTLDR; Don't agree to pass your API secret to third-party code; instead sign the requests yourself, with your own code. It's safer.\n\nThis library doesn't want to ever access your Kraken secret keys.\n\nMost libraries expect you to provide your api key and secret. I'm not comfortable doing that with third-party code, even open sourced.\n\nHere instead, the library prepares the request, which you then sign using your own code and the library finishes the job. It has NO access to your secret.\n\nThankfully this is quite straightforward: you need to implement a `sign(x: tuple[PreparedRequest, str, dict[str, Any]]) -> PreparedRequest` method which sets the correct headers. Below is an example of such a signature function:\n\n```python\nfrom os import getenv\nimport urllib, hmac, base64, hashlib\nfrom pathlib import Path\n\n# Taken from https://docs.kraken.com/rest/#section/Authentication/Headers-and-Signature\ndef generate_kraken_signature(urlpath, data, secret):\n    post_data = urllib.parse.urlencode(data)\n    encoded = (str(data[\"nonce\"]) + post_data).encode()\n    message = urlpath.encode() + hashlib.sha256(encoded).digest()\n    mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)\n    signature_digest = base64.b64encode(mac.digest())\n    return signature_digest.decode()\n\n# Here the key/secret are loaded from a .gitignored folder, but you can use environment variables or other method of configuration\ndef sign(x: tuple[PreparedRequest, str, dict[str, Any]]):\n    request, url, data = x\n    request.headers[\"API-Key\"] = Path(\"./config_local/key\").read_text()\n    request.headers[\"API-Sign\"] = generate_kraken_signature(\n        url, data, Path(\"./config_local/secret\").read_text()\n    )\n    return request\n\n```\n\nWith that in place, a observable pipe will get you the result you need:\n\n\n```python\nfrom bittrade_kraken_rest import get_websockets_token_request, get_websockets_token_result\nfrom reactivex import operators\n\nresult = get_websockets_token_request().pipe(\n    operators.map(sign),\n    get_websockets_token_result()\n).run()\n```\n\n### Full example with signature function\n\n```python\nfrom os import getenv\nimport urllib, hmac, base64, hashlib\nfrom pathlib import Path\nfrom bittrade_kraken_rest import get_websockets_token_request, get_websockets_token_result\nfrom reactivex import operators\n\n# Taken from https://docs.kraken.com/rest/#section/Authentication/Headers-and-Signature\ndef generate_kraken_signature(urlpath, data, secret):\n    post_data = urllib.parse.urlencode(data)\n    encoded = (str(data[\"nonce\"]) + post_data).encode()\n    message = urlpath.encode() + hashlib.sha256(encoded).digest()\n    mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)\n    signature_digest = base64.b64encode(mac.digest())\n    return signature_digest.decode()\n\n# Here the key/secret are loaded from a .gitignored folder, but you can use environment variables or other method of configuration\ndef sign(x: tuple[PreparedRequest, str, dict[str, Any]]):\n    request, url, data = x\n    request.headers[\"API-Key\"] = Path(\"./config_local/key\").read_text()\n    request.headers[\"API-Sign\"] = generate_kraken_signature(\n        url, data, Path(\"./config_local/secret\").read_text()\n    )\n    return request\n\nresult = get_websockets_token_request().pipe(\n    operators.map(sign),\n    get_websockets_token_result()\n).run()\n\n```\n\n*The above example is complete, it should run as is*\n\n### Observables\n\nThe above examples use `.run()` to trigger the observable subscription but Observables make it very easy to create pipes, retries and more. All operators can be found on the [RxPy read the docs](https://rxpy.readthedocs.io/en/latest/).\n\n## Tests\n\n```\npytest\n```\n\nNote that integration tests require a valid key/secret pair saved as `key` and `secret` files in a `.config_local` folder placed at the root of the repo.\n\n## CLI\n\n\nThe CLI has been moved to [its own repo](https://github.com/TechSpaceAsia/bittrade-kraken-cli)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Kraken REST library",
    "version": "0.13.15",
    "project_urls": {
        "Homepage": "https://github.com/TechSpaceAsia/bittrade-kraken-rest",
        "Repository": "https://github.com/TechSpaceAsia/bittrade-kraken-rest"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cff3a9dbad79ec96ecacdc9a13ab685b43dfdef0b0ddf7a12c7fb19f93fe8eef",
                "md5": "d7720dd98b3461b876a060ba62d286fd",
                "sha256": "b16e84ef24ea8e76a1cd34d952319cc6c6fd514326a260433e95aaab18b48c6e"
            },
            "downloads": -1,
            "filename": "bittrade_kraken_rest-0.13.15-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d7720dd98b3461b876a060ba62d286fd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 24083,
            "upload_time": "2024-08-02T07:41:29",
            "upload_time_iso_8601": "2024-08-02T07:41:29.556802Z",
            "url": "https://files.pythonhosted.org/packages/cf/f3/a9dbad79ec96ecacdc9a13ab685b43dfdef0b0ddf7a12c7fb19f93fe8eef/bittrade_kraken_rest-0.13.15-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a33d6ef26676948952f71a3682384109f88a233d6423510b1de15705a020f059",
                "md5": "e58c7bc0439d95d3ee86f344110b0313",
                "sha256": "b879bbfe36ebaaf023e27ecab54018c3dd06ef41c40660516265fa6e1faf57f2"
            },
            "downloads": -1,
            "filename": "bittrade_kraken_rest-0.13.15.tar.gz",
            "has_sig": false,
            "md5_digest": "e58c7bc0439d95d3ee86f344110b0313",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 13350,
            "upload_time": "2024-08-02T07:41:31",
            "upload_time_iso_8601": "2024-08-02T07:41:31.135634Z",
            "url": "https://files.pythonhosted.org/packages/a3/3d/6ef26676948952f71a3682384109f88a233d6423510b1de15705a020f059/bittrade_kraken_rest-0.13.15.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-02 07:41:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "TechSpaceAsia",
    "github_project": "bittrade-kraken-rest",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "bittrade-kraken-rest"
}
        
Elapsed time: 0.40605s