Linking a namespace to an address

Link a namespace to an account.

Prerequisites

Method #01: Using the SDK

  1. Define the namespace identifier and the address you want to alias.

Note

The account signing the transaction must own the namespace.

// replace with namespace name
const namespaceId = new NamespaceId('foo');
// replace with address
const rawAddress = 'TBULEA-UG2CZQ-ISUR44-2HWA6U-AKGWIX-HDABJV-IPS4';
const address = Address.createFromRawAddress(rawAddress);
// replace with namespace name
const namespaceId = new nem2_sdk_1.NamespaceId('foo');
// replace with address
const rawAddress = 'TBULEA-UG2CZQ-ISUR44-2HWA6U-AKGWIX-HDABJV-IPS4';
const address = nem2_sdk_1.Address.createFromRawAddress(rawAddress);

Then, announce the AliasTransaction that links the namespace and the address.

// replace with network type
const networkType = NetworkType.TEST_NET;

const addressAliasTransaction = AliasTransaction.createForAddress(
    Deadline.create(),
    AliasAction.Link,
    namespaceId,
    address,
    networkType,
    UInt64.fromUint(2000000));

// replace with private key
const privateKey = '1111111111111111111111111111111111111111111111111111111111111111';
const account = Account.createFromPrivateKey(privateKey, networkType);
// replace with meta.generationHash (nodeUrl + '/block/1')
const networkGenerationHash = '45870419226A7E51D61D94AD728231EDC6C9B3086EF9255A8421A4F26870456A';
const signedTransaction = account.sign(addressAliasTransaction, networkGenerationHash);
// replace with node endpoint
const nodeUrl = 'http://api-xym-harvest-3-01.us-west-2.nemtech.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const transactionHttp = repositoryFactory.createTransactionRepository();

transactionHttp
    .announce(signedTransaction)
    .subscribe((x) => console.log(x), (err) => console.error(err));
// replace with network type
const networkType = nem2_sdk_1.NetworkType.TEST_NET;
const addressAliasTransaction = nem2_sdk_1.AliasTransaction.createForAddress(nem2_sdk_1.Deadline.create(), nem2_sdk_1.AliasAction.Link, namespaceId, address, networkType, nem2_sdk_1.UInt64.fromUint(2000000));
// replace with private key
const privateKey = '1111111111111111111111111111111111111111111111111111111111111111';
const account = nem2_sdk_1.Account.createFromPrivateKey(privateKey, networkType);
// replace with meta.generationHash (nodeUrl + '/block/1')
const networkGenerationHash = '45870419226A7E51D61D94AD728231EDC6C9B3086EF9255A8421A4F26870456A';
const signedTransaction = account.sign(addressAliasTransaction, networkGenerationHash);
// replace with node endpoint
const nodeUrl = 'http://api-xym-harvest-3-01.us-west-2.nemtech.network:3000';
const repositoryFactory = new nem2_sdk_1.RepositoryFactoryHttp(nodeUrl);
const transactionHttp = repositoryFactory.createTransactionRepository();
transactionHttp
    .announce(signedTransaction)
    .subscribe((x) => console.log(x), (err) => console.error(err));

Note

If you want to unlink the alias, change alias action type to AliasActionType.Unlink.

Method #02: Using the CLI

nem2-cli transaction addressalias --action 1 --address TBULEA-UG2CZQ-ISUR44-2HWA6U-AKGWIX-HDABJV-IPS4 --namespace-name foo

What’s next?

Now you can send transactions to the namespace linked to the account instead of using the complete address.

// Replace with network type
const networkType = NetworkType.TEST_NET;
// Replace with aliased address
const recipientAddress = new NamespaceId('foo');

TransferTransaction.create(
    Deadline.create(),
    recipientAddress,
    [],
    EmptyMessage,
    networkType,
    UInt64.fromUint(2000000));
// Replace with network type
const networkType = nem2_sdk_1.NetworkType.TEST_NET;
// Replace with aliased address
const recipientAddress = new nem2_sdk_1.NamespaceId('foo');
nem2_sdk_1.TransferTransaction.create(nem2_sdk_1.Deadline.create(), recipientAddress, [], nem2_sdk_1.EmptyMessage, networkType, nem2_sdk_1.UInt64.fromUint(2000000));
            final NetworkType networkType = repositoryFactory.getNetworkType().toFuture().get();
            // replace with aliased address
            final String namespaceName = "foo";
            final UnresolvedAddress recipientAddress = NamespaceId.createFromName(namespaceName);

            TransferTransactionFactory
                    .create(
                            networkType,
                            recipientAddress,
                            Collections.emptyList(),
                            PlainMessage.Empty)
                    .maxFee(BigInteger.valueOf(2000000)).build();