New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

eth-crypto-js

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eth-crypto-js - npm Package Compare versions

Comparing version 0.2.15 to 0.2.16

CHANGELOG.md

16

dist/createIdentity.d.ts

@@ -1,7 +0,13 @@

/// <reference types="node" />
export interface Identity {
/**
* create a privateKey
* @return {string}
*/
export declare const createPrivateKey: () => string;
/**
* creates a new object with
* private-Key and public-Key
*/
export declare const createIdentity: () => {
privateKey: string;
publicKey: string;
address: string;
}
export declare const createIdentity: (entropy?: Buffer) => Identity;
};

@@ -1,1 +0,2 @@

export declare function decryptWithPrivateKey(privateKey: string, encrypted: any): Promise<string>;
import { Encrypted } from './types';
export declare const decryptWithPrivateKey: (privateKey: string, encrypted: Encrypted) => Promise<string>;

@@ -1,2 +0,6 @@

import { Encrypted } from './types/Encrypted';
export declare function encryptWithPublicKey(publicKey: string, message: string, opts?: {}): Promise<Encrypted>;
export declare const encryptWithPublicKey: (publicKey: string, message: string) => Promise<{
iv: string;
ephemPublicKey: string;
ciphertext: string;
mac: string;
}>;

@@ -1,4 +0,1 @@

/// <reference types="node" />
import { createPrivateKey } from './createPrivateKey';
import { publicKeyByPrivateKey } from './publicKeyByPrivateKey';
import { createIdentity } from './createIdentity';

@@ -8,44 +5,30 @@ import { sign } from './sign';

import { decryptWithPrivateKey } from './decryptWithPrivateKey';
import { keccak256 } from './hash';
import { publicKeyByPrivateKey } from './publicKeyByPrivateKey';
import { recoverPublicKey } from './recoverPublicKey';
import { solidityKeccak256 as keccak256 } from './utils/hash/solidityKeccak256';
import { compressPublicKey } from './utils/publicKey/compressPublicKey';
import { decompressPublicKey } from './utils/publicKey/decompressPublicKey';
import { cipherParser } from './utils/cipher/cipherParser';
import { cipherStringify } from './utils/cipher/cipherStringify';
import { Encrypted } from './types/Encrypted';
import { Encrypted } from './types';
declare const hash: {
keccak256: typeof keccak256;
keccak256: (params: string | import("./hash").Param[]) => string;
};
declare const publicKey: {
compress: typeof compressPublicKey;
decomporess: typeof decompressPublicKey;
};
declare const cipher: {
parse: typeof cipherParser;
stringify: typeof cipherStringify;
};
export { createPrivateKey, publicKeyByPrivateKey, createIdentity, sign, encryptWithPublicKey, decryptWithPrivateKey, recoverPublicKey, keccak256, cipherParser, cipherStringify, hash, publicKey, cipher, Encrypted, };
export { createIdentity, decryptWithPrivateKey, encryptWithPublicKey, hash, keccak256, publicKeyByPrivateKey, recoverPublicKey, sign, Encrypted, };
declare const _default: {
createPrivateKey: typeof createPrivateKey;
publicKeyByPrivateKey: typeof publicKeyByPrivateKey;
createIdentity: (entropy?: Buffer) => import("./createIdentity").Identity;
sign: typeof sign;
encryptWithPublicKey: typeof encryptWithPublicKey;
decryptWithPrivateKey: typeof decryptWithPrivateKey;
recoverPublicKey: typeof recoverPublicKey;
keccak256: typeof keccak256;
cipherParser: typeof cipherParser;
cipherStringify: typeof cipherStringify;
createIdentity: () => {
privateKey: string;
publicKey: string;
};
decryptWithPrivateKey: (privateKey: string, encrypted: Encrypted) => Promise<string>;
encryptWithPublicKey: (publicKey: string, message: string) => Promise<{
iv: string;
ephemPublicKey: string;
ciphertext: string;
mac: string;
}>;
hash: {
keccak256: typeof keccak256;
keccak256: (params: string | import("./hash").Param[]) => string;
};
publicKey: {
compress: typeof compressPublicKey;
decomporess: typeof decompressPublicKey;
};
cipher: {
parse: typeof cipherParser;
stringify: typeof cipherStringify;
};
keccak256: (params: string | import("./hash").Param[]) => string;
publicKeyByPrivateKey: (privateKey: string) => string;
recoverPublicKey: (signature: string, hash: string) => string;
sign: (privateKey: string, hash: string) => string;
};
export default _default;

