Writing the CreateAccount utility

Let's start writing our Node.js create account utility, as follows:

  1. Create a new nodejs project directory. Install the stellar-sdk JavaScript module by executing the following command in the directory:
npm install --save stellar-sdk
  1. Create a new project file called CreateAccount.js.
  2. Start by importing the stellar-sdk, as follows:
const StellarSdk = require('stellar-sdk');
  1. Next, we'll define a new instance of the stellar-sdk, pointed to our local Horizon instance:
const server = new StellarSdk.Server('http://127.0.0.1:8000', {allowHttp: true});

const passphrase = 'Standalone Network ; February 2017'
  1. The passphrase allows only selected users to connect to our network. You can find the network passphrase by navigating to the Horizon landing page at localhost:8000 in the browser. Locate the network-passphrase tag , as follows:
"network_passphrase": "Standalone Network ; February 2017"

For the quickstart Docker image, this passphrase is set to "Standalone Network ; February 2017", by default.

  1. Next, we identify our MasterKey. The master key is the root account to which all lumens are credited when a new Stellar network is created. By default, the root account is issued 100 billion lumens at initiation.
  2. To fund new accounts, we'll need to transfer lumens from this account to the newly created accounts.
  3. The master key pair is fetched using the Keypair.master utility from the network passphrase, as follows:
const MasterKey = StellarSdk.Keypair.master(passphrase)
const MasterSecret = MasterKey.secret();
const MasterPublicKey = MasterKey.publicKey();

console.log ('Master Account',MasterSecret, MasterPublicKey);
  1. The .secret() and .publicKey() methods for a key pair give us its secret key and public key, respectively.
In a production implementation of Stellar, you are expected to use a custom network phrase and transfer all the lumens or native currency from the master currency to another Stellar account that you control on the network.
  1. Next, we generate three random ed25519 key pairs (public and private key pairs) that will act as our accounts. To do so, we use the Keypair.random method in the StellarSdk, as follows:
const pair1 = StellarSdk.Keypair.random(passphrase);
const pair2 = StellarSdk.Keypair.random(passphrase);
const pair3 = StellarSdk.Keypair.random(passphrase);
  1. For each of the three newly generated random key pairs, we retrieve the public key and the private, or secret, key, as follows:
var SecretKey1 = pair1.secret();
var PublicKey1 = pair1.publicKey();
console.log ('Account1',SecretKey1, PublicKey1);

var SecretKey2 = pair2.secret();
var PublicKey2 = pair2.publicKey();
console.log ('Account2',SecretKey2, PublicKey2);

var SecretKey3 = pair3.secret();
var PublicKey3 = pair3.publicKey();
console.log ('Account3',SecretKey3, PublicKey3);

Make sure you log the newly generated keys and the master key to the console. We'll require these later.

Before we can use the newly generated keys, we need to fund them with lumens, which is the native currency of Stellar. Stellar does not allow users to send or receive transactions to any key pair with a lumens balance of less than 20. This is done to avoid spamming the network. You can only send the "Create Account" transaction to newly generated key pairs, to fund the account. The Stellar network also requires accounts to submit a "fee" to the network for every transaction, which is paid in lumens. Thus, to enable our accounts to create assets and trade, we'll transfer 100,000 lumens to each of the newly generated accounts.

Now, let's start building our CreateAccount transaction by following these steps:

  1. We need to make asynchronous calls to fetch the transaction sequence number and fee, so we start by declaring an asynchronous method, like this:
(async function main() {

const account = await server.loadAccount(MasterPublicKey);
const fee = await server.fetchBaseFee();
  • The server.loadAccount method fetches the current sequence number of the Stellar account. It is essential that transactions are submitted to the network in sequence, to dictate the order in which they'll be processed and verified. Before submitting a transaction from an account, we fetch the current transaction sequence number for the account.
  • The server.fetchBaseFee method fetches the minimum fee required for the transaction to go through on the network. You can think of it as being similar to fetching the current gas price in an Ethereum network.
  1. Next, we use the TransactionBuilder class to build a new create account transaction, like this:
const transaction = new StellarSdk.TransactionBuilder(account, { fee, networkPassphrase: passphrase})
.addOperation(StellarSdk.Operation.createAccount({
source: MasterPublicKey,
destination: PublicKey1,
startingBalance: "100000"
}))
.addOperation(StellarSdk.Operation.createAccount({
source: MasterPublicKey,
destination: PublicKey2,
startingBalance: "100000"
}))
.addOperation(StellarSdk.Operation.createAccount({
source: MasterPublicKey,
destination: PublicKey3,
startingBalance: "100000"
}))
.setTimeout(30)
.build();

Let's go through each step, one by one:

const transaction = new StellarSdk.TransactionBuilder(account, { fee, networkPassphrase: passphrase})
  1. We start by creating a new transaction constant. This is returned by the TransactionBuilder class. We first pass the source account (master key account), the network fee, and passphrase as input parameters. Next, we send the operations to be carried out by the transaction as input parameters, like this:
.addOperation(StellarSdk.Operation.createAccount({
source: MasterPublicKey,
destination: PublicKey1,
startingBalance: "100000"
}))
  1. The first operation is createAccount. It'll fund our newly created random key pair with a starting balance of 100,000 lumens. The source account is our master account public key, and the destination is the public key of the first random account we generated. The same operation is repeated for the other two random key pairs we generated, as follows:
.addOperation(StellarSdk.Operation.createAccount({
source: MasterPublicKey,
destination: PublicKey2,
startingBalance: "100000"
}))
.addOperation(StellarSdk.Operation.createAccount({
source: MasterPublicKey,
destination: PublicKey3,
startingBalance: "100000"
}))

Notice how stellar-sdk allows you to chain and link multiple operations in a single transaction.

  1. Lastly, we add the transaction timeout and call build() to build the transaction, as follows:
.setTimeout(30)
.build();

The transaction timeout indicates that the transaction won't be valid more than 30 seconds after the transaction object is created. build() instructs the TransactionBuilder class to create a new transaction object using the parameters we submitted. This object is stored in the transaction constant. 

The transaction object is then signed using the master key pair, like this:

transaction.sign(MasterKey);

Lastly, the transaction is posted to the transaction endpoint of the Horizon server, as follows:

try {
const transactionResult = await server.submitTransaction(transaction);
console.log(transactionResult);
} catch (err) {
console.error(err);
}
})()

This is done using server.submitTransaction. The result is logged to the console.

This brings us to an end of the CreateAccount utility.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.67.26