Linking a namespace to a token

Alias an token 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.

resources/images/screenshots/desktop-link-token-1.gif
  1. Click on the edit icon of the namespace you desire to link to a token. Click “Link”.

  2. Select “Link a token” as the alias type. Select the ID of the token you desire to connect to the namespace. Click “Send”. Verify the information on the next page and enter your wallet password. Click “Confirm”.

resources/images/screenshots/desktop-link-token-2.gif
  1. You can check that the token has been linked by going to the “Token” page. The name displayed for the token should be the linked namespace.

resources/images/screenshots/desktop-link-token-3.gif

Method #02: Using the SDK

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

Note

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

// replace with namespace name
const namespaceId = new NamespaceId('foo');
// replace with token id
const tokenId = new TokenId('7cdf3b117a3c40cc');
// replace with namespace name
const namespaceId = new bitxor_sdk_1.NamespaceId('foo');
// replace with token id
const tokenId = new bitxor_sdk_1.TokenId('7cdf3b117a3c40cc');
  1. Then, announce the AliasTransaction that links the namespace and the token.

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

const tokenAliasTransaction = AliasTransaction.createForToken(
  Deadline.create(epochAdjustment),
  AliasAction.Link,
  namespaceId,
  tokenId,
  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(
  tokenAliasTransaction,
  networkGenerationHash,
);
// replace with node endpoint
const nodeUrl = 'NODE_URL';
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 = bitxor_sdk_1.NetworkType.TEST_NET;
const tokenAliasTransaction = bitxor_sdk_1.AliasTransaction.createForToken(
  bitxor_sdk_1.Deadline.create(epochAdjustment),
  bitxor_sdk_1.AliasAction.Link,
  namespaceId,
  tokenId,
  networkType,
  bitxor_sdk_1.UInt64.fromUint(2000000),
);
// replace with private key
const privateKey =
  '1111111111111111111111111111111111111111111111111111111111111111';
const account = bitxor_sdk_1.Account.createFromPrivateKey(
  privateKey,
  networkType,
);
// replace with meta.networkGenerationHash (nodeUrl + '/node/info')
const networkGenerationHash =
  '1DFB2FAA9E7F054168B0C5FCB84F4DEB62CC2B4D317D861F3168D161F54EA78B';
const signedTransaction = account.sign(
  tokenAliasTransaction,
  networkGenerationHash,
);
// replace with node endpoint
const nodeUrl = 'NODE_URL';
const repositoryFactory = new bitxor_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 token instead of defining the complete TokenId.

// replace with network type
const networkType = NetworkType.TEST_NET;
// replace with aliased tokenId
const tokenId = new NamespaceId('foo');
TransferTransaction.create(
  Deadline.create(epochAdjustment),
  Account.generateNewAccount(networkType).address,
  [new Token(tokenId, UInt64.fromUint(10000000))],
  EmptyMessage,
  networkType,
  UInt64.fromUint(2000000),
);
// replace with network type
const networkType = bitxor_sdk_1.NetworkType.TEST_NET;
// replace with aliased tokenId
const tokenId = new bitxor_sdk_1.NamespaceId('foo');
bitxor_sdk_1.TransferTransaction.create(
  bitxor_sdk_1.Deadline.create(epochAdjustment),
  bitxor_sdk_1.Account.generateNewAccount(networkType).address,
  [new bitxor_sdk_1.Token(tokenId, bitxor_sdk_1.UInt64.fromUint(10000000))],
  bitxor_sdk_1.EmptyMessage,
  networkType,
  bitxor_sdk_1.UInt64.fromUint(2000000),
);
            final NetworkType networkType = repositoryFactory.getNetworkType().toFuture().get();
            // replace with aliased token
            final String namespaceName = "foo";
            final NamespaceId tokenId = NamespaceId.createFromName(namespaceName);

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

Method #03: Using the CLI

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

bitxor-cli transaction tokenalias --action Link --token-id 7cdf3b117a3c40cc --namespace-name foo