@@ -1,215 +0,2 @@

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var ethers = require('ethers');
var ethereumjsUtil = require('ethereumjs-util');
var secp256k1 = require('secp256k1');
var eccrypto = require('eccrypto');
const MIN_ENTROPY_SIZE = 128;
function createPrivateKey(entropy) {
if (entropy) {
if (Buffer.byteLength(entropy, 'utf8') < MIN_ENTROPY_SIZE) {
throw new Error(`WallabyCrypto.createPrivateKey(): Entropy-size must be at least ${MIN_ENTROPY_SIZE} but was ${Buffer.byteLength(entropy, 'utf8')}`);
}
const outerHex = ethers.keccak256(entropy);
return outerHex;
}
const innerHex = ethers.keccak256(ethers.concat([ethers.randomBytes(32), ethers.randomBytes(32)]));
const middleHex = ethers.concat([ethers.concat([ethers.randomBytes(32), innerHex]), ethers.randomBytes(32)]);
const outerHex = ethers.keccak256(middleHex);
return outerHex;
}
function addLeading0x(str) {
if (!str.startsWith('0x'))
return '0x' + str;
return str;
}
/**
* Generate publicKey from the privateKey with leading 0x.
* @returns {string}
*/
function publicKeyByPrivateKey(privateKey) {
const privateKeyWithLeading0x = addLeading0x(privateKey);
const publicKeyBuffer = ethereumjsUtil.privateToPublic(ethereumjsUtil.toBuffer(privateKeyWithLeading0x));
return publicKeyBuffer.toString('hex');
}
const createIdentity = (entropy) => {
const privateKey = createPrivateKey(entropy);
const wallet = new ethers.Wallet(privateKey);
const identity = {
privateKey: privateKey,
publicKey: publicKeyByPrivateKey(privateKey),
address: wallet.address,
};
return identity;
};
function removeLeading0x(str) {
if (str.startsWith('0x'))
return str.substring(2);
return str;
}
/**
* 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) {
hash = addLeading0x(hash);
if (hash.length !== 66)
throw new Error('WallabyCrypto.sign(): Can only sign hashes, given: ' + hash);
const sigObj = secp256k1.ecdsaSign(new Uint8Array(Buffer.from(removeLeading0x(hash), 'hex')), new Uint8Array(Buffer.from(removeLeading0x(privateKey), 'hex')));
const recoveryId = sigObj.recid === 1 ? '1c' : '1b';
const newSignature = '0x' + Buffer.from(sigObj.signature).toString('hex') + recoveryId;
return newSignature;
}
function uint8ArrayToHex(arr) {
return Buffer.from(arr).toString('hex');
}
function hexToUnit8Array(str) {
return new Uint8Array(Buffer.from(str, 'hex'));
}
function decompressPublicKey(startsWith02Or03) {
// if already decompressed an not has trailing 04
const testBuffer = Buffer.from(startsWith02Or03, 'hex');
if (testBuffer.length === 64)
startsWith02Or03 = '04' + startsWith02Or03;
let decompressed = uint8ArrayToHex(secp256k1.publicKeyConvert(hexToUnit8Array(startsWith02Or03), false));
// remove trailing 04
decompressed = decompressed.substring(2);
return decompressed;
}
async function encryptWithPublicKey(publicKey, message, opts = {}) {
const decompressedPublicKey = decompressPublicKey(publicKey);
const pubString = `04${decompressedPublicKey}`;
return eccrypto.encrypt(Buffer.from(pubString, 'hex'), Buffer.from(message), opts).then((encryptedBuffers) => {
const encrypted = {
iv: encryptedBuffers.iv.toString('hex'),
ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'),
ciphertext: encryptedBuffers.ciphertext.toString('hex'),
mac: encryptedBuffers.mac.toString('hex'),
};
return encrypted;
});
}
function cipherParser(str) {
if (typeof str !== 'string') {
return str;
}
const buf = Buffer.from(str, 'hex');
const 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),
};
ret.ephemPublicKey = `04${decompressPublicKey(ret.ephemPublicKey)}`;
return ret;
}
async function decryptWithPrivateKey(privateKey, encrypted) {
encrypted = cipherParser(encrypted);
// remove trailing '0x' from privateKey
const twoStripped = removeLeading0x(privateKey);
const encryptedBuffer = {
iv: Buffer.from(encrypted.iv, 'hex'),
ephemPublicKey: Buffer.from(encrypted.ephemPublicKey, 'hex'),
ciphertext: Buffer.from(encrypted.ciphertext, 'hex'),
mac: Buffer.from(encrypted.mac, 'hex'),
};
return eccrypto.decrypt(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then((decryptedBuffer) => decryptedBuffer.toString());
}
function recoverPublicKey(signature, hash) {
signature = removeLeading0x(signature);
// split into v-value and sig
const sigOnly = signature.substring(0, signature.length - 2); // all but last 2 chars
const vValue = signature.slice(-2); // last 2 chars
const recoveryNumber = vValue === '1c' ? 1 : 0;
let pubKey = uint8ArrayToHex(secp256k1.ecdsaRecover(hexToUnit8Array(sigOnly), recoveryNumber, hexToUnit8Array(removeLeading0x(hash)), false));
// remove trailing '04'
pubKey = pubKey.slice(2);
return pubKey;
}
function solidityKeccak256(str) {
const hash = ethers.keccak256(ethers.toUtf8Bytes(str));
return hash;
}
function compressPublicKey(startsWith04) {
// add trailing 04 if not done before
const testBuffer = Buffer.from(startsWith04, 'hex');
if (testBuffer.length === 64)
startsWith04 = '04' + startsWith04;
return uint8ArrayToHex(secp256k1.publicKeyConvert(hexToUnit8Array(startsWith04), true));
}
function cipherStringify(cipher) {
if (typeof cipher === 'string')
return cipher;
const compressedKey = compressPublicKey(cipher.ephemPublicKey);
const 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');
}
const hash = {
keccak256: solidityKeccak256,
};
const publicKey = {
compress: compressPublicKey,
decomporess: decompressPublicKey,
};
const cipher = {
parse: cipherParser,
stringify: cipherStringify,
};
var index = {
createPrivateKey,
publicKeyByPrivateKey,
createIdentity,
sign,
encryptWithPublicKey,
decryptWithPrivateKey,
recoverPublicKey,
keccak256: solidityKeccak256,
cipherParser,
cipherStringify,
hash,
publicKey,
cipher,
};
exports.cipher = cipher;
exports.cipherParser = cipherParser;
exports.cipherStringify = cipherStringify;
exports.createIdentity = createIdentity;
exports.createPrivateKey = createPrivateKey;
exports.decryptWithPrivateKey = decryptWithPrivateKey;
exports.default = index;
exports.encryptWithPublicKey = encryptWithPublicKey;
exports.hash = hash;
exports.keccak256 = solidityKeccak256;
exports.publicKey = publicKey;
exports.publicKeyByPrivateKey = publicKeyByPrivateKey;
exports.recoverPublicKey = recoverPublicKey;
exports.sign = sign;
var e=require("ethers"),r=require("ethereum-cryptography/secp256k1.js"),t=require("ethereum-cryptography/utils"),c=require("ethereum-cryptography/sha512.js"),i=require("ethereum-cryptography/secp256k1"),n=require("ethereum-cryptography/utils.js"),s=require("crypto-js"),o=require("ethereum-cryptography/secp256k1-compat"),y=function(e){if("string"!=typeof e)throw new Error("[stripHexPrefix] input must be type 'string', received "+typeof e);return function(e){if("string"!=typeof e)throw new Error("[isHexPrefixed] input must be type 'string', received type "+typeof e);return"0"===e[0]&&"x"===e[1]}(e)?e.slice(2):e},u=function(e){return e.startsWith("0x")?e:"0x"+e},a=function(){var r,t,c=(r=e.keccak256(e.concat([e.randomBytes(32),e.randomBytes(32)])),t=e.concat([e.concat([e.randomBytes(32),r]),e.randomBytes(32)]),e.keccak256(t)),i=new e.SigningKey(c),n=e.SigningKey.computePublicKey(i.publicKey,!1);return{privateKey:c,publicKey:y(n).slice(2)}},p=function(e,c){if(66!==u(c).length)throw new Error("Can only sign hashes, given: "+c);var i=r.secp256k1.sign(t.hexToBytes(y(c)),t.hexToBytes(y(e))),n=1===i.recovery?"1c":"1b";return"0x"+i.toCompactHex()+n},h=function(r,o){try{var y=(a=u=r,64===t.hexToBytes(u).length&&(a="04"+u),a.substring(2));return Promise.resolve(function(r,o){try{var y=e.randomBytes(32),u=e.randomBytes(16),a=i.secp256k1.getPublicKey(y,!1),p=i.secp256k1.getSharedSecret(y,n.hexToBytes(r),!0).slice(1),h=c.sha512(p),x=h.subarray(0,32),m=h.subarray(32),v=function(e,r,c){var i=s.enc.Hex.parse(e),n=t.bytesToHex(r),o=s.enc.Hex.parse(n),y=s.AES.encrypt(c,o,{mode:s.mode.CBC,iv:i});return s.enc.Hex.stringify(y.ciphertext)}(n.bytesToHex(u),x,o),g=function(r,c){var i=e.computeHmac("sha256",r,c);return t.hexToBytes(i)}(m,(b=(l=[u,a,n.hexToBytes(v)]).reduce(function(e,r){return e+r.byteLength},0),f=new Uint8Array(b),K=0,l.forEach(function(e){f.set(e,K),K+=e.byteLength}),f));return Promise.resolve({iv:u,ephemPublicKey:a,ciphertext:v,mac:g})}catch(e){return Promise.reject(e)}var l,b,f,K}("04"+y,o)).then(function(e){return{iv:t.bytesToHex(e.iv),ephemPublicKey:t.bytesToHex(e.ephemPublicKey),ciphertext:e.ciphertext,mac:t.bytesToHex(e.mac)}})}catch(e){return Promise.reject(e)}var u,a},x=function(e,r){try{var o=y(e);return Promise.resolve(function(e,r){var o=i.secp256k1.getSharedSecret(n.hexToBytes(e),r.ephemPublicKey,!0).slice(1),y=c.sha512(o).subarray(0,32);return function(e,r,c){var i=s.enc.Hex.parse(e),n=s.enc.Hex.parse(t.bytesToHex(r)),o=s.enc.Hex.parse(c),y=s.lib.CipherParams.create({ciphertext:o,iv:i,key:n});return s.AES.decrypt(y,n,{mode:s.mode.CBC,iv:i}).toString(s.enc.Utf8)}(r.iv,y,r.ciphertext)}(o,r))}catch(e){return Promise.reject(e)}},m=function(r){var t=[],c=[];return"string"==typeof r?(t.push("string"),c.push(r)):r.forEach(function(e){t.push(e.type),c.push(""+e.value)}),e.solidityPackedKeccak256(t,c)},v=function(r){var t=u(r),c=new e.SigningKey(t),i=e.SigningKey.computePublicKey(c.publicKey,!1);return y(i).slice(2)},g=function(e,r){var c=y(e),i=c.substring(0,c.length-2),n="1c"===c.slice(-2)?1:0;return t.bytesToHex(o.ecdsaRecover(t.hexToBytes(i),n,t.hexToBytes(y(r)),!1)).slice(2)},l={keccak256:m},b={createIdentity:a,decryptWithPrivateKey:x,encryptWithPublicKey:h,hash:l,keccak256:m,publicKeyByPrivateKey:v,recoverPublicKey:g,sign:p};exports.createIdentity=a,exports.decryptWithPrivateKey=x,exports.default=b,exports.encryptWithPublicKey=h,exports.hash=l,exports.keccak256=m,exports.publicKeyByPrivateKey=v,exports.recoverPublicKey=g,exports.sign=p;
//# sourceMappingURL=index.js.map
/**
* Generate publicKey from the privateKey with leading 0x.
* Generate publicKey from the privateKey.
* This creates the uncompressed publicKey,
* where 04 has stripped from left
* @returns {string}
*/
export declare function publicKeyByPrivateKey(privateKey: string): string;
export declare const publicKeyByPrivateKey: (privateKey: string) => string;

