@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.
68 lines (67 loc) • 3.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = require("@oclif/command");
const cli_ux_1 = require("cli-ux");
const StorageItem_1 = require("../StorageItem");
const ION = require('@decentralized-identity/ion-tools');
class Publish extends command_1.Command {
async run() {
const { args, flags } = this.parse(Publish);
let jsonInitialState;
let didPackage;
let updateStorageItem = false;
if (args.initialState) {
cli_ux_1.default.action.start('Checking initial state.');
// Check if we have been passed a storage item and extract
// the initial state, then check that the initial state
// contains the expected properties.
jsonInitialState = JSON.parse(args.initialState.initialState ? args.initialState.initialState : args.initialState);
if (!jsonInitialState.longForm || !jsonInitialState.ops || jsonInitialState.ops.length === 0) {
throw new Error(`The initialState argument provided does not include the expected properties.`);
}
cli_ux_1.default.action.stop();
}
else {
// Load the DID and return
cli_ux_1.default.action.start(`Loading DID '${flags.friendlyName}' from '${flags.directory}'.`);
didPackage = await StorageItem_1.default.load(flags.directory, flags.friendlyName);
// Check if the DID has already been published
if (didPackage.published) {
throw new Error(`The DID '${flags.friendlyName}' has already been published.`);
}
jsonInitialState = didPackage.initialState;
updateStorageItem = true;
cli_ux_1.default.action.stop();
}
cli_ux_1.default.action.start(`Publishing DID '${flags.friendlyName}'.`);
const did = new ION.DID(jsonInitialState);
const requestBody = await did.generateRequest();
const request = new ION.AnchorRequest(requestBody);
// Now submit the anchoring request
await request.submit();
if (updateStorageItem) {
didPackage.published = true;
didPackage.save(flags.directory);
}
cli_ux_1.default.action.stop();
}
}
exports.default = Publish;
Publish.description = 'Publishes the specified DID to the ION network.';
Publish.examples = [
'$ ion publish {ESCAPED INITIAL STATE} --friendlyName={FRIENDLY_NAME}',
];
Publish.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 should be saved. Defaults to environment variable DID_PATH if set.', env: 'DID_PATH', required: false, dependsOn: ['friendlyName'] }),
// Flag for specifying the friendly name of the DID to load
friendlyName: command_1.flags.string({ description: 'specifies the friendly name of the DID to load and publish.', required: false, dependsOn: ['directory'] }),
};
Publish.args = [
{
name: 'initialState',
required: false,
description: 'the initial state of the DID being published.',
}
];