Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a Stellar account

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to create a Stellar account". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to create a Stellar account.

Stellar block chain is a widely used block chain network. In this tutorial, we will learn to use JavaScript code to create Stellar accounts, recharge Stellar accounts, and submit transfer transactions between accounts to the Stellar blockchain.

1. Overview of Stellar

To quickly grasp the blockchain and digital currency application development, Huizhi Network's blockchain development series of online tutorials are recommended. The content covers a variety of mainstream blockchain platforms such as Bitcoin, ethernet, EOS, super ledger, Tendermint, etc., and supports Java, C#, Python, PHP, JavaScript, Dart and other development languages.

Horizon: applications interact with the Stellar network through the Horizon development interface. Horizon is a RESTful HTTP API server where Horizon API developers can submit transactions, check account balances and subscribe to events. You can use Horizon API in a web browser, or use the cURL command line to access Horizon API, but the easiest way is to use the official Stellar SDK, which officially supports JavaScript, Java and Go, and the community has also developed SDK for Python, C# and Ruby.

Stellar Core: is the backbone of the Stellar network, and every Horizon server needs to connect to the Stellar Core. Stellar Core is responsible for validating the deal and reaching a consensus. The Stellar Core run by all individuals forms a decentralized Stellar network, and each transaction on the network has to pay a small fee: 100 stroop (0.00001 XLM). The purpose of the transaction fee is to prevent spam on the network.

Stellar consensus protocol-SCP:SCP chooses security between security and activity-when there is a partition event or a node with abnormal behavior, SCP will stop the operation of the network until a network consensus can be reached. For more information about SCP, please refer to the Stellar white paper.

Run Stellar Core using Docker: you can join the Stellar network as long as you run an instance of Stellar Core. We can use the docker image of Stellar/quickstart to quickly create an Stellar Core instance.

Fortunately for developers, Stellar also provides a test chain for developers to use. You can check its details here.

2. Overview of Stellar development

Before continuing with your studies, I hope you have a preliminary understanding of JavaScript, Express and Async/Await.

We will create two accounts on the test chain, then recharge them, and make some random transfers between the two accounts. Finally, we want to use the transaction browser to extract transaction information. In short, the purpose of this tutorial is to give you a quick understanding of the basics of Stellar blockchain development.

You can find the complete code for this tutorial here. The complete code is also available at the end of this tutorial.

3. Create a Stellar account

Stellar JavaScript SDK is the most stable of all official SDK. We can install JS SDK using the following command:

Npm I-S stellar-sdk

Use the following JS code to initialize a Server instance that connects to the test chain:

Const server = new Stellar.Server ('https://horizon-testnet.stellar.org')Stellar.Network.useTestNetwork()

Now we can create two random accounts. Stellar SDK provides a function for creating random key pairs without providing cryptographic seeds:

Let pairA = Stellar.Keypair.random () let pairB = Stellar.Keypair.random ()

The resulting Keypair object provides two methods for obtaining the public key and ciphertext seed of the key pair, respectively:

PublicKey (): get the public key

Secret (): get the seed of ciphertext

Next we define the route to initialize the account. In the following function, we will send a request to friendbot on the Stellar test chain to recharge the key pair account created before recharging. Friendbot will automatically recharge our account with 10.000 Lumen each. The following code uses the request-promise package to send a request to the Horizon test chain API, which works well with async/await. We pass in the public key address to recharge in the query string.

Await rp.get ({uri: 'https://horizon-testnet.stellar.org/friendbot', qs: {addr: pairA.publicKey ()}, json: true})

The account has been created now, but we don't have an account object in the application yet. Use the following code to get:

AccountB = await server.loadAccount (pairB.publicKey ())

Now we can check to see if we already have some Stellar coins in our account:

AccountA.balances.forEach ((balance) = > {console.log ('Type:', balance.asset_type,', Balance:', balance.balance)})

The above code can get the balance of account B with a little modification, you can give it a try!

4. Stellar transfer transaction

A transaction (transaction) can contain multiple operations (operation). For the sake of simplicity, we will only use the transfer operation. In the following example, the public key of pairB is used as the transfer target address, and the number of transfers is 30.000000001. Stellar SDK requires that the mount parameter should declare 7 decimal places:

Const transaction = new Stellar.TransactionBuilder (accountA) .addOperation (Stellar.Operation.payment ({destination: pairB.publicKey (), asset: Stellar.Asset.native (), amount: '30.0000001'})) .build ()

Cool! The transaction is now waiting for the account to be sent for signature, and we can use transaction.sign (pairA) to do this. All right, after signing, you can send it to Horizon:

Const transactionResult = await server.submitTransaction (transaction) 5, extract Stellar transaction history

The following code returns only one transaction per page. You can use historyPage's next () method to continue to extract subsequent transactions:

Let historyPage = await server.transactions () .forAccount (accountA.accountId ()) .call ()

We want to print the details of the operation in the XDR-coded transaction. First, you need to decode it to base64 and iterate over all the actions to display the amount sent:

Let txDetails = Stellar.xdr.TransactionEnvelope .fromXDR (historyPage.records [1] .base64' _ xdr, 'base64') txDetails._attributes.tx._attributes.operations .map (operation = > console.log (`fromXDR: ${operation._attributes.body._value._attributes.amount.low} XLM`))

Finally, let's get the next historyPage:

HistoryPage = await historyPage.next () 6, Stellar supplementary skills

You can browse the transactions on the Stellar network to see this example.

The transaction metadata is saved in XDR format, where you can deserialize it.

You can view all transactions with a specified public key, which is an example.

At this point, I believe you have a deeper understanding of "how to create a Stellar account". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report