Skip to main content

Binance decoder

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

binance-decoder-0.0.5.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file binance-decoder-0.0.5.tar.gz.

File metadata

  • Download URL: binance-decoder-0.0.5.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for binance-decoder-0.0.5.tar.gz
Algorithm Hash digest
SHA256 2dc2dc088baebfd4f04a8beff86afc85547ad4480fbe9215f191a01897aebfd0
MD5 249d2970ef22823a4f38025cd650519b
BLAKE2b-256 61c2f3327de6fff6981808f265351aafb19d0a0ff22022a14f73207d02f573cb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page