Blog

Filter posts by the Tags of Blog section!

Tron wallet, creating, siging and broadcasting

Sunday, 20 February 2022

If you have worked with blockchain technology for cryptocurrencies; the scenario of creating a wallet and broadcasting it to the network is approximately similar. Maybe they would be different in some cases in the way of broadcasting a wallet or signing.

Let’s create a wallet as offline; sign and then broadcast it to the Tron network. There are lots of libraries that create wallets for you in offline mode as it's nothing more than an algorithm. I’m gonna use TonNet as it’s so simple to use and straightforward.
 

 public class TronService
    {
        private readonly ITransactionClient _transactionClient;

        public TronService(ITransactionClient transactionClient)
        {
            _transactionClient = transactionClient;
        }

        public async Task CreateWalletAsync()
        {
            var key = TronECKey.GenerateKey(TronNetwork.MainNet);
            var address = key.GetPublicAddress();

            var privateKey = key.GetPrivateKey();
            var publicKey = key.GetPubKey();

                }

         }


As you can see, we created a wallet in Mainnet and got the wallet address, private key, and public key from the key that MainNet gave us. This wallet does not exist in the main blockchain yet as we have only created it offline. In order to make this address a real wallet in the blockchain, we should sign and then broadcast it. Let’s modify our code: 

 

 public class TronService
    {
        private readonly ITransactionClient _transactionClient;

        public TronService(ITransactionClient transactionClient)
        {
            _transactionClient = transactionClient;
        }

        public async Task CreateWalletAsync()
        {
            var key = TronECKey.GenerateKey(TronNetwork.MainNet);
            var address = key.GetPublicAddress();

            var privateKey = key.GetPrivateKey();
            var publicKey = key.GetPubKey();

           await SignAndBroadcastAsync(address);
        }

        private async Task<Return> SignAndBroadcastAsync(string toWallet)
        {
            var fromWallet = "you wallet that you have created in blockchain";
            var privateKey = "private key of your wallet";
            decimal amount = 1;

            var transactionExtension = await _transactionClient.CreateTransactionAsync(fromWallet, toWallet, (long)amount);
            var transactionSigned = _transactionClient.GetTransactionSign(transactionExtension.Transaction, privateKey);
            var result = await _transactionClient.BroadcastTransactionAsync(transactionSigned);
            return result;
        }
    }



In the second method, we should take at least 1 Tron from a wallet that we have created in blockchain before (you can create it via TonLink ) and transfer it to our newly created wallet. After this operation, our new wallet will be available in the Tron network and everybody can transfer Tron to it.

Note that, I just showed the simplest form of creating, singing, and broadcasting a wallet. You obviously don’t use such a code in the production as the security of your wallets and private keys are so vital. 
 

comments powered by Disqus