@@ -1,1 +0,7 @@

export declare function recoverPublicKey(signature: string, hash: string): string;
/**
* returns the publicKey for the privateKey with which the messageHash was signed
* @param {string} signature
* @param {string} hash
* @return {string} publicKey
*/
export declare const recoverPublicKey: (signature: string, hash: string) => string;
/**
* signs the given message
* we do not use sign from eth-lib because the pure secp256k1-version is 90% faster
* @param {string} privateKey

@@ -8,2 +7,3 @@ * @param {string} hash

*/
export declare function sign(privateKey: any, hash: any): string;
export declare const sign: (privateKey: string, hash: string) => string;
export declare const hmacSha256Sign: (key: Uint8Array, msg: Uint8Array) => Uint8Array;
{
"name": "eth-crypto-js",
"version": "0.2.15",
"version": "0.2.16",
"description": "Cryptographic javascript-functions for ethereum and tutorials on how to use them together with web3js and solidity",

@@ -73,2 +73,3 @@ "author": "Olivier Esuka <oesukan@gmail.com> (https://oesukam.me)",

"@rollup/plugin-typescript": "^11.1.6",
"@types/crypto-js": "^4.2.2",
"@types/jest": "^29.5.12",

@@ -79,3 +80,2 @@ "@types/node": "^20.14.8",

"babel-jest": "^29.7.0",
"coveralls": "^3.1.1",
"eslint": "^8.0.1",

@@ -93,5 +93,2 @@ "eslint-config-prettier": "^9.1.0",

"rollup": "^4.18.0",
"rollup-plugin-node-builtins": "^2.0.0",
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-polyfill-node": "^0.13.0",
"size-limit": "^8.2.4",

@@ -103,7 +100,6 @@ "ts-jest": "^29.1.5",

"dependencies": {
"eccrypto": "^1.1.6",
"ethereumjs-util": "^7.1.5",
"ethers": "^6.13.1",
"secp256k1": "^5.0.0"
"crypto-js": "^4.2.0",
"ethereum-cryptography": "^2.2.1",
"ethers": "^6.13.1"
}
}

