@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.
86 lines (85 loc) • 4.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = require("@oclif/command");
const Output_1 = require("../Output");
const StorageItem_1 = require("../StorageItem");
const ION = require('@decentralized-identity/ion-tools');
const { cli } = require('cli-ux');
class New extends command_1.Command {
async run() {
const { args, flags } = this.parse(New);
let did;
let privateKey;
let keyPair;
if (flags.input) {
cli.action.start('Creating new ION DID using the specified input');
did = new ION.DID(JSON.parse(flags.input));
privateKey = JSON.parse(flags.jwk);
cli.action.stop();
}
else {
// Create the key pair for the new DID
cli.action.start('Generating key pair');
keyPair = await ION.generateKeyPair(flags.curve);
// Mix in the kid
privateKey = Object.assign({ kid: flags.kid }, keyPair.privateJwk);
cli.action.stop();
// Now generate the DID using the new key
cli.action.start('Creating new ION DID');
did = new ION.DID({
content: {
publicKeys: [
{
id: flags.kid,
type: 'EcdsaSecp256k1VerificationKey2019',
publicKeyJwk: keyPair.publicJwk,
purposes: ['authentication', 'keyAgreement'],
},
],
},
});
}
cli.action.stop();
const didState = await did.getState();
this.log(Output_1.default.toJson(didState, flags.escape));
// If a directory has been specified, save the
// keys and document to the directory to the directory.
if (flags.directory) {
cli.action.start(`Saving DID with name '${args.name}' and to directory path '${flags.directory}.`);
// Create a new storage item
const storageItem = new StorageItem_1.default(args.name, didState, privateKey);
await storageItem.save(flags.directory);
cli.action.stop();
}
}
}
exports.default = New;
New.description = 'Creates a new ION DID with either defaults or the specified input.';
New.examples = [
'$ ion new FriendlyName',
'$ ion new FriendlyName -d d:/dids',
'$ ion new FriendlyName -d d:/dids --curve secp256k1 --kid key-1',
'$ ion new FriendlyName -d d:/dids --input {ESCAPED JSON STRING} --jwk {ESCAPED PRIVATE KEY JWK}',
];
New.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 curve to use when generating keys.
curve: command_1.flags.enum({ char: 'c', description: 'specify the elliptic curve to use for the keys.', options: ['secp256k1', 'Ed25519'], default: 'secp256k1', exclusive: ['input', 'jwk'] }),
// Flag for specifying key pair identifier.
kid: command_1.flags.string({ description: ' for the key pair.', default: 'key-1', exclusive: ['input', 'jwk'] }),
// Flag for specifying the input to use when creating a new DID.
input: command_1.flags.string({ description: 'specifies the input to use when generating the ION DID.', exclusive: ['curve', 'kid'], dependsOn: ['jwk'] }),
// Flag for specifying the private key for the DID if the input flag is being used
jwk: command_1.flags.string({ description: 'specifies the private key for the DID.', exclusive: ['curve', 'kid'], dependsOn: ['input'] }),
// 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.' }),
};
New.args = [
{
name: 'name',
required: true,
description: 'name for the new DID. Name should not include spaces or special characters.',
},
];