Build an Alias Output
The following code example will:
- Create a
Client
which will connect to the Shimmer Testnet. - Build an Alias output.
Code Example
- Rust
- Nodejs
- Python
- Java
Dotenv
This example uses dotenv, which is not safe to use in production environments.
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! cargo run --example build_alias_output --release
use iota_client::{
block::{
address::Address,
output::{
feature::{IssuerFeature, MetadataFeature, SenderFeature},
unlock_condition::{
GovernorAddressUnlockCondition, StateControllerAddressUnlockCondition, UnlockCondition,
},
AliasId, AliasOutputBuilder, Feature,
},
},
Client, Result,
};
/// In this example we will build an alias output
#[tokio::main]
async fn main() -> Result<()> {
// This example uses dotenv, which is not safe for use in production!
dotenv::dotenv().ok();
let node_url = std::env::var("NODE_URL").unwrap();
// Create a client instance.
let client = Client::builder().with_node(&node_url)?.finish()?;
let token_supply = client.get_token_supply().await?;
let rent_structure = client.get_rent_structure().await?;
let address = Address::try_from_bech32("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy")?.1;
// Alias id needs to be null the first time
let alias_output = AliasOutputBuilder::new_with_minimum_storage_deposit(rent_structure, AliasId::null())?
// `hello` in bytes
.with_state_metadata(vec![104, 101, 108, 108, 111])
.add_feature(Feature::Sender(SenderFeature::new(address)))
.add_feature(Feature::Metadata(MetadataFeature::new(vec![104, 101, 108, 108, 111])?))
.add_immutable_feature(Feature::Issuer(IssuerFeature::new(address)))
.add_immutable_feature(Feature::Metadata(MetadataFeature::new(vec![104, 101, 108, 108, 111])?))
.add_unlock_condition(UnlockCondition::StateControllerAddress(
StateControllerAddressUnlockCondition::new(address),
))
.add_unlock_condition(UnlockCondition::GovernorAddress(GovernorAddressUnlockCondition::new(
address,
)))
.finish_output(token_supply)?;
println!("{alias_output:#?}");
Ok(())
}
Run the Example
Run the example by running the following command:
cargo run --example build_alias_output --release
Dotenv
This example uses dotenv, which is not safe to use in production environments.
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { Client, initLogger } from '@iota/client';
require('dotenv').config({ path: '../.env' });
// Run with command:
// node ./dist/13_build_alias_output.js
// Build a basic output
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
}
const client = new Client({
nodes: [process.env.NODE_URL],
});
try {
const hexAddress = await client.bech32ToHex(
'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy',
);
const aliasOutput = await client.buildAliasOutput({
aliasId:
'0x0000000000000000000000000000000000000000000000000000000000000000',
// `hello` hex encoded
stateMetadata: '0x68656c6c6f',
unlockConditions: [
{
// StateControllerAddressUnlockCondition
type: 4,
address: {
type: 0,
pubKeyHash: hexAddress,
},
},
{
// GovernorAddressUnlockCondition
type: 5,
address: {
type: 0,
pubKeyHash: hexAddress,
},
},
],
features: [
{
// sender feature
type: 0,
address: {
type: 0,
pubKeyHash: hexAddress,
},
},
{
// MetadataFeature
type: 2,
// `hello` hex encoded
data: '0x68656c6c6f',
},
],
immutableFeatures: [
{
// issuer feature
type: 1,
address: {
type: 0,
pubKeyHash: hexAddress,
},
},
{
// MetadataFeature
type: 2,
// `hello` hex encoded
data: '0x68656c6c6f',
},
],
});
console.log(JSON.stringify(aliasOutput, null, 2));
} catch (error) {
console.error('Error: ', error);
}
}
run();
You can run the example by running the following command from the bindings/node/examples/
folder:
node dist/13_build_alias_output.js
from iota_client import IotaClient
import json
# Create an IotaClient instance
client = IotaClient({'nodes': ['https://api.testnet.shimmer.network']})
hexAddress = client.bech32_to_hex(
'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy')
alias_id = '0x0000000000000000000000000000000000000000000000000000000000000000'
# `hello` hex encoded
state_metadata = '0x68656c6c6f'
unlock_conditions = [
# StateControllerAddressUnlockCondition
{'type': 4, 'address': {'type': 0, 'pubKeyHash': hexAddress}},
# GovernorAddressUnlockCondition
{'type': 5, 'address': {'type': 0, 'pubKeyHash': hexAddress}},
]
features = [
{
# sender feature
'type': 0,
'address': {
'type': 0,
'pubKeyHash': hexAddress,
},
},
{
# MetadataFeature
'type': 2,
# `hello` hex encoded
'data': '0x68656c6c6f',
}
]
immutable_features = [
{
# issuer feature
'type': 1,
'address': {
'type': 0,
'pubKeyHash': hexAddress,
},
},
{
# MetadataFeature
'type': 2,
# `hello` hex encoded
'data': '0x68656c6c6f',
},
]
# Build alias output
alias_output = client.build_alias_output(
alias_id=alias_id,
state_metadata=state_metadata,
unlock_conditions=unlock_conditions,
features=features,
immutable_features=immutable_features
)
# Print the output
print(json.dumps(alias_output, indent=4))
import org.iota.Client;
import org.iota.types.ClientConfig;
import org.iota.types.expections.ClientException;
import org.iota.types.Feature;
import org.iota.types.Output;
import org.iota.types.UnlockCondition;
import org.iota.types.expections.InitializeClientException;
import org.iota.types.ids.AliasId;
import org.iota.types.output_builder.AliasOutputBuilderParams;
import org.iota.types.secret.GenerateAddressesOptions;
import org.iota.types.secret.MnemonicSecretManager;
import org.iota.types.secret.Range;
import com.google.gson.GsonBuilder;
public class BuildAliasOutput {
public static void main(String[] args) throws ClientException, InitializeClientException {
// Build the client.
Client client = new Client(new ClientConfig().withNodes(new String[]{"https://api.testnet.shimmer.network"}));
// Configure a simple Alias output.
String hexAddress = client.bech32ToHex("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy");
AliasId aliasId = new AliasId("0x0000000000000000000000000000000000000000000000000000000000000000");
UnlockCondition[] unlockConditions = new UnlockCondition[]{
new UnlockCondition("{ type: 4, address: { type: 0, pubKeyHash: \"" + hexAddress + "\" } }"),
new UnlockCondition("{ type: 5, address: { type: 0, pubKeyHash: \"" + hexAddress + "\" } }")
};
Feature[] features = new Feature[]{
// sender feature
new Feature("{ type: 0, address: { type: 0, pubKeyHash: \"" + hexAddress + "\" } }"),
// metadata feature, `hello` hex encoded
new Feature("{ type: 2, data: \"0x68656c6c6f\" }")
};
Feature[] immutableFeatures = new Feature[]{
// issuer feature
new Feature("{ type: 1, address: { type: 0, pubKeyHash: \"" + hexAddress + "\" } }"),
// metadata feature, `hello` hex encoded
new Feature("{ type: 2, data: \"0x68656c6c6f\" }")
};
AliasOutputBuilderParams params = new AliasOutputBuilderParams()
.withAliasId(aliasId)
.withStateMetadata("0x68656c6c6f")
.withUnlockConditions(unlockConditions)
.withFeatures(features)
.withImmutableFeatures(immutableFeatures);
// Build the output.
Output output = client.buildAliasOutput(params);
// Print the output.
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(output));
}
}
Expected Output
- Rust
- Nodejs
- Python
- Java
Alias(
AliasOutput {
amount: 59200,
native_tokens: NativeTokens(
BoxedSlicePrefix([]),
),
alias_id: AliasId(0x0000000000000000000000000000000000000000000000000000000000000000),
state_index: 0,
state_metadata: BoxedSlicePrefix([
104,
101,
108,
108,
111,
]),
foundry_counter: 0,
unlock_conditions: UnlockConditions(
BoxedSlicePrefix([
StateControllerAddress(
StateControllerAddressUnlockCondition(
Ed25519(
Ed25519Address(0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3),
),
),
),
GovernorAddress(
GovernorAddressUnlockCondition(
Ed25519(
Ed25519Address(0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3),
),
),
),
]),
),
features: Features(
BoxedSlicePrefix([
Sender(
SenderFeature(
Ed25519(
Ed25519Address(0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3),
),
),
),
Metadata(
MetadataFeature(0x68656c6c6f),
),
]),
),
immutable_features: Features(
BoxedSlicePrefix([
Issuer(
IssuerFeature(
Ed25519(
Ed25519Address(0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3),
),
),
),
Metadata(
MetadataFeature(0x68656c6c6f),
),
]),
),
},
)
{
"type": 4,
"amount": "59200",
"aliasId": "0x0000000000000000000000000000000000000000000000000000000000000000",
"stateIndex": 0,
"stateMetadata": "0x68656c6c6f",
"foundryCounter": 0,
"unlockConditions": [
{
"type": 4,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
},
{
"type": 5,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
}
],
"features": [
{
"type": 0,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
},
{
"type": 2,
"data": "0x68656c6c6f"
}
],
"immutableFeatures": [
{
"type": 1,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
},
{
"type": 2,
"data": "0x68656c6c6f"
}
]
}
{
"type": 4,
"amount": "59200",
"aliasId": "0x0000000000000000000000000000000000000000000000000000000000000000",
"stateIndex": 0,
"stateMetadata": "0x68656c6c6f",
"foundryCounter": 0,
"unlockConditions": [
{
"type": 4,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
},
{
"type": 5,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
}
],
"features": [
{
"type": 0,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
},
{
"type": 2,
"data": "0x68656c6c6f"
}
],
"immutableFeatures": [
{
"type": 1,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
},
{
"type": 2,
"data": "0x68656c6c6f"
}
]
}
{
"jsonObject": {
"type": 4,
"amount": "59200",
"aliasId": "0x0000000000000000000000000000000000000000000000000000000000000000",
"stateIndex": 0,
"stateMetadata": "0x68656c6c6f",
"foundryCounter": 0,
"unlockConditions": [
{
"type": 4,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
},
{
"type": 5,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
}
],
"features": [
{
"type": 0,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
},
{
"type": 2,
"data": "0x68656c6c6f"
}
],
"immutableFeatures": [
{
"type": 1,
"address": {
"type": 0,
"pubKeyHash": "0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3"
}
},
{
"type": 2,
"data": "0x68656c6c6f"
}
]
}
}