The Aleo SDK is a developer platform that makes it easy to create a new account, create a transaction, and broadcast it to the network. For more information about Aleo, visit the Welcome to Aleo page to get started.
The account’s private key is used to authorize a transaction that updates the global state of the account’s records. The account view key is used to decrypt account entries that are encrypted under the user’s account address. Finally, the account address allows users to interact with each other by sending and receiving records that encode application values and data.
To protect user assets and record data , you should never disclose your account’s private key to third parties. For real applications on Aleo, users must obtain a compute key from their account’s private key to allow third parties to insecurely run applications and generate transactions on behalf of the user.
The command can be run with the following optional parameters:
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
-s, --seed <seed>
2 Create and build a new project
To create a new project, we will use newthe command. Our project:
aleo new foo
This will create the foo directory and files with the basic project structure:
README.md contains a skeleton README with compilation instructions.
main.aleo is the main source code file.
program.json containing the project identification in JSON format. In particular, the address of the developer and his private key for the program.
The main.aleo file should have the following content:
// The 'foo.aleo' program.
program foo.aleo;
function hello:
input r0 as u32.public;
input r1 as u32.private;
add r0 r1 into r2;
output r2 as u32.private;
To compile the project, run in the main directory:
aleo build
You will see output like this:
⏳ Compiling 'foo.aleo'...
• Loaded universal setup (in 1478 ms)
• Built 'hello' (in 3250 ms)
✅ Built 'foo.aleo' (in "~/foo")
First, a “universal install” is loaded into your environment. You can read more about it here or in the Marlin article .
Once the generic install is ready, each function in your main.aleo file will be created, generating this in the output folder:
hello.prover proving the function hello.
hello.verifier – hellofunction verifier.
main.avm is the bytecode of your aleo program that the virtual machine will run.
As you may have guessed, .avmwe have only one file for the entire program, but each function has its own prover and verifier.
3 Starting the program
You can run a program with a aleo runcommand followed by the name of the function you want to execute and its input parameters.
aleo run hello 2u32 3u32
When the execution completes, you should see the following output:
As you can see, the output register has been assigned 5u32a value representing the sum of the inputs.
4 Program overview
Let’s look at the foo program inside the main.aleo file :
// The 'foo.aleo' program.
program foo.aleo;
function hello:
input r0 as u32.public;
input r1 as u32.private;
add r0 r1 into r2;
output r2 as u32.private;
First, we need to declare the program like this:
program foo.aleo;
After that, we can start writing its functions (or other Aleo structures like structs, records, closures, as we’ll see later).
In the case of functions, everything is very simple:
function [function_name]:
Functions consist of three main parts:
Input section Here we declare its input parameters: input r0 as u32.public; input r1 as u32.private; Everything in Aleo instructions is declared/stored inside a register with type ( i8, field, booletc.) and visibility option ( publicor private), registers are named r0, r1, …, rn. In this case, we use r0and r1for storing the input data passed in sequential order to the program as u32values, where we can store unsigned 32-bit integers to perform our sum operation.
Instruction section The next section consists of the core of our function, here we call the number of Aleo instructions we need to make our program do what we want. For example, performing an addition operation: add r0 r1 into r2; Each Aleo instruction is followed by its input parameters with specific types, and the result is stored in the into register. You can find all available aleo instructions here .
Output section Like the input section, the output section does the same for the program’s output. This is the return of the function. output r2 as u32.private;
5 Types
Aleo uses a strongly typed syntax. The language supports 16 primitive types and allows users to define their own types.
Primitive Aleo types include:
address
boolean
field
group
i8
i16
i32
i64
i128
u8
u16
u32
u64
u128
scalar
string
structsUsers can define custom types using the or keywords record. We will cover them in the next few sections.
1 Registers
Registers are places where you store data so that you can change it later.
2 Interfaces
Interfaces are user-defined data structures. They are very similar to traditional structures in conventional programming languages. You can store structures in registers like any other Aleo data type.
For example, let’s create a structure that represents a fixed-size array of 3 elements. Add this at the bottom of your main.aleo file :
struct array3:
a0 as u32;
a1 as u32;
a2 as u32;
Now, just as an example, let’s write a function that adds one to each element of a register with array3a data type stored in it.
function sum_one_to_array3:
input r0 as array3.private;
add r0.a0 1u32 into r1;
add r0.a1 1u32 into r2;
add r0.a2 1u32 into r3;
cast r1 r2 r3 into r4 as array3;
output r4 as array3.private;
As you can see, we can enter a structure into a register r0and access the members of the structure using .syntax. We execute the addinstruction for each element, storing the results in registers r1, r2and r3finally use the cast command to create a new array3structure in r4.
Now let’s run it. In this case, the only new thing you need to know is that structures are passed to the CLI in the following format:
"{a0: 1u32, a1: 2u32, a2: 3u32}"
Now we can execute aleo runthe command. We’ll clean up the project to pick up the new code:
A record is the main data structure for encoding custom assets and application state. They are very similar to structs, but have two optional parameters:
record token:
owner as address.private
gates as u64.private
refers to the ownerAleo address the entry belongs to and gatesis the amount of credits the entry must spend.
Records are important because they are Aleo’s basic structure for managing state in your application.
When executing the Aleo function, only records belonging to the application address can be transferred as input registers. Otherwise, an error will occur and the application will not start.
You can find the address of your development application inside the program.json file :
In Aleo, application state is managed using entries. An Aleo account can create a transaction to use an entry and create a new entry in its place. Records in Aleo are encrypted at the address of the owner of the record, which guarantees complete confidentiality of all records in Aleo.
6 Your first Aleo program: translating
Consider this program:
// The 'foo.aleo' program.
program foo.aleo;
record token:
owner as address.private;
gates as u64.private;
amount as u64.private;
function transfer_amount:
// 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 of sender
sub r0.amount r2 into r3;
// final balance of 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;
First, we define our own record data type named token, which has two optional parameters ownerand gates, as well as a user-defined parameter named amount, representing the number of tokens we have.
This transfer_amountfunction takes 3 input parameters ( senderwrite, receiverwrite and amount) and stores them in 3 registers ( r0, r1and r2). After that, it calculates the final balance for both of them and stores it in r3and r4(using the sub and add instructions to calculate the subtraction and addition, respectively). With these final amounts, it creates output records for the sender and receiver, saving them in files r5and r6. Finally, both entries are sent out of the function along with the output instruction .
To run this function, the first parameter is the input entry of the program. The format of this parameter is the same as for structure types:
I’ve been mining Aleo since last week at https://aleo.zionodes.com/ and they’re giving away around 8-13 credits per day (10,000 c/s each). By the end of my contract, I should have over 320 credits (roughly) that will be converted at a 5:1 ratio to a coin. So, by the launch of the main network, I should have about 60-65 coins. While I think the upside potential is huge, how much do you think each coin will be worth? According to the current forecast, each coin is expected to be worth between $40 and $60. If you guys have already explored Aleo like me, how far do you think the project will go? FYI: they have already raised about $290 million from investors.
If you like to read such articles and want to support the author, then you can subscribe to our telegram channel and recommend us to your friends, this will help a lot to support our project! Telegram: CRYPTO WIKIES | Bitcoin & Altcoins Mining
Be the first to know all the news, read more about cryptocurrencies and mining at CRYPTO-MINING.BLOG.
No Comments