Giter Club home page Giter Club logo

mavryk-domains-client's Introduction

Tezos Domains Client

Library for resolving and managing tezos domains. Can be used with Taquito or ConseilJS.

WARNING: This project is in beta. We welcome users and feedback, please be aware that this project is a work in progress.

Using the library with Taquito

1) Install @tezos-domains/taquito-client package

yarn add @tezos-domains/taquito-client @tezos-domains/core @taquito/taquito @taquito/rpc @taquito/tzip16
--or--
npm install @tezos-domains/taquito-client @tezos-domains/core @taquito/taquito @taquito/rpc @taquito/tzip16

2a) Use resolver to resolve names and addresses

Example of resolving and address from domain name:

import { TezosToolkit } from '@taquito/taquito';
import { TaquitoTezosDomainsClient } from '@tezos-domains/taquito-client';
import { Tzip16Module } from '@taquito/tzip16';

async function main() {
    const tezos = new TezosToolkit('https://ghostnet.smartpy.io');
    tezos.addExtension(new Tzip16Module());
    const client = new TaquitoTezosDomainsClient({ tezos, network: 'ghostnet', caching: { enabled: true } });

    const address = await client.resolver.resolveNameToAddress('bob.flo');

    console.log(address);
}

2b) Use manager to register and manage domains

Example of registering a domain:

NOTE: registering a domain uses commitment scheme.

NOTE: Installing a signer is necessary for sending transactions.

yarn add @taquito/signer
--or--
npm install @taquito/signer
import { InMemorySigner } from '@taquito/signer';
import { TezosToolkit } from '@taquito/taquito';
import { Tzip16Module } from '@taquito/tzip16';
import { TaquitoTezosDomainsClient } from '@tezos-domains/taquito-client';
import { getTld, getLabel, DomainNameValidationResult, RecordMetadata, generateNonce } from '@tezos-domains/core';

async function main() {
    const tezos = new TezosToolkit('https://ghostnet.smartpy.io');
    tezos.addExtension(new Tzip16Module());
    tezos.setSignerProvider(new InMemorySigner('<your signing key>'));
    const client = new TaquitoTezosDomainsClient({ tezos, network: 'ghostnet' });

    const name = 'foobar.flo';

    // Validate the domain name syntax
    if (client.validator.validateDomainName(name) !== DomainNameValidationResult.VALID) {
        throw new Error('Domain name not valid');
    }

    // Check if the name is not taken already
    const existing = await client.resolver.resolveDomainRecord(name);
    if (existing) {
        throw new Error('Domain name taken.');
    }

    // Use utility function to parse and get parts of a domain name
    const tld = getTld(name);
    const label = getLabel(name);
    const nonce = generateNonce();

    const params = {
        label,
        owner: 'tz1VxMudmADssPp6FPDGRsvJXE41DD6i9g6n',
        nonce
    };

    // First step of registering a domain - create a commitment for
    const commitOperation = await client.manager.commit(tld, params);
    await commitOperation.confirmation();

    // Wait until commitment is usable (usually time between blocks)
    const commitment = await client.manager.getCommitment(tld, params);
    await commitment.waitUntilUsable();

    // Final step - reveal and confirm the registration for specified duration in days
    const buyOperation = await client.manager.buy(tld, {
        ...params,
        duration: 365,
        address: 'tz1VxMudmADssPp6FPDGRsvJXE41DD6i9g6n',
        data: new RecordMetadata(),
    });
    await buyOperation.confirmation();

    console.log(`Domain ${name} has been registered.`);
}

Using the library with ConseilJS

1) Install @tezos-domains/conseil-client package

yarn add @tezos-domains/conseil-client @tezos-domains/core conseiljs node-fetch loglevel @types/node-fetch @types/loglevel
--or--
npm install @tezos-domains/conseil-client @tezos-domains/core conseiljs node-fetch loglevel @types/node-fetch @types/loglevel

2) Use resolver to resolve names and addresses

Example of resolving and address from domain name:

import fetch from 'node-fetch';
import * as log from 'loglevel';
import { registerFetch, registerLogger } from 'conseiljs';
import { ConseilTezosDomainsClient } from '@tezos-domains/conseil-client';

async function main() {
    const logger = log.getLogger('conseiljs');
    logger.setLevel('silent', false);
    registerLogger(logger);
    registerFetch(fetch);

    const client = new ConseilTezosDomainsClient({
        conseil: { server: 'https://ghostnet.smartpy.io' },
        network: 'ghostnet',
        caching: { enabled: true },
    });

    const address = await client.resolver.resolveNameToAddress('bob.flo');

    console.log(address);
}

Options

The client takes options that can customize it's behavior.

network (default: 'mainnet')

  • Specifies which contracts addresses to use. There are built in ones specified for mainnet and ghostnet. For custom you need to also specify contractAddresses.

contractAddresses (default: undefined)

  • Which tezos domains contracts to connect to to get data. Must be specified if network is custom. Uses built in addresses otherwise.

tlds (default: undefined)

  • Which top level domains are supported and the validator function to use to validate domain names of each tld. Must be specified if network is custom. Uses built in tlds otherwise.

tracing (default: false)

  • Whether to output debugging information.

caching (default { enabled: false, defaultRecordTtl: 600, defaultReverseRecordTtl: 600 })

  • Specifies how to handle caching of name and address resolution.

Taquito client specific

tezos (required)

  • Specifies an instance of TezosToolkit to use to make rpc requests.

Conseil client specific

conseil (required)

  • server Specifies tezos rpc url to make requests to.

Developping and building locally

This project requires Yarn.

Building

yarn build

Running tests

yarn test

mavryk-domains-client's People

Contributors

necroskillz avatar sirrocco avatar mbodecek avatar andrewpaulicek avatar alex-tsx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.