1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-05 07:24:56 +01:00
metamask-extension/ui/pages/confirm-approve/confirm-approve.util.js
Filip Sekulic 6e13524bcd
Remove related UI code from the app dir (#15384)
Co-authored-by: metamaskbot <metamaskbot@users.noreply.github.com>
Co-authored-by: Brad Decker <bhdecker84@gmail.com>
Co-authored-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
2022-09-16 14:05:21 -05:00

48 lines
1.6 KiB
JavaScript

import { calcTokenValue } from '../../../shared/lib/swaps-utils';
import { decimalToHex } from '../../../shared/lib/transactions-controller-utils';
import { TRANSACTION_TYPES } from '../../../shared/constants/transaction';
import { parseStandardTokenTransactionData } from '../../../shared/modules/transaction.utils';
import { getTokenAddressParam } from '../../helpers/utils/token-util';
export function getCustomTxParamsData(
data,
{ customPermissionAmount, decimals },
) {
const tokenData = parseStandardTokenTransactionData(data);
if (!tokenData) {
throw new Error('Invalid data');
} else if (tokenData.name !== TRANSACTION_TYPES.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;
}