@@ -1,22 +0,30 @@

import { Wallet } from 'ethers';
import { createPrivateKey } from './createPrivateKey';
import { publicKeyByPrivateKey } from './publicKeyByPrivateKey';
import { SigningKey, concat, keccak256, randomBytes } from 'ethers';
import { stripHexPrefix } from './util';
export interface Identity {
privateKey: string;
publicKey: string;
address: string;
}
/**
* create a privateKey
* @return {string}
*/
export const createIdentity = (entropy?: Buffer): Identity => {
const privateKey = createPrivateKey(entropy);
const wallet = new Wallet(privateKey);
export const createPrivateKey = () => {
const innerHex = keccak256(concat([randomBytes(32), randomBytes(32)]));
const middleHex = concat([concat([randomBytes(32), innerHex]), randomBytes(32)]);
const outerHex = keccak256(middleHex);
return outerHex;
};
/**
* creates a new object with
* private-Key and public-Key
*/
export const createIdentity = () => {
const privateKey = createPrivateKey();
const sign = new SigningKey(privateKey);
const walletPublicKey = SigningKey.computePublicKey(sign.publicKey, false);
const identity = {
privateKey: privateKey,
publicKey: publicKeyByPrivateKey(privateKey),
address: wallet.address,
publicKey: stripHexPrefix(walletPublicKey).slice(2),
};
return identity;
};

@@ -1,21 +0,11 @@

import { decrypt } from 'eccrypto';
import { removeLeading0x } from './utils/removeLeading0x';
import { cipherParser } from './utils/cipher/cipherParser';
import { stripHexPrefix } from './util';
import { decrypt } from './encryption-utils';
import { Encrypted } from './types';
export async function decryptWithPrivateKey(privateKey: string, encrypted): Promise<string> {
encrypted = cipherParser(encrypted);
export const decryptWithPrivateKey = async (privateKey: string, encrypted: Encrypted) => {
// remove '0x' from privateKey
const twoStripped = stripHexPrefix(privateKey);
// remove trailing '0x' from privateKey
const twoStripped = removeLeading0x(privateKey);
const encryptedBuffer = {
iv: Buffer.from(encrypted.iv, 'hex'),
ephemPublicKey: Buffer.from(encrypted.ephemPublicKey, 'hex'),
ciphertext: Buffer.from(encrypted.ciphertext, 'hex'),
mac: Buffer.from(encrypted.mac, 'hex'),
};
return decrypt(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then((decryptedBuffer) =>
decryptedBuffer.toString(),
);
}
const decrypted = await decrypt(twoStripped, encrypted);
return decrypted;
};

@@ -1,19 +0,20 @@

import { encrypt } from 'eccrypto';
import { Encrypted } from './types/Encrypted';
import { decompressPublicKey } from './utils/publicKey/decompressPublicKey';
import { decompress } from './decompress';
import { encrypt } from './encryption-utils';
import { bytesToHex } from 'ethereum-cryptography/utils';
export async function encryptWithPublicKey(publicKey: string, message: string, opts = {}): Promise<Encrypted> {
const decompressedPublicKey = decompressPublicKey(publicKey);
export const encryptWithPublicKey = async (publicKey: string, message: string) => {
// ensure its an uncompressed publicKey
const decompressedKey = decompress(publicKey);
const pubString = `04${decompressedPublicKey}`;
// re-add the compression-flag
const pubString = '04' + decompressedKey;
return encrypt(Buffer.from(pubString, 'hex'), Buffer.from(message), opts).then((encryptedBuffers) => {
const encrypted = {
iv: encryptedBuffers.iv.toString('hex'),
ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'),
ciphertext: encryptedBuffers.ciphertext.toString('hex'),
mac: encryptedBuffers.mac.toString('hex'),
};
return encrypted;
});
}
const result = await encrypt(pubString, message);
const encrypted = {
iv: bytesToHex(result.iv),
ephemPublicKey: bytesToHex(result.ephemPublicKey),
ciphertext: result.ciphertext,
mac: bytesToHex(result.mac),
};
return encrypted;
};

@@ -1,3 +0,1 @@

import { createPrivateKey } from './createPrivateKey';
import { publicKeyByPrivateKey } from './publicKeyByPrivateKey';
import { createIdentity } from './createIdentity';

@@ -7,12 +5,7 @@ import { sign } from './sign';

import { decryptWithPrivateKey } from './decryptWithPrivateKey';
import { keccak256 } from './hash';
import { publicKeyByPrivateKey } from './publicKeyByPrivateKey';
import { recoverPublicKey } from './recoverPublicKey';
import { Encrypted } from './types';
import { solidityKeccak256 as keccak256 } from './utils/hash/solidityKeccak256';
import { compressPublicKey } from './utils/publicKey/compressPublicKey';
import { decompressPublicKey } from './utils/publicKey/decompressPublicKey';
import { cipherParser } from './utils/cipher/cipherParser';
import { cipherStringify } from './utils/cipher/cipherStringify';
import { Encrypted } from './types/Encrypted';
const hash = {

@@ -22,26 +15,11 @@ keccak256,

const publicKey = {
compress: compressPublicKey,
decomporess: decompressPublicKey,
};
const cipher = {
parse: cipherParser,
stringify: cipherStringify,
};
export {
createPrivateKey,
publicKeyByPrivateKey,
createIdentity,
sign,
decryptWithPrivateKey,
encryptWithPublicKey,
decryptWithPrivateKey,
hash,
keccak256,
publicKeyByPrivateKey,
recoverPublicKey,
keccak256,
cipherParser,
cipherStringify,
hash,
publicKey,
cipher,
sign,
Encrypted,

@@ -51,15 +29,10 @@ };

export default {
createPrivateKey,
publicKeyByPrivateKey,
createIdentity,
sign,
decryptWithPrivateKey,
encryptWithPublicKey,
decryptWithPrivateKey,
hash,
keccak256,
publicKeyByPrivateKey,
recoverPublicKey,
keccak256,
cipherParser,
cipherStringify,
hash,
publicKey,
cipher,
sign,
};

@@ -1,14 +0,15 @@

import { privateToPublic, toBuffer } from 'ethereumjs-util';
import { addLeading0x } from './utils/addLeading0x';
import { SigningKey } from 'ethers';
import { addLeading0x, stripHexPrefix } from './util';
/**
* Generate publicKey from the privateKey with leading 0x.
* Generate publicKey from the privateKey.
* This creates the uncompressed publicKey,
* where 04 has stripped from left
* @returns {string}
*/
export function publicKeyByPrivateKey(privateKey: string): string {
const privateKeyWithLeading0x = addLeading0x(privateKey);
const publicKeyBuffer = privateToPublic(toBuffer(privateKeyWithLeading0x));
return publicKeyBuffer.toString('hex');
}
export const publicKeyByPrivateKey = (privateKey: string) => {
const key = addLeading0x(privateKey);
const sign = new SigningKey(key);
const publicKey = SigningKey.computePublicKey(sign.publicKey, false);
return stripHexPrefix(publicKey).slice(2);
};

@@ -1,18 +0,21 @@

import { ecdsaRecover } from 'secp256k1';
import { uint8ArrayToHex } from './utils/uint8ArrayToHex';
import { removeLeading0x } from './utils/removeLeading0x';
import { hexToUnit8Array } from './utils/hexToUnit8Array';
import { ecdsaRecover } from 'ethereum-cryptography/secp256k1-compat';
import { stripHexPrefix } from './util';
import { bytesToHex, hexToBytes } from 'ethereum-cryptography/utils';
export function recoverPublicKey(signature: string, hash: string): string {
signature = removeLeading0x(signature);
/**
* returns the publicKey for the privateKey with which the messageHash was signed
* @param {string} signature
* @param {string} hash
* @return {string} publicKey
*/
export const recoverPublicKey = (signature: string, hash: string) => {
const noHex = stripHexPrefix(signature);
// split into v-value and sig
const sigOnly = signature.substring(0, signature.length - 2); // all but last 2 chars
const vValue = signature.slice(-2); // last 2 chars
const sigOnly = noHex.substring(0, noHex.length - 2); // all but last 2 chars
const vValue = noHex.slice(-2); // last 2 chars
const recoveryNumber = vValue === '1c' ? 1 : 0;
let pubKey = uint8ArrayToHex(
ecdsaRecover(hexToUnit8Array(sigOnly), recoveryNumber, hexToUnit8Array(removeLeading0x(hash)), false),
);
let pubKey = bytesToHex(ecdsaRecover(hexToBytes(sigOnly), recoveryNumber, hexToBytes(stripHexPrefix(hash)), false));

@@ -23,2 +26,2 @@ // remove trailing '04'

return pubKey;
}
};

@@ -1,8 +0,8 @@

import { ecdsaSign as secp256k1_sign } from 'secp256k1';
import { addLeading0x } from './utils/addLeading0x';
import { removeLeading0x } from './utils/removeLeading0x';
import { secp256k1 } from 'ethereum-cryptography/secp256k1.js';
import { hexToBytes } from 'ethereum-cryptography/utils';
import { addLeading0x, stripHexPrefix } from './util';
import { computeHmac } from 'ethers';
/**
* signs the given message
* we do not use sign from eth-lib because the pure secp256k1-version is 90% faster
* @param {string} privateKey

@@ -12,15 +12,16 @@ * @param {string} hash

*/
export function sign(privateKey, hash): string {
hash = addLeading0x(hash);
if (hash.length !== 66) throw new Error('WallabyCrypto.sign(): Can only sign hashes, given: ' + hash);
export const sign = (privateKey: string, hash: string) => {
const hashWith0x = addLeading0x(hash);
if (hashWith0x.length !== 66) throw new Error('Can only sign hashes, given: ' + hash);
const sigObj = secp256k1_sign(
new Uint8Array(Buffer.from(removeLeading0x(hash), 'hex')),
new Uint8Array(Buffer.from(removeLeading0x(privateKey), 'hex')),
);
const sigObj = secp256k1.sign(hexToBytes(stripHexPrefix(hash)), hexToBytes(stripHexPrefix(privateKey)));
const recoveryId = sigObj.recid === 1 ? '1c' : '1b';
const recoveryId = sigObj.recovery === 1 ? '1c' : '1b';
const newSignature = '0x' + sigObj.toCompactHex() + recoveryId;
return newSignature;
};
const newSignature = '0x' + Buffer.from(sigObj.signature).toString('hex') + recoveryId;
return newSignature;
}
export const hmacSha256Sign = (key: Uint8Array, msg: Uint8Array) => {
const result = computeHmac('sha256', key, msg);
return hexToBytes(result);
};

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc