#!/usr/bin/env node 'use strict' var stdin = require('get-stdin') var eccrypto = require('eccrypto') var command = process.argv[0] var script = process.argv[1] var public_key = process.argv[2] var message = process.argv[3] var pk, m stdin.buffer().then(function(s) { if (!s.length && typeof message === 'undefined') { return process.stdout.write(JSON.stringify({ error: 'public key or message not given' }), function() { process.exit(1) }) } m = s || message if (!public_key) { return process.stdout.write(JSON.stringify({ error: 'public key or message not given' }), function() { process.exit(1) }) } try { pk = Buffer(public_key, 'hex') } catch (err) { return process.stdout.write(JSON.stringify({ error: 'public key is not a hexadecimal string' }), function() { process.exit(1) }) } return eccrypto.encrypt(pk, m) }).then(function(encrypted) { process.stdout.write(JSON.stringify({ iv: encrypted.iv.toString('hex'), ephemPublicKey: encrypted.ephemPublicKey.toString('hex'), ciphertext: encrypted.ciphertext.toString('hex'), mac: encrypted.mac.toString('hex') }), function() { process.exit(0) }) }).catch(function(err) { process.stdout.write(JSON.stringify({ eccrypto_error: JSON.stringify(err) }), function() { process.exit(1) }) })