Linking a namespace to a mosaic

Alias an mosaic with a namespace so that others can reference it in a more friendly way when issuing transactions.

Prerequisites

Method #01: Using the Desktop Wallet

  1. Click on “Namespace” on the left-side menu.
../../_images/desktop-link-mosaic-1.gif
  1. Click on the edit icon of the namespace you desire to link to a mosaic. Click “Link”.
  2. Select “Link a mosaic” as the alias type. Select the ID of the mosaic you desire to connect to the namespace. Click “Send”. Verify the information on the next page and enter your wallet password. Click “Confirm”.
../../_images/desktop-link-mosaic-2.gif
  1. You can check that the mosaic has been linked by going to the “Mosaic” page. The name displayed for the mosaic should be the linked namespace.
../../_images/desktop-link-mosaic-3.gif

Method #02: Using the SDK

  1. Open a new file and define the namespace identifier and the mosaic identifier you want to alias.

Note

The account signing the transaction must own the namespace and mosaic being aliased.

// replace with namespace name
const namespaceId = new NamespaceId('foo');
// replace with mosaic id
const mosaicId = new MosaicId('7cdf3b117a3c40cc');
// replace with namespace name
const namespaceId = new symbol_sdk_1.NamespaceId('foo');
// replace with mosaic id
const mosaicId = new symbol_sdk_1.MosaicId('7cdf3b117a3c40cc');
  1. Then, announce the AliasTransaction that links the namespace and the mosaic.
// replace with networkType
const networkType = NetworkType.TEST_NET;

const mosaicAliasTransaction = AliasTransaction.createForMosaic(
    Deadline.create(),
    AliasAction.Link,
    namespaceId,
    mosaicId,
    networkType,
    UInt64.fromUint(2000000));

// replace with private key
const privateKey = '1111111111111111111111111111111111111111111111111111111111111111';
const account = Account.createFromPrivateKey(privateKey, networkType);
// replace with meta.networkGenerationHash (nodeUrl + '/node/info')
const networkGenerationHash = '1DFB2FAA9E7F054168B0C5FCB84F4DEB62CC2B4D317D861F3168D161F54EA78B';
const signedTransaction = account.sign(mosaicAliasTransaction, networkGenerationHash);
// replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.096x.symboldev.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 networkType
const networkType = symbol_sdk_1.NetworkType.TEST_NET;
const mosaicAliasTransaction = symbol_sdk_1.AliasTransaction.createForMosaic(symbol_sdk_1.Deadline.create(), symbol_sdk_1.AliasAction.Link, namespaceId, mosaicId, networkType, symbol_sdk_1.UInt64.fromUint(2000000));
// replace with private key
const privateKey = '1111111111111111111111111111111111111111111111111111111111111111';
const account = symbol_sdk_1.Account.createFromPrivateKey(privateKey, networkType);
// replace with meta.networkGenerationHash (nodeUrl + '/node/info')
const networkGenerationHash = '1DFB2FAA9E7F054168B0C5FCB84F4DEB62CC2B4D317D861F3168D161F54EA78B';
const signedTransaction = account.sign(mosaicAliasTransaction, networkGenerationHash);
// replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.096x.symboldev.network:3000';
const repositoryFactory = new symbol_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.

  1. Now you can send transactions using the namespace linked to the mosaic instead of defining the complete MosaicId.
// replace with network type
const networkType = NetworkType.TEST_NET;
// replace with aliased mosaicId
const mosaicId = new NamespaceId('foo');
TransferTransaction.create(
    Deadline.create(),
    Account.generateNewAccount(networkType).address,
    [new Mosaic(mosaicId, UInt64.fromUint(10000000))],
    EmptyMessage,
    networkType,
    UInt64.fromUint(2000000));
// replace with network type
const networkType = symbol_sdk_1.NetworkType.TEST_NET;
// replace with aliased mosaicId
const mosaicId = new symbol_sdk_1.NamespaceId('foo');
symbol_sdk_1.TransferTransaction.create(symbol_sdk_1.Deadline.create(), symbol_sdk_1.Account.generateNewAccount(networkType).address, [new symbol_sdk_1.Mosaic(mosaicId, symbol_sdk_1.UInt64.fromUint(10000000))], symbol_sdk_1.EmptyMessage, networkType, symbol_sdk_1.UInt64.fromUint(2000000));
            final NetworkType networkType = repositoryFactory.getNetworkType().toFuture().get();
            // replace with aliased mosaic
            final String namespaceName = "foo";
            final NamespaceId mosaicId = NamespaceId.createFromName(namespaceName);

            TransferTransactionFactory
                    .create(
                            networkType,
                            Account.generateNewAccount(networkType).getAddress(),
                            Collections.singletonList(
                                    new Mosaic(mosaicId, BigInteger.valueOf(10000000))),
                            PlainMessage.Empty)
                    .maxFee(BigInteger.valueOf(2000000)).build();

Method #03: Using the CLI

To link a namespace and a mosaic, open a terminal window and run the following command. Replace 7cdf3b117a3c40cc with the mosaic identifier and foo with the namespace name to be linked.

symbol-cli transaction mosaicalias --action Link --mosaic 7cdf3b117a3c40cc --namespace-name foo