如何从十进制的Hexa中删除错误?

发布于 2025-02-12 20:37:33 字数 4414 浏览 0 评论 0原文

我正在YouTube上的FreecodeCamp课程,并遇到了关于我跑步的错误。当我试图将交易纳入Ganache时。我在Ganache日志中看到有活动,但在我的交易中找不到。再次,我得到了Binascii错误

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(simple_storage_file)

# compile our solidity

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",
)
install_solc("0.6.0")
# print(compiled_sol)
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 = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]

# for connecting to ganache
#  for the chain_id, make sure you change the settings from the ganache settings by going to the server and change the chain_id to 1337
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))
chain_id = 1337
my_address = "0x43daFF8768E4e2e0b90DdEFd08016D309692b4aA"
private_key = "0x21bxg2MenSJAWYKP9EpgTNK8KaVunCGTre6a96053b7c073f5a05ac70a2531c8"
# print(private_key)
# os.getenv("PRIVATE_KEY")


# Create the contract in python  os.getenv("PRIVATE_KEY")
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# Get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# print(nonce)

# 1. Build a transaction
# 2. Sign a transaction
# 3. Send a transaction

# # note if you are following the tutorial from freecodecamp, add  ("gasPrice": w3.eth.gas_price), to ur code.
transaction = SimpleStorage.constructor().buildTransaction(
     {
         "gasPrice": w3.eth.gas_price,
         "chainId": chain_id,
         "from": my_address,
         "nonce": nonce,
     }
 )
# this print function is to check if the transaction is working before it will be printed out.
# print(transaction)

signed_txn = w3.eth.account.sign_transaction(transaction, private_key = private_key)
# print(signed_txn)
# # set environment variable by using export PRIVATE_KEY= 0x21bvgwyWBnUUNPQ166PQSFuBYG8ETmbsy6a96053b7c073f5a05ac70a2531c8
# # note "export" will give error because it is being used in windows. And export only works for linux. So for windows,make use of "set"

# # send the signed transaction
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

追溯(最新电话):

  File "C:\Users\udoha\OneDrive\Desktop\solidBlockchain\deploy.py", line 74, in <module>
    signed_txn = w3.eth.account.sign_transaction(transaction, private_key = private_key)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_utils\decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_account\account.py", line 728, in sign_transaction
    account = self.from_key(private_key)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_utils\decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_account\account.py", line 250, in from_key
    key = self._parsePrivateKey(private_key)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_utils\decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_account\account.py", line 775, in _parsePrivateKey
    return self._keys.PrivateKey(HexBytes(key))
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\hexbytes\main.py", line 23, in __new__
    bytesval = to_bytes(val)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\hexbytes\_utils.py", line 17, in to_bytes
    return hexstr_to_bytes(val)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\hexbytes\_utils.py", line 50, in hexstr_to_bytes
    return binascii.unhexlify(ascii_hex)
binascii.Error: Non-hexadecimal digit found

I am going through the freecodecamp course on Youtube and have caught an error regarding when I run. As I am trying to build a transaction into Ganache. I see in Ganache logs that there is activity but cannot find it in my transaction. Again I am getting the binascii error

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(simple_storage_file)

# compile our solidity

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",
)
install_solc("0.6.0")
# print(compiled_sol)
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 = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]

# for connecting to ganache
#  for the chain_id, make sure you change the settings from the ganache settings by going to the server and change the chain_id to 1337
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))
chain_id = 1337
my_address = "0x43daFF8768E4e2e0b90DdEFd08016D309692b4aA"
private_key = "0x21bxg2MenSJAWYKP9EpgTNK8KaVunCGTre6a96053b7c073f5a05ac70a2531c8"
# print(private_key)
# os.getenv("PRIVATE_KEY")


# Create the contract in python  os.getenv("PRIVATE_KEY")
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# Get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# print(nonce)

# 1. Build a transaction
# 2. Sign a transaction
# 3. Send a transaction

# # note if you are following the tutorial from freecodecamp, add  ("gasPrice": w3.eth.gas_price), to ur code.
transaction = SimpleStorage.constructor().buildTransaction(
     {
         "gasPrice": w3.eth.gas_price,
         "chainId": chain_id,
         "from": my_address,
         "nonce": nonce,
     }
 )
# this print function is to check if the transaction is working before it will be printed out.
# print(transaction)

signed_txn = w3.eth.account.sign_transaction(transaction, private_key = private_key)
# print(signed_txn)
# # set environment variable by using export PRIVATE_KEY= 0x21bvgwyWBnUUNPQ166PQSFuBYG8ETmbsy6a96053b7c073f5a05ac70a2531c8
# # note "export" will give error because it is being used in windows. And export only works for linux. So for windows,make use of "set"

# # send the signed transaction
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

Traceback (most recent call last):

  File "C:\Users\udoha\OneDrive\Desktop\solidBlockchain\deploy.py", line 74, in <module>
    signed_txn = w3.eth.account.sign_transaction(transaction, private_key = private_key)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_utils\decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_account\account.py", line 728, in sign_transaction
    account = self.from_key(private_key)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_utils\decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_account\account.py", line 250, in from_key
    key = self._parsePrivateKey(private_key)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_utils\decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\eth_account\account.py", line 775, in _parsePrivateKey
    return self._keys.PrivateKey(HexBytes(key))
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\hexbytes\main.py", line 23, in __new__
    bytesval = to_bytes(val)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\hexbytes\_utils.py", line 17, in to_bytes
    return hexstr_to_bytes(val)
  File "C:\Users\udoha\AppData\Roaming\Python\Python310\site-packages\hexbytes\_utils.py", line 50, in hexstr_to_bytes
    return binascii.unhexlify(ascii_hex)
binascii.Error: Non-hexadecimal digit found

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2025-02-19 20:37:33

BINASCII错误指出,通过的值具有非Xexecimal元素。
十六进制数据的有效值是数字(0到9)和字母(a至f)。

从tracelog中,您可以看到这是由有一些无效数据的private_key参数引起的: 0x21b xg 2 m e nsj a wykp 9e pgtnk 8 k a vun c gtr e6a96053b7c073f5A05AC70A2531C8

The binascii error states that the value passed on have non-hexadecimal elements.
valid values for hex data are numbers (0 to 9) and letters (A to F).

From the tracelog you see that this was caused by the private_key parameter which have some invalid data: 0x21bxg2MenSJAWYKP9EpgTNK8KaVunCGTre6a96053b7c073f5a05ac70a2531c8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文