tronprotocol / java-tron

Java implementation of the Tron whitepaper
GNU Lesser General Public License v3.0
3.74k stars 1.41k forks source link

How to properly sign a transaction? #6077

Open mcqwy opened 4 days ago

mcqwy commented 4 days ago

Hello, due to network issues, I have been unable to access the API documentation I have created a transaction using api.trongrid.io/wallet/triggersmartcontract. Return data: { "visible": true, "txID": "fbc18aa61f0ef6feed8683d44fe9b854bcc256430a4c2ad194d71d23dcdc3cb1", "raw_data": { "contract": [{ "parameter": { "value": { "data": "a9059cbb000000000000000000000000986759cc653c16cfd3e6d7f3f9421c947aa4ca7100000000000000000000000000000000000000000000000000000000000003e8", "owner_address": "TVtwXQg4dRii7BBXAojGMb1sqiiyt4zg9p", "contract_address": "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" }, "type_url": "type.googleapis.com/protocol.TriggerSmartContract" }, "type": "TriggerSmartContract" }], "ref_block_bytes": "2695", "ref_block_hash": "c8ca42772393529d", "expiration": 1731612804000, "fee_limit": 50000000, "timestamp": 1731612745991 }, "raw_data_hex": "0a0226952208c8ca42772393529d40a087aae1b2325aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a1541da93eeb8f001b49b9603a67c83dc738d529ac4d1121541a614f803b6fd780986a42c78ec9c7f77e6ded13c2244a9059cbb000000000000000000000000986759cc653c16cfd3e6d7f3f9421c947aa4ca7100000000000000000000000000000000000000000000000000000000000003e87087c2a6e1b232900180e1eb17" } After signing, I will use it https://api.trongrid.io/wallet/broadcasttransaction radio broadcast. code

Here is the returned data. {"code":"SIGERROR","txid":"ba9fb59c54cd1fd1d0baf5a0957d6af345dab46d92976a60179e9010ebaabf80","message":"Validate signature error: cff19378604ad6464edca050dfd35b33bac5d0b33ad3adc9fa939f8b0c80c9782669f3271bd7e45a11eefdb1f521efdd4556518f3c3f5bd6cb7933c97b69558500 is signed by THpoiicz3X6wtnPRJ5KkpQgauAjvYMEcJ4 but it is not contained of permission."} Based on the returned data, it can be analyzed that the problem is likely caused by incorrect signatures. Here are the incorrect methods I tried to use to create a transaction. code1 System.out.println(transaction.toString()); raw_data { contract { type: TriggerSmartContract parameter { type_url: "type.googleapis.com/protocol.TriggerSmartContract" value: "8888" } } } The value: "8888" here is different from the data returned through HTTP, and I cannot manually fill the returned JSON data into Transaction How can I use JSON correctly https://api.trongrid.io/wallet/broadcasttransaction Interface for broadcasting transactions? Or how can I correctly use ECKey to sign the Transaction class? My English is not very good, I hope you can understand Please help me, thank you

DongDongSunny commented 4 days ago

Hi Mcqwy, I understand use offline sign is not very straightforward, I would suggest you use wallet-cli to import your private key and do signature for you, please refer the usage here , it is a simple warper of java-tron api, it will skip the mannul do signature part. Also you could check the source code of wallet-cli and check how it do sign if you really want to know the detail.

DongDongSunny commented 4 days ago

if you trace the sendcoin logic, you will find the https://github.com/tronprotocol/wallet-cli/blob/develop/src/main/java/org/tron/walletserver/WalletApi.java#L459 give you exact detail of signTransaction

mcqwy commented 2 days ago

if you trace the sendcoin logic, you will find the https://github.com/tronprotocol/wallet-cli/blob/develop/src/main/java/org/tron/walletserver/WalletApi.java#L459 give you exact detail of signTransaction

After several days of effort, my problem was solved through other means However, thank you very much for your response.