pull/15/head
pubkey 7 years ago
parent 28ef384bfe
commit 888daf429c
  1. 34
      dist/es/cipher.js
  2. 12
      dist/es/decrypt-with-private-key.js
  3. 4
      dist/es/index.js
  4. 42
      dist/lib/cipher.js
  5. 14
      dist/lib/decrypt-with-private-key.js
  6. 8
      dist/lib/index.js
  7. 2
      package.json

34
dist/es/cipher.js vendored

@ -0,0 +1,34 @@
import { compress, decompress } from './public-key';
export function stringify(cipher) {
if (typeof cipher === 'string') return cipher;
// use compressed key because it's smaller
var compressedKey = compress(cipher.ephemPublicKey);
var ret = Buffer.concat([new Buffer(cipher.iv, 'hex'), // 16bit
new Buffer(compressedKey, 'hex'), // 33bit
new Buffer(cipher.mac, 'hex'), // 32bit
new Buffer(cipher.ciphertext, 'hex') // var bit
]);
return ret.toString('hex');
}
export function parse(str) {
if (typeof str !== 'string') return str;
var buf = new Buffer(str, 'hex');
var ret = {
iv: buf.toString('hex', 0, 16),
ephemPublicKey: buf.toString('hex', 16, 49),
mac: buf.toString('hex', 49, 81),
ciphertext: buf.toString('hex', 81, buf.length)
};
// decompress publicKey
ret.ephemPublicKey = '04' + decompress(ret.ephemPublicKey);
return ret;
}

