Last active
April 4, 2016 02:31
-
-
Save notslang/2fdd3d342575ccd0e38d774887119ac0 to your computer and use it in GitHub Desktop.
Multihash CLI in coffeescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base58 = require 'bs58' | |
{ArgumentParser} = require 'argparse' | |
packageInfo = require '../package' | |
multihash = require '../src' | |
printJSON = (obj) -> | |
# print out one key/value per line, so (for example) piping into `wc -l` gives | |
# a count of the total supported functions | |
console.log JSON.stringify(obj, null, 1).replace(/\s*(\}|\{)\s*/gm, '$1') | |
return | |
encodingOption = (parser) -> | |
parser.addArgument( | |
['--encoding'] | |
type: 'string' | |
choices: ['ascii', 'base58', 'base64', 'binary', 'hex', 'utf16le', 'utf8'] | |
help: "The encoding of MULTIHASH. Defaults to base58." | |
defaultValue: 'base58' | |
) | |
argparser = new ArgumentParser( | |
addHelp: true | |
description: packageInfo.description | |
version: packageInfo.version | |
) | |
subcommands = argparser.addSubparsers(dest: 'subcommand') | |
subcommand = subcommands.addParser( | |
'names' | |
description: 'Print out supported hash functions and their associated codes.' | |
addHelp: true | |
) | |
subcommand = subcommands.addParser( | |
'decode' | |
description: 'Validate and decode the supplied MULTIHASH. Digest is output in | |
hex.' | |
addHelp: true | |
) | |
encodingOption(subcommand) | |
subcommand.addArgument( | |
['multihash'] | |
type: 'string' | |
metavar: 'MULTIHASH' | |
) | |
subcommand = subcommands.addParser( | |
'encode' | |
description: 'Encode the supplied digest and hash function into a MULTIHASH' | |
addHelp: true | |
) | |
subcommand.addArgument( | |
['--fn'] | |
type: 'string' | |
help: 'Hash function used to generate DIGEST. Defaults to sha2-256.' | |
choices: Object.keys(multihash.names) | |
defaultValue: 'sha2-256' | |
) | |
encodingOption(subcommand) | |
subcommand.addArgument( | |
['digest'] | |
type: 'string' | |
help: 'The digest generated by the supplied hash function, encoded in hex.' | |
metavar: 'DIGEST' | |
) | |
argv = argparser.parseArgs() | |
switch argv.subcommand | |
when 'names' | |
printJSON(multihash.names) | |
when 'decode' | |
buf = ( | |
if argv.encoding is 'base58' | |
new Buffer(base58.decode(argv.multihash)) | |
else | |
new Buffer(argv.multihash, argv.encoding) | |
) | |
try | |
info = multihash.decode(buf) | |
catch error | |
console.error error.message | |
process.exit(1) | |
info.digest = info.digest.toString('hex') | |
printJSON(info) | |
when 'encode' | |
try | |
buf = multihash.encode(new Buffer(argv.digest, 'hex'), argv.fn) | |
catch error | |
console.error error.message | |
process.exit(1) | |
console.log( | |
if argv.encoding is 'base58' | |
base58.encode(buf) | |
else | |
buf.toString(argv.encoding) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment