farukterzioglu / HDWallet

A generic HD wallet for Elliptic Curve (Secp256k1) and Edwards-curve (Ed25519) based crypto currencies like Bitcoin, Ethereum, Cosmos, Tezos, Tron, Cardano, Polkadot, Avalanche, FileCoin and more.
MIT License
79 stars 36 forks source link

Tron Signature #10

Closed artem03 closed 3 years ago

artem03 commented 3 years ago

In the documentation of the Tron it is written: Note: The size of the signature result is 65 bytes. https://tronprotocol.github.io/documentation-en/mechanism-algorithm/account/#signature The sign method returns a 68-byte signature to you, if you use your signature method to send a regular transfer, everything will work correctly, but if you use it for TRC 20 TriggerSmartContract when you broadcast, we will get a "SIGERROR" @farukterzioglu Could you fix this? This is the only library I know on C # that allows you to sign a transaction offline, it would be great if we could use it to sign TriggerSmartContract

farukterzioglu commented 3 years ago

@artem03 right now I am using this library to create&sign&send TRC20 TriggerSmartContract transactions without problem. Did you try to send any? I will check if there was any custom modification for TriggerSmartContract on my side, but as I remember I done nothing regarding to it.

artem03 commented 3 years ago

@farukterzioglu I was trying to send a TRC20 TriggerSmartContract for USDT just today, and got a SIGERROR error when broadcasting.

farukterzioglu commented 3 years ago

Let me check more

artem03 commented 3 years ago

Yes of course, maybe I made a mistake somewhere, but I've checked my code many times, maybe I'm just doing something wrong

artem03 commented 3 years ago

@farukterzioglu Did you find any "custom modification" for the TRC20 TriggerSmartContract sign?

farukterzioglu commented 3 years ago

@artem03 Just checked and saw they are same, there is no difference between signing TRX transaction and TRC20 transaction. Both signs the txid and adds the result to the signatures field. Both signatures is 68 bytes and this works in Tron Mainnet and Shasta.

artem03 commented 3 years ago

@farukterzioglu Oh, sorry, I sent a transaction to the broadcast without the fee_limit field, after adding this field everything worked, the problem can be closed, I wanted to ask if you know how to decode the data field (to get the recipient and the amount you received) when receiving the transaction block

farukterzioglu commented 3 years ago

@artem03 normal TRX transaction or TRC20 transaction?

farukterzioglu commented 3 years ago

I guess you are asking about this

Amount = Convert.ToInt64(transactionEvent.Result.Value),
From = transactionEvent.Result.From.Replace("0x", "41").ToVisibleAddress(),
To = transactionEvent.Result.To.Replace("0x", "41").ToVisibleAddress(),

with

using NBitcoin.DataEncoders;

public static string ToVisibleAddress(this string tronAddress)
        {
            if (string.IsNullOrWhiteSpace(tronAddress))
            {
                return null;
            }

            byte[] bytes = tronAddress.FromHexToByteArray();
            return Encoders.Base58Check.EncodeData(bytes);
        }

public static byte[] FromHexToByteArray(this string input)
        {
            var numberChars = input.Length;
            var bytes = new byte[numberChars / 2];
            for (var i = 0; i < numberChars; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(input.Substring(i, 2), 16);
            }
            return bytes;
        }   
artem03 commented 3 years ago

Not sure what exactly I need, I asked about Data in TriggerSmartContract, I use the following code to encode the data

public class TestParamsInput
    {
        [Parameter("address", 1)]
        public string First { get; set; }
        [Parameter("uint256", 2)]
        public UInt64 Second { get; set; }
    }
var abiEncode = new ABIEncode();
var data = abiEncode.GetABIParamsEncoded<TestParamsInput>(new TestParamsInput() { First = "address", Second = UInt64.Parse("amount") });

But I don't know how to decode them

farukterzioglu commented 3 years ago

can you paste the encoded data here

artem03 commented 3 years ago

"00000000000000000000000033a53245269c4706e378e7c58788bfb47c8d4fbd00000000000000000000000000000000000000000000000000000000000f4240"

farukterzioglu commented 3 years ago

not sure this what you want but you can check this request: https://api.trongrid.io/event/transaction/f87d6f7973895f1d8de5dd7bbbf23b4a9fcb7ae8961378b556147df3d42e2386?visible=true

artem03 commented 3 years ago

Thanks, it will help me

farukterzioglu commented 3 years ago

to decode that kind of data, I use Nethereum heavily

some code to give an idea:

_contractBuilder = new ContractBuilder(abi, contractAddressVisible);
_contract = new Nethereum.Contracts.Contract(null, abi, contractAddressVisible);

...

public Function GetFunction(string functionName)
        {
            return _contract.GetFunction(functionName);
        }

public TReponse DecodeSimpleResponse<TReponse>(string functionName, string data)
        {
            Function function = GetFunction(functionName);
            return function.DecodeSimpleTypeOutput<TReponse>(data);
        }
sinaptech commented 2 years ago

May you please help me with some c# code how to transfer Trc20 contract balance to another trx wallet? both creating the transaction signing and broadcasting to the network

willbetter10 commented 2 years ago

to decode that kind of data, I use Nethereum heavily

some code to give an idea:

_contractBuilder = new ContractBuilder(abi, contractAddressVisible);
_contract = new Nethereum.Contracts.Contract(null, abi, contractAddressVisible);

...

public Function GetFunction(string functionName)
        {
            return _contract.GetFunction(functionName);
        }

public TReponse DecodeSimpleResponse<TReponse>(string functionName, string data)
        {
            Function function = GetFunction(functionName);
            return function.DecodeSimpleTypeOutput<TReponse>(data);
        }

Hey, I have the similar problems, might need your help. Can you do me a favor?