# Core - Full Integration

Our Core Library allows developers to integrate fully with Decentraweb and interact with our Smart Contracts, providing tools for :&#x20;

* Reading and Setting Decentraweb Domain Records&#x20;
* 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 :**&#x20;

{% embed url="<https://www.npmjs.com/package/@decentraweb/core>" %}

**View our Advanced Docs :**&#x20;

{% embed url="<https://decentraweb.github.io/decentrawebjs/modules/_decentraweb_core.html>" %}

***

### Installation

To install Decentraweb Core Library, run the following within your terminal to your project directory

```
npm install --save @decentraweb/core ethers@5
```

***

### Initialization

Initialize Decentraweb instance to read/write domain records :&#x20;

```javascript
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. `provider` (required) - [ethers Provider](https://docs.ethers.io/v5/api/providers/provider/) instance to read blockchain data.
3. `signer` (optional) - [ethers Signer](https://docs.ethers.io/v5/api/signer/) instance. Only required if you want to write data to blockchain.
4. `contracts` (optional) - used to override default Decentraweb contract addresses. Only needs to be used for development purposes.

This library is using [ethers.js](https://docs.ethers.io/v5/) to interact with Ethereum blockchain. `ethers.js` is included as peer dependency, so don't forget to add it to your `package.json`

***

### Browser Bundle (CDN Option)

In most cases importing library using `npm` is preferred way, but for fast prototyping you can load it from our CDN :&#x20;

```html
<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>
```

***

### Allowing others to Register Web3 Domains from your Web3 TLD

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](https://docs.decentraweb.org/decentraweb/learn/using-decentraweb-domains/using-domains-bridge))

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](https://dns.decentraweb.org)

To initialize allowing Web3 Domain Registrations from your TLD within your dApp :&#x20;

```javascript
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);
});
```

***

### Reading & Writing Decentraweb Domain Records

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.

#### Writing Data to the Blockchain

A valid signer instance must be provided to enable writing data to blockchain.

All write operations return instance of ethers.js [TransactionResponse](https://docs.ethers.io/v5/api/providers/types/#providers-TransactionResponse) class. You can call `transaction.wait(n)` to wait until transaction get `n` confirmations.

#### Address Resolution

Decentraweb domain supports setting wallet addresses for multiple cryptocurrencies. Decentraweb is compatible with ENS ([EIP-2304](https://eips.ethereum.org/EIPS/eip-2304)) and uses [@ensdomains/address-encoder](https://www.npmjs.com/package/@ensdomains/address-encoder) package to encode/decode wallet addresses.

Full list of supported cryptocurrencies can be found in [@ensdomains/address-encoder](https://www.npmjs.com/package/@ensdomains/address-encoder) documentation.

**Get Wallet Address**

```javascript
const name = dweb.name('test');
const addr = await name.getAddress('ETH');
```

**Set Wallet Address**

```javascript
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 :

```javascript
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:

```javascript
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:

```javascript
const name = await dweb.getReverseRecord('0x71C7656EC7ab88b098defB751B7401B5f6d8976F', true);
```

***

### Reading & Setting DNS Records

DNS records are stored in binary format known as DNS Wireformat. This library utilize [dns-packet](https://www.npmjs.com/package/dns-packet) package to encode/decode DNS data. This library exports `RecordSet` utility class to help with encoding/decoding.

#### Reading DNS Records

```javascript
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);
```

#### Setting DNS Records

```javascript
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);
```

***

### Reading & Setting Content Hash

Decentraweb domains support storing content hash. This feature is following ENS [EIP-1577](https://eips.ethereum.org/EIPS/eip-1577) standard. Supported content hash URL formats:

```
 (ipfs|ipns|bzz|onion|onion3)://{hash}
    /(ipfs|ipns)/{hash}
```

#### Reading Content URL

```javascript
const name = dweb.name('test');
const contentURL = await name.getContenthash();
```

#### Setting Content URL

```javascript
const name = dweb.name('test');
const tx = await name.setContenthash('ipfs://bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze');
//Optionally wait until first transaction confirmation
await tx.wait(1);
```

***

### Reading & Setting Text Records

Text records allow domain owner to store simple key-value string data in domain. To remove text record simply set it to empty string.

#### Reading Text Records

```javascript
const name = dweb.name('test');
const email = await name.getText('email');
```

#### Setting Text Records

```javascript
const name = dweb.name('test');
const tx = await name.setText('email', 'foo@acme.com');
//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 - <integrations@decentraweb.org>
