state-channel
pubkey 7 years ago
parent dbd1cc0cd4
commit 49bcdee2e2
  1. 4
      dist/es/index.js
  2. 28
      dist/es/recover-public-key.js
  3. 15
      dist/es/sign.js
  4. 8
      dist/lib/index.js
  5. 48
      dist/lib/recover-public-key.js
  6. 24
      dist/lib/sign.js
  7. 2
      package.json

4
dist/es/index.js vendored

@ -4,6 +4,7 @@ import decryptWithPrivateKey from './decrypt-with-private-key';
import encryptWithPublicKey from './encrypt-with-public-key';
import publicKeyByPrivateKey from './public-key-by-private-key';
import recover from './recover';
import recoverPublicKey from './recover-public-key';
import sign from './sign';
import signTransaction from './sign-transaction';
import txDataByCompiled from './tx-data-by-compiled';
@ -12,7 +13,7 @@ import * as hex from './hex';
import * as vrs from './vrs';
import * as util from './util';
export { addressByPublicKey, createIdentity, decryptWithPrivateKey, encryptWithPublicKey, publicKeyByPrivateKey, recover, sign, signTransaction, txDataByCompiled, hash, hex, vrs, util };
export { addressByPublicKey, createIdentity, decryptWithPrivateKey, encryptWithPublicKey, publicKeyByPrivateKey, recover, recoverPublicKey, sign, signTransaction, txDataByCompiled, hash, hex, vrs, util };
export default {
addressByPublicKey: addressByPublicKey,
@ -21,6 +22,7 @@ export default {
encryptWithPublicKey: encryptWithPublicKey,
publicKeyByPrivateKey: publicKeyByPrivateKey,
recover: recover,
recoverPublicKey: recoverPublicKey,
sign: sign,
signTransaction: signTransaction,
txDataByCompiled: txDataByCompiled,

@ -0,0 +1,28 @@
import { decodeSignature, toChecksum } from 'eth-lib/lib/account';
import Bytes from 'eth-lib/lib/bytes';
import * as vrs from './vrs';
import elliptic from 'elliptic';
var secp256k1 = new elliptic.ec('secp256k1');
/**
* returns the publicKey for the privateKEy with which the messageHash was signed
* @param {string} signature
* @param {string} hash
* @return {string} publicKey
*/
export default function recoverPublicKey(signature, hash) {
// parse signature
var vals = vrs.fromString(signature);
var vrsOfSig = {
v: Bytes.toNumber(vals.v),
r: vals.r.slice(2),
s: vals.s.slice(2)
};
// because odd vals mean v=0... sadly that means v=0 means v=1... I hate that
var ecPublicKey = secp256k1.recoverPubKey(new Buffer(hash.slice(2), 'hex'), vrsOfSig, vrsOfSig.v < 2 ? vrsOfSig.v : 1 - vrsOfSig.v % 2);
var publicKey = ecPublicKey.encode('hex', false).slice(2);
return publicKey;
}

15
dist/es/sign.js vendored

@ -1,12 +1,21 @@
import Account from 'eth-lib/lib/account';
import * as secp256k1 from 'secp256k1';
import * as util from './util';
/**
* signs the given message
* we do not use sign from eth-lib because the pure secp256k1-version is 90% faster
* @param {string} privateKey
* @param {string} hash
* @return {string} hexString
*/
export default function sign(privateKey, hash) {
var signature = Account.sign(hash, privateKey);
return signature;
hash = util.addTrailing0x(hash);
if (hash.length !== 66) throw new Error('EthCrypto.sign(): Can only sign hashes, given: ' + hash);
var sigObj = secp256k1.sign(new Buffer(util.removeTrailing0x(hash), 'hex'), new Buffer(util.removeTrailing0x(privateKey), 'hex'));
var recoveryId = sigObj.recovery === 1 ? '1c' : '1b';
var newSignature = '0x' + sigObj.signature.toString('hex') + recoveryId;
return newSignature;
}

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.txDataByCompiled = exports.signTransaction = exports.sign = exports.recover = exports.publicKeyByPrivateKey = exports.encryptWithPublicKey = exports.decryptWithPrivateKey = exports.createIdentity = exports.addressByPublicKey = undefined;
exports.util = exports.vrs = exports.hex = exports.hash = exports.txDataByCompiled = exports.signTransaction = exports.sign = exports.recoverPublicKey = exports.recover = exports.publicKeyByPrivateKey = exports.encryptWithPublicKey = exports.decryptWithPrivateKey = exports.createIdentity = exports.addressByPublicKey = undefined;
var _addressByPublicKey = require('./address-by-public-key');
@ -29,6 +29,10 @@ var _recover = require('./recover');
var _recover2 = _interopRequireDefault(_recover);
var _recoverPublicKey = require('./recover-public-key');
var _recoverPublicKey2 = _interopRequireDefault(_recoverPublicKey);
var _sign = require('./sign');
var _sign2 = _interopRequireDefault(_sign);
@ -67,6 +71,7 @@ exports.decryptWithPrivateKey = _decryptWithPrivateKey2['default'];
exports.encryptWithPublicKey = _encryptWithPublicKey2['default'];
exports.publicKeyByPrivateKey = _publicKeyByPrivateKey2['default'];
exports.recover = _recover2['default'];
exports.recoverPublicKey = _recoverPublicKey2['default'];
exports.sign = _sign2['default'];
exports.signTransaction = _signTransaction2['default'];
exports.txDataByCompiled = _txDataByCompiled2['default'];
@ -81,6 +86,7 @@ exports['default'] = {
encryptWithPublicKey: _encryptWithPublicKey2['default'],
publicKeyByPrivateKey: _publicKeyByPrivateKey2['default'],
recover: _recover2['default'],
recoverPublicKey: _recoverPublicKey2['default'],
sign: _sign2['default'],
signTransaction: _signTransaction2['default'],
txDataByCompiled: _txDataByCompiled2['default'],

@ -0,0 +1,48 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports['default'] = recoverPublicKey;
var _account = require('eth-lib/lib/account');
var _bytes = require('eth-lib/lib/bytes');
var _bytes2 = _interopRequireDefault(_bytes);
var _vrs = require('./vrs');
var vrs = _interopRequireWildcard(_vrs);
var _elliptic = require('elliptic');
var _elliptic2 = _interopRequireDefault(_elliptic);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var secp256k1 = new _elliptic2['default'].ec('secp256k1');
/**
* returns the publicKey for the privateKEy with which the messageHash was signed
* @param {string} signature
* @param {string} hash
* @return {string} publicKey
*/
function recoverPublicKey(signature, hash) {
// parse signature
var vals = vrs.fromString(signature);
var vrsOfSig = {
v: _bytes2['default'].toNumber(vals.v),
r: vals.r.slice(2),
s: vals.s.slice(2)
};
// because odd vals mean v=0... sadly that means v=0 means v=1... I hate that
var ecPublicKey = secp256k1.recoverPubKey(new Buffer(hash.slice(2), 'hex'), vrsOfSig, vrsOfSig.v < 2 ? vrsOfSig.v : 1 - vrsOfSig.v % 2);
var publicKey = ecPublicKey.encode('hex', false).slice(2);
return publicKey;
}

24
dist/lib/sign.js vendored

@ -1,23 +1,35 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports['default'] = sign;
var _account = require('eth-lib/lib/account');
var _secp256k = require('secp256k1');
var _account2 = _interopRequireDefault(_account);
var secp256k1 = _interopRequireWildcard(_secp256k);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _util = require('./util');
var util = _interopRequireWildcard(_util);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
/**
* signs the given message
* we do not use sign from eth-lib because the pure secp256k1-version is 90% faster
* @param {string} privateKey
* @param {string} hash
* @return {string} hexString
*/
function sign(privateKey, hash) {
var signature = _account2['default'].sign(hash, privateKey);
return signature;
hash = util.addTrailing0x(hash);
if (hash.length !== 66) throw new Error('EthCrypto.sign(): Can only sign hashes, given: ' + hash);
var sigObj = secp256k1.sign(new Buffer(util.removeTrailing0x(hash), 'hex'), new Buffer(util.removeTrailing0x(privateKey), 'hex'));
var recoveryId = sigObj.recovery === 1 ? '1c' : '1b';
var newSignature = '0x' + sigObj.signature.toString('hex') + recoveryId;
return newSignature;
}

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

Loading…
Cancel
Save