Linking a namespace to an address

Link a namespace to an account.

Prerequisites

Let’s get into some code

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

  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 address = Address.createFromRawAddress('SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54');
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 address = Address.createFromRawAddress('SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54');
  1. Announce the alias transaction.
const addressAliasTransaction = AliasTransaction.createForAddress(
    Deadline.create(),
    AliasActionType.Link,
    namespaceId,
    address,
    NetworkType.MIJIN_TEST
);

const signedTransaction = account.sign(addressAliasTransaction);

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

const signedTransaction = account.sign(addressAliasTransaction);

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 to the namespace linked to the account instead of using the complete address.

const recipientAddress = new NamespaceId('foo');

const transferTransaction = TransferTransaction.create(
    Deadline.create(),
    recipientAddress,
    [NetworkCurrencyMosaic.createRelative(10)],
    EmptyMessage,
    NetworkType.MIJIN_TEST);
const recipientAddress = new NamespaceId('foo');

const transferTransaction = TransferTransaction.create(
    Deadline.create(),
    recipientAddress,
    [NetworkCurrencyMosaic.createRelative(10)],
    EmptyMessage,
    NetworkType.MIJIN_TEST);