diff --git a/packages/caver-utils/src/index.js b/packages/caver-utils/src/index.js index c0247069..995953bd 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) { + let 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 && 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() + } } /**