Merge pull request #7922 from MetaMask/Version-v7.7.4
Version v7.7.4 RCfeature/default_network_editable
commit
de3b98c9ee
@ -1,28 +1,33 @@ |
||||
import { decimalToHex } from '../../helpers/utils/conversions.util' |
||||
import { calcTokenValue } from '../../helpers/utils/token-util.js' |
||||
import { getTokenData } from '../../helpers/utils/transactions.util' |
||||
|
||||
export function getCustomTxParamsData (data, { customPermissionAmount, tokenAmount, decimals }) { |
||||
if (customPermissionAmount) { |
||||
const tokenValue = decimalToHex(calcTokenValue(tokenAmount, decimals)) |
||||
export function getCustomTxParamsData (data, { customPermissionAmount, decimals }) { |
||||
const tokenData = getTokenData(data) |
||||
|
||||
const re = new RegExp('(^.+)' + tokenValue + '$') |
||||
const matches = re.exec(data) |
||||
|
||||
if (!matches || !matches[1]) { |
||||
return data |
||||
} |
||||
let dataWithoutCurrentAmount = matches[1] |
||||
const customPermissionValue = decimalToHex(calcTokenValue(Number(customPermissionAmount), decimals)) |
||||
if (!tokenData) { |
||||
throw new Error('Invalid data') |
||||
} else if (tokenData.name !== 'approve') { |
||||
throw new Error(`Invalid data; should be 'approve' method, but instead is '${tokenData.name}'`) |
||||
} |
||||
let spender = tokenData.params[0].value |
||||
if (spender.startsWith('0x')) { |
||||
spender = spender.substring(2) |
||||
} |
||||
const [signature, tokenValue] = data.split(spender) |
||||
|
||||
const differenceInLengths = customPermissionValue.length - tokenValue.length |
||||
const zeroModifier = dataWithoutCurrentAmount.length - differenceInLengths |
||||
if (differenceInLengths > 0) { |
||||
dataWithoutCurrentAmount = dataWithoutCurrentAmount.slice(0, zeroModifier) |
||||
} else if (differenceInLengths < 0) { |
||||
dataWithoutCurrentAmount = dataWithoutCurrentAmount.padEnd(zeroModifier, 0) |
||||
} |
||||
if (!signature || !tokenValue) { |
||||
throw new Error('Invalid data') |
||||
} else if (tokenValue.length !== 64) { |
||||
throw new Error('Invalid token value; should be exactly 64 hex digits long (u256)') |
||||
} |
||||
|
||||
const customTxParamsData = dataWithoutCurrentAmount + customPermissionValue |
||||
return customTxParamsData |
||||
let customPermissionValue = decimalToHex(calcTokenValue(customPermissionAmount, decimals)) |
||||
if (customPermissionValue.length > 64) { |
||||
throw new Error('Custom value is larger than u256') |
||||
} |
||||
|
||||
customPermissionValue = customPermissionValue.padStart(tokenValue.length, '0') |
||||
const customTxParamsData = `${signature}${spender}${customPermissionValue}` |
||||
return customTxParamsData |
||||
} |
||||
|
Loading…
Reference in new issue