Skip to content

Commit

Permalink
usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
wjmelements committed Aug 3, 2019
1 parent 999c6be commit 484fb91
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ __pycache__
# Packaging
build/
dist/
*.egg-info

# Pipenv
.venv
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ verify_ssl = true

[dev-packages]
pytest = "~=4.0"
flake8 = "==3.6"
flake8 = "==3.7"


[packages]
Expand Down
27 changes: 17 additions & 10 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions binance_transaction/__init__.py
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion binance_transaction/bnb_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
12 changes: 12 additions & 0 deletions binance_transaction/dex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down
5 changes: 4 additions & 1 deletion binance_transaction/gov.py
Original file line number Diff line number Diff line change
@@ -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
)


"""
Expand All @@ -8,6 +10,7 @@
Their message type usually starts with cosmos-sdk/*
* Proposal (cosmos-sdk/MsgSubmitProposal)
* Vote
"""


Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

setuptools.setup(
name="binance_transaction",
version="0.0.1",
version="0.0.3",
author="William Morriss",
author_email="[email protected]",
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",
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

max-line-length = 120
exclude = build,.venv
per-file-ignores =
binance_transaction/__init__.py:F401

0 comments on commit 484fb91

Please sign in to comment.