@decentralized-identity/ion-cli
Version:
A Command Line Interface (CLI) to make working with the ION network and using ION DIDs easy peasy lemon squeezy.
104 lines (103 loc) • 5.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = require("@oclif/command");
const CacheItem_1 = require("../CacheItem");
const Output_1 = require("../Output");
const ION = require('@decentralized-identity/ion-tools');
const { cli } = require('cli-ux');
class Resolve extends command_1.Command {
/**
* Checks whether the state should be refreshed by comparing the
* refreshedAt with the current time adjusted by timeToLive.
* @param refreshedAt date and time the state was last refreshed.
* @param timeToLive to determine whether the state should be refreshed.
*/
static isExpired(refreshedAt, timeToLive) {
const now = Date.now() / 1000;
const nextRefresh = new Date(refreshedAt).getTime() / 1000 + timeToLive;
return (nextRefresh <= now);
}
async run() {
var _a, _b, _c;
const { args, flags } = this.parse(Resolve);
const options = {};
if (flags.node) {
options.nodeEndpoint = flags.node;
}
let didDocument;
// Check if the cache flag has been enabled. If
// so check if we have a cached instance and
// if it is with in the cache lifetime.
cli.action.start('Resolving DID');
if (flags.cache) {
cli.action.start('Checking DID cache');
const cachedItem = await CacheItem_1.default.read(flags.directory, flags.name);
if (cachedItem) {
cli.action.stop('DID found in cache.');
cli.action.start('Checking expiry.');
const isExpired = Resolve.isExpired(cachedItem.lastResolved, flags.cacheTtl);
if (isExpired) {
cli.action.stop('cache item expired. Resolving DID.');
didDocument = await ION.resolve(args.DID, options);
// Update the cache item and then save
cachedItem.document = didDocument;
cachedItem.lastResolved = new Date();
cachedItem.published = (_c = (_b = (_a = didDocument === null || didDocument === void 0 ? void 0 : didDocument.didDocumentMetadata) === null || _a === void 0 ? void 0 : _a.method) === null || _b === void 0 ? void 0 : _b.published) !== null && _c !== void 0 ? _c : 'N/A';
await cachedItem.save(flags.directory);
cli.action.stop();
}
else {
cli.action.stop('returning cached DID.');
// Return the document from the state
didDocument = cachedItem.document;
}
}
else {
cli.action.stop('DID not in cache.');
// Never resolved before, so resolve first then
// create a cache item and save
didDocument = await ION.resolve(args.DID, options);
const newCacheItem = new CacheItem_1.default(flags.name, args.DID, didDocument, new Date(), didDocument.didDocumentMetadata.method.published);
cli.action.stop();
cli.action.start('Caching DID.');
await newCacheItem.save(flags.directory);
cli.action.stop();
}
}
else {
didDocument = await ION.resolve(args.DID, options);
}
cli.action.stop();
this.log(Output_1.default.toJson(didDocument, flags.escape));
}
}
exports.default = Resolve;
Resolve.description = 'Resolves the provided DID and outputs the document to the console, optionally caching the DID state.';
Resolve.examples = [
'$ ion resolve did:ion:EiB29JB4R0mbLmJ6_BEYjr8bGZKEPABwFopSNsDJBh_Diw',
'$ ion resolve did:ion:EiB29JB4R0mbLmJ6_BEYjr8bGZKEPABwFopSNsDJBh_Diw --node https://some.node --escape',
'$ ion resolve did:ion:EiB29JB4R0mbLmJ6_BEYjr8bGZKEPABwFopSNsDJBh_Diw --node https://some.node --cache',
'$ ion resolve did:ion:EiB29JB4R0mbLmJ6_BEYjr8bGZKEPABwFopSNsDJBh_Diw --node https://some.node --cache --cacheTtl 60 --name SomeDID',
];
Resolve.flags = {
help: command_1.flags.help({ char: 'h' }),
// Flag for specifying a directory to which keys and documents should be saved.
directory: command_1.flags.string({ char: 'd', description: 'to which the DID package should be saved. Defaults to environment variable DID_PATH if set.', env: 'DID_PATH' }),
// Flag for specifying the node to use for resolving DIDs.
node: command_1.flags.string({ description: 'URI of the node you desire to contact for resolution. If you are running your own node, use this to pass in your node\'s resolution endpoint.' }),
// Flag for specifying the JSON string output should be escaped.
escape: command_1.flags.boolean({ description: 'specifies that the output JSON string should be escaped. Use this when using the output as input to another command.' }),
// Flag for specifying the name of the DID to use when caching.
name: command_1.flags.string({ description: 'URI of the node you desire to contact for resolution. If you are running your own node, use this to pass in your node\'s resolution endpoint.' }),
// Flag for specifying the ttl for the document cache. Default is 24hrs.
cacheTtl: command_1.flags.integer({ description: 'specifies the time to live (ttl) for a cached document in in seconds.', default: 86400 }),
// Flag for specifying that a successful resolution should be cached.
cache: command_1.flags.boolean({ description: 'specifies that the resolved document should be cached in the specified directory and if cached read from the directory.', dependsOn: ['directory', 'name'] }),
};
Resolve.args = [
{
name: 'DID',
required: true,
description: 'The DID to resolve',
},
];