# Aleo Development Started Pack
In this post, we would make an overview of the different tools available in the aleo SDK to get your aleo development stated.
The Aleo SDK is a toolchain that makes developing on Aleo easy and seamless for developers. With this tool, you can create new **accounts**, **circuits** (also knowns as **programs** in the aleo ecosystem), craft a **transaction**, and broadcast it to the network.
## 1. Setting up the Environment
To start playing with aleo, we need the aleo CLI available in our environment. You will need the rust compiler to build Aleo, if you don't have it installed yet, just run the following in your terminal:
```
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
You must refresh the environment variables to be able to use Rust at this point. You can do that easily closing the terminal and opening it again.
Next, to install the Aleo SDK run:
```
# Download the source code
git clone https://github.com/AleoHQ/aleo && cd aleo
# Install Aleo
$ cargo install --path .
```
Now you can check if it works:
```
aleo -h
```
and you should see the following help output:
```
aleo
The Aleo Team <hello@aleo.org>
USAGE:
aleo [OPTIONS] <SUBCOMMAND>
OPTIONS:
-h, --help Print help information
-v, --verbosity <VERBOSITY> Specify the verbosity [options: 0, 1, 2, 3] [default: 2]
SUBCOMMANDS:
account Commands to manage Aleo accounts
build Compiles an Aleo program
clean Cleans the Aleo package build directory
help Print this message or the help of the given subcommand(s)
new Create a new Aleo package
node Commands to operate a local development node
run Executes an Aleo program function
update Update Aleo
```
Easy peasy. Let's move on.
## 2. Creating and Building a Simple Program
In this step, we are going to create a simple program to transfer our own defined tokens. For this purpose, we are going to define our own record called token (the basic Aleo structure for handling state), and write a function to transfer these tokens between accounts.
### Program Creation
Let's start a new aleo program:
```
aleo new token
cd token
```
This would build a template for start coding your new program call token.
For now, we will populate our *main.aleo* file with the following code:
``` aleo
// The 'token.aleo' program.
program token.aleo;
// Our own defined record called token
record token:
owner as address.private;
gates as u64.private;
amount as u64.private;
// function to mint a new token
function mint_token:
// token address
input r0 as address.private;
// token amount
input r1 as u64.private;
// password
input r2 as field.private;
// checks if the password is correct
hash.psd2 r2 into r3;
is.eq 7202470996857839225873911078012225723419856133099120809866608931983814353616field r3 into r4;
// stores amount input into r5 is the password was right
// otherwise, it stores 0u64 to avoid token creation
ternary r4 r1 0u64 into r5;
// create a new token in r2 register
cast r0 0u64 r5 into r6 as token.record;
// return the new token
output r6 as token.record;
// Function to transfer tokens between accounts
function transfer_token:
// sender token record
input r0 as token.record;
// receiver address
input r1 as address.private;
// amount to transfer
input r2 as u64.private;
// final balance for sender
sub r0.amount r2 into r3;
// final balance for receiver
add 0u64 r2 into r4;
// sender token record after the transfer
cast r0.owner r0.gates r3 into r5 as token.record;
// receiver token record after the transfer
cast r1 0u64 r4 into r6 as token.record;
// sender new token record
output r5 as token.record;
// receiver new token record
output r6 as token.record;
```
These lines of code are called Aleo Instructions. Aleo Instructions are kind of the assembly language for the Aleo snarkVM (The virtual machine that would execute our programs), but with pretty high level functionalities, such as a typing system, complex instructions to do hashing, and so on.
You can read more about these instructions in this post: [Getting Started with Aleo instructions](https://www.entropy1729.com/getting-started-aleo-instructions/]
Let's build our project:
```
aleo build
```
```
⏳ Compiling 'token.aleo'...
• Loaded universal setup (in 1459 ms)
• Built 'transfer' (in 23549 ms)
✅ Built 'token.aleo' (in "/[...]/token")
```
*note:*
If it is the first time you build an aleo program, you would probably have to wait for the universal setup to be downloaded. Don't panic if you see a diferent output the first time!
After building, you will find 3 files in your */build* directory:
- *main.avm*: The file that contains your compiled code.
- 2 files per function in your *main.aleo* file:
- *function_name.prover*: the provers for your functions
- *function_name.vefifier*: the verifier for your functions
This files will be very important when deploying our app.
## 3. Accounts Creation
We will create two accounts to work and make transfers with:
**Account S**:
```
aleo account new
```
```
Private Key APrivateKey1zkpAQpLeFgujVMkMEVKXhotR9XVa8B8nGfugMXYXHMdeHnN
View Key AViewKey1f6ZSnCkgsatCTDDSX5UgXXfjR14pyQ8oizxE5QGcWTxB
Address aleo1nmjzszxsejh0ec6s9tgdx03g24l8e3ev0jy6fx2ckz7s4vt7hgxqu8hz04
```
**Account R**:
```
aleo account new
```
```
Private Key APrivateKey1zkpFoVnVMTGvYKRALnzHkU9nKbMZ9Ueu9ZdsouvdmZzEmhh
View Key AViewKey1jf1aLTv7mu1zQ8FyNK4So6VrVoE5rk2xT7jywzS9Nptq
Address aleo13tny7lhjh7ckjm6ettxvzvdhj856dq6s657nef0p9h9gfruyavzqftkscg
```
We would use **Account S** for the sender, and **Account R** for the receiver.
In Aleo, every account have several related keys:
You can think of them as a pair of keys in an asymmetric cryptography system, where the `Private key` is the private key of the pair, and the rest of them are public keys that derives from it. In that sense, the `Address` is a public key that identifies your account, and the `View Key` is a public key that allows, to anyone who knows it, to decrypt the records owned by the account address.
Enough talk for now! Let's run and deploy our program.
## 4.Running our Program
To run our token program, we need to setup the *program.json* file first. Update the development private_key and adress with the ones of the **Account S** we just created.
It should look like the following:
``` json
{
"program": "token.aleo",
"version": "0.0.0",
"description": "",
"development": {
"private_key": "APrivateKey1zkpAQpLeFgujVMkMEVKXhotR9XVa8B8nGfugMXYXHMdeHnN",
"address": "aleo1nmjzszxsejh0ec6s9tgdx03g24l8e3ev0jy6fx2ckz7s4vt7hgxqu8hz04"
},
"license": "MIT"
}
```
This is important, because from the aleo execution point of view, the **Account S** will be the one running the program to send tokens to the **Account R**. This have some security implications, like you can only input records, when running a program, owned by the account indicated in the *program.json* as we will see in a second.
We need to run the mint function first, to generate the initial amount of tokens owned by **S**. This function requires a password to output a record with the indicated amount, if the given password is incorrect, the token amount output defaults to 0u64 to avoid tokens creation by anyone who does not know the password.
the password for this example is:
```
3634422524977942384127113436866104517282080062207687912678345956934082270693field
```
so we need to run the following command:
``` bash
aleo run mint_token aleo1nmjzszxsejh0ec6s9tgdx03g24l8e3ev0jy6fx2ckz7s4vt7hgxqu8hz04 100u64 3634422524977942384127113436866104517282080062207687912678345956934082270693field
```
and we get the following token record:
```
• Loaded universal setup (in 1472 ms)
🚀 Executing 'token.aleo/mint_token'...
• Executing 'token.aleo/mint_token'...
• Executed 'mint_token' (in 2174 ms)
➡️ Output
• {
owner: aleo1nmjzszxsejh0ec6s9tgdx03g24l8e3ev0jy6fx2ckz7s4vt7hgxqu8hz04.private,
gates: 0u64.private,
amount: 100u64.private,
_nonce: 3024738992072387217402876176731225730589877991873828351104009809002984426287group.public
}
✅ Executed 'token.aleo/mint_token' (in "[...]/token")
```
Now let's use this record to transfer 10 tokens from **S** to **R**
``` bash
aleo run transfer_token "{
owner: aleo1nmjzszxsejh0ec6s9tgdx03g24l8e3ev0jy6fx2ckz7s4vt7hgxqu8hz04.private,
gates: 0u64.private,
amount: 100u64.private,
_nonce: 3024738992072387217402876176731225730589877991873828351104009809002984426287group.public
}" aleo13tny7lhjh7ckjm6ettxvzvdhj856dq6s657nef0p9h9gfruyavzqftkscg 10u64
```
and we should see two records as the output, each of them representing the current state of our token records for each account.
``` bash
• Loaded universal setup (in 1473 ms)
🚀 Executing 'token.aleo/transfer_token'...
• Executing 'token.aleo/transfer_token'...
• Executed 'transfer_token' (in 3620 ms)
➡️ Outputs
• {
owner: aleo1nmjzszxsejh0ec6s9tgdx03g24l8e3ev0jy6fx2ckz7s4vt7hgxqu8hz04.private,
gates: 0u64.private,
amount: 90u64.private,
_nonce: 2178231086979351800436660566645386776768595203243256243508388701123108642520group.public
}
• {
owner: aleo13tny7lhjh7ckjm6ettxvzvdhj856dq6s657nef0p9h9gfruyavzqftkscg.private,
gates: 0u64.private,
amount: 10u64.private,
_nonce: 4704084834675699921301424200265500298050115484153136728500446828605991979105group.public
}
✅ Executed 'token.aleo/transfer_token' (in "[...]/token")
```
And that's it! We have made our first transfer of tokens.
Just for the sake of curiosity, let's see what would happen if we swap the two addresses (**S** and **R**) in the last `aleo run transfer_token` command:
```
aleo run transfer_token "{
owner: aleo13tny7lhjh7ckjm6ettxvzvdhj856dq6s657nef0p9h9gfruyavzqftkscg.private,
gates: 0u64.private,
amount: 100u64.private,
_nonce: 3024738992072387217402876176731225730589877991873828351104009809002984426287group.public
}" aleo1nmjzszxsejh0ec6s9tgdx03g24l8e3ev0jy6fx2ckz7s4vt7hgxqu8hz04 10u64
```
We will get the following output:
```
• Loaded universal setup (in 1461 ms)
⚠️ Input record for 'token.aleo' must belong to the signer
```
It tells us that the input record owner does not belong to the address of the sender, in other words, the address that we updated in our *program.json*.
As you can already appreciate, Aleo Programs perform several high-level vulnerabilities checks, making our program pretty fault tolerant by default.
## 5. Deploying our Program Locally
To start a Aleo Node locally, all you need to do is to run the following command inside you program path in a separete terminal:
```
aleo node start
```
Wait a couple of seconds and pay attention to the first outputs:
```
⏳ Starting a local development node for 'token.aleo' (in-memory)...
• Loaded universal setup (in 1471 ms)
• Executing 'credits.aleo/genesis'...
• Executed 'genesis' (in 1872 ms)
• Loaded universal setup (in 1423 ms)
• Verified 'genesis' (in 46 ms)
• Verified 'genesis' (in 46 ms)
🌐 Server is running at http://0.0.0.0:4180
📦 Deploying 'token.aleo' to the local development node...
• Built 'mint_token' (in 11832 ms)
• Certified 'mint_token': 264 ms
• Built 'transfer_token' (in 23166 ms)
• Certified 'transfer_token': 537 ms
• Calling 'credits.aleo/fee'...
• Executed 'fee' (in 2616 ms)
• Verified certificate for 'mint_token': 89 ms
• Verified certificate for 'transfer_token': 143 ms
• Verified 'fee' (in 47 ms)
• Verified certificate for 'mint_token': 89 ms
• Verified certificate for 'transfer_token': 141 ms
• Verified 'fee' (in 47 ms)
• Verified certificate for 'mint_token': 88 ms
• Verified certificate for 'transfer_token': 143 ms
• Verified 'fee' (in 46 ms)
🛡️ Produced block 1 (ab1cz4e3zrw8xje7q6gutsjkqktvmwhq0yx3j4tqjeqnfr3c2j0f5gs5hwmf9)
{
"previous_state_root": "1864323752948026853541213650623314215454919286649566834473029713121712183360field",
"transactions_root": "2938300578986025380747616267226890561116633830917000361321232469947771414785field",
"metadata": {
"network": 3,
"round": 1,
"height": 1,
"coinbase_target": 18446744073709551615,
"proof_target": 18446744073709551615,
"timestamp": 1660331897
}
}
✅ Deployed 'token.aleo' in transaction 'at13pzudena0mk7xrj7m4tyar5x96ynr7m2q30c27lvc3gr7w5rn58q4agsk2'
• Executing 'credits.aleo/transfer'...
• Executed 'transfer' (in 3336 ms)
• Verified 'transfer' (in 47 ms)
• Verified 'transfer' (in 47 ms)
🛡️ Produced block 2 (ab1d2cypkhgv7fpardzd8pnn8mq8pwtwk453jyzc36cvt7z9r24vcpskj4tmd)
...
```
As you can see here, we have a local node running and mining blocks, our token program already deployed in a transaction, and a server runing at [http://localhost:4180](http://localhost:4180) waiting for request. Pretty amazing, right?
### Overview of the most important server endpoints
In this section, we will explain the most useful HTTP RESTful endpoints you can invoke to retrieve information from the ledger of your node. You can use either a web browser or the `curl` command.
> 💡 By default the web service of the node is listening connection in the port 4180.
#### Getting the latest block's height
To get the latest height of the ledger of your node you can make a `GET` request to `http://localhost:4180/testnet3/latest/height`.
The response contains the value of the latest height of your running node.
#### Getting the latest block's hash
To get the latest hash of the ledger of your node you can make a `GET` request to
```
http://localhost:4180/testnet3/latest/hash
```
You'll see the response containing a hash like this one:
> "ab14ja24wr9rdg2hmpym35kvw08ywwp6eyhknn23zr3ypwrxvqsjyzqpns7ml"
#### Getting the ledger's latest block
To get the latest block of the ledger of your node you can make a `GET` request to
```
http://localhost:4180/testnet3/latest/block
```
You'll see a response in JSON format that contains the attributes of the block.
The root attributes are: `block_hash`, `previous_hash`, `header`, `transactions` and `signature`.
#### Getting a block by its height
To get the block a given height from the ledger of your node you can make a `GET` request to
```
http://localhost:4180/testnet3/block/{height}
```
For example:`http://localhost:4180/testnet3/block/8` will return the block at height 8.
#### Getting records belonging to an account
You can retrieve records in three different ways. Depending on what type of records you want to query. You will just need your `ViewKey` and your `GraphKey` (Note: the graph key allows you to search for the spent and unspent records easily) at your disposal. There are two types of records, spent and unspent, we would talk about these later on. You can see the list of the endpoints associated below:
- `GET /testnet3/records/all`: this endpoint retrieves all the records belonging to a given `ViewKey`.
```
curl --location --request GET 'localhost:4180/testnet3/records/all' -H 'Content-Type: application/json' -d '"AViewKey1iAf6a7fv6ELA4ECwAth1hDNUJJNNoWNThmREjpybqder"'
```
- `GET /testnet3/records/spent`: this endpoint retrieves only the spent records belonging to a given `ViewKey` and `GraphKey`.
```
curl --location --request GET 'localhost:4180/testnet3/records/spent' -H 'Content-Type: application/json' -d '{"view_key": <view_key>, "graph_key": <graph_key>}'
```
- `GET /testnet3/records/all`: this endpoint retrieves only the unspent records belonging to a given `ViewKey` and `GraphKey`.
```
curl --location --request GET 'localhost:4180/testnet3/records/unspent' -H 'Content-Type: application/json' -d '{"view_key": <view_key>, "graph_key": <graph_key>}'
```
In the following posts, we will start making and broadcasting transactions to our local development blockchain, achieving private off-chain execution, and private on-chain state storage, key features of the Aleo ecosystem.