From 5c27b85642eee03d7b73297c97d1bc4daef17f27 Mon Sep 17 00:00:00 2001 From: Jasmine/kimjimin Date: Tue, 17 Aug 2021 16:39:55 +0900 Subject: [PATCH 1/3] Added validation logic in utils and isEqual function in SignatureData --- packages/caver-utils/src/index.js | 17 +++++++++++++---- packages/caver-utils/src/utils.js | 9 ++++++--- .../caver-wallet/src/keyring/signatureData.js | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/packages/caver-utils/src/index.js b/packages/caver-utils/src/index.js index c0247069..3122df87 100644 --- a/packages/caver-utils/src/index.js +++ b/packages/caver-utils/src/index.js @@ -318,13 +318,21 @@ const unitKlayToEthMap = { TKLAY: 'tether', } -const getKlayUnitValue = function(unit) { - unit = unit || 'KLAY' +const getKlayUnitValue = function(u) { + unit = u || 'KLAY' + + if (_.isObject(u) && u.unit) unit = u.unit + if (!unitKlayMap[unit]) { throw new Error( `This unit "${unit}" doesn't exist, please use the one of the following units${JSON.stringify(unitKlayMap, null, 2)}` ) } + + if (u.pebFactor !== undefined && KlayUnit[u.unit].pebFactor !== u.pebFactor) { + throw new Error(`peb factor does not match with given unit`) + } + return unit } @@ -378,7 +386,6 @@ const toPeb = function(number, unit) { * @return {string} The string number. */ const convertFromPeb = function(amount, unitString) { - if (_.isObject(unitString) && unitString.unit) unitString = unitString.unit const converted = fromPeb(amount, unitString) return utils.isBN(converted) ? converted.toString(10) : converted } @@ -400,7 +407,6 @@ const convertFromPeb = function(amount, unitString) { * @return {string|BN} */ const convertToPeb = function(number, unitString) { - if (_.isObject(unitString) && unitString.unit) unitString = unitString.unit const converted = toPeb(number, unitString) return utils.isBN(converted) ? converted.toString(10) : converted } @@ -555,6 +561,9 @@ const stripHexPrefix = function(str) { * @return {SignatureData} */ const decodeSignature = signature => { + if (Buffer.byteLength(stripHexPrefix(signature), 'hex') !== 65) + throw new Error(`Invalid signature: The length of raw signature must be 65 byte.`) + const ret = Account.decodeSignature(signature).map(sig => utils.makeEven(utils.trimLeadingZero(sig))) return new SignatureData(ret) } diff --git a/packages/caver-utils/src/utils.js b/packages/caver-utils/src/utils.js index 355e7060..58c39b24 100644 --- a/packages/caver-utils/src/utils.js +++ b/packages/caver-utils/src/utils.js @@ -1453,14 +1453,17 @@ const recover = (message, signature, isHashed = false) => { * @inner * * @method publicKeyToAddress - * @param {string} publicKey The public key string to get the address. + * @param {string} pubKey The public key string to get the address. * @return {string} */ -const publicKeyToAddress = publicKey => { - publicKey = publicKey.slice(0, 2) === '0x' ? publicKey : `0x${publicKey}` +const publicKeyToAddress = pubKey => { + let publicKey = pubKey.slice(0, 2) === '0x' ? pubKey : `0x${pubKey}` if (isCompressedPublicKey(publicKey)) publicKey = decompressPublicKey(publicKey) + // With '0x' prefix, 65 bytes in uncompressed format. + if (Buffer.byteLength(publicKey, 'hex') !== 65) throw new Error(`Invalid public key: ${pubKey}`) + const publicHash = Hash.keccak256(publicKey) const address = `0x${publicHash.slice(-40)}` diff --git a/packages/caver-wallet/src/keyring/signatureData.js b/packages/caver-wallet/src/keyring/signatureData.js index 32940ec5..5b43351e 100644 --- a/packages/caver-wallet/src/keyring/signatureData.js +++ b/packages/caver-wallet/src/keyring/signatureData.js @@ -162,6 +162,20 @@ class SignatureData { toString() { return this.v + this.r + this.s } + + /** + * Checks that the signature data is the same. + * + * @example + * const isEqual = signatureData.isEqual([ '0x1b', '0xc6901...', '0x642d8...' ]) + * + * @param {Array.|SignatureData} sig - The ECDSA signatureData to compare + * @return {boolean} + */ + isEqual(sig) { + sig = new SignatureData(sig) + return this.toString() === sig.toString() + } } /** From 733462788f00c3382257066ce5bcdac5afdeeb62 Mon Sep 17 00:00:00 2001 From: Jasmine/kimjimin Date: Tue, 17 Aug 2021 16:48:52 +0900 Subject: [PATCH 2/3] Defined undefined value --- packages/caver-utils/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/caver-utils/src/index.js b/packages/caver-utils/src/index.js index 3122df87..6b5103f2 100644 --- a/packages/caver-utils/src/index.js +++ b/packages/caver-utils/src/index.js @@ -319,7 +319,7 @@ const unitKlayToEthMap = { } const getKlayUnitValue = function(u) { - unit = u || 'KLAY' + let unit = u || 'KLAY' if (_.isObject(u) && u.unit) unit = u.unit From 4c61e2b944117e835adbbf311631e93a3cb81767 Mon Sep 17 00:00:00 2001 From: Jasmine/kimjimin Date: Tue, 17 Aug 2021 16:54:29 +0900 Subject: [PATCH 3/3] Added if condition for optional value --- packages/caver-utils/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/caver-utils/src/index.js b/packages/caver-utils/src/index.js index 6b5103f2..995953bd 100644 --- a/packages/caver-utils/src/index.js +++ b/packages/caver-utils/src/index.js @@ -329,7 +329,7 @@ const getKlayUnitValue = function(u) { ) } - if (u.pebFactor !== undefined && KlayUnit[u.unit].pebFactor !== u.pebFactor) { + if (u && u.pebFactor !== undefined && KlayUnit[u.unit].pebFactor !== u.pebFactor) { throw new Error(`peb factor does not match with given unit`) }