@stacks/bns
A package for interacting with the BNS contract
on the Stacks blockchain.
What is BNS?
The Blockchain Naming System
(BNS) is a network system that binds Stacks usernames to off-chain
state without relying on any central points of control.
Installation
npm install --save @stacks/bns
Example Usages
Check availability
Check if name can be registered
import { canRegisterName } from '@stacks/bns';
import { StacksTestnet, StacksMainnet } from '@stacks/network';
const network = new StacksTestnet();
const fullyQualifiedName = 'name.id';
const result = await canRegisterName({ fullyQualifiedName, network });
Get name price
Get price of name registration in microstacks
import { getNamePrice } from '@stacks/bns';
import { StacksTestnet, StacksMainnet } from '@stacks/network';
const network = new StacksTestnet();
const fullyQualifiedName = 'name.id';
const price = await getNamePrice({ fullyQualifiedName, network });
Steps to register name
Send two transaction to secure a name, preorder and then register.
- Preorder: Generates a name preorder transaction. First step in registering a name. This transaction does not reveal the name that is about to be registered. And it sets the amount of STX to be burned for the registration.
- Register: Generates a name transfer transaction. This changes the owner of the registered name.
Preorder
import { buildPreorderNameTx } from '@stacks/bns';
import { StacksTestnet, StacksMainnet } from '@stacks/network';
import {
TxBroadcastResult,
broadcastTransaction,
publicKeyToString,
TransactionSigner,
createStacksPrivateKey,
pubKeyfromPrivKey,
} from '@stacks/transactions';
const network = new StacksTestnet();
const fullyQualifiedName = 'name.id';
const salt = 'salt';
const stxToBurn = 200n;
const privateKey = '<insert private key here>';
const publicKey = publicKeyToString(pubKeyfromPrivKey(privateKey));
const unsignedTX = await buildPreorderNameTx({
fullyQualifiedName,
salt,
stxToBurn,
publicKey,
network,
});
const signer = new TransactionSigner(unsignedTX);
signer.signOrigin(createStacksPrivateKey(privateKey));
const reply: TxBroadcastResult = await broadcastTransaction(signer.transaction, network);
Register
import { buildRegisterNameTx } from '@stacks/bns';
import { StacksTestnet, StacksMainnet } from '@stacks/network';
import {
TxBroadcastResult,
broadcastTransaction,
publicKeyToString,
TransactionSigner,
createStacksPrivateKey,
pubKeyfromPrivKey,
} from '@stacks/transactions';
const network = new StacksTestnet();
const fullyQualifiedName = 'name.id';
const privateKey = '<insert private key here>';
const zonefile = 'zonefile';
const salt = 'salt';
const publicKey = publicKeyToString(pubKeyfromPrivKey(privateKey));
const unsignedTX = await buildRegisterNameTx({
fullyQualifiedName,
publicKey,
salt,
zonefile,
network,
});
const signer = new TransactionSigner(unsignedTX);
signer.signOrigin(createStacksPrivateKey(privateKey));
const reply: TxBroadcastResult = await broadcastTransaction(
signer.transaction,
network,
Buffer.from(zonefile)
);
Transfer name
Transfer the ownership to other address
import { buildTransferNameTx } from '@stacks/bns';
import { StacksTestnet, StacksMainnet } from '@stacks/network';
import {
TxBroadcastResult,
broadcastTransaction,
publicKeyToString,
TransactionSigner,
createStacksPrivateKey,
pubKeyfromPrivKey,
} from '@stacks/transactions';
const network = new StacksTestnet();
const fullyQualifiedName = 'name.id';
const newOwnerAddress = 'ST1HB1T8WRNBYB0Y3T7WXZS38NKKPTBR3EG9EPJKR';
const privateKey = '<insert private key here>';
const zonefile = 'zonefile';
const publicKey = publicKeyToString(pubKeyfromPrivKey(privateKey));
const unsignedTX = await buildTransferNameTx({
fullyQualifiedName,
newOwnerAddress,
publicKey,
zonefile,
network,
});
const signer = new TransactionSigner(unsignedTX);
signer.signOrigin(createStacksPrivateKey(privateKey));
const reply: TxBroadcastResult = await broadcastTransaction(
signer.transaction,
network,
Buffer.from(zonefile)
);
Update name
Generates a name update transaction. This changes the zonefile for the registered name.
import { buildUpdateNameTx } from '@stacks/bns';
import { StacksTestnet, StacksMainnet } from '@stacks/network';
import {
TxBroadcastResult,
broadcastTransaction,
publicKeyToString,
TransactionSigner,
createStacksPrivateKey,
pubKeyfromPrivKey,
} from '@stacks/transactions';
const network = new StacksTestnet();
const fullyQualifiedName = 'name.id';
const privateKey = '<insert private key here>';
const zonefile = 'zonefile';
const publicKey = publicKeyToString(pubKeyfromPrivKey(privateKey));
const unsignedTX = await buildUpdateNameTx({
fullyQualifiedName,
zonefile,
publicKey,
network,
});
const signer = new TransactionSigner(unsignedTX);
signer.signOrigin(createStacksPrivateKey(privateKey));
const reply: TxBroadcastResult = await broadcastTransaction(
signer.transaction,
network,
Buffer.from(zonefile)
);
Renew name
Generates a name renew transaction. This renews a name registration.
import { buildRenewNameTx } from '@stacks/bns';
import { StacksTestnet, StacksMainnet } from '@stacks/network';
import {
TxBroadcastResult,
broadcastTransaction,
publicKeyToString,
TransactionSigner,
createStacksPrivateKey,
pubKeyfromPrivKey,
} from '@stacks/transactions';
const network = new StacksTestnet();
const fullyQualifiedName = 'name.id';
const stxToBurn = 10n;
const newOwnerAddress = 'ST1HB1T8WRNBYB0Y3T7WXZS38NKKPTBR3EG9EPJKR';
const privateKey = '<insert private key here>';
const zonefile = 'zonefile';
const publicKey = publicKeyToString(pubKeyfromPrivKey(privateKey));
const unsignedTX = await buildRenewNameTx({
fullyQualifiedName,
stxToBurn,
newOwnerAddress,
zonefile,
publicKey,
network,
});
const signer = new TransactionSigner(unsignedTX);
signer.signOrigin(createStacksPrivateKey(privateKey));
const reply: TxBroadcastResult = await broadcastTransaction(
signer.transaction,
network,
Buffer.from(zonefile)
);
Revoke name
Generates a name revoke transaction. This revokes a name registration.
import { buildRevokeNameTx } from '@stacks/bns';
import { StacksTestnet, StacksMainnet } from '@stacks/network';
import {
TxBroadcastResult,
broadcastTransaction,
publicKeyToString,
TransactionSigner,
createStacksPrivateKey,
pubKeyfromPrivKey,
} from '@stacks/transactions';
const network = new StacksTestnet();
const fullyQualifiedName = 'name.id';
const privateKey = '<insert private key here>';
const publicKey = publicKeyToString(pubKeyfromPrivKey(privateKey));
const unsignedTX = await buildRevokeNameTx({
fullyQualifiedName,
publicKey,
network,
});
const signer = new TransactionSigner(unsignedTX);
signer.signOrigin(createStacksPrivateKey(privateKey));
const reply: TxBroadcastResult = await broadcastTransaction(signer.transaction, network);