Registering a namespace

Create a unique place for your assets.

Background

Namespaces allow you to create an on-chain unique place for your business and your assets on the NEM blockchain.

A namespace starts with a name you choose, similar to an internet domain name. If one account creates a namespace, this will appear as unique in the network.

An account can use the registered namespaces to alias addresses or mosaics identifiers.

Prerequisites

Getting into some code

1. Choose a unique name for your namespace. One common option is to use your company’s or own name. For this example, we are going to register a namespace named foo.

  1. Check if your namespace name is available.
const namespaceHttp = new NamespaceHttp('http://localhost:3000');

const namespace = new NamespaceId('foo');

namespaceHttp
    .getNamespace(namespace)
    .subscribe(namespace => console.log(namespace), err => console.error(err));
const namespaceHttp = new NamespaceHttp('http://localhost:3000');

const namespace = new NamespaceId('foo');

namespaceHttp
    .getNamespace(namespace)
    .subscribe(namespace => console.log(namespace), err => console.error(err));
nem2-cli namespace info --name foo
  1. Is the namespace available? Try to register it before someone else does it! Announce a NamespaceRegistrationTransaction with the chosen name and renting duration expressed in blocks.

Note

A new block completes every 15 seconds on average. Also, the maximum duration you can register a namespace for is a year.

const namespaceName = "foo";
const namespaceRegistrationTransaction = NamespaceRegistrationTransaction.createRootNamespace(
    Deadline.create(),
    namespaceName,
    UInt64.fromUint(1000),
    NetworkType.MIJIN_TEST);

const privateKey = process.env.PRIVATE_KEY as string;
const account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);
const networkGenerationHash = process.env.NETWORK_GENERATION_HASH as string;
const signedTransaction = account.sign(namespaceRegistrationTransaction, networkGenerationHash);

const transactionHttp = new TransactionHttp('http://localhost:3000');
transactionHttp
    .announce(signedTransaction)
    .subscribe(x => console.log(x), err => console.error(err));
const namespaceName = "foo";

const namespaceRegistrationTransaction = NamespaceRegistrationTransaction.createRootNamespace(
    Deadline.create(),
    namespaceName,
    UInt64.fromUint(1000),
    NetworkType.MIJIN_TEST);

const privateKey = process.env.PRIVATE_KEY;
const account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);
const networkGenerationHash = process.env.NETWORK_GENERATION_HASH;
const signedTransaction = account.sign(namespaceRegistrationTransaction, networkGenerationHash);

const transactionHttp = new TransactionHttp('http://localhost:3000');
transactionHttp
    .announce(signedTransaction)
    .subscribe(x => console.log(x), err => console.error(err));
nem2-cli transaction namespace --name foo --rootnamespace --duration 1000

To keep the ownership of your namespace, you will have to extend its duration before it expires.

What’s next?

When the transaction is confirmed, register a subnamespace following the next guide.