parent
dbd1cc0cd4
commit
49bcdee2e2
@ -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; |
||||
} |
@ -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; |
||||
} |
@ -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; |
||||
} |
@ -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; |
||||
} |
Loading…
Reference in new issue