Skip to main content

binance-decoder

Motivation

Binance Chain using Amino logic in the transaction encoding, I have looked for decoder in Python but can't find properly solution for that and I made this Python package using https://github.com/bnb-chain/node-binary/issues/53 Github discussion.

Pre-Installation

Please make sure you have up-to-date pip and setuptools

python -m pip install --upgrade pip
python -m pip install --upgrade setuptools

Install Package

You can use PyPi to get binance-decoder package.

pip install binance-decoder

Usage

from binance_decoder import binance_decoder
import json

encoded_tx = "6wHwYl3uCnHObcBDChSIhrF81iDjeLmz0sHwkVU0WIR5xRIwODg4NkIxN0NENjIwRTM3OEI5QjNEMkMxRjA5MTU1MzQ1ODg0NzlDNS0xOTE2MDI1GhFCQUtFLTVFMF9CVVNELUJEMSACKAEwlNqqEDiAjI7jgB5AARJyCibrWumHIQOhYvTqdPrZQxXuEW4VhRdTqhAHi1kbnJUagWQGVOCqqxJAvLXcvsQuGGOfnS+aw9+6/DBpn7ptHU3Tgz406JelZetV4P0icmXOlu530maz7+VlwFtoTuQKZTQ2rjac57a1OxjCjwEg+Ph0"

decoded_tx_str = binance_decoder(encoded_tx)  # Decode transaction, return type will be `str`
decoded_tx_json = json.loads(decoded_tx_str)

About

First version of project was just use binance-tx-decoder binary from Python writen by Golang. But here we got some issue related on OS arg parse limitations.

Example

  • we had binance-tx-decoder binary file writen by Golang
package main

import (
	"crypto/sha256"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"github.com/binance-chain/go-sdk/types"
	"github.com/binance-chain/go-sdk/types/tx"
	"os"
)

type Transaction struct {
	Hash string   `json:"hash"`
	Data tx.StdTx `json:"data"`
}

func main() {
	txs := os.Args[1:]

	codec := types.NewCodec()
	parsedTxs := make([]Transaction, len(txs))
	for i := range txs {
		decodeTx64, err := base64.StdEncoding.DecodeString(txs[i])
		if err != nil {
			fmt.Println("error:", err)
		}

		decodeTxStr := string(decodeTx64)

		h := sha256.New()
		h.Write(decodeTx64)
		parsedTxs[i].Hash = hex.EncodeToString(h.Sum(nil))

		err = codec.UnmarshalBinaryLengthPrefixed([]byte(decodeTxStr), &parsedTxs[i].Data)
		if err != nil {
			fmt.Println("Error - codec unmarshal")
		}
	}

	bz, err := json.Marshal(parsedTxs)

	if err != nil {
		fmt.Println("Error - json marshal")

	}
	fmt.Println(string(bz))
}
  • used following code to call binance-tx-decoder from Python code
import os
import json


txs = [
  "2AHwYl3uCl4WbmgbChTXj1deyGieL/2pir/GvjCJzzoRZRIRUlVORS1CMUFfQlVTRC1CRDEaL0Q3OEY1NzVFQzg2ODlFMkZGREE5OEFCRkM2QkUzMDg5Q0YzQTExNjUtMjc3MzM4EnIKJuta6YchAof/uuUFK7rjP6j+XolHCm6iwEI6OL9pay27Td50xFE8EkAry+YVeHb/KYo6ptBKcxmFv5uIZtpTHw4QrajK3dntKAZ1UkpV62jfUVNPVj01fUQdrAe5QBrSLRjRL4nwJn8GGIqeJyDb9hA=",
  "7AHwYl3uCnHObcBDChRlmTns6WEaqmu7vqLF/cM5jKgDmBIwNjU5OTM5RUNFOTYxMUFBQTZCQkJCRUEyQzVGREMzMzk4Q0E4MDM5OC0zODk4MjQzGhFBVE9NLTU5Nl9CVVNELUJEMSACKAEwuIjOsgM4gJK4rCJAARJzCibrWumHIQPzxSPM0ZHrLoHz0G83k77S+g0WlS/14u2TT9Oj9IrCAxJAU+D1OiN9DbULcOqAdZ//sXq3ZIgdZ4DrL9N883JOpr9EN2IXe5tvwyPzbi6XtvsQo5chI2vzp/jULs1dQhkzoBiEgy8ggvftAQ==",
]

cmd = f'./binance-tx-decoder {" ".join(txs)}'

with os.popen(cmd) as proc:
    transactions = json.loads(proc.read())
    
print(ransactions)

Finally, I had some idea to check is there way to make Python Package by Golang. This is Open source Python Package and everyone can take to use.

Metadata

Release files for binance-decoder 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for binance-decoder 0.0.5
File Size Uploaded Provenance
binance-decoder-0.0.5.tar.gz 5.2 kB Without provenance Details

Release files / binance-decoder-0.0.5.tar.gz

Download URL binance-decoder-0.0.5.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2dc2dc088baebfd4f04a8beff86afc85547ad4480fbe9215f191a01897aebfd0
BLAKE2b-256 checksum
How to use checksums
61c2f3327de6fff6981808f265351aafb19d0a0ff22022a14f73207d02f573cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Security for binance-decoder 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page