npm install -g coffee-script
npm install bitcore-lib bitcore-ecies
coffee test.coffee decode 7jDFoiJz...(base58 message)
coffee test.coffee encode "Hello, world!" KzEad1...(private key)
#!coffee | |
vm = require 'vm' | |
{ PrivateKey, encoding } = require 'bitcore-lib' | |
ECIES = require 'bitcore-ecies' | |
[_, __, method, input, privateKey] = process.argv | |
if not input | |
throw 'input is empty!' | |
switch method | |
when 'decode' | |
decoded = encoding.Base58.decode input | |
sandbox = vm.createContext { | |
encryptMethod: '' | |
decryptKey: '' | |
encryptedMsg: '' | |
} | |
vm.runInContext decoded, sandbox | |
if sandbox.encryptMethod isnt 'ecies' | |
throw 'unsupported encrypt method!' | |
key = new PrivateKey(sandbox.decryptKey or privateKey) | |
msg = new Buffer sandbox.encryptedMsg, 'base64' | |
decrypted = ECIES().privateKey(key).publicKey(key.publicKey).decrypt(msg) | |
console.log decrypted.toString 'utf-8' | |
when 'encode' | |
if not privateKey | |
throw 'private key is required!' | |
buffer = new Buffer input, 'utf-8' | |
key = new PrivateKey privateKey | |
encrypted = ECIES().privateKey(key).publicKey(key.publicKey).encrypt(buffer) | |
struct = [ | |
"encryptMethod='ecies';" | |
"decryptKey='#{key.toWIF()}';" | |
"encryptedMsg='#{encrypted.toString('base64')}';" | |
].join '' | |
output = encoding.Base58.encode new Buffer(struct) | |
console.log output |