Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create transaction with API then sign a transaction offline and then broadcast using API #135

Open
CoticiGabi opened this issue Aug 6, 2024 · 3 comments

Comments

@CoticiGabi
Copy link

I want to create a transaction by using api call https://api.nile.trongrid.io/wallet/createtransaction , which gives me this response for example :

{"visible":false,"txID":"ca3d16a01baa0cc7506d1ce08d6f741b1b085c7f597d6f518758c5cd1ebe9355","raw_data":{"contract":[{"parameter":{"value":{"amount":1,"owner_address":"4120a913a920deb99f6e3a98724712e2c0447db89f","to_address":"41e2a881d0c71612624ba204ebb9a559a751e2852a"},"type_url":"type.googleapis.com/protocol.TransferContract"},"type":"TransferContract"}],"ref_block_bytes":"38f2","ref_block_hash":"97436e309100ba8c","expiration":1722934221000,"timestamp":1722934163579},"raw_data_hex":"0a0238f2220897436e309100ba8c40c8b187b792325a65080112610a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412300a154120a913a920deb99f6e3a98724712e2c0447db89f121541e2a881d0c71612624ba204ebb9a559a751e2852a180170fbf083b79232"}

Then I want to sign the transaction offline using the trident library. From what I saw in other closed issues, you recommend to use this :
public Chain.Transaction signTransaction(Response.TransactionExtention txnExt, KeyPair keyPair) {
byte[] txid = txnExt.getTxid().toByteArray();
byte[] signature = KeyPair.signTransaction(txid, keyPair);
Chain.Transaction signedTxn = txnExt.getTransaction().toBuilder().addSignature(ByteString.copyFrom(signature)).build();
return signedTxn;
}
Which uses a TransactionExtension object, that i do not get from the /createtransaction API response, and I didn't find a way to construct it from the API response.

My question is : How can I create a trasnaction using the API , then sign it using the trident sdk and then broadcast it using the API ?

@endiaoekoe
Copy link
Contributor

How can I create a trasnaction using the API , then sign it using the trident sdk and then broadcast it using the API ?

@CoticiGabi
basically, you can refer to this doc which detailed a normal routine for sending a transaction(including:Create -> Sign -> Broadcast -> (wait) -> Lookup and get receipt)
byw, TransactionExtension struct is the newer version of Transaction struct which contains more response infos ,and it is preferred to use TransactionExtension struct.

@CoticiGabi
Copy link
Author

Hello @endiaoekoe ,

I tried following the doc that you gave me, i created and signed the transaction using ApiWrapper class. But the signature is in ByteString format, when i transform it in hex and then call the Trongrid API to broadcast with the provided signature I keep getting the following error :

{"code":"SIGERROR","txid":"e8b4c08c29a1a19d10c39511aeca2f68c67be6b0f24de5d6a165a58eb9dd210a","message":"Validate signature error: 53db352cee757e299d2cd851a9cc31b99b7fe471162e5aa66bbc952a6938fc9960845838bd2fcaf7b31305a36158f29d097471b77989b3dcb8c5534556feec7400 is signed by random_trx_address but it is not contained of permission."}
But when I broadcast the transaction using the ApiWrapper method broadcastTransaction() it works, so the address that signs the transactions is my address, the correct one, not a random trx address like the error message form the API.

Can I use ApiWrapper with an API like Trongrid ? I see that it has constructors only for grpc full nodes.
And how can I use trident SDK with the API ? I see that the objects in the sdk (Transaction, TransactionExtention etc.) are not compatible with the responses given by the API.

@endiaoekoe
Copy link
Contributor

endiaoekoe commented Aug 12, 2024

@CoticiGabi
here is a code example for sending tx;
if you want to use trongrid API , try 'grpc.shasta.trongrid.io:50051' in shata testnet and 'grpc.trongrid.io:50051' in mainnet and 'grpc.nile.trongrid.io:50051' in nile testnet which configed in this file.
by the way,make sure to configure the right API endpoint ;
hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants