Linking a namespace to a mosaic

Link a namespace to a mosaic.

Prerequisites

Let’s get into some code

An account can link a registered name (namespace or subnamespace) with a mosaic.

  1. Define the namespaceId and the address you want to link.
const transactionHttp = new TransactionHttp('http://localhost:3000');
const privateKey = process.env.PRIVATE_KEY as string;
const account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

const namespaceId = new NamespaceId('foo');
const mosaicId = new MosaicId('7cdf3b117a3c40cc');
const transactionHttp = new TransactionHttp('http://localhost:3000');
const privateKey = process.env.PRIVATE_KEY;
const account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

const namespaceId = new NamespaceId('foo');
const mosaicId = new MosaicId('7cdf3b117a3c40cc');
  1. Announce the alias transaction.
const mosaicAliasTransaction = AliasTransaction.createForMosaic(
    Deadline.create(),
    AliasActionType.Link,
    namespaceId,
    mosaicId,
    NetworkType.MIJIN_TEST
);

const signedTransaction = account.sign(mosaicAliasTransaction);

transactionHttp
    .announce(signedTransaction)
    .subscribe(x => console.log(x), err => console.error(err));
const mosaicAliasTransaction = AliasTransaction.createForMosaic(
    Deadline.create(),
    AliasActionType.Link,
    namespaceId,
    mosaicId,
    NetworkType.MIJIN_TEST
);

const signedTransaction = account.sign(mosaicAliasTransaction);

transactionHttp
    .announce(signedTransaction)
    .subscribe(x => console.log(x), err => console.error(err));

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

What’s next?

Now you can send transactions using the namespace linked to the mosaic instead of defining the complete mosaicId.

const transferTransaction = TransferTransaction.create(
    Deadline.create(),
    recipientAddress,
    [new Mosaic(new NamespaceId('foo'),
            UInt64.fromUint(10000000))],
    EmptyMessage,
    NetworkType.MIJIN_TEST);
const transferTransaction = TransferTransaction.create(
    Deadline.create(),
    recipientAddress,
    [new Mosaic(new NamespaceId('foo'),
            UInt64.fromUint(10000000))],
    EmptyMessage,
    NetworkType.MIJIN_TEST);