Registering a namespace
Register your own namespace.
Background Information
Namespaces allow you to create an on-chain unique place for your business and your assets on the Sirius Chain.
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
- Finish the getting started section.
- XPX-Chain-SDK or XPX-Chain-CLI.
- A text editor or IDE.
- An account with XPX.
Getting into some code
- 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
. - Check if this namespace name is available.
conf, err := sdk.NewConfig(context.Background(), []string{"http://localhost:3000"})
if err != nil {
panic(err)
}
// Use the default http client
client := sdk.NewClient(nil, conf)
namespaceId, err := sdk.NewNamespaceIdFromName("foo")
if err != nil {
panic(err)
}
namespaceInfo, err := client.Namespace.GetNamespaceInfo(context.Background(), namespaceId)
if err != nil {
panic(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));
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);
xpx2-cli namespace info --name foo
- 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 Sirius Chain, blocks are complete every 15
seconds in average. You will have to renew your namespace before it expires.
// Create an account from a private key
account, err := client.NewAccountFromPrivateKey(os.Getenv("PRIVATE_KEY"))
if err != nil {
panic(err)
}
// Create a new namespace type transaction
transaction, err := client.NewRegisterRootNamespaceTransaction(
// The maximum amount of time to include the transaction in the blockchain.
sdk.NewDeadline(time.Hour),
// Name of namespace
"foo",
// Duration of namespace life in blocks
sdk.Duration(1000),
)
if err != nil {
panic(err)
}
// Sign transaction
signedTransaction, err := account.Sign(transaction)
if err != nil {
panic(err)
}
// Announce transaction
_, err = client.Transaction.Announce(context.Background(), signedTransaction)
if err != nil {
panic(err)
}
const transactionHttp = new TransactionHttp('http://localhost:3000');
const privateKey = process.env.PRIVATE_KEY as string;
const account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
const namespaceName = "foo"; //Replace with an unique namespace name
const registerNamespaceTransaction = RegisterNamespaceTransaction.createRootNamespace(
Deadline.create(),
namespaceName,
UInt64.fromUint(1000),
NetworkType.TEST_NET);
const signedTransaction = account.sign(registerNamespaceTransaction, generationHash);
transactionHttp
.announce(signedTransaction)
.subscribe(x => console.log(x), err => console.error(err));
const transactionHttp = new TransactionHttp('http://localhost:3000');
const privateKey = process.env.PRIVATE_KEY;
const account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
const namespaceName = "foo"; //Replace with an unique namespace name
const registerNamespaceTransaction = RegisterNamespaceTransaction.createRootNamespace(
Deadline.create(),
namespaceName,
UInt64.fromUint(1000),
NetworkType.TEST_NET);
const signedTransaction = account.sign(registerNamespaceTransaction, generationHash);
transactionHttp
.announce(signedTransaction)
.subscribe(x => console.log(x), err => console.error(err));
// Replace with private key
final String privateKey = "<privateKey>";
final Account account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
// Replace with namespace name
final String namespaceName = "foo";
final RegisterNamespaceTransaction registerNamespaceTransaction = new TransactionBuilderFactory()
.registerNamespace().rootNamespace(namespaceName)
.duration(BigInteger.valueOf(1000))
.deadline(new Deadline(2, ChronoUnit.HOURS))
.networkType(NetworkType.TEST_NET).build();
final SignedTransaction signedTransaction = account.sign(registerNamespaceTransaction, generationHash);
final TransactionHttp transactionHttp = new TransactionHttp("http://localhost:3000");
transactionHttp.announce(signedTransaction).toFuture().get();
What’s next?
When the transaction is confirmed, register a subnamespace following the next guide.