pull/649/head
pubkey 2 years ago
parent ff746f02e1
commit da252e34e1
  1. 1
      dist/es/browserify.index.js
  2. 16
      dist/es/cipher.js
  3. 6
      dist/es/create-identity.js
  4. 3
      dist/es/decrypt-with-private-key.js
  5. 3
      dist/es/encrypt-with-public-key.js
  6. 2
      dist/es/hash.js
  7. 11
      dist/es/hex.js
  8. 2
      dist/es/public-key-by-private-key.js
  9. 5
      dist/es/public-key.js
  10. 9
      dist/es/recover-public-key.js
  11. 2
      dist/es/recover.js
  12. 2
      dist/es/sign.js
  13. 3
      dist/es/tx-data-by-compiled.js
  14. 3
      dist/es/vrs.js
  15. 1
      dist/lib/browserify.index.js
  16. 3
      dist/lib/calculate-contract-address.js
  17. 19
      dist/lib/cipher.js
  18. 11
      dist/lib/create-identity.js
  19. 7
      dist/lib/decrypt-with-private-key.js
  20. 6
      dist/lib/encrypt-with-public-key.js
  21. 5
      dist/lib/hash.js
  22. 14
      dist/lib/hex.js
  23. 27
      dist/lib/index.js
  24. 3
      dist/lib/public-key-by-private-key.js
  25. 11
      dist/lib/public-key.js
  26. 10
      dist/lib/recover-public-key.js
  27. 4
      dist/lib/recover.js
  28. 7
      dist/lib/sign-transaction.js
  29. 3
      dist/lib/sign.js
  30. 7
      dist/lib/tx-data-by-compiled.js
  31. 4
      dist/lib/util.js
  32. 6
      dist/lib/vrs.js
  33. 2
      package.json

@ -1,3 +1,2 @@
var EthCrypto = require('./index.js');
window['EthCrypto'] = EthCrypto;

16
dist/es/cipher.js vendored

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

