mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-18 07:23:21 +01:00
a49a4a066c
* expand transaction constants coverage * touchups * dont import inside of e2e * Update app/scripts/controllers/transactions/tx-state-manager.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> * Update test/unit/app/controllers/transactions/tx-controller-test.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Co-authored-by: Mark Stacey <markjstacey@gmail.com>
47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
import { TRANSACTION_CATEGORIES } from '../../../../shared/constants/transaction'
|
|
import { decimalToHex } from '../../helpers/utils/conversions.util'
|
|
import {
|
|
calcTokenValue,
|
|
getTokenAddressParam,
|
|
} from '../../helpers/utils/token-util'
|
|
import { getTokenData } from '../../helpers/utils/transactions.util'
|
|
|
|
export function getCustomTxParamsData(
|
|
data,
|
|
{ customPermissionAmount, decimals },
|
|
) {
|
|
const tokenData = getTokenData(data)
|
|
|
|
if (!tokenData) {
|
|
throw new Error('Invalid data')
|
|
} else if (tokenData.name !== TRANSACTION_CATEGORIES.TOKEN_METHOD_APPROVE) {
|
|
throw new Error(
|
|
`Invalid data; should be 'approve' method, but instead is '${tokenData.name}'`,
|
|
)
|
|
}
|
|
let spender = getTokenAddressParam(tokenData)
|
|
if (spender.startsWith('0x')) {
|
|
spender = spender.substring(2)
|
|
}
|
|
const [signature, tokenValue] = data.split(spender)
|
|
|
|
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)',
|
|
)
|
|
}
|
|
|
|
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
|
|
}
|