'Sign signature buyTokenByTokenWithPermission ETH BSC memory _signature bytes _signature

I am currently writing a python script to interact with a BSC contract.

I don't know how to generate the bytes_signature for this function for the below txhash. It must be something simple that I am missing.

[8]:  3deec388befec448046d583d65e15cd77aa9c74ba8c534305089f5869ffe9208
[9]:  7f29af0ba8d129018f19a520a8b342b0ea90df02aed99a4b28f8db2dc61df072

https://bscscan.com/tx/0x94e9df05600e46ae0ef5fd7dc890a44d514e8dd08283a4e909410fac6220efe5

Function: buyTokenByTokenWithPermission(address _beneficiary, address _token, uint256 _amount, address _candidate, uint256 _maxAmount, uint256 _minAmount, bytes _signature) ***

MethodID: 0xa1491efc
[0]:  0000000000000000000000002cff6fb5a463735d65c29d933ce7f1c45350cb33
[1]:  000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d56
[2]:  000000000000000000000000000000000000000000000005e74a8505e80a0000
[3]:  0000000000000000000000002cff6fb5a463735d65c29d933ce7f1c45350cb33
[4]:  000000000000000000000000000000000000000000000094f138d51f01ba2f70
[5]:  0000000000000000000000000000000000000000000000000000000000000000
[6]:  00000000000000000000000000000000000000000000000000000000000000e0
[7]:  0000000000000000000000000000000000000000000000000000000000000041
[8]:  3deec388befec448046d583d65e15cd77aa9c74ba8c534305089f5869ffe9208
[9]:  7f29af0ba8d129018f19a520a8b342b0ea90df02aed99a4b28f8db2dc61df072
[10]: 1c00000000000000000000000000000000000000000000000000000000000000

See more info below

function buyTokenByTokenWithPermission(
address _beneficiary,
address _token,
uint256 _amount,
address _candidate,
uint256 _maxAmount,
uint256 _minAmount,
bytes memory _signature
) public whenNotPaused nonReentrant {
require(offeredCurrencies[_token].rate != 0, "POOL::PURCHASE_METHOD_NOT_ALLOWED");
require(_validPurchase(), "POOL::ENDED");
require(_verifyWhitelist(_candidate, _maxAmount, _minAmount, _signature), "POOL:INVALID_SIGNATURE");

_preValidatePurchase(_beneficiary, _amount);

uint256 tokens = _getOfferedCurrencyToTokenAmount(_token, _amount);
require(getAvailableTokensForSale() >= tokens, "POOL::NOT_ENOUGHT_TOKENS_FOR_SALE");
require(tokens >= _minAmount || userPurchased[_candidate].add(tokens) >= _minAmount, "POOL::MIN_AMOUNT_UNREACHED");
require(userPurchased[_candidate].add(tokens) <= _maxAmount, "POOL:PURCHASE_AMOUNT_EXCEED_ALLOWANCE");

_forwardTokenFunds(_token, _amount);

_updatePurchasingState(_amount, tokens);

investedAmountOf[_token][_candidate] = investedAmountOf[address(0)][_candidate].add(_amount);

emit TokenPurchaseByToken(
msg.sender,
_beneficiary,
_token,
_amount,
tokens
);
}


Solution 1:[1]

Example of how to decode calldata:

const InputDataDecoder = require('ethereum-input-data-decoder')

const abi = [
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "_beneficiary",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "_token",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "_amount",
        "type": "uint256"
      },
      {
        "internalType": "address",
        "name": "_candidate",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "_maxAmount",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "_minAmount",
        "type": "uint256"
      },
      {
        "internalType": "bytes",
        "name": "_signature",
        "type": "bytes"
      }
    ],
    "name": "buyTokenByTokenWithPermission",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  }
]

const data = '0xa1491efc0000000000000000000000002cff6fb5a463735d65c29d933ce7f1c45350cb33000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d56000000000000000000000000000000000000000000000005e74a8505e80a00000000000000000000000000002cff6fb5a463735d65c29d933ce7f1c45350cb33000000000000000000000000000000000000000000000094f138d51f01ba2f70000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000413deec388befec448046d583d65e15cd77aa9c74ba8c534305089f5869ffe92087f29af0ba8d129018f19a520a8b342b0ea90df02aed99a4b28f8db2dc61df0721c00000000000000000000000000000000000000000000000000000000000000'

const decoder = new InputDataDecoder(abi)
const decoded = decoder.decodeData(data)

const signature = '0x' + decoded.inputs[6].toString('hex')

console.log(signature) // 0x3deec388befec448046d583d65e15cd77aa9c74ba8c534305089f5869ffe92087f29af0ba8d129018f19a520a8b342b0ea90df02aed99a4b28f8db2dc61df0721c

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Miguel Mota