@ -2,12 +2,12 @@ import { utils as ethersUtils, Wallet } from 'ethers';
import { stripHexPrefix } from 'ethereumjs-util';
var MIN_ENTROPY_SIZE = 128;
var keccak256 = ethersUtils.keccak256;
/**
* create a privateKey from the given entropy or a new one
* @param {Buffer} entropy
* @return {string}
*/
export function createPrivateKey(entropy) {
if (entropy) {
if (!Buffer.isBuffer(entropy)) throw new Error('EthCrypto.createPrivateKey(): given entropy is no Buffer');
@ -17,18 +17,16 @@ export function createPrivateKey(entropy) {
} else {
var innerHex = keccak256(ethersUtils.concat([ethersUtils.randomBytes(32), ethersUtils.randomBytes(32)]));
var middleHex = ethersUtils.concat([ethersUtils.concat([ethersUtils.randomBytes(32), innerHex]), ethersUtils.randomBytes(32)]);
var _outerHex = keccak256(middleHex);
return _outerHex;
}
}
/**
* creates a new object with
* private-, public-Key and address
* @param {Buffer?} entropy if provided, will use that as single random-source
*/
export default function createIdentity(entropy) {
var privateKey = createPrivateKey(entropy);
var wallet = new Wallet(privateKey);

@ -2,8 +2,9 @@ import { decrypt } from 'eccrypto';
import { parse } from './cipher';
import { removeLeading0x } from './util';
export default function decryptWithPrivateKey(privateKey, encrypted) {
encrypted = parse(encrypted); // remove trailing '0x' from privateKey
encrypted = parse(encrypted);
// remove trailing '0x' from privateKey
var twoStripped = removeLeading0x(privateKey);
var encryptedBuffer = {
iv: Buffer.from(encrypted.iv, 'hex'),

@ -2,8 +2,9 @@ import { encrypt } from 'eccrypto';
import { decompress } from './public-key';
export default function encryptWithPublicKey(publicKey, message, opts) {
// ensure its an uncompressed publicKey
publicKey = decompress(publicKey); // re-add the compression-flag
publicKey = decompress(publicKey);
// re-add the compression-flag
var pubString = '04' + publicKey;
return encrypt(Buffer.from(pubString, 'hex'), Buffer.from(message), opts ? opts : {}).then(function (encryptedBuffers) {
var encrypted = {

2
dist/es/hash.js vendored

@ -2,7 +2,6 @@ import { utils as ethersUtils } from 'ethers';
export function keccak256(params) {
var types = [];
var values = [];
if (!Array.isArray(params)) {
types.push('string');
values.push(params);
@ -12,7 +11,6 @@ export function keccak256(params) {
values.push(p.value);
});
}
return ethersUtils.solidityKeccak256(types, values);
}
export var SIGN_PREFIX = '\x19Ethereum Signed Message:\n32';

11
dist/es/hex.js vendored

@ -3,42 +3,37 @@
* thx @juvian
* @link https://stackoverflow.com/a/40471908/3443137
*/
import { removeLeading0x, addLeading0x } from './util';
export function compress(hex) {
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
hex = removeLeading0x(hex); // if base64:true, we use our own function because it results in a smaller output
hex = removeLeading0x(hex);
// if base64:true, we use our own function because it results in a smaller output
if (base64 === true) return Buffer.from(hex, 'hex').toString('base64');
var string = '';
while (hex.length % 4 != 0) {
// we need it to be multiple of 4
hex = '0' + hex;
}
for (var i = 0; i < hex.length; i += 4) {
// get char from ascii code which goes from 0 to 65536
string += String.fromCharCode(parseInt(hex.substring(i, i + 4), 16));
}
return string;
}
export function decompress(compressedString) {
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
// if base64:true, we use our own function because it results in a smaller output
if (base64 === true) {
var ret = Buffer.from(compressedString, 'base64').toString('hex');
return addLeading0x(ret);
}
var hex = '';
for (var i = 0; i < compressedString.length; i++) {
// get character ascii code and convert to hexa string, adding necessary 0s
hex += ((i == 0 ? '' : '000') + compressedString.charCodeAt(i).toString(16)).slice(-4);
}
hex = hex.toLowerCase();
return addLeading0x(hex);
}

@ -1,12 +1,12 @@
import { privateToPublic, toBuffer } from 'ethereumjs-util';
import { addLeading0x } from './util';
/**
* Generate publicKey from the privateKey.
* This creates the uncompressed publicKey,
* where 04 has stripped from left
* @returns {string}
*/
export default function publicKeyOfPrivateKey(privateKey) {
privateKey = addLeading0x(privateKey);
var publicKeyBuffer = privateToPublic(toBuffer(privateKey));

@ -11,17 +11,18 @@ export function decompress(startsWith02Or03) {
// if already decompressed an not has trailing 04
var testBuffer = Buffer.from(startsWith02Or03, 'hex');
if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03;
var decompressed = uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith02Or03), false)); // remove trailing 04
var decompressed = uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith02Or03), false));
// remove trailing 04
decompressed = decompressed.substring(2);
return decompressed;
}
/**
* generates the ethereum-adress of the publicKey
* We create the checksum-adress which is case-sensitive
* @returns {string} address
*/
export function toAddress(publicKey) {
// normalize key
publicKey = decompress(publicKey);

@ -1,22 +1,23 @@
import { ecdsaRecover } from 'secp256k1';
import { removeLeading0x, hexToUnit8Array, uint8ArrayToHex } from './util';
/**
* 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) {
signature = removeLeading0x(signature); // split into v-value and sig
signature = removeLeading0x(signature);
// split into v-value and sig
var sigOnly = signature.substring(0, signature.length - 2); // all but last 2 chars
var vValue = signature.slice(-2); // last 2 chars
var recoveryNumber = vValue === '1c' ? 1 : 0;
var pubKey = uint8ArrayToHex(ecdsaRecover(hexToUnit8Array(sigOnly), recoveryNumber, hexToUnit8Array(removeLeading0x(hash)), false)); // remove trailing '04'
var pubKey = uint8ArrayToHex(ecdsaRecover(hexToUnit8Array(sigOnly), recoveryNumber, hexToUnit8Array(removeLeading0x(hash)), false));
// remove trailing '04'
pubKey = pubKey.slice(2);
return pubKey;
}

@ -1,12 +1,12 @@
import recoverPublicKey from './recover-public-key';
import { toAddress as addressByPublicKey } from './public-key';
/**
* returns the adress with which the messageHash was signed
* @param {string} sigString
* @param {string} hash
* @return {string} address
*/
export default function recover(sigString, hash) {
var pubkey = recoverPublicKey(sigString, hash);
var address = addressByPublicKey(pubkey);

2
dist/es/sign.js vendored

@ -1,5 +1,6 @@
import { ecdsaSign as secp256k1_sign } from 'secp256k1';
import { addLeading0x, removeLeading0x } from './util';
/**
* signs the given message
* we do not use sign from eth-lib because the pure secp256k1-version is 90% faster
@ -7,7 +8,6 @@ import { addLeading0x, removeLeading0x } from './util';
* @param {string} hash
* @return {string} hexString
*/
export default function sign(privateKey, hash) {
hash = addLeading0x(hash);
if (hash.length !== 66) throw new Error('EthCrypto.sign(): Can only sign hashes, given: ' + hash);

@ -2,8 +2,9 @@ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import { ContractFactory } from 'ethers';
export default function txDataByCompiled(abi, bytecode, args) {
// solc returns a string which is often passed instead of the json
if (typeof abi === 'string') abi = JSON.parse(abi); // Construct a Contract Factory
if (typeof abi === 'string') abi = JSON.parse(abi);
// Construct a Contract Factory
var factory = new ContractFactory(abi, '0x' + bytecode);
var deployTransaction = factory.getDeployTransaction.apply(factory, _toConsumableArray(args));
return deployTransaction.data;

3
dist/es/vrs.js vendored

@ -4,7 +4,6 @@ import { utils as ethersUtils } from 'ethers';
* @param {string} hexString
* @return {{v: string, r: string, s: string}}
*/
export function fromString(hexString) {
var arr = ethersUtils.splitSignature(hexString);
return {
@ -14,12 +13,12 @@ export function fromString(hexString) {
s: arr.s
};
}
/**
* merge signature-parts to one string
* @param {{v: string, r: string, s: string}} sig
* @return {string} hexString
*/
export function toString(sig) {
return ethersUtils.joinSignature(sig);
}

@ -1,5 +1,4 @@
"use strict";
var EthCrypto = require('./index.js');
window['EthCrypto'] = EthCrypto;

@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = calculateContractAddress;
var _ethereumjsUtil = require("ethereumjs-util");
var _util = require("./util");
function calculateContractAddress(creatorAddress, nonce) {
var addressBuffer = (0, _ethereumjsUtil.generateAddress)((0, _ethereumjsUtil.toBuffer)((0, _util.addLeading0x)(creatorAddress)), (0, _ethereumjsUtil.toBuffer)(nonce));
var address = addressBuffer.toString('hex');

19
dist/lib/cipher.js vendored

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

@ -5,19 +5,16 @@ Object.defineProperty(exports, "__esModule", {
});
exports.createPrivateKey = createPrivateKey;
exports["default"] = createIdentity;
var _ethers = require("ethers");
var _ethereumjsUtil = require("ethereumjs-util");
var MIN_ENTROPY_SIZE = 128;
var keccak256 = _ethers.utils.keccak256;
/**
* create a privateKey from the given entropy or a new one
* @param {Buffer} entropy
* @return {string}
*/
function createPrivateKey(entropy) {
if (entropy) {
if (!Buffer.isBuffer(entropy)) throw new Error('EthCrypto.createPrivateKey(): given entropy is no Buffer');
@ -26,21 +23,17 @@ function createPrivateKey(entropy) {
return outerHex;
} else {
var innerHex = keccak256(_ethers.utils.concat([_ethers.utils.randomBytes(32), _ethers.utils.randomBytes(32)]));
var middleHex = _ethers.utils.concat([_ethers.utils.concat([_ethers.utils.randomBytes(32), innerHex]), _ethers.utils.randomBytes(32)]);
var _outerHex = keccak256(middleHex);
return _outerHex;
}
}
/**
* creates a new object with
* private-, public-Key and address
* @param {Buffer?} entropy if provided, will use that as single random-source
*/
function createIdentity(entropy) {
var privateKey = createPrivateKey(entropy);
var wallet = new _ethers.Wallet(privateKey);

@ -4,16 +4,13 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = decryptWithPrivateKey;
var _eccrypto = require("eccrypto");
var _cipher = require("./cipher");
var _util = require("./util");
function decryptWithPrivateKey(privateKey, encrypted) {
encrypted = (0, _cipher.parse)(encrypted); // remove trailing '0x' from privateKey
encrypted = (0, _cipher.parse)(encrypted);
// remove trailing '0x' from privateKey
var twoStripped = (0, _util.removeLeading0x)(privateKey);
var encryptedBuffer = {
iv: Buffer.from(encrypted.iv, 'hex'),

@ -4,15 +4,13 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = encryptWithPublicKey;
var _eccrypto = require("eccrypto");
var _publicKey = require("./public-key");
function encryptWithPublicKey(publicKey, message, opts) {
// ensure its an uncompressed publicKey
publicKey = (0, _publicKey.decompress)(publicKey); // re-add the compression-flag
publicKey = (0, _publicKey.decompress)(publicKey);
// re-add the compression-flag
var pubString = '04' + publicKey;
return (0, _eccrypto.encrypt)(Buffer.from(pubString, 'hex'), Buffer.from(message), opts ? opts : {}).then(function (encryptedBuffers) {
var encrypted = {

5
dist/lib/hash.js vendored

@ -5,13 +5,10 @@ Object.defineProperty(exports, "__esModule", {
});
exports.SIGN_PREFIX = void 0;
exports.keccak256 = keccak256;
var _ethers = require("ethers");
function keccak256(params) {
var types = [];
var values = [];
if (!Array.isArray(params)) {
types.push('string');
values.push(params);
@ -21,9 +18,7 @@ function keccak256(params) {
values.push(p.value);
});
}
return _ethers.utils.solidityKeccak256(types, values);
}
var SIGN_PREFIX = '\x19Ethereum Signed Message:\n32';
exports.SIGN_PREFIX = SIGN_PREFIX;

14
dist/lib/hex.js vendored

@ -5,50 +5,42 @@ Object.defineProperty(exports, "__esModule", {
});
exports.compress = compress;
exports.decompress = decompress;
var _util = require("./util");
/**
* compress/decompress hex-strings to utf16 or base64
* thx @juvian
* @link https://stackoverflow.com/a/40471908/3443137
*/
function compress(hex) {
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
hex = (0, _util.removeLeading0x)(hex); // if base64:true, we use our own function because it results in a smaller output
hex = (0, _util.removeLeading0x)(hex);
// if base64:true, we use our own function because it results in a smaller output
if (base64 === true) return Buffer.from(hex, 'hex').toString('base64');
var string = '';
while (hex.length % 4 != 0) {
// we need it to be multiple of 4
hex = '0' + hex;
}
for (var i = 0; i < hex.length; i += 4) {
// get char from ascii code which goes from 0 to 65536
string += String.fromCharCode(parseInt(hex.substring(i, i + 4), 16));
}
return string;
}
function decompress(compressedString) {
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
// if base64:true, we use our own function because it results in a smaller output
if (base64 === true) {
var ret = Buffer.from(compressedString, 'base64').toString('hex');
return (0, _util.addLeading0x)(ret);
}
var hex = '';
for (var i = 0; i < compressedString.length; i++) {
// get character ascii code and convert to hexa string, adding necessary 0s
hex += ((i == 0 ? '' : '000') + compressedString.charCodeAt(i).toString(16)).slice(-4);
}
hex = hex.toLowerCase();
return (0, _util.addLeading0x)(hex);
}

27
dist/lib/index.js vendored

@ -1,9 +1,7 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
@ -71,55 +69,30 @@ Object.defineProperty(exports, "txDataByCompiled", {
}
});
exports.vrs = exports.util = void 0;
var _createIdentity = _interopRequireDefault(require("./create-identity"));
var publicKey = _interopRequireWildcard(require("./public-key"));
exports.publicKey = publicKey;
var _decryptWithPrivateKey = _interopRequireDefault(require("./decrypt-with-private-key"));
var _encryptWithPublicKey = _interopRequireDefault(require("./encrypt-with-public-key"));
var cipher = _interopRequireWildcard(require("./cipher"));
exports.cipher = cipher;
var _publicKeyByPrivateKey = _interopRequireDefault(require("./public-key-by-private-key"));
var _recover = _interopRequireDefault(require("./recover"));
var _recoverPublicKey = _interopRequireDefault(require("./recover-public-key"));
var _sign = _interopRequireDefault(require("./sign"));
var _signTransaction = _interopRequireDefault(require("./sign-transaction"));
var _txDataByCompiled = _interopRequireDefault(require("./tx-data-by-compiled"));
var _calculateContractAddress = _interopRequireDefault(require("./calculate-contract-address"));
var hash = _interopRequireWildcard(require("./hash"));
exports.hash = hash;
var hex = _interopRequireWildcard(require("./hex"));
exports.hex = hex;
var vrs = _interopRequireWildcard(require("./vrs"));
exports.vrs = vrs;
var util = _interopRequireWildcard(require("./util"));
exports.util = util;
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var _default = {
createIdentity: _createIdentity["default"],
publicKey: publicKey,

@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = publicKeyOfPrivateKey;
var _ethereumjsUtil = require("ethereumjs-util");
var _util = require("./util");
/**
* Generate publicKey from the privateKey.
* This creates the uncompressed publicKey,

@ -6,36 +6,31 @@ Object.defineProperty(exports, "__esModule", {
exports.compress = compress;
exports.decompress = decompress;
exports.toAddress = toAddress;
var _secp256k = require("secp256k1");
var _ethereumjsUtil = require("ethereumjs-util");
var _util = require("./util");
function compress(startsWith04) {
// add trailing 04 if not done before
var testBuffer = Buffer.from(startsWith04, 'hex');
if (testBuffer.length === 64) startsWith04 = '04' + startsWith04;
return (0, _util.uint8ArrayToHex)((0, _secp256k.publicKeyConvert)((0, _util.hexToUnit8Array)(startsWith04), true));
}
function decompress(startsWith02Or03) {
// if already decompressed an not has trailing 04
var testBuffer = Buffer.from(startsWith02Or03, 'hex');
if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03;
var decompressed = (0, _util.uint8ArrayToHex)((0, _secp256k.publicKeyConvert)((0, _util.hexToUnit8Array)(startsWith02Or03), false)); // remove trailing 04
var decompressed = (0, _util.uint8ArrayToHex)((0, _secp256k.publicKeyConvert)((0, _util.hexToUnit8Array)(startsWith02Or03), false));
// remove trailing 04
decompressed = decompressed.substring(2);
return decompressed;
}
/**
* generates the ethereum-adress of the publicKey
* We create the checksum-adress which is case-sensitive
* @returns {string} address
*/
function toAddress(publicKey) {
// normalize key
publicKey = decompress(publicKey);

@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = recoverPublicKey;
var _secp256k = require("secp256k1");
var _util = require("./util");
/**
* returns the publicKey for the privateKey with which the messageHash was signed
* @param {string} signature
@ -16,15 +13,16 @@ var _util = require("./util");
* @return {string} publicKey
*/
function recoverPublicKey(signature, hash) {
signature = (0, _util.removeLeading0x)(signature); // split into v-value and sig
signature = (0, _util.removeLeading0x)(signature);
// split into v-value and sig
var sigOnly = signature.substring(0, signature.length - 2); // all but last 2 chars
var vValue = signature.slice(-2); // last 2 chars
var recoveryNumber = vValue === '1c' ? 1 : 0;
var pubKey = (0, _util.uint8ArrayToHex)((0, _secp256k.ecdsaRecover)((0, _util.hexToUnit8Array)(sigOnly), recoveryNumber, (0, _util.hexToUnit8Array)((0, _util.removeLeading0x)(hash)), false)); // remove trailing '04'
var pubKey = (0, _util.uint8ArrayToHex)((0, _secp256k.ecdsaRecover)((0, _util.hexToUnit8Array)(sigOnly), recoveryNumber, (0, _util.hexToUnit8Array)((0, _util.removeLeading0x)(hash)), false));
// remove trailing '04'
pubKey = pubKey.slice(2);
return pubKey;
}

@ -1,16 +1,12 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = recover;
var _recoverPublicKey = _interopRequireDefault(require("./recover-public-key"));
var _publicKey = require("./public-key");
/**
* returns the adress with which the messageHash was signed
* @param {string} sigString

@ -1,18 +1,13 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = signTransaction;
var _tx = require("@ethereumjs/tx");
var _publicKeyByPrivateKey = _interopRequireDefault(require("./public-key-by-private-key"));
var _publicKey = require("./public-key");
function signTransaction(rawTx, privateKey) {
var txOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
// check if privateKey->address matches rawTx.from
@ -20,9 +15,7 @@ function signTransaction(rawTx, privateKey) {
var address = (0, _publicKey.toAddress)(publicKey);
if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey');
var privateKeyBuffer = Buffer.from(privateKey.replace(/^.{2}/g, ''), 'hex');
var tx = _tx.Transaction.fromTxData(rawTx, txOptions);
var signedTx = tx.sign(privateKeyBuffer);
var serializedTx = signedTx.serialize().toString('hex');
return serializedTx;

3
dist/lib/sign.js vendored

@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = sign;
var _secp256k = require("secp256k1");
var _util = require("./util");
/**
* signs the given message
* we do not use sign from eth-lib because the pure secp256k1-version is 90% faster

@ -1,20 +1,17 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = txDataByCompiled;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ethers = require("ethers");
function txDataByCompiled(abi, bytecode, args) {
// solc returns a string which is often passed instead of the json
if (typeof abi === 'string') abi = JSON.parse(abi); // Construct a Contract Factory
if (typeof abi === 'string') abi = JSON.parse(abi);
// Construct a Contract Factory
var factory = new _ethers.ContractFactory(abi, '0x' + bytecode);
var deployTransaction = factory.getDeployTransaction.apply(factory, (0, _toConsumableArray2["default"])(args));
return deployTransaction.data;

4
dist/lib/util.js vendored

@ -7,19 +7,15 @@ exports.addLeading0x = addLeading0x;
exports.hexToUnit8Array = hexToUnit8Array;
exports.removeLeading0x = removeLeading0x;
exports.uint8ArrayToHex = uint8ArrayToHex;
function removeLeading0x(str) {
if (str.startsWith('0x')) return str.substring(2);else return str;
}
function addLeading0x(str) {
if (!str.startsWith('0x')) return '0x' + str;else return str;
}
function uint8ArrayToHex(arr) {
return Buffer.from(arr).toString('hex');
}
function hexToUnit8Array(str) {
return new Uint8Array(Buffer.from(str, 'hex'));
}

6
dist/lib/vrs.js vendored

@ -5,9 +5,7 @@ Object.defineProperty(exports, "__esModule", {
});
exports.fromString = fromString;
exports.toString = toString;
var _ethers = require("ethers");
/**
* split signature-hex into parts
* @param {string} hexString
@ -15,7 +13,6 @@ var _ethers = require("ethers");
*/
function fromString(hexString) {
var arr = _ethers.utils.splitSignature(hexString);
return {
// convert "v" to hex
v: "0x".concat(arr.v.toString(16)),
@ -23,13 +20,12 @@ function fromString(hexString) {
s: arr.s
};
}
/**
* merge signature-parts to one string
* @param {{v: string, r: string, s: string}} sig
* @return {string} hexString
*/
function toString(sig) {
return _ethers.utils.joinSignature(sig);
}

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

Loading…
Cancel
Save