Registering a namespace

Rregister your own namespace.

Background

A namespace is an on-chain unique domain for your assets. The easiest way to understand it is by means of the domain-file analogy on the internet.

A mosaic is like a file hosted on a domain and represents an asset. Like a website and directory, a mosaic can have the same name as other files on other domains. However, a namespace + mosaic is always unique.

If an account creates a namespace, that namespace will appear as unique in the NEM ecosystem. For example, if one were to create a namespace called foo, a second person cannot create the same namespace.

Prerequisites

Let’s get into some code

Register your namespace by choosing a name you like. One common option is to use your company’s or own name. In this example, we will register a namespace called foo.

  1. Check if this nampespace 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));
        final NamespaceId namespaceId = new NamespaceId("foo");

        final NamespaceHttp namespaceHttp = new NamespaceHttp("http://localhost:3000");

        final NamespaceInfo namespaceInfo = namespaceHttp.getNamespace(namespaceId).toFuture().get();

        System.out.println(namespaceInfo);
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 register namespace transaction with the chosen name and renting duration expressed in blocks.

Note

In Catapult, NEM blocks are complete every 15 seconds in average.

const transactionHttp = new TransactionHttp('http://localhost:3000');

const privateKey = process.env.PRIVATE_KEY as string;
const account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

const namespaceName = "foo"; //Replace with an unique namespace name

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

const signedTransaction = account.sign(registerNamespaceTransaction);

transactionHttp
    .announce(signedTransaction)
    .subscribe(x => console.log(x), err => console.error(err));
        // Replace with private key
        final String privateKey = "";

        final Account account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

        // Replace with namespace name
        final String namespaceName = "foo";

        final RegisterNamespaceTransaction registerNamespaceTransaction = RegisterNamespaceTransaction.createRootNamespace(
                Deadline.create(2, ChronoUnit.HOURS),
                namespaceName,
                BigInteger.valueOf(1000),
                NetworkType.MIJIN_TEST
        );

        final SignedTransaction signedTransaction = account.sign(registerNamespaceTransaction);

        final TransactionHttp transactionHttp = new TransactionHttp("http://localhost:3000");

        transactionHttp.announce(signedTransaction).toFuture().get();
const transactionHttp = new TransactionHttp('http://localhost:3000');

const privateKey = process.env.PRIVATE_KEY;
const account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

const namespaceName = "foo"; //Replace with an unique namespace name

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

const signedTransaction = account.sign(registerNamespaceTransaction);

transactionHttp
    .announce(signedTransaction)
    .subscribe(x => console.log(x), err => console.error(err));
nem2-cli transaction namespace --name foo --rootnamespace --duration 1000

What’s next?

Now that you have registered your namespace, check how you can create mosaics.

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