1

How to convert my metamask private key into 32 bytes hexa ?

i am using web3 to start trading on pancake but when i try to convert my bsc metamask phrase keys into hexa is like 150 bytes but require 32 bytes what to do?>

import time
import config as config
from web3 import Web3
 
PancakeABI = open('pancakeABI.py','r').read().replace('\n','')
 
bsc="https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
print(web3.isConnected())
 
#My own address to swap from
sender_address = "0x13AE8Bb065407595681034dDA97361424AF14C00"
 
#This is global Pancake V2 Swap router address
router_address = "0x10ED43C718714eb63d5aA57B78B54704E256024E"
 
#always spend using Wrapped BNB
#I guess you want to use other coins to swap you can do that, but for me I used Wrapped BNB
spend = web3.toChecksumAddress("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c")
 
#This is your private key info
private="how i can set my phrase keys 32 bytes while they are 150 bytes"
 
#Print out your balances just for checking
balance = web3.eth.get_balance(sender_address)
print(balance)
 
humanReadable = web3.fromWei(balance,'ether')
print(humanReadable)
 
#Contract id is the new token we are swaping to
contract_id = web3.toChecksumAddress("0xe9e7cea3dedca5984780bafc599bd69add087d56")
 
#Setup the PancakeSwap contract
contract = web3.eth.contract(address=router_address, abi=PancakeABI)
 
nonce = web3.eth.get_transaction_count(sender_address)
 
start = time.time()
print(web3.toWei('0.02','ether'))
 
pancakeswap2_txn = contract.functions.swapExactETHForTokens(
  1074184953676691292, # here setup the minimum destination token you want to have, you can do some math, or you can put a 0 if you don't want to care
  [spend,contract_id],
  sender_address,
  (int(time.time()) + 1000000)
).buildTransaction({
  'from': sender_address,
  'value': web3.toWei(0.02,'ether'),#This is the Token(BNB) amount you want to Swap from
  'gas': 250000,
  'gasPrice': web3.toWei('5','gwei'),
  'nonce': nonce,
})
 
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key=private)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(web3.toHex(tx_token))```
8
  • why do you need to convert your private key? you only need your private key to sign transactions locally if you don't have a node that you control that you are using the rpc endpoint of. Aug 2 at 16:10
  • i am not using any node , i wrote my keys so its says non hexa decimal found so when i oonvet it its says required 32 bytes , so how i do sign my transaction locally? i want to trade in pancakeswap
    – Usama Khan
    Aug 2 at 16:17
  • @KakiMasterOfTime i have edited my code
    – Usama Khan
    Aug 2 at 16:19
  • your privateKey should not have 0x at the start that's it. just make sure you write it right. Aug 2 at 16:29
  • @KakiMasterOfTime there is no 0x it is started from 666 and its like moe than 100 alphanumric 0 to f
    – Usama Khan
    Aug 2 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.