Linking a namespace to a mosaic

Link a namespace to a mosaic.

Prerequisites

Method #01: Using the SDK

  1. 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 nem2_sdk_1.NamespaceId('foo');
// replace with mosaic id
const mosaicId = new nem2_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.generationHash (nodeUrl + '/block/1')
const networkGenerationHash = '45870419226A7E51D61D94AD728231EDC6C9B3086EF9255A8421A4F26870456A';
const signedTransaction = account.sign(mosaicAliasTransaction, 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 networkType
const networkType = nem2_sdk_1.NetworkType.TEST_NET;
const mosaicAliasTransaction = nem2_sdk_1.AliasTransaction.createForMosaic(nem2_sdk_1.Deadline.create(), nem2_sdk_1.AliasAction.Link, namespaceId, mosaicId, 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(mosaicAliasTransaction, 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 mosaicalias --action 1 --mosaic 7cdf3b117a3c40cc --namespace-name foo

What’s next?

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 = nem2_sdk_1.NetworkType.TEST_NET;
// replace with aliased mosaicId
const mosaicId = new nem2_sdk_1.NamespaceId('foo');
nem2_sdk_1.TransferTransaction.create(nem2_sdk_1.Deadline.create(), nem2_sdk_1.Account.generateNewAccount(networkType).address, [new nem2_sdk_1.Mosaic(mosaicId, nem2_sdk_1.UInt64.fromUint(10000000))], nem2_sdk_1.EmptyMessage, networkType, nem2_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();