5

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.

2
  • What sort of help do you need? If you can give us an idea of where your problem lies, we may be better able to help.
    – Chenmunka
    Commented May 3, 2018 at 14:51
  • i am trying to write a code that will receive xdr and able to sign and submit to stellar. i can able to decode the xdr and see the details through java sdk. But cannot able to sign and submit it. JS sdk has constructor for that where we can import xdr (var transaction = new Transaction(envelope);). Iam looking for similar kind of think in java
    – Sharmilan
    Commented May 4, 2018 at 4:39

3 Answers 3

1

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.

0
0

You can find an example in the official Stellar guide.

Make sure that to choose "Java" in code preview.

Java switch

See also basic examples for account creation here.

3
  • The question is how to sign a XDR envelop and submit. not submitting the transaction
    – Sharmilan
    Commented May 4, 2018 at 4:35
  • In JS StellarSDK the 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.
    – Orbit Lens
    Commented May 4, 2018 at 8:22
  • Yeah can do this in js. Do you know any ways to do it in go. As i saw we can create a blob using a xdr and submit to the server. But i don't know how to sign that xdr before submit
    – Sharmilan
    Commented May 4, 2018 at 10:07
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.