Comment on page
Full Integration - Core Library
Learn how to integrate our full core library for interacting with our Smart Contracts
Our Core Library allows developers to integrate fully with Decentraweb and interact with our Smart Contracts, providing tools for :
- Reading and Setting Decentraweb Domain Records
- Reading and Setting DNS & Text Records for Decentraweb Domains
- Registering Web3 Domains (absolutely.anything) and Web3 Subdomains (wow.absolutely.anything)
Our Core Library is perfect for developers looking to provide or sell Web3 Domain registrations, as well as allowing management of Web3 Domains all within their own platform, Website or dApp
Our Core Library contains Decentraweb's latest ABIs and can be used directly or embedded in other projects such as the Decentraweb Resolver and CLI.
View our NPM Package :
To install Decentraweb Core Library, run the following within your terminal to your project directory
npm install --save @decentraweb/core ethers@5
Initialize Decentraweb instance to read/write domain records :
import {providers, Wallet} from "ethers";
import {DWEBRegistry} from "@decentraweb/core";
const ETH_NETWORK = 'goerli';
const JSONRPC_URL = '';
const PRIVATE_KEY = '';
const provider = new providers.JsonRpcProvider(JSONRPC_URL, ETH_NETWORK);
//Signer only required if you want to write data to blockchain
const signer = new Wallet(PRIVATE_KEY, provider);
const contracts = {
"DWEBRegistry": "0x8eb93AB94A6Afa8d416aB1884Ebb5A3f00920a7A",
"DefaultReverseResolver": "0x7d770Cfe9608Ff3AA3F5A34bdCd27c3870a370Da",
"PublicResolver": "0xf157D3559DF1F8c69cb757A1A2cdF8736618E083",
"ReverseRegistrar": "0x3D8f878584199e47a2d40A1E269042E10aa50754"
}
const dweb = new DWEBRegistry({network: ETH_NETWORK, provider, signer, contracts});
Parameters:
- 1.
network
(required) - Ethereum network name (mainnet
,goerli
,matic
,maticmum
) - 2.
- 3.
- 4.
contracts
(optional) - used to override default Decentraweb contract addresses. Only needs to be used for development purposes.
This library is using ethers.js to interact with Ethereum blockchain.
ethers.js
is included as peer dependency, so don't forget to add it to your package.json
In most cases importing library using
npm
is preferred way, but for fast prototyping you can load it from our CDN : <script src="https://cdn.ethers.io/lib/ethers-5.7.umd.min.js" type="application/javascript"></script>
<script src="https://cdn.decentraweb.org/decentraweb-core-2.0.0.min.js" type="application/javascript"></script>
<script>
window.addEventListener('load', () => {
const {DWEBRegistry} = Decentraweb;
const dweb = new DWEBRegistry({network: 'goerli', provider: ethers.getDefaultProvider('goerli')});
const name = dweb.name('some_dweb_name');
})
</script>
Decentraweb Web3 Domains can be registered on Ethereum or Polygon Networks, depending on which network the associated TLD exists within. Owners of Web3 Domains, and yourself as a Web3 TLD owner, can opt to bridge all levels of Decentraweb Domains, Ethereum -> Polygon or Polygon -> Ethereum, at any time by using our Domains Bridge (Read Guide)
To provide Web3 Domain Registrations (e.g absolutely.anything) from your Web3 TLD(s) (e.g .anything) on your own platform, website or dApp, please ensure that you have registered and staked your Web3 TLD(s) within our DNS Platform
To initialize allowing Web3 Domain Registrations from your TLD within your dApp :
import {ethers, providers, Wallet} from "ethers";
import {sld} from "@decentraweb/core";
const ETH_NETWORK = 'mainnet';
const JSONRPC_URL = 'https://mainnet.infura.io/v3/00000000000000000000000000000000';
const PRIVATE_KEY = '0000000000000000000000000000000000000000000000000000000000000000';
const provider = new providers.JsonRpcProvider(JSONRPC_URL, ETH_NETWORK);
const signer = new Wallet(PRIVATE_KEY, provider);
const registrar = new sld.EthereumSLDRegistrar({network: ETH_NETWORK, provider, signer});
async function registerSuddomains() {
const approvedRegistration = await registrar.approveOndemandRegistration([
{name: 'cooldomain', label: 'hello'},
{name: 'anything', label: 'absolutely'}
]);
//It is recommended to cache approvedRegistration object, so you can resume registration if next step fails
const tx = await registrar.registerSubdomains(approvedRegistration);
const receipt = await tx.wait(1);
console.log('Registered, TX hash', receipt.transactionHash);
}
registerSuddomains().then(() => {
console.log('Done');
}).catch(err => {
console.error(err);
});
Domain names support following types of records:
- 1.Wallet Addresses
- 2.Content hash
- 3.Text records
- 4.DNS records
Domains on the Ethereum network supports all types of records, while domains on the Polygon network support only Wallet Addresses at this moment.
A valid signer instance must be provided to enable writing data to blockchain.
All write operations return instance of ethers.js TransactionResponse class. You can call
transaction.wait(n)
to wait until transaction get n
confirmations.Decentraweb domain supports setting wallet addresses for multiple cryptocurrencies. Decentraweb is compatible with ENS (EIP-2304) and uses @ensdomains/address-encoder package to encode/decode wallet addresses.
Get Wallet Address
const name = dweb.name('test');
const addr = await name.getAddress('ETH');
Set Wallet Address
const name = dweb.name('test');
const tx = await name.setAddress('ETH', '0x13BCb838DAEFF08f4E56237098dB1d814eeB837D');
//Optionally wait until first transaction confirmation
await tx.wait(1);
Reverse Resolution
Reverse address resolution is only possible for Ethereum wallets.
To allow reverse resolution for Wallet Address -> Decentraweb Domain :
const tx = await dweb.setReverseRecord('wow.greatdomain');
//Optionally wait until first transaction confirmation
await tx.wait(1);
This will set name "wow.greatdomain" for the wallet address that was used to sign the transaction.
To resolve Ethereum address to name use following method:
const name = await dweb.getReverseRecord('0x71C7656EC7ab88b098defB751B7401B5f6d8976F');
By default
getReverseRecord
also performing forward check. This mean that after finding name by address, it will also check that found name is owned by given address. To resolve address without this check, pass true
as second argument:const name = await dweb.getReverseRecord('0x71C7656EC7ab88b098defB751B7401B5f6d8976F', true);
DNS records are stored in binary format known as DNS Wireformat. This library utilize dns-packet package to encode/decode DNS data. This library exports
RecordSet
utility class to help with encoding/decoding.import {DWEBRegistry, RecordSet} from "@decentraweb/core";
const name = dweb.name('test');
const data = await name.getDNS(RecordSet.recordType.toType('A'));
const aRecords = RecordSet.decode(data);
import {DWEBRegistry, RecordSet} from "@decentraweb/core";
const name = dweb.name('test');
const data = RecordSet.encode([
{
type: 'A',
name: 'test',
ttl: 3600,
class: 'IN',
data: '192.168.0.1'
},
{
type: "TXT",
name: 'test',
ttl: 3600,
class: 'IN',
data: 'this is TXT value'
}
]);
const tx = await name.setDNS(data);
//Optionally wait until first transaction confirmation
await tx.wait(1);
Decentraweb domains support storing content hash. This feature is following ENS EIP-1577 standard. Supported content hash URL formats:
(ipfs|ipns|bzz|onion|onion3)://{hash}
/(ipfs|ipns)/{hash}
const name = dweb.name('test');
const contentURL = await name.getContenthash();
const name = dweb.name('test');
const tx = await name.setContenthash('ipfs://bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze');
//Optionally wait until first transaction confirmation
await tx.wait(1);
Text records allow domain owner to store simple key-value string data in domain. To remove text record simply set it to empty string.
const name = dweb.name('test');
const email = await name.getText('email');
const name = dweb.name('test');
const tx = await name.setText('email', '[email protected]');
//Optionally wait until first transaction confirmation
await tx.wait(1);
This concludes our guide on installing our Core Library. If you should have any questions or need assistance, please reach out to our integrations team - [email protected]