@ -1,6 +1,8 @@
import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import eccrypto from 'eccrypto';
import { parse } from './cipher';
import { removeTrailing0x } from './util';
export default (function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(privateKey, encrypted) {
@ -10,22 +12,24 @@ export default (function () {
switch (_context.prev = _context.next) {
case 0:
encrypted = parse(encrypted);
// remove trailing '0x' from privateKey
twoStripped = privateKey.replace(/^.{2}/g, '');
twoStripped = removeTrailing0x(privateKey);
encryptedBuffer = {
iv: new Buffer(encrypted.iv, 'hex'),
ephemPublicKey: new Buffer(encrypted.ephemPublicKey, 'hex'),
ciphertext: new Buffer(encrypted.ciphertext, 'hex'),
mac: new Buffer(encrypted.mac, 'hex')
};
_context.next = 4;
_context.next = 5;
return eccrypto.decrypt(new Buffer(twoStripped, 'hex'), encryptedBuffer);
case 4:
case 5:
decryptedBuffer = _context.sent;
return _context.abrupt('return', decryptedBuffer.toString());
case 6:
case 7:
case 'end':
return _context.stop();
}

4
dist/es/index.js vendored

@ -3,6 +3,7 @@ import createIdentity from './create-identity';
import * as publicKey from './public-key';
import decryptWithPrivateKey from './decrypt-with-private-key';
import encryptWithPublicKey from './encrypt-with-public-key';
import * as cipher from './cipher';
import publicKeyByPrivateKey from './public-key-by-private-key';
import recover from './recover';
import recoverPublicKey from './recover-public-key';
@ -15,13 +16,14 @@ import * as hex from './hex';
import * as vrs from './vrs';
import * as util from './util';
export { createIdentity, publicKey, decryptWithPrivateKey, encryptWithPublicKey, publicKeyByPrivateKey, recover, recoverPublicKey, sign, signTransaction, txDataByCompiled, calculateContractAddress, hash, hex, vrs, util };
export { createIdentity, publicKey, decryptWithPrivateKey, encryptWithPublicKey, cipher, publicKeyByPrivateKey, recover, recoverPublicKey, sign, signTransaction, txDataByCompiled, calculateContractAddress, hash, hex, vrs, util };
export default {
createIdentity: createIdentity,
publicKey: publicKey,
decryptWithPrivateKey: decryptWithPrivateKey,
encryptWithPublicKey: encryptWithPublicKey,
cipher: cipher,
publicKeyByPrivateKey: publicKeyByPrivateKey,
recover: recover,
recoverPublicKey: recoverPublicKey,

42
dist/lib/cipher.js vendored

@ -0,0 +1,42 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.stringify = stringify;
exports.parse = parse;
var _publicKey = require('./public-key');
function stringify(cipher) {
if (typeof cipher === 'string') return cipher;
// use compressed key because it's smaller
var compressedKey = (0, _publicKey.compress)(cipher.ephemPublicKey);
var ret = Buffer.concat([new Buffer(cipher.iv, 'hex'), // 16bit
new Buffer(compressedKey, 'hex'), // 33bit
new Buffer(cipher.mac, 'hex'), // 32bit
new Buffer(cipher.ciphertext, 'hex') // var bit
]);
return ret.toString('hex');
}
function parse(str) {
if (typeof str !== 'string') return str;
var buf = new Buffer(str, 'hex');
var ret = {
iv: buf.toString('hex', 0, 16),
ephemPublicKey: buf.toString('hex', 16, 49),
mac: buf.toString('hex', 49, 81),
ciphertext: buf.toString('hex', 81, buf.length)
};
// decompress publicKey
ret.ephemPublicKey = '04' + (0, _publicKey.decompress)(ret.ephemPublicKey);
return ret;
}

@ -16,6 +16,10 @@ var _eccrypto = require('eccrypto');
var _eccrypto2 = _interopRequireDefault(_eccrypto);
var _cipher = require('./cipher');
var _util = require('./util');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
exports['default'] = function () {
@ -26,22 +30,24 @@ exports['default'] = function () {
switch (_context.prev = _context.next) {
case 0:
encrypted = (0, _cipher.parse)(encrypted);
// remove trailing '0x' from privateKey
twoStripped = privateKey.replace(/^.{2}/g, '');
twoStripped = (0, _util.removeTrailing0x)(privateKey);
encryptedBuffer = {
iv: new Buffer(encrypted.iv, 'hex'),
ephemPublicKey: new Buffer(encrypted.ephemPublicKey, 'hex'),
ciphertext: new Buffer(encrypted.ciphertext, 'hex'),
mac: new Buffer(encrypted.mac, 'hex')
};
_context.next = 4;
_context.next = 5;
return _eccrypto2['default'].decrypt(new Buffer(twoStripped, 'hex'), encryptedBuffer);
case 4:
case 5:
decryptedBuffer = _context.sent;
return _context.abrupt('return', decryptedBuffer.toString());
case 6:
case 7:
case 'end':
return _context.stop();
}

8
dist/lib/index.js vendored

@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.util = exports.vrs = exports.hex = exports.hash = exports.calculateContractAddress = exports.txDataByCompiled = exports.signTransaction = exports.sign = exports.recoverPublicKey = exports.recover = exports.publicKeyByPrivateKey = exports.encryptWithPublicKey = exports.decryptWithPrivateKey = exports.publicKey = exports.createIdentity = undefined;
exports.util = exports.vrs = exports.hex = exports.hash = exports.calculateContractAddress = exports.txDataByCompiled = exports.signTransaction = exports.sign = exports.recoverPublicKey = exports.recover = exports.publicKeyByPrivateKey = exports.cipher = exports.encryptWithPublicKey = exports.decryptWithPrivateKey = exports.publicKey = exports.createIdentity = undefined;
var _createIdentity = require('./create-identity');
@ -21,6 +21,10 @@ var _encryptWithPublicKey = require('./encrypt-with-public-key');
var _encryptWithPublicKey2 = _interopRequireDefault(_encryptWithPublicKey);
var _cipher = require('./cipher');
var cipher = _interopRequireWildcard(_cipher);
var _publicKeyByPrivateKey = require('./public-key-by-private-key');
var _publicKeyByPrivateKey2 = _interopRequireDefault(_publicKeyByPrivateKey);
@ -73,6 +77,7 @@ exports.createIdentity = _createIdentity2['default'];
exports.publicKey = publicKey;
exports.decryptWithPrivateKey = _decryptWithPrivateKey2['default'];
exports.encryptWithPublicKey = _encryptWithPublicKey2['default'];
exports.cipher = cipher;
exports.publicKeyByPrivateKey = _publicKeyByPrivateKey2['default'];
exports.recover = _recover2['default'];
exports.recoverPublicKey = _recoverPublicKey2['default'];
@ -89,6 +94,7 @@ exports['default'] = {
publicKey: publicKey,
decryptWithPrivateKey: _decryptWithPrivateKey2['default'],
encryptWithPublicKey: _encryptWithPublicKey2['default'],
cipher: cipher,
publicKeyByPrivateKey: _publicKeyByPrivateKey2['default'],
recover: _recover2['default'],
recoverPublicKey: _recoverPublicKey2['default'],

@ -1,6 +1,6 @@
{
"name": "eth-crypto",
"version": "1.0.1",
"version": "1.1.0",
"description": "Cryptographic functions for ethereum and how to use them with web3 and solidity",
"keywords": [
"ethereum",

Loading…
Cancel
Save