Algorithms, Blockchain and Cloud

How to Generate an Account on Tron Blockchain using Python SDK?


tron-blockchain How to Generate an Account on Tron Blockchain using Python SDK?

tron-blockchain

Account Generation Process in ETH or Tron Blockchain

Tron Blockchain, similar to ETH Blockchain, has the following Wallet Generation Process:

  1. Generate a 256-bit Random Number as private key
  2. Convert Private Key to secp256k1 uncompressed public key aka 512-bit public key
  3. Using Keccak256 to compute the hash value of Public Key, and convert to Hex String
  4. Get the last 40 characters of the Hex String, and prefix with “0x” in ETH or “TL” in Tron.

Using Python SDK to generate the Wallet in Tron Blockchain

First, make sure you have “tronpy” installed via “pip3 install tronpy”. Then run the following Python code to get the private key and wallet Address on Tron Blockchain:

from tronpy import Tron, Contract
from tronpy.keys import PrivateKey

priv_key = PrivateKey.random()
print("Private Key is ", priv_key)

account = priv_key.public_key.to_base58check_address()
print("Accuont Address is ", account)

The private key generation is based on the Random function that generates a 256-bit bytes – it is in theory possible for a collision thus the account hacked – however, extremely unlikely.

def random(cls) -> "PrivateKey":
    """Generate a random private key."""
    return cls(bytes([random.randint(0, 255) for _ in range(32)]))

The account existence could be checked by simply assuming non-zero balance on Tron Blockchain.

Creating Tron Wallets/Accounts on Shasta TestNet

We can call the API directly to Shasta TestNet to get accounts/private keys instantly via Web API:

import requests
url = "https://api.shasta.trongrid.io/wallet/generateaddress"

response = requests.request("GET", url)
print(response.text)

Example of the Returned Account Details via JSON:

{
  "privateKey":"a3c8cfa7c5adb2e9fcb7fb1a915f8336238a6223da415e1e6515926cc45efdef",
  "address":"TAL2JTQepe2pc8RBucFZkVCW7UmbzgJr3M",
  "hexAddress":"4103efcda1877de247db47d32a36e895b6f1f8c187"
}

However, the API has been deactivated for now on main-net for security concerns.

Tron Blockchain

–EOF (The Ultimate Computing & Technology Blog) —

902 words
Last Post: Go Lang Programming Exercise: Single-Row Keyboard
Next Post: Teaching Kids Programming - Two Pointer Algorithm to Rescue People in Rocketship

The Permanent URL is: How to Generate an Account on Tron Blockchain using Python SDK? (AMP Version)

Exit mobile version