diff --git a/.solhint.json b/.solhint.json index 1968b23..5800f0b 100644 --- a/.solhint.json +++ b/.solhint.json @@ -1,7 +1,7 @@ { "extends": "solhint:default", "rules": { - "func-order": false, - "not-rely-on-time": false + "func-order": "off", + "not-rely-on-time": "off" } } diff --git a/dist/es/cipher.js b/dist/es/cipher.js index ea1ae50..f2cc5a3 100644 --- a/dist/es/cipher.js +++ b/dist/es/cipher.js @@ -6,10 +6,10 @@ export function stringify(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 + 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'); @@ -18,7 +18,7 @@ export function stringify(cipher) { export function parse(str) { if (typeof str !== 'string') return str; - var buf = new Buffer(str, 'hex'); + var buf = Buffer.from(str, 'hex'); var ret = { iv: buf.toString('hex', 0, 16), diff --git a/dist/es/decrypt-with-private-key.js b/dist/es/decrypt-with-private-key.js index 6b0802d..a17a93a 100644 --- a/dist/es/decrypt-with-private-key.js +++ b/dist/es/decrypt-with-private-key.js @@ -10,13 +10,13 @@ export default function decryptWithPrivateKey(privateKey, encrypted) { var twoStripped = removeTrailing0x(privateKey); var 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') + 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(new Buffer(twoStripped, 'hex'), encryptedBuffer).then(function (decryptedBuffer) { + return decrypt(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then(function (decryptedBuffer) { return decryptedBuffer.toString(); }); } \ No newline at end of file diff --git a/dist/es/encrypt-with-public-key.js b/dist/es/encrypt-with-public-key.js index 40d82b0..9247618 100644 --- a/dist/es/encrypt-with-public-key.js +++ b/dist/es/encrypt-with-public-key.js @@ -9,7 +9,7 @@ export default function encryptWithPublicKey(publicKey, message) { // re-add the compression-flag var pubString = '04' + publicKey; - return encrypt(new Buffer(pubString, 'hex'), Buffer(message)).then(function (encryptedBuffers) { + return encrypt(Buffer.from(pubString, 'hex'), Buffer.from(message)).then(function (encryptedBuffers) { var encrypted = { iv: encryptedBuffers.iv.toString('hex'), ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'), diff --git a/dist/es/hex.js b/dist/es/hex.js index 2c20357..e715f2b 100644 --- a/dist/es/hex.js +++ b/dist/es/hex.js @@ -12,7 +12,7 @@ export function compress(hex) { hex = removeTrailing0x(hex); // if base64:true, we use our own function because it results in a smaller output - if (base64 === true) return new Buffer(hex, 'hex').toString('base64'); + if (base64 === true) return Buffer.from(hex, 'hex').toString('base64'); var string = ''; while (hex.length % 4 != 0) { @@ -32,7 +32,7 @@ export function decompress(compressedString) { // if base64:true, we use our own function because it results in a smaller output if (base64 === true) { - var ret = new Buffer(compressedString, 'base64').toString('hex'); + var ret = Buffer.from(compressedString, 'base64').toString('hex'); return addTrailing0x(ret); } diff --git a/dist/es/public-key.js b/dist/es/public-key.js index 28c6a0d..aa1377a 100644 --- a/dist/es/public-key.js +++ b/dist/es/public-key.js @@ -1,22 +1,23 @@ import { publicKeyConvert } from 'secp256k1'; import { pubToAddress, toChecksumAddress } from 'ethereumjs-util'; +import { hexToUnit8Array, uint8ArrayToHex } from './util'; export function compress(startsWith04) { // add trailing 04 if not done before - var testBuffer = new Buffer(startsWith04, 'hex'); + var testBuffer = Buffer.from(startsWith04, 'hex'); if (testBuffer.length === 64) startsWith04 = '04' + startsWith04; - return publicKeyConvert(new Buffer(startsWith04, 'hex'), true).toString('hex'); + return uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith04), true)); } export function decompress(startsWith02Or03) { // if already decompressed an not has trailing 04 - var testBuffer = new Buffer(startsWith02Or03, 'hex'); + var testBuffer = Buffer.from(startsWith02Or03, 'hex'); if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03; - var decompressed = publicKeyConvert(new Buffer(startsWith02Or03, 'hex'), false).toString('hex'); + var decompressed = uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith02Or03), false)); // remove trailing 04 decompressed = decompressed.substring(2); @@ -33,7 +34,7 @@ export function toAddress(publicKey) { // normalize key publicKey = decompress(publicKey); - var addressBuffer = pubToAddress(new Buffer(publicKey, 'hex')); + var addressBuffer = pubToAddress(Buffer.from(publicKey, 'hex')); var checkSumAdress = toChecksumAddress(addressBuffer.toString('hex')); return checkSumAdress; } \ No newline at end of file diff --git a/dist/es/recover-public-key.js b/dist/es/recover-public-key.js index 14d4a6f..4f7e43e 100644 --- a/dist/es/recover-public-key.js +++ b/dist/es/recover-public-key.js @@ -1,5 +1,5 @@ -import { recover } from 'secp256k1'; -import { removeTrailing0x } from './util'; +import { ecdsaRecover } from 'secp256k1'; +import { removeTrailing0x, hexToUnit8Array, uint8ArrayToHex } from './util'; /** * returns the publicKey for the privateKey with which the messageHash was signed @@ -16,7 +16,7 @@ export default function recoverPublicKey(signature, hash) { var recoveryNumber = vValue === '1c' ? 1 : 0; - var pubKey = recover(new Buffer(removeTrailing0x(hash), 'hex'), new Buffer(sigOnly, 'hex'), recoveryNumber, false).toString('hex'); + var pubKey = uint8ArrayToHex(ecdsaRecover(hexToUnit8Array(sigOnly), recoveryNumber, hexToUnit8Array(removeTrailing0x(hash)), false)); // remove trailing '04' pubKey = pubKey.slice(2); diff --git a/dist/es/sign-transaction.js b/dist/es/sign-transaction.js index 34c8083..728e245 100644 --- a/dist/es/sign-transaction.js +++ b/dist/es/sign-transaction.js @@ -9,7 +9,7 @@ export default function signTransaction(rawTx, privateKey) { var address = addressByPublicKey(publicKey); if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey'); - var privateKeyBuffer = new Buffer(privateKey.replace(/^.{2}/g, ''), 'hex'); + var privateKeyBuffer = Buffer.from(privateKey.replace(/^.{2}/g, ''), 'hex'); var tx = new Transaction(rawTx); tx.sign(privateKeyBuffer); diff --git a/dist/es/sign.js b/dist/es/sign.js index 7073218..607f2fa 100644 --- a/dist/es/sign.js +++ b/dist/es/sign.js @@ -1,4 +1,4 @@ -import { sign as secp256k1_sign } from 'secp256k1'; +import { ecdsaSign as secp256k1_sign } from 'secp256k1'; import { addTrailing0x, removeTrailing0x } from './util'; /** @@ -12,10 +12,10 @@ export default function sign(privateKey, hash) { hash = addTrailing0x(hash); if (hash.length !== 66) throw new Error('EthCrypto.sign(): Can only sign hashes, given: ' + hash); - var sigObj = secp256k1_sign(new Buffer(removeTrailing0x(hash), 'hex'), new Buffer(removeTrailing0x(privateKey), 'hex')); + var sigObj = secp256k1_sign(new Uint8Array(Buffer.from(removeTrailing0x(hash), 'hex')), new Uint8Array(Buffer.from(removeTrailing0x(privateKey), 'hex'))); - var recoveryId = sigObj.recovery === 1 ? '1c' : '1b'; + var recoveryId = sigObj.recid === 1 ? '1c' : '1b'; - var newSignature = '0x' + sigObj.signature.toString('hex') + recoveryId; + var newSignature = '0x' + Buffer.from(sigObj.signature).toString('hex') + recoveryId; return newSignature; } \ No newline at end of file diff --git a/dist/es/util.js b/dist/es/util.js index e31363c..876035f 100644 --- a/dist/es/util.js +++ b/dist/es/util.js @@ -4,4 +4,12 @@ export function removeTrailing0x(str) { export function addTrailing0x(str) { if (!str.startsWith('0x')) return '0x' + str;else return str; +} + +export function uint8ArrayToHex(arr) { + return Buffer.from(arr).toString('hex'); +} + +export function hexToUnit8Array(str) { + return new Uint8Array(Buffer.from(str, 'hex')); } \ No newline at end of file diff --git a/dist/lib/cipher.js b/dist/lib/cipher.js index 126c5bc..3bd24ef 100644 --- a/dist/lib/cipher.js +++ b/dist/lib/cipher.js @@ -14,10 +14,10 @@ function stringify(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 + 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'); @@ -26,7 +26,7 @@ function stringify(cipher) { function parse(str) { if (typeof str !== 'string') return str; - var buf = new Buffer(str, 'hex'); + var buf = Buffer.from(str, 'hex'); var ret = { iv: buf.toString('hex', 0, 16), diff --git a/dist/lib/decrypt-with-private-key.js b/dist/lib/decrypt-with-private-key.js index 95dc83b..0219385 100644 --- a/dist/lib/decrypt-with-private-key.js +++ b/dist/lib/decrypt-with-private-key.js @@ -19,13 +19,13 @@ function decryptWithPrivateKey(privateKey, encrypted) { var twoStripped = (0, _util.removeTrailing0x)(privateKey); var 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') + 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 (0, _eccrypto.decrypt)(new Buffer(twoStripped, 'hex'), encryptedBuffer).then(function (decryptedBuffer) { + return (0, _eccrypto.decrypt)(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then(function (decryptedBuffer) { return decryptedBuffer.toString(); }); } \ No newline at end of file diff --git a/dist/lib/encrypt-with-public-key.js b/dist/lib/encrypt-with-public-key.js index 91b9180..c158e50 100644 --- a/dist/lib/encrypt-with-public-key.js +++ b/dist/lib/encrypt-with-public-key.js @@ -17,7 +17,7 @@ function encryptWithPublicKey(publicKey, message) { // re-add the compression-flag var pubString = '04' + publicKey; - return (0, _eccrypto.encrypt)(new Buffer(pubString, 'hex'), Buffer(message)).then(function (encryptedBuffers) { + return (0, _eccrypto.encrypt)(Buffer.from(pubString, 'hex'), Buffer.from(message)).then(function (encryptedBuffers) { var encrypted = { iv: encryptedBuffers.iv.toString('hex'), ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'), diff --git a/dist/lib/hex.js b/dist/lib/hex.js index 909a3bb..a10e3cb 100644 --- a/dist/lib/hex.js +++ b/dist/lib/hex.js @@ -14,7 +14,7 @@ function compress(hex) { hex = (0, _util.removeTrailing0x)(hex); // if base64:true, we use our own function because it results in a smaller output - if (base64 === true) return new Buffer(hex, 'hex').toString('base64'); + if (base64 === true) return Buffer.from(hex, 'hex').toString('base64'); var string = ''; while (hex.length % 4 != 0) { @@ -38,7 +38,7 @@ function decompress(compressedString) { // if base64:true, we use our own function because it results in a smaller output if (base64 === true) { - var ret = new Buffer(compressedString, 'base64').toString('hex'); + var ret = Buffer.from(compressedString, 'base64').toString('hex'); return (0, _util.addTrailing0x)(ret); } diff --git a/dist/lib/public-key.js b/dist/lib/public-key.js index c55904b..6a42363 100644 --- a/dist/lib/public-key.js +++ b/dist/lib/public-key.js @@ -11,22 +11,24 @@ 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 = new Buffer(startsWith04, 'hex'); + var testBuffer = Buffer.from(startsWith04, 'hex'); if (testBuffer.length === 64) startsWith04 = '04' + startsWith04; - return (0, _secp256k.publicKeyConvert)(new Buffer(startsWith04, 'hex'), true).toString('hex'); + 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 = new Buffer(startsWith02Or03, 'hex'); + var testBuffer = Buffer.from(startsWith02Or03, 'hex'); if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03; - var decompressed = (0, _secp256k.publicKeyConvert)(new Buffer(startsWith02Or03, 'hex'), false).toString('hex'); + var decompressed = (0, _util.uint8ArrayToHex)((0, _secp256k.publicKeyConvert)((0, _util.hexToUnit8Array)(startsWith02Or03), false)); // remove trailing 04 decompressed = decompressed.substring(2); @@ -43,7 +45,7 @@ function toAddress(publicKey) { // normalize key publicKey = decompress(publicKey); - var addressBuffer = (0, _ethereumjsUtil.pubToAddress)(new Buffer(publicKey, 'hex')); + var addressBuffer = (0, _ethereumjsUtil.pubToAddress)(Buffer.from(publicKey, 'hex')); var checkSumAdress = (0, _ethereumjsUtil.toChecksumAddress)(addressBuffer.toString('hex')); return checkSumAdress; } \ No newline at end of file diff --git a/dist/lib/recover-public-key.js b/dist/lib/recover-public-key.js index e972395..446f98e 100644 --- a/dist/lib/recover-public-key.js +++ b/dist/lib/recover-public-key.js @@ -24,7 +24,7 @@ function recoverPublicKey(signature, hash) { var recoveryNumber = vValue === '1c' ? 1 : 0; - var pubKey = (0, _secp256k.recover)(new Buffer((0, _util.removeTrailing0x)(hash), 'hex'), new Buffer(sigOnly, 'hex'), recoveryNumber, false).toString('hex'); + var pubKey = (0, _util.uint8ArrayToHex)((0, _secp256k.ecdsaRecover)((0, _util.hexToUnit8Array)(sigOnly), recoveryNumber, (0, _util.hexToUnit8Array)((0, _util.removeTrailing0x)(hash)), false)); // remove trailing '04' pubKey = pubKey.slice(2); diff --git a/dist/lib/sign-transaction.js b/dist/lib/sign-transaction.js index 4121fc3..f6c4a84 100644 --- a/dist/lib/sign-transaction.js +++ b/dist/lib/sign-transaction.js @@ -22,7 +22,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 = new Buffer(privateKey.replace(/^.{2}/g, ''), 'hex'); + var privateKeyBuffer = Buffer.from(privateKey.replace(/^.{2}/g, ''), 'hex'); var tx = new _ethereumjsTx.Transaction(rawTx); tx.sign(privateKeyBuffer); diff --git a/dist/lib/sign.js b/dist/lib/sign.js index 987487b..5dc58ec 100644 --- a/dist/lib/sign.js +++ b/dist/lib/sign.js @@ -20,10 +20,10 @@ function sign(privateKey, hash) { hash = (0, _util.addTrailing0x)(hash); if (hash.length !== 66) throw new Error('EthCrypto.sign(): Can only sign hashes, given: ' + hash); - var sigObj = (0, _secp256k.sign)(new Buffer((0, _util.removeTrailing0x)(hash), 'hex'), new Buffer((0, _util.removeTrailing0x)(privateKey), 'hex')); + var sigObj = (0, _secp256k.ecdsaSign)(new Uint8Array(Buffer.from((0, _util.removeTrailing0x)(hash), 'hex')), new Uint8Array(Buffer.from((0, _util.removeTrailing0x)(privateKey), 'hex'))); - var recoveryId = sigObj.recovery === 1 ? '1c' : '1b'; + var recoveryId = sigObj.recid === 1 ? '1c' : '1b'; - var newSignature = '0x' + sigObj.signature.toString('hex') + recoveryId; + var newSignature = '0x' + Buffer.from(sigObj.signature).toString('hex') + recoveryId; return newSignature; } \ No newline at end of file diff --git a/dist/lib/util.js b/dist/lib/util.js index 1f83c67..df7f5b2 100644 --- a/dist/lib/util.js +++ b/dist/lib/util.js @@ -5,10 +5,20 @@ Object.defineProperty(exports, "__esModule", { }); exports.removeTrailing0x = removeTrailing0x; exports.addTrailing0x = addTrailing0x; +exports.uint8ArrayToHex = uint8ArrayToHex; +exports.hexToUnit8Array = hexToUnit8Array; function removeTrailing0x(str) { if (str.startsWith('0x')) return str.substring(2);else return str; } function addTrailing0x(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')); } \ No newline at end of file diff --git a/package.json b/package.json index 84542bc..5e71e15 100644 --- a/package.json +++ b/package.json @@ -115,6 +115,6 @@ "ethereumjs-tx": "2.1.2", "ethereumjs-util": "6.2.0", "ethers": "4.0.47", - "secp256k1": "3.8.0" + "secp256k1": "4.0.1" } } diff --git a/src/cipher.js b/src/cipher.js index 34bb7b5..53f46ac 100644 --- a/src/cipher.js +++ b/src/cipher.js @@ -10,10 +10,10 @@ export function stringify(cipher) { const compressedKey = compress(cipher.ephemPublicKey); const 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 + 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'); @@ -25,7 +25,7 @@ export function parse(str) { if (typeof str !== 'string') return str; - const buf = new Buffer(str, 'hex'); + const buf = Buffer.from(str, 'hex'); const ret = { iv: buf.toString('hex', 0, 16), diff --git a/src/decrypt-with-private-key.js b/src/decrypt-with-private-key.js index ee2552e..62e576a 100644 --- a/src/decrypt-with-private-key.js +++ b/src/decrypt-with-private-key.js @@ -16,15 +16,15 @@ export default function decryptWithPrivateKey(privateKey, encrypted) { const twoStripped = removeTrailing0x(privateKey); const 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') + 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( - new Buffer(twoStripped, 'hex'), + Buffer.from(twoStripped, 'hex'), encryptedBuffer ).then(decryptedBuffer => decryptedBuffer.toString()); } diff --git a/src/encrypt-with-public-key.js b/src/encrypt-with-public-key.js index ee4a904..fb007b4 100644 --- a/src/encrypt-with-public-key.js +++ b/src/encrypt-with-public-key.js @@ -15,8 +15,8 @@ export default function encryptWithPublicKey(publicKey, message) { return encrypt( - new Buffer(pubString, 'hex'), - Buffer(message) + Buffer.from(pubString, 'hex'), + Buffer.from(message) ).then(encryptedBuffers => { const encrypted = { iv: encryptedBuffers.iv.toString('hex'), diff --git a/src/hex.js b/src/hex.js index a9e7e7f..430a5e7 100644 --- a/src/hex.js +++ b/src/hex.js @@ -14,7 +14,7 @@ export function compress(hex, base64 = false) { // if base64:true, we use our own function because it results in a smaller output if (base64 === true) - return new Buffer(hex, 'hex').toString('base64'); + return Buffer.from(hex, 'hex').toString('base64'); let string = ''; while (hex.length % 4 != 0) { // we need it to be multiple of 4 @@ -31,7 +31,7 @@ export function decompress(compressedString, base64 = false) { // if base64:true, we use our own function because it results in a smaller output if (base64 === true) { - const ret = new Buffer(compressedString, 'base64').toString('hex'); + const ret = Buffer.from(compressedString, 'base64').toString('hex'); return addTrailing0x(ret); } diff --git a/src/public-key.js b/src/public-key.js index 22d709d..7442cd3 100644 --- a/src/public-key.js +++ b/src/public-key.js @@ -5,30 +5,34 @@ import { pubToAddress, toChecksumAddress } from 'ethereumjs-util'; +import { + hexToUnit8Array, + uint8ArrayToHex +} from './util'; export function compress(startsWith04) { // add trailing 04 if not done before - const testBuffer = new Buffer(startsWith04, 'hex'); + const testBuffer = Buffer.from(startsWith04, 'hex'); if (testBuffer.length === 64) startsWith04 = '04' + startsWith04; - return publicKeyConvert( - new Buffer(startsWith04, 'hex'), + return uint8ArrayToHex(publicKeyConvert( + hexToUnit8Array(startsWith04), true - ).toString('hex'); + )); } export function decompress(startsWith02Or03) { // if already decompressed an not has trailing 04 - const testBuffer = new Buffer(startsWith02Or03, 'hex'); + const testBuffer = Buffer.from(startsWith02Or03, 'hex'); if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03; - let decompressed = publicKeyConvert( - new Buffer(startsWith02Or03, 'hex'), + let decompressed = uint8ArrayToHex(publicKeyConvert( + hexToUnit8Array(startsWith02Or03), false - ).toString('hex'); + )); // remove trailing 04 decompressed = decompressed.substring(2); @@ -45,7 +49,7 @@ export function toAddress(publicKey) { // normalize key publicKey = decompress(publicKey); - const addressBuffer = pubToAddress(new Buffer(publicKey, 'hex')); + const addressBuffer = pubToAddress(Buffer.from(publicKey, 'hex')); const checkSumAdress = toChecksumAddress(addressBuffer.toString('hex')); return checkSumAdress; } diff --git a/src/recover-public-key.js b/src/recover-public-key.js index 4db0261..2074a43 100644 --- a/src/recover-public-key.js +++ b/src/recover-public-key.js @@ -1,8 +1,10 @@ import { - recover + ecdsaRecover } from 'secp256k1'; import { - removeTrailing0x + removeTrailing0x, + hexToUnit8Array, + uint8ArrayToHex } from './util'; @@ -21,12 +23,12 @@ export default function recoverPublicKey(signature, hash) { const recoveryNumber = vValue === '1c' ? 1 : 0; - let pubKey = recover( - new Buffer(removeTrailing0x(hash), 'hex'), - new Buffer(sigOnly, 'hex'), + let pubKey = uint8ArrayToHex(ecdsaRecover( + hexToUnit8Array(sigOnly), recoveryNumber, + hexToUnit8Array(removeTrailing0x(hash)), false - ).toString('hex'); + )); // remove trailing '04' pubKey = pubKey.slice(2); diff --git a/src/sign-transaction.js b/src/sign-transaction.js index ed75812..7ecba20 100644 --- a/src/sign-transaction.js +++ b/src/sign-transaction.js @@ -15,7 +15,7 @@ export default function signTransaction( if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey'); - const privateKeyBuffer = new Buffer(privateKey.replace(/^.{2}/g, ''), 'hex'); + const privateKeyBuffer = Buffer.from(privateKey.replace(/^.{2}/g, ''), 'hex'); const tx = new Transaction(rawTx); tx.sign(privateKeyBuffer); diff --git a/src/sign.js b/src/sign.js index 98d2fb7..afa2bbe 100644 --- a/src/sign.js +++ b/src/sign.js @@ -1,5 +1,5 @@ import { - sign as secp256k1_sign + ecdsaSign as secp256k1_sign } from 'secp256k1'; import { addTrailing0x, @@ -19,12 +19,12 @@ export default function sign(privateKey, hash) { throw new Error('EthCrypto.sign(): Can only sign hashes, given: ' + hash); const sigObj = secp256k1_sign( - new Buffer(removeTrailing0x(hash), 'hex'), - new Buffer(removeTrailing0x(privateKey), 'hex') + new Uint8Array(Buffer.from(removeTrailing0x(hash), 'hex')), + new Uint8Array(Buffer.from(removeTrailing0x(privateKey), 'hex')) ); - const recoveryId = sigObj.recovery === 1 ? '1c' : '1b'; + const recoveryId = sigObj.recid === 1 ? '1c' : '1b'; - const newSignature = '0x' + sigObj.signature.toString('hex') + recoveryId; + const newSignature = '0x' + Buffer.from(sigObj.signature).toString('hex') + recoveryId; return newSignature; } diff --git a/src/util.js b/src/util.js index 982f103..60620ab 100644 --- a/src/util.js +++ b/src/util.js @@ -9,3 +9,11 @@ export function addTrailing0x(str) { return '0x' + str; else return str; } + +export function uint8ArrayToHex(arr) { + return Buffer.from(arr).toString('hex'); +} + +export function hexToUnit8Array(str) { + return new Uint8Array(Buffer.from(str, 'hex')); +} diff --git a/test/unit.test.js b/test/unit.test.js index 7ecd4f5..f701dfa 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -170,7 +170,7 @@ describe('unit.test.js', () => { AsyncTestUtil.randomString(12), message ), - 'RangeError' + 'Error' ); }); });