|
| 1 | +--- |
| 2 | +title: Create an Account |
| 3 | +--- |
| 4 | + |
| 5 | +# Create an Account |
| 6 | + |
| 7 | +The first thing you’ll need to do anything on Stellar is an account. Accounts hold all your money inside Stellar and allow you to send and receive payments—in fact, pretty much everything in Stellar is in some way tied to an account. |
| 8 | + |
| 9 | +Every Stellar account has a pair of public and private keys. Stellar uses public key cryptography in order to ensure that every transaction is secure. The private key is a secret piece of information that proves you own the account, so you should never share it with anyone. It’s kind of like the combination to a lock—anyone who knows it can open the lock. In the same way, anyone who knows your account’s secret key can control your account. The public key is the way other people identify your account and verify that you authorized a transaction. |
| 10 | + |
| 11 | +Because the private key must be kept secret, the first step in creating an account is creating your own keys (when you create the account, you’ll only send the public key to a Stellar server). You can do so with the following command: |
| 12 | + |
| 13 | +<example name="Generating Keys"> |
| 14 | +```sh |
| 15 | +# You'll need to install stellar-core for this step. |
| 16 | +$ stellar-core --genseed |
| 17 | +Secret seed: SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7 |
| 18 | +Public: GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB |
| 19 | +``` |
| 20 | + |
| 21 | +```js |
| 22 | +// create a completely new and unique pair of keys |
| 23 | +// see more about KeyPair objects: https://stellar.github.io/js-stellar-sdk/Keypair.html |
| 24 | +var pair = StellarSdk.Keypair.random(); |
| 25 | + |
| 26 | +pair.seed(); |
| 27 | +// SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7 |
| 28 | +pair.accountId(); |
| 29 | +// GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB |
| 30 | +``` |
| 31 | + |
| 32 | +```java |
| 33 | +// create a completely new and unique pair of keys see more: |
| 34 | +// https://stellar.github.io/java-stellar-sdk/org/stellar/sdk/KeyPair.html |
| 35 | +import org.stellar.sdk.*; |
| 36 | +KeyPair pair = KeyPair.random(); |
| 37 | + |
| 38 | +pair.getSecretSeed(); |
| 39 | +// SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7 |
| 40 | +pair.getAccountId(); |
| 41 | +// GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB |
| 42 | +``` |
| 43 | +</example> |
| 44 | + |
| 45 | +[TODO: should this only show if viewing the JS/Java examples?] |
| 46 | +You might notice that, in Stellar’s SDKs, you ask for the *account ID* instead of the public key. That’s because an account’s ID is its public key[1]. |
| 47 | + |
| 48 | +Now that you have a pair of keys, you can make an account. In order to prevent people from making a huge number of unnecessary accounts, each account must have a minimum balance of 20 lumens (lumens are the built-in currency of the Stellar network).[2] Since you don’t yet have any lumens, though, you can’t pay for an account! In the real world, you’ll usually pay an exchange that sells lumens in order to create a new account.[3] On Stellar’s test network, however, you can ask Friendbot, our friendly robot with a very fat wallet, to create an account for you. |
| 49 | + |
| 50 | +To create a test account, send Friendbot the public key you created. It’ll create and pay for a new account using that public key as its ID. |
| 51 | + |
| 52 | +<example name="Creating a test account"> |
| 53 | +```sh |
| 54 | +$ curl "https://horizon-testnet.stellar.org/friendbot?addr=GDGOKHIRX63EIAXKVI77BQV7LMDUH7DR4BMDDU77DJUXLPUU5HAXGN64" |
| 55 | +``` |
| 56 | + |
| 57 | +```js |
| 58 | +// The SDK does not have tools for creating test accounts, so you'll have to |
| 59 | +// make your own HTTP request. |
| 60 | +var request = require('request'); |
| 61 | +request.get({ |
| 62 | + url: 'https://horizon-testnet.stellar.org/friendbot', |
| 63 | + qs: { addr: pair.accountId() }, |
| 64 | + json: true |
| 65 | +}, function(error, response, body) { |
| 66 | + if (error || response.statusCode !== 200) { |
| 67 | + console.error('ERROR!', error || body); |
| 68 | + } |
| 69 | + else { |
| 70 | + console.log('SUCCESS! You have a new account :)\n', body); |
| 71 | + } |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +```java |
| 76 | +// The SDK does not have tools for creating test accounts, so you'll have to |
| 77 | +// make your own HTTP request. |
| 78 | +import java.net.*; |
| 79 | +import java.io.*; |
| 80 | +import java.util.*; |
| 81 | + |
| 82 | +String friendbotUrl = String.format( |
| 83 | + "https://horizon-testnet.stellar.org/friendbot?addr=%s", |
| 84 | + pair.getAccountId()); |
| 85 | +InputStream response = new URL(friendbotUrl).openStream(); |
| 86 | +String body = new Scanner(response, "UTF-8").useDelimiter("\\A").next(); |
| 87 | +System.out.println("SUCCESS! You have a new account :)\n" + body); |
| 88 | +``` |
| 89 | +</example> |
| 90 | + |
| 91 | +Now for last step: getting the account’s details and checking its balance! Accounts can carry multiple balances—one for each type of currency they hold. |
| 92 | + |
| 93 | +<example name="Getting account details"> |
| 94 | +```sh |
| 95 | +curl "https://horizon-testnet.stellar.org/accounts/GCRDH24DCTMKRL3SESRQ4QRKHJ56XGAJBQHHXXED3RTBQTBC36RCX4JI" |
| 96 | +``` |
| 97 | + |
| 98 | +```js |
| 99 | +var server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); |
| 100 | + |
| 101 | +// the JS SDK uses promises for most actions, such as retrieving an account |
| 102 | +server.accounts().accountId(pair.accountId()).call().then(function(account) { |
| 103 | + console.log('Balaces for account: ' + pair.accountId()); |
| 104 | + account.balances.forEach(function(balance) { |
| 105 | + console.log('Type:', balance.asset_type, ', Balance:', balance.balance); |
| 106 | + }); |
| 107 | +}); |
| 108 | +``` |
| 109 | + |
| 110 | +```java |
| 111 | +Server server = new Server("https://horizon-testnet.stellar.org"); |
| 112 | +AccountResponse account = server.accounts().account(pair.getAccountId()); |
| 113 | +System.out.println("Balances for account " + pair.getAccountId()); |
| 114 | +for (AccountResponse.Balance balance : account.getBalances()) { |
| 115 | + System.out.println(String.format( |
| 116 | + "Type: %s, Code: %s, Balance: %s", |
| 117 | + balance.getAssetType(), |
| 118 | + balance.getAssetCode(), |
| 119 | + balance.getBalance())); |
| 120 | +} |
| 121 | +``` |
| 122 | +</example> |
| 123 | + |
| 124 | +Now that you’ve got an account you can [start making and receiving payments](transactions.html). |
| 125 | + |
| 126 | +<a class="button button--previous" href="index.html">Back</a> |
| 127 | +<a class="button button--next" href="transactions.html">Next</a> |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +[1]: You can change an account’s public key later, but the account ID will always stay the same. When you retrieve an account’s data, you’ll be able to get both its ID and its public key. |
| 132 | + |
| 133 | +[2]: Other features of Stellar, like [trust lines](https://www.stellar.org/developers/learn/concepts/assets.html#trustlines), require higher minimum balances. |
| 134 | + |
| 135 | +[3]: There are lots of exchanges out there that sell lumens, but here are a few: (Need links to exchanges here) |
0 commit comments