Registering a namespace

Register your own namespace.

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 that you choose, similar to an internet domain name. If one account creates a namespace, that will appear as unique in the network.

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

Prerequisites

Let’s get into some code

  1. Choose 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.
  2. Check if this 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));
        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

A new block completes every 15 seconds on average. You will have to renew your namespace before it expires.

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?

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