|
| 1 | +import { Argv } from "yargs"; |
| 2 | +import { error, success } from "signale"; |
| 3 | +import { getLogger } from "../../../logger"; |
| 4 | +import { DnsCreateTxtRecordCommand } from "./dns-command.type"; |
| 5 | +import fetch, { RequestInit } from "node-fetch"; |
| 6 | + |
| 7 | +const { trace } = getLogger("dns:txt-record"); |
| 8 | + |
| 9 | +export const command = "create [options]"; |
| 10 | + |
| 11 | +export const describe = "Creates an Issuer's DNS entry in OpenAttestation's sandbox environment for tutorial purposes"; |
| 12 | + |
| 13 | +export const builder = (yargs: Argv): Argv => |
| 14 | + yargs |
| 15 | + .option("address", { |
| 16 | + alias: "a", |
| 17 | + description: "Contract address of the Document Store or Token Registry", |
| 18 | + type: "string", |
| 19 | + demandOption: true |
| 20 | + }) |
| 21 | + .option("networkId", { |
| 22 | + description: "Ethereum network (chain ID) that this record is for", |
| 23 | + type: "number", |
| 24 | + demandOption: true |
| 25 | + }); |
| 26 | + |
| 27 | +const baseUrl = "https://sandbox.openattestation.com"; |
| 28 | + |
| 29 | +const request = (url: string, options?: RequestInit): Promise<any> => { |
| 30 | + return fetch(url, options) |
| 31 | + .then(response => { |
| 32 | + if (!response.ok) { |
| 33 | + throw new Error(`unexpected response ${response.statusText}`); |
| 34 | + } |
| 35 | + return response; |
| 36 | + }) |
| 37 | + .then(response => response.json()); |
| 38 | +}; |
| 39 | + |
| 40 | +export const handler = async (args: DnsCreateTxtRecordCommand): Promise<string | undefined> => { |
| 41 | + trace(`Args: ${JSON.stringify(args, null, 2)}`); |
| 42 | + try { |
| 43 | + const { executionId } = await request(baseUrl, { |
| 44 | + method: "POST", |
| 45 | + headers: { |
| 46 | + "Content-Type": "application/json" |
| 47 | + }, |
| 48 | + body: JSON.stringify({ address: args.address, networkId: args.networkId }) |
| 49 | + }); |
| 50 | + const { name, expiryDate } = await request(`${baseUrl}/execution/${executionId}`); |
| 51 | + success(`Record created at ${name} and will stay valid until ${new Date(expiryDate)}`); |
| 52 | + return name; |
| 53 | + } catch (e) { |
| 54 | + error(e.message); |
| 55 | + } |
| 56 | +}; |
0 commit comments