Skip to content

Instantly share code, notes, and snippets.

@johnnywu-namebase
Created August 3, 2020 21:36
Show Gist options
  • Select an option

  • Save johnnywu-namebase/8528bda43339ace9cc01de2cc5cbf828 to your computer and use it in GitHub Desktop.

Select an option

Save johnnywu-namebase/8528bda43339ace9cc01de2cc5cbf828 to your computer and use it in GitHub Desktop.

Revisions

  1. johnnywu-namebase created this gist Aug 3, 2020.
    1 change: 1 addition & 0 deletions "Records" array
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    { ttl: 10800, type: ‘NS’, host: ‘ns1’, value: ‘44.231.6.183’ }
    3 changes: 3 additions & 0 deletions Authenticate API
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    const credentials = Buffer.from(`${ACCESS_KEY}:${SECRET_KEY}`);
    const encodedCredentials = credentials.toString('base64');
    const authorization = `Basic ${encodedCredentials}`;
    1 change: 1 addition & 0 deletions Delete all TXT records
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    node namebaseApi.js put nameserver example ‘{ “records”: [], “deleteRecords”: [{ “type”: “TXT”, “host”: “foo” }] }’
    75 changes: 75 additions & 0 deletions HTTP requests to command line
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    const fetch = require('node-fetch');

    const ACCESS_KEY = '';
    const SECRET_KEY = '';

    const credentials = Buffer.from(`${ACCESS_KEY}:${SECRET_KEY}`);
    const encodedCredentials = credentials.toString('base64');
    const authorization = `Basic ${encodedCredentials}`;

    async function get(endpoint, body = null) {
    const options = {
    method: 'GET',
    headers: {
    Authorization: authorization,
    Accept: 'application/json',
    'Content-Type': 'application/json',
    },
    };
    const url = `https://namebase.io${endpoint}`;
    return fetch(url, options)
    .then(res => res.json())
    .catch(err => err);
    }

    async function put(endpoint, body) {
    const options = {
    method: 'PUT',
    body: body,
    headers: {
    Authorization: authorization,
    Accept: 'application/json',
    'Content-Type': 'application/json',
    },
    };
    const url = `https://namebase.io${endpoint}`;
    return fetch(url, options)
    .then(res => res.json())
    .catch(err => err);
    }

    async function main() {
    const args = process.argv.slice(2);

    let func, path, data;

    const domain = args[2];

    if (args[0] === 'get') {
    func = get;
    } else if (args[0] === 'put') {
    func = put;

    if (args.length < 4) throw new Error('Put requires 4 arguments');

    data = args[3];
    } else {
    throw new Error("Method should be either 'get' or 'put'");
    }

    if (args[1] === 'blockchain') {
    path = `/api/v0/dns/domains/${domain}`;
    } else if (args[1] === 'blockchain-advanced') {
    path = `/api/v0/dns/domains/${domain}/advanced`;
    } else if (args[1] === 'nameserver') {
    path = `/api/v0/dns/domains/${domain}/nameserver`;
    } else {
    throw new Error("Service should be 'blockchain', 'blockchain-advanced' or 'nameserver'");
    }

    return func(path, data);
    }

    main().then(res => {
    console.log(res);
    });
    1 change: 1 addition & 0 deletions Retrieve your name's Handshake records
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    node ./namebaseApi.js get blockchain YOUR_DOMAIN
    1 change: 1 addition & 0 deletions Run to set record
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    node ./namebaseApi.js put blockchain YOUR_DOMAIN ‘{“records”: [{ “ttl”: 10800, “type”: “NS”, “host”: “ns1”, “value”: “44.231.6.183” }] }’
    1 change: 1 addition & 0 deletions Set up Skynet portal
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    node namebaseApi.js put blockchain YOUR_DOMAIN ‘{ “records”: [{ “type”: “TXT”, “host”: “”, “value”: “[YOUR_SKYLINK]”, “ttl”: 0 }] }’
    1 change: 1 addition & 0 deletions Update blockchain Skylink
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    node namebaseApi.js put blockchain example ‘{ “records”: [{ “type”: “TXT”, “host”: “”, “value”: “[YOUR_SKYLINK]”, “ttl”: 0 }] }’
    1 change: 1 addition & 0 deletions Using the Script
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    node ./namebaseApi.js METHOD SERVICE DOMAIN RECORD_DATA