'from field must match key's x, but it was y
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!")
Solution 1:[1]
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
Solution 2:[2]
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.
Hope this helps...
Note: for ganache, you must use 1337 as chainid and not 5777 (which is the network id) nor 1377.
Solution 3:[3]
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")
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 | Yilmaz |
Solution 2 | CryptoBlock |
Solution 3 | richardec |