Please help me creating a new account in a private Stellar network. The existing documentation is surprisingly confusing to follow!

There is zero documentation in the javadoc of the Java SDK to create an account. The example mentioned claims that the SDK does not support test network. This is surprising! Given that, all requests route to the Horizon server as HTTP requests, why should it matter whether one connects to a test, main or private network? Sure, there is the friendly bot; but, the bot should be used only for funding not for account creation.

Then, the example mentions that, "you will have to make your own HTTP request". ok, where are the details - endpoint, body, parameters, etc.? Also, it is not clear, if the request made is a POST as a new resource is to be added as an account in the network.

Moving on to the REST API reference. Do I have to build the payload with an id, links to previous and successor just so that I can create an account? I don't recall coming across such an interface to create a new resource. At best, the HTTP POST verb should use the following payload at an end-point as /accounts

{ 
"account": "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ",
"funder": "GBIA4FH6TV64KSPDAJCNUQSM7PFL4ILGUVJDPCLUOPJ7ONMKBBVUQHRO",
"starting_balance": "10000.0",  
}

and respond with a JSON somewhat like this.

{"transactionId" : "9430630328265928"}

Javadoc: https://stellar.github.io/java-stellar-sdk/
Example in SDK: https://www.stellar.org/developers/guides/get-started/create-account.html
REST API: https://www.stellar.org/developers/horizon/reference/resources/operation.html#create-account

jed

The SDK does support the testnet.

For transactions, yes. But not for creating accounts; as mentioned in the example in this page. https://www.stellar.org/developers/guides/get-started/create-account.html

BTW, the Java example for submitting transactions, uses KeyPair.fromSecretSeed("string") and KeyPair.fromPublicKey("string") whereas, the javadoc says that the argument should be a byte[]. So, I had to use getBytes() for the strings to get past the compilation error.

StellarSdk.Network("name of your network");

Did you mean Network.use() for other networks? When I tried this, I get a compilation error as, The constructor Network(String) is not visible. That is how the useTestNetwork() and usePublicNetwork() methods are written. https://github.com/stellar/java-stellar-sdk/blob/master/src/main/java/org/stellar/sdk/Network.java

private String PRIVATE_NETWORK = System.getenv("NETWORK_PASSPHRASE");
Network.use(new Network(PRIVATE_NETWORK));

And, how does one use curl to create an account?

jed

BTW, the example provided in your link will work only if the account already existed, right? I used the following modification..

var StellarSdk = require('stellar-sdk');

var HORIZON_ENDPOINT = process.env.HORIZON_ENDPOINT;
var NETWORK_PASSPHRASE = process.env.NETWORK_PASSPHRASE;

var fromWallet = process.argv[2];
var toWallet = process.argv[3];
var xferAmount = process.argv[4];

StellarSdk.Network.use(new StellarSdk.Network(NETWORK_PASSPHRASE));
var opts = new StellarSdk.Config.setAllowHttp(true);
var server = new StellarSdk.Server(HORIZON_ENDPOINT, opts);
var sourceKeys = StellarSdk.Keypair.fromSecret(fromWallet);
var destinationId = toWallet;

server.loadAccount(sourceKeys.publicKey())
  .then(function(sourceAccount) {
    var transaction = new StellarSdk.TransactionBuilder(sourceAccount)
      .addOperation(StellarSdk.Operation.payment({
      destination: destinationId,
      asset: StellarSdk.Asset.native(),
      amount: xferAmount
    }))
      .addMemo(StellarSdk.Memo.text('Test Transaction'))
      .build();
      
  transaction.sign(sourceKeys);
  return server.submitTransaction(transaction);
})
  .then(function(result) {
    console.log('Success! Results:', result);
  })
  .catch(function(error) {
    console.error('Something went wrong!', error);
});

I get a HTTP 400 status; exploring....

    chainhead

    For the 400 error, the XDR for transaction envelop is as below.

    tx
    sourceAccount: [publicKeyTypeEd25519]
        ed25519: GAC...L7C
    fee: 100
    seqNum: 1
    timeBounds: none
    memo: [memoText]
        text: Test Transaction
    operations: Array[1]
        [0]
        sourceAccount: none
        body: [payment]
            paymentOp
                destination: [publicKeyTypeEd25519]
                    ed25519: GBH...FL3
                asset: [assetTypeNative]
                amount: 25.36 (raw: 253600000)
    signatures: Array[1]
        [0]
            hint: G______________________________________________2JKHC____
            signature: PVg...9DQ==

    And, the result XDR as below

    TransactionResult
      feeCharged: 100
      result: [txFailed]
        results: Array[1]
          [0]: [opInner]
            tr: [payment]
              paymentResult: [paymentNoDestination]

    Well, obviously, the destination does not exist; that is the point of making this transaction!

    a month later

    when sending to non-existing accounts (unfunded) you need to use the StellarSdk.Operation.createAccount function example:

                     return StellarSdk.Operation.createAccount({
                       destination: destination.value,
                       startingBalance: fix7dec(amount.value)
                     });
                   }
      8 months later

      sacarlson You seem to have made some significant progress with stellar using Java. Do you have some setup documentation you can share. I have a private network setup with in my basement using a virtualized environment that would love to use for my e2e testing. I have Core, Horizon, Bridge, Federation and Compliance services all working but I can't seem to create and account and start making transactions even on the test environment. Please help if you can, thanks in advance.

      Horizon is complaining that my account does not exit though it is created and funded on the test region.

      level=error msg="Account does not exist" accountID=GBW3EZBZKRERB4JUDWGQPIBGHKJ4XPOFG2VQ2WTFR4F7TYC5WS7F3XGR service=Horizon

      Horizon instance is on the test network.