from field must match key's x, but it was y

Viewed 385

I write the code below. It seems it has a problem with the signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key part. I face to the error below every time I deploy:

"Traceback (most recent call last): File "E:\Blockchain-Developing\deploy.py", line 81, in signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key) File "C:\Users\Rezli\AppData\Local\Programs\Python\Python310\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper return self.method(obj, *args, **kwargs) File "C:\Users\Rezli\AppData\Local\Programs\Python\Python310\lib\site-packages\eth_account\account.py", line 734, in sign_transaction raise TypeError("from field must match key's %s, but it was %s" % ( TypeError: from field must match key's 0x0A651A5976Ee3cfB68719Dcf6f1E65f7a691f803, but it was 0x95ac4081466196F6Bc79B56D7EE1a002D0407820"

The address that I use in ganache was 0x95ac4081466196F6Bc79B56D7EE1a002D0407820.

from solcx import compile_standard, install_solc
import json
from web3 import Web3
import os
from dotenv import load_dotenv

load_dotenv()

with open("./SimpleStorage.sol", "r") as file:
    simple_storage_file = file.read()

print("Installing...")
install_solc("0.6.0")

# compile our solidity
install_solc("0.6.0")

compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {
                    "*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
                }
            }
        },
    },
    solc_version="0.6.0",
)

with open("compiled_code.json", "w") as file:
    json.dump(compiled_sol, file)


# get bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
    "bytecode"
]["object"]

# get abi
abi = json.loads(
    compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["metadata"]
)["output"]["abi"]


# for connecting to ganache
w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
chain_id = 5777
my_address = "0x95ac4081466196F6Bc79B56D7EE1a002D0407820"
private_key = os.getenv("PRIVATE_KEY")
print(private_key)

# Create the contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)


# get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)


# 1. build a transaction
transaction = SimpleStorage.constructor().buildTransaction(
    {
        "chainID": chain_id,
        "gasPrice": w3.eth.gas_price,
        "from": my_address,
        "nonce": nonce,
    }
)

# print(transaction)
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
print(signed_txn)
print("Deploying Contract!")
4 Answers

I believe your private key is not matching the address. this is your ganache address

 my_address = "0x95ac4081466196F6Bc79B56D7EE1a002D0407820"

so private key should be its private key.

private_key = os.getenv("PRIVATE_KEY")

Also chainId for ganace is not "5777", It is 1377

in your terminal (mac)

unset PRIVATE_KEY

this works if you have conflicting environments variable names with different values.

In my case I had set a PRIVATE_KEY environment variable using the terminal and then set a second PRIVATE_KEY(same name different value) environment variable in my .env file in my working directory.

so when running the program using python3 the PRIVATE_KEY set in the terminal was being used and that value was for another address. Hence the address and private key did not match as you can see in the bottom of the error message:

File "/usr/local/lib/python3.9/site-packages/eth_account/account.py", line 735, in sign_transaction
    raise TypeError("from field must match key's %s, but it was %s" % (
TypeError: from field must match key's 0x623E2E33250d72C1588E50933bA93Ab23B0936e2, but it was 0xFcc971aa122b3BCbA8379bf53015C725F4F93BE5

In case you still have this issue, you seem to have a conflict between private keys. You have probably defined an environment variable PRIVATE_KEY outside your ".env" file. The issue here is that you have two private keys, one that you have defined before you set up your ".env" file and one in the ".env" file. So think about deleting the previously defined one.

Note: for ganache, you must use 1337 as chainid and not 5777 (which is the network id) nor 1377.

Run a bash command source .env and check.

If that doesn't work then just change your variable name of PRIVATE_KEY to PRIVATE_KEY1 or anything else in the .env file.

Also, update it in your deploy.py file as given below

private_key = os.getenv("PRIVATE_KEY1")
Related