Writing the createAccount utility

Now, let's create a new nodejs project directory:

  1. 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 and import the stellar-sdk, as follows:
const StellarSdk = require('stellar-sdk');
  1. Next, we'll define a new instance of the stellar-sdk that points 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 only allows selected users to connect to our network. You can find the network passphrase by navigating to the Horizon landing page at localhost:8000. Locate the network-passphrase tag, as shown in the following code:
"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 will identify our MasterKey. The master key is the root account that all the lumens are credited to when a new Stellar network is created. By default, the root account is issued 100 billion lumens at initialization. To fund new accounts, we'll need to transfer lumens from this account to the newly created accounts. As shown in the following code, the master key pair is fetched using the Keypair.master utility from the network passphrase:
 const MasterKey = StellarSdk.Keypair.master(passphrase)
const MasterSecret = MasterKey.secret();
const MasterPublicKey = MasterKey.publicKey();

console.log ('Master Account',MasterSecret, MasterPublicKey);

From the preceding code, we can make the following observations:

  • 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 will generate three random ed25519 key pairs (public key and private key pairs) that will act as our accounts. We'll use the first account to issue the USD asset; the other two accounts will be for Bank A and Bank B. As shown in the following code, we will use the Keypair.random method in the StellarSdk to generate the public-private key pair:
 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 keypairs, we will 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 need 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 less than 20 lumens. This is done to avoid spamming the network. You can only send the createAccount 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. So, to allow our accounts to create assets and trade, we'll transfer 100,000 lumens to each of the newly generated accounts.

  1. Now, let's start building our createAccount transaction. We need to make asynchronous calls to fetch the transaction sequence number and fee, so we will start by declaring an asynchronous method, as follows:
(async function main() {

const account = await server.loadAccount(MasterPublicKey);
const fee = await server.fetchBaseFee();

From the preceding code, we can make the following observations:

  • 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 that's 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 will use the TransactionBuilder class to build a new createAccount transaction. Let's go through each part of the code, bit by bit. We'll start by creating a new transaction constant, as follows: 
const transaction = new StellarSdk.TransactionBuilder(account, { fee, networkPassphrase: passphrase})

This is returned by the TransactionBuilder class. First, we pass the source account (master key account), the network fee, and passphrase as input parameters. Now, we need to send the operations to be carried out by the transaction as input parameters:

.addOperation(StellarSdk.Operation.createAccount({
source: MasterPublicKey,
destination: PublicKey1,
startingBalance: "100000"
}))

The first operation is createAccount. This will fund our newly created random key pair with a starting balance of 100,000 lumens. The source account is our master account public key, while the destination is the public key of the first random account we generated. In the following code, the same operation is repeated for the other two random key pairs we generated:

.addOperation(StellarSdk.Operation.createAccount({
source: MasterPublicKey,
destination: PublicKey2,
startingBalance: "100000"
}))
.addOperation(StellarSdk.Operation.createAccount({
source: MasterPublicKey,
destination: PublicKey3,
startingBalance: "100000"
}))

Notice how the stellar-sdk allows you to chain and link multiple operations in a single transaction? Lastly, we add the transaction timeout and call build() to build the transaction:

.setTimeout(30)
.build();

The transaction timeout indicates that the transaction won't be valid for 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.

  1. The transaction object is then signed using the master key pair, as shown in the following code:
transaction.sign(MasterKey);
  1. Lastly, as shown in the following code, the transaction is posted to the transaction endpoint of the Horizon server:
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 the 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.140.185.123