From 484fb91c10fe0ee9d334fddbbecbe7aa4817a404 Mon Sep 17 00:00:00 2001 From: William Morriss Date: Fri, 2 Aug 2019 17:32:04 -0700 Subject: [PATCH] usage example --- .gitignore | 1 + Pipfile | 2 +- Pipfile.lock | 27 +++++++++++++++--------- README.md | 29 ++++++++++++++++++++++++-- binance_transaction/__init__.py | 6 ++++++ binance_transaction/bnb_transaction.py | 2 +- binance_transaction/dex.py | 12 +++++++++++ binance_transaction/gov.py | 5 ++++- setup.py | 3 ++- tox.ini | 2 ++ 10 files changed, 73 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 9fe3640..02b286f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ __pycache__ # Packaging build/ dist/ +*.egg-info # Pipenv .venv diff --git a/Pipfile b/Pipfile index 93bb783..acb4124 100644 --- a/Pipfile +++ b/Pipfile @@ -6,7 +6,7 @@ verify_ssl = true [dev-packages] pytest = "~=4.0" -flake8 = "==3.6" +flake8 = "==3.7" [packages] diff --git a/Pipfile.lock b/Pipfile.lock index 3d653c2..ec3157a 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "4b1b63a805b556107f8d3cec6fef711d70760b6df5cbad35a943843e8f11530f" + "sha256": "e6af1ab13c70b5addbe36b4f8b8401738770f3899011556de07a2492a8a7f012" }, "pipfile-spec": 6, "requires": { @@ -40,13 +40,20 @@ ], "version": "==19.1.0" }, + "entrypoints": { + "hashes": [ + "sha256:589f874b313739ad35be6e0cd7efde2a4e9b6fea91edcc34e58ecbb8dbe56d19", + "sha256:c70dd71abe5a8c85e55e12c19bd91ccfeec11a6e99044204511f9ed547d48451" + ], + "version": "==0.3" + }, "flake8": { "hashes": [ - "sha256:6a35f5b8761f45c5513e3405f110a86bea57982c3b75b766ce7b65217abe1670", - "sha256:c01f8a3963b3571a8e6bd7a4063359aff90749e160778e03817cd9b71c9e07d2" + "sha256:2baac1c277d917f3e01ce17a60dcfad4a4ce13a2c5d50a15d0811302a3bdf7aa", + "sha256:7eda8e5c29ac9e0d3c0fd44649298c29768efc79a9b872b41c8f05ca5f502c83" ], "index": "pypi", - "version": "==3.6" + "version": "==3.7" }, "importlib-metadata": { "hashes": [ @@ -93,17 +100,17 @@ }, "pycodestyle": { "hashes": [ - "sha256:cbc619d09254895b0d12c2c691e237b2e91e9b2ecf5e84c26b35400f93dcfb83", - "sha256:cbfca99bd594a10f674d0cd97a3d802a1fdef635d4361e1a2658de47ed261e3a" + "sha256:95a2219d12372f05704562a14ec30bc76b05a5b297b21a5dfe3f6fac3491ae56", + "sha256:e40a936c9a450ad81df37f549d676d127b1b66000a6c500caa2b085bc0ca976c" ], - "version": "==2.4.0" + "version": "==2.5.0" }, "pyflakes": { "hashes": [ - "sha256:9a7662ec724d0120012f6e29d6248ae3727d821bba522a0e6b356eff19126a49", - "sha256:f661252913bc1dbe7fcfcbf0af0db3f42ab65aabd1a6ca68fe5d466bace94dae" + "sha256:17dbeb2e3f4d772725c777fabc446d5634d1038f234e77343108ce445ea69ce0", + "sha256:d976835886f8c5b31d47970ed689944a0262b5f3afa00a5a7b4dc81e5449f8a2" ], - "version": "==2.0.0" + "version": "==2.1.1" }, "pyparsing": { "hashes": [ diff --git a/README.md b/README.md index 7374a5f..beeaf49 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,35 @@ This library provides a low-level interface for interacting with different Binan TODO ## Usage +The following is an example that uses the `ecdsa` library to sign. + ```python3 -from binance_transaction import BnbTransaction +import ecdsa +def uncompressed_public_key(sk): + """ Derive uncompressed public key """ + order = sk.curve.generator.order() + p = sk.verifying_key.pubkey.point + x_str = ecdsa.util.number_to_string(p.x(), order) + y_str = ecdsa.util.number_to_string(p.y(), order) + uncompressed = b'\x04' + x_str + y_str + return uncompressed + + +from binance_transaction import BnbTransaction, NewOrder, address_bytes, BUY, GTE, LIMIT_ORDER -# TODO +sk = ecdsa.SigningKey.from_pem(open('secp256k1-key.pem').read()) +from_address = 'bnb100dxzy02a6k7vysc5g4kk4fqamr7jhjg4m83l0' +account_number = 96025 # https://docs.binance.org/api-reference/dex-api/paths.html#apiv1accountaddress +sequence_number = 888 +tx = BnbTransaction(account_number, sequence_number) +order_id = f'{address_bytes(from_address).hex().upper()}-{sequence_number + 1}' +tx.add_msg(NewOrder(from_address, order_id, 'BNB_TUSDB-888', LIMIT_ORDER, BUY, 3500000000, 500000000, GTE)) +print(tx.signing_json()) +sig = sk.sign_digest(tx.signing_hash()) +public_key = uncompressed_public_key(sk) +tx.apply_sig(sig, public_key) +signed_transaction_bytes = tx.encode() +print(f'Signed bytes: {signed_transaction_bytes.hex()}') ``` ## Support diff --git a/binance_transaction/__init__.py b/binance_transaction/__init__.py index 25d999c..f3fb98f 100644 --- a/binance_transaction/__init__.py +++ b/binance_transaction/__init__.py @@ -1,6 +1,12 @@ from .bech32 import bech32_encode, address_bytes from .base import Amino, Repeated, String, Address, StringVarInt, StringToken, Token, VarInt, make_prefix from .crypto import uncompress_key, compress_key, verify_sig +from .bnb_transaction import BnbTransaction, TestBnbTransaction +from .dex import DexList, NewOrder, CancelOrder, BUY, SELL, GTE, IOC, LIMIT_ORDER +from .gov import Proposal, Vote +from .token import Send, Issue, Mint, Burn, Freeze, Unfreeze, TimeLock, TimeUnlock, TimeRelock +from .msg import Msg +from .signature import BnbSignature, PubKeySecp256k1 name = "binance_transaction" diff --git a/binance_transaction/bnb_transaction.py b/binance_transaction/bnb_transaction.py index 3eb5428..3442838 100644 --- a/binance_transaction/bnb_transaction.py +++ b/binance_transaction/bnb_transaction.py @@ -27,7 +27,7 @@ class BnbTransaction(Amino): def chain_id(): return String("Binance-Chain-Tigris") - def __init__(self, account_number, sequence, source): + def __init__(self, account_number, sequence, source='887'): dict.__init__( self, account_number=StringVarInt(account_number), diff --git a/binance_transaction/dex.py b/binance_transaction/dex.py index 7258af9..9b68e78 100644 --- a/binance_transaction/dex.py +++ b/binance_transaction/dex.py @@ -14,6 +14,18 @@ """ +# side +BUY = 1 +SELL = 2 + +# timeinforce +GTE = 1 +IOC = 3 + +# ordertype +LIMIT_ORDER = 2 + + class DexList(Amino): def __init__(self, from_address, proposal_id, base_asset_symbol, quote_asset_symbol, init_price): dict.__init__( diff --git a/binance_transaction/gov.py b/binance_transaction/gov.py index ac303df..22ab7fd 100644 --- a/binance_transaction/gov.py +++ b/binance_transaction/gov.py @@ -1,4 +1,6 @@ -from binance_transaction.base import Amino, Repeated, String, Address, StringVarInt, StringToken, Token, VarInt, make_prefix +from binance_transaction.base import ( + Amino, Repeated, String, Address, StringVarInt, StringToken, Token, VarInt, make_prefix +) """ @@ -8,6 +10,7 @@ Their message type usually starts with cosmos-sdk/* * Proposal (cosmos-sdk/MsgSubmitProposal) +* Vote """ diff --git a/setup.py b/setup.py index 5919f31..1ef9408 100644 --- a/setup.py +++ b/setup.py @@ -5,10 +5,11 @@ setuptools.setup( name="binance_transaction", - version="0.0.1", + version="0.0.3", author="William Morriss", author_email="wjmelements@gmail.com", description="Binance Chain Transactions", + install_requires=['ecdsa'], long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/trusttoken/bnb-tx-python", diff --git a/tox.ini b/tox.ini index d4e7569..bdc3925 100644 --- a/tox.ini +++ b/tox.ini @@ -2,3 +2,5 @@ max-line-length = 120 exclude = build,.venv +per-file-ignores = + binance_transaction/__init__.py:F401