Coding for NXT Crypto Platform #5: Transactions on the Nxt Blockchain
The Nxt Blockchain is the heart of Nxt. Every Transaction ever made is recorded on the Blockchain. The Blockchain is a decentralized database, which is saved on your computer. To prevent spam, for every transaction you have to pay fees in the native currency, which will be NXT for us using the Nxt Blockchain. In previous articles I have shown how to setup and configure your Nxt Software as well as how to read data, this time we write data on the Blockchain, starting with a simple example. Throughout the article, I want to focus on tools you can use while creating Transactions on Nxt.
Codewise, creating a transaction on Nxt is very similar to reading data from the Blockchain; There are some topics we have to cover before creating our first transaction. To create a transaction, we first need a Nxt Account and some NXT, in order to pay for Blockchain fees. So, let us log in to Nxt and create our first account.
Create a Nxt account
In order to have a secure Nxt account, you need a strong passphrase. Working With virtual currencies we use the term passphrase instead of password, this describes strong entropy and should be more secure than a regular password. When you are using the official Nxt Client, you can use the implemented passphrase creation function, which will give you a passphrase which contains 12 words.
Create your own passphrase
You can also create your own passphrase, make sure it contains enough characters and entropy. To generate a passphrase it is recommended to use Software such as KeePass, LastPass, Dashlane or Online Tools like PasswordsGenerator.net or Fourmilab.ch.
Nxt passphrase generator
When running Nxt on http://localhost:7876 you will get to the client, a click on the link quoting
DON’T HAVE AN ACCOUNT? CLICK HERE TO CREATE ONE!
will bring you to the passphrase generator. Afterwards (or with your self-created passphrase), you can log in to your Account. If you do not have NXT yet, the quickest method is when you already got some virtual Currencies like Bitcoin, Litecoin, Ethereum or others. You can exchange them via Shapeshift or buy your coins on another exchange and transfer them to your wallet.
In the next steps, we use your passphrase to sign and broadcast transactions. Your passphrase can also be seen as your private Key.
Create the Transaction
With a secure passphrase, we can now look into the code of creating a send-NXT-Transaction. We send out 1 NXT to a recipient, which you see in the code and change it there for another recipient. Insert your passphrase in the following code and run it to create the transaction.
Outgoing Transaction JSON
This script is creating a sendMoney transaction. As you see we have been changing the function used in previous articles $.getJSON to $.post. When creating transactions, it is necessary to switch to POST format instead of GET requests as with the previous functions. For reference to the API details, have a look at http://localhost:7876/test?requestType=sendMoney.
The output you see on the page contains various information, the variable feeNQT has been filled in with the correct information, you can check how much the fee is for the transaction you have created. You see the whole transactionJSON you have inserted in JSON format and you receive the transaction transactionBytes. The transactionBytes embeds all the transaction information you have in the transactionJSON.
The above created transaction will not be broadcasted to the network yet because we set “broadcast”: false in our object. When setting this true the transaction will be broadcasted and will cost you 1 NXT. As you can see, we do not need many variables to create our first transaction.
The recipe for a transaction is:
recipient You can use either the RS format e.g. NXT-XXX-XXX-XXX-XXXX or the numerical account format
amountNQT The amount you want to send needs to be calculated in NQT, the smallest number in NXT (1 NXT / 10⁸ = 1 NQT)
secretPhrase Your secret passphrase
feeNQT Place 0 if you want the server to calculate minimum fees for you
deadline measured in minutes, when the transaction expires
broadcast false will not broadcast and just create the transaction. Set to true the transaction will be broadcast to other peers and will cost you NXT
Now that we know how to create a sendMoney transaction that has not yet been broadcasted, we can use a script that looks similar to the above one to broadcast the transaction. To make it more understandable I am extending the above script with a button that takes the previously created transaction and transfers it to the network on submitting the button. Effectively when running the script you have your first (small, not yet secure!) interactive Nxt wallet.
Outgoing Transaction JSON
Loading the site will directly load the transaction not yet broadcasted. As the button click function is nested within the first call, it can access the data from the transaction not broadcasted. To broadcast the transaction we use the following recipe
transactionBytes The transactionBytes we have created with the previous declared function.
With these few lines of code, we can create a transaction and broadcast it. Yet, we are still defining all the variables directly within the code. In the next articles, I am going to show you how to add encrypted messages to a transaction and how to get these data from a user input you have on the page. We will have a deeper look into phasing and pruning of transactions.
More resources, example code and dev blogs: https://nxter.org/devcom
More in this series:
Coding for NXT Crypto Platform #1: Configuration
Coding for NXT Crypto Platform #2: First Steps
Coding for NXT Crypto Platform #3: Output from the Asset Exchange
Coding for NXT Crypto Platform #4: Historical Asset Data