Registering namespaces

Registering a namespace

The purpose of this guide is to register your own namespace.

Background

A namespace is an on-chain unique domain for your assets.

The easiest way to appreciate it is the domain and file analogy on the internet. Imagine that a domain address has to be unique in a root (lowest level).

If one account creates a namespace, that namespace will appear 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

The first step is to choose a name for your namespace. A root namespace may have a length of N characters.

One common option is to choose your company’s name. This name should be available, as is unique. In this example, we will register a namespace called foo. Let’s check if this name is available.

const namespaceHttp = new NamespaceHttp('http://localhost:3000');

//Replace with namespaceId
const namespace = new NamespaceId('foo');

namespaceHttp.getNamespace(namespace).subscribe(namespace => console.log(namespace));
        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');

//Replace with namespaceId
const namespace = new NamespaceId('foo');

namespaceHttp.getNamespace(namespace).subscribe(namespace => console.log(namespace));
nem2-cli namespace info --name foo

Is the namespace available? Try to register it before someone else does it!

To register a new namespace, announce a register namespace transaction with the chosen name and renting duration expressed in blocks.

//Replace with private key
const privateKey = process.env.PRIVATE_KEY as string;

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

//Replace with namespace name
const namespace = "foo";

const registerNamespaceTransaction = RegisterNamespaceTransaction.createRootNamespace(
    Deadline.create(),
    namespace, // use your own namespace name
    UInt64.fromUint(100000),
    NetworkType.MIJIN_TEST,
);

const signedTransaction = account.sign(registerNamespaceTransaction);

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

transactionHttp.announce(signedTransaction).subscribe(x => console.log(x));
        // 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(100000),
                NetworkType.MIJIN_TEST
        );

        final SignedTransaction signedTransaction = account.sign(registerNamespaceTransaction);

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

        transactionHttp.announce(signedTransaction).toFuture().get();
//Replace with private key
const privateKey = process.env.PRIVATE_KEY;

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

//Replace with namespace name
const namespace = "foo";

const registerNamespaceTransaction = RegisterNamespaceTransaction.createRootNamespace(
    Deadline.create(),
    namespace, // use your own namespace name
    UInt64.fromUint(100000),
    NetworkType.MIJIN_TEST,
);

const signedTransaction = account.sign(registerNamespaceTransaction);

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

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

What’s next?

Now that you have registered your namespace, you can start creating mosaics.

Also, when the transaction is confirmed, you will be able to register a subnamespace by following next guide.

Registering a subnamespace

After creating a namespace, this tutorial will help you to register a subnamespace.

Background

Having a root namespace registered we can create up to 3 levels of subnamespaces.

Subnamespaces do not have a renting duration by its own. They have the same one as their parent namespace.

It is possible to create multiple subnamespaces with the same name in different namespaces (example: foo.bar and foo2.bar).

Prerequisites

Let’s get into some code

The first step is to choose a name for your subnamespace.

In this example, we will register a namespace called bar under foo namespace. Subnamespaces may have a length of 64 characters maximum.

// Replace with private key
const privateKey = process.env.PRIVATE_KEY as string;

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

//Replace with root namespace name
const rootNamespaceName = 'foo';

//Replace with subnamespace name
const subnamespace = 'bar';

const registerNamespaceTransaction = RegisterNamespaceTransaction.createSubNamespace(
    Deadline.create(),
    subnamespace,
    rootNamespaceName,
    NetworkType.MIJIN_TEST
);

const signedTransaction = account.sign(registerNamespaceTransaction);

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

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

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

        // Replace with root namespace name
        final NamespaceId rootNamespaceId = new NamespaceId("foo");

        //Replace with subnamespace name
        final String subnamespaceName = "bar";

        final RegisterNamespaceTransaction registerNamespaceTransaction = RegisterNamespaceTransaction.createSubNamespace(
                Deadline.create(2, ChronoUnit.HOURS),
                subnamespaceName,
                rootNamespaceId,
                NetworkType.MIJIN_TEST
        );

        final SignedTransaction signedTransaction = account.sign(registerNamespaceTransaction);

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

        transactionHttp.announce(signedTransaction).toFuture().get();
// Replace with private key
const privateKey = process.env.PRIVATE_KEY;

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

//Replace with root namespace name
const rootNamespaceName = 'foo';

//Replace with subnamespace name
const subnamespace = 'bar';

const registerNamespaceTransaction = RegisterNamespaceTransaction.createSubNamespace(
    Deadline.create(),
    subnamespace,
    rootNamespaceName,
    NetworkType.MIJIN_TEST
);

const signedTransaction = account.sign(registerNamespaceTransaction);

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

transactionHttp.announce(signedTransaction).subscribe(x => console.log(x));
nem2-cli transaction namespace --subnamespace --parentname foo --name bar