Program
Overview
WebAssembly representation of an Aleo program. The Program class provides methods for parsing, inspecting, and analyzing Aleo programs including their functions, records, structs, and mappings.
Methods
fromString
Create a program from a program string
fromString(program) ► Program
| Param | Type | Description |
|---|---|---|
| program | string | Aleo program source code |
| return | Program | Program object |
toString
Get a string representation of the program
toString() ► string
| Param | Type |
|---|---|
| return | string |
hasFunction
Determine if a function is present in the program
hasFunction(functionName) ► boolean
| Param | Type | Description |
|---|---|---|
| functionName | string | Name of the function to check for |
| return | boolean | True if the program has the function, false otherwise |
getFunctions
Get javascript array of function names in the program
getFunctions() ► Array
| Param | Type |
|---|---|
| return | Array |
Example
const expected_functions = [
"mint",
"transfer_private",
"transfer_private_to_public",
"transfer_public",
"transfer_public_to_private",
"join",
"split",
"fee"
];
const credits_program = aleo_wasm.Program.getCreditsProgram();
const credits_functions = credits_program.getFunctions();
console.log(credits_functions === expected_functions); // Output should be "true"
getFunctionInputs
Get a javascript object representation of the function inputs and types. This can be used to generate a web form to capture user inputs for an execution of a function.
getFunctionInputs(function_name) ► Array
| Param | Type | Description |
|---|---|---|
| function_name | string | Name of the function to get inputs for |
| return | Array | Array of function inputs |
Example
const expected_inputs = [
{
type:"record",
visibility:"private",
record:"credits",
members:[
{
name:"microcredits",
type:"u64",
visibility:"private"
}
],
register:"r0"
},
{
type:"address",
visibility:"private",
register:"r1"
},
{
type:"u64",
visibility:"private",
register:"r2"
}
];
const credits_program = aleo_wasm.Program.getCreditsProgram();
const transfer_function_inputs = credits_program.getFunctionInputs("transfer_private");
console.log(transfer_function_inputs === expected_inputs); // Output should be "true"
getMappings
Get a list of a program's mappings and the names/types of their keys and values.
getMappings() ► Array
| Param | Type |
|---|---|
| return | Array |
Example
const expected_mappings = [
{
name: "account",
key_name: "owner",
key_type: "address",
value_name: "microcredits",
value_type: "u64"
}
];
const credits_program = aleo_wasm.Program.getCreditsProgram();
const credits_mappings = credits_program.getMappings();
console.log(credits_mappings === expected_mappings); // Output should be "true"
getRecordMembers
Get a javascript object representation of a program record and its types
getRecordMembers(record_name) ► Object
| Param | Type | Description |
|---|---|---|
| record_name | string | Name of the record to get members for |
| return | Object | Object containing the record name, type, and members |
Example
const expected_record = {
type: "record",
record: "Credits",
members: [
{
name: "owner",
type: "address",
visibility: "private"
},
{
name: "microcredits",
type: "u64",
visibility: "private"
}
]
};
const credits_program = aleo_wasm.Program.getCreditsProgram();
const credits_record = credits_program.getRecordMembers("Credits");
console.log(credits_record === expected_record); // Output should be "true"
getStructMembers
Get a javascript object representation of a program struct and its types
getStructMembers(struct_name) ► Array
| Param | Type | Description |
|---|---|---|
| struct_name | string | Name of the struct to get members for |
| return | Array | Array containing the struct members |
Example
const STRUCT_PROGRAM = `program token_issue.aleo;
struct token_metadata:
network as u32;
version as u32;
struct token:
token_id as u32;
metadata as token_metadata;
function no_op:
input r0 as u64;
output r0 as u64;`;
const expected_struct_members = [
{
name: "token_id",
type: "u32",
},
{
name: "metadata",
type: "struct",
struct_id: "token_metadata",
members: [
{
name: "network",
type: "u32",
},
{
name: "version",
type: "u32",
}
]
}
];
const program = aleo_wasm.Program.fromString(STRUCT_PROGRAM);
const struct_members = program.getStructMembers("token");
console.log(struct_members === expected_struct_members); // Output should be "true"
getCreditsProgram
Get the credits.aleo program
getCreditsProgram() ► Program
| Param | Type |
|---|---|
| return | Program |
id
Get the id of the program
id() ► string
| Param | Type |
|---|---|
| return | string |
address
Get a unique address of the program
address() ► Address
| Param | Type |
|---|---|
| return | Address |
isEqual
Determine equality with another program
isEqual(other) ► boolean
| Param | Type | Description |
|---|---|---|
| other | Program | The other program to compare |
| return | boolean | True if the programs are equal, false otherwise |
getImports
Get program imports
getImports() ► Array
| Param | Type |
|---|---|
| return | Array |
Example
const DOUBLE_TEST = `import multiply_test.aleo;
program double_test.aleo;
function double_it:
input r0 as u32.private;
call multiply_test.aleo/multiply 2u32 r0 into r1;
output r1 as u32.private;`;
const expected_imports = [
"multiply_test.aleo"
];
const program = aleo_wasm.Program.fromString(DOUBLE_TEST);
const imports = program.getImports();
console.log(imports === expected_imports); // Output should be "true"