Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Added validation logic in utils and isEqual function in SignatureData #520

Merged
merged 3 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/caver-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 6 additions & 3 deletions packages/caver-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`

Expand Down
14 changes: 14 additions & 0 deletions packages/caver-wallet/src/keyring/signatureData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string>|SignatureData} sig - The ECDSA signatureData to compare
* @return {boolean}
*/
isEqual(sig) {
sig = new SignatureData(sig)
return this.toString() === sig.toString()
}
}

/**
Expand Down