Giter Site home page Giter Site logo

erkansukgen / bnb-tx-python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from trusttoken/bnb-tx-python

1.0 0.0 0.0 44 KB

A simple module for creating, manipulating, signing, and decoding Binance Chain transactions

License: GNU Lesser General Public License v3.0

Python 100.00%

bnb-tx-python's Introduction

Binance Chain Transaction Library

This library provides a low-level interface for interacting with different Binance Chain transactions.

Installation

pip3 install bnb-tx

Or, in your Pipfile:

bnb-tx = "==0.0.4"

Usage

The following is an example that uses the ecdsa library to sign.

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

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

Not all transaction types are supported. Please consult this table for details. If you need support, please submit a pull request.

Message Type encode decode from_obj
Send
NewOrder
CancelOrder
Issue
Mint
Burn
Freeze
Unfreeze
TimeLock
TimeUnlock
TimeRelock
Proposal
Vote

Contributing

See CONTRIBUTING.

License

LGPLv3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.