I received the XDR envelope of a transaction. I need to sign it and submit using Java SDK.
I am able to decode the XDR, but need help on how to sign and submit.
I received the XDR envelope of a transaction. I need to sign it and submit using Java SDK.
I am able to decode the XDR, but need help on how to sign and submit.
The following code will do the trick:
import java.io.IOException;
import org.stellar.sdk.KeyPair;
import org.stellar.sdk.Network;
import org.stellar.sdk.Server;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.responses.SubmitTransactionResponse;
public class TransactionSigningMain {
public static void main(String[] args) throws IOException {
Network.useTestNetwork();
Server server = new Server("https://horizon-testnet.stellar.org");
String base64XDREnvelope = "SOME_VALID_XDR_ENVELOPE";
Transaction tx = Transaction.fromEnvelopeXdr(base64XDREnvelope);
KeyPair yourPrivateKey = KeyPair.fromSecretSeed("SCZANGBA5YHTNYVVV4C3U252E2B6P6F5T3U6MM63WBSBZATAQI3EBTQ4");
tx.sign(yourPrivateKey);
try {
SubmitTransactionResponse response = server.submitTransaction(tx);
System.out.println("Success!");
System.out.println(response);
} catch (Exception e) {
System.err.println("Something went wrong!");
System.err.println(e.getMessage());
}
}
}
Essentially, you use take your base64 encoded XDR envelope String, and use Transaction.fromEnvelopeXdr
to serialize it back into a Transaction object. From there, you can use the Transaction.sign
method to add an additional signature, and use Server.submitTransaction
to submit the transaction, as shown in the Stellar Dev Getting Started Guide.
You can find an example in the official Stellar guide.
Make sure that to choose "Java" in code preview.
See also basic examples for account creation here.
Transaction
object has a public constructor that accepts transaction envelope XDR. So you can easily reconstruct a Transaction
. But Java SDK lacks this functionality, so I even don't know where to start here.
Commented
May 4, 2018 at 8:22
Have a look at my kotlin wrapper. It uses the official sdk and I have lots of tests that do things like creating assets, trustlines, doing payments, and placing offers.
Intended for kotlin users but should be usable from Java as well.