我正在关注一个在 python 中使用 brownie 来部署合同的教程。如果要将合约部署到测试网(在本例中为 rinkeby),程序应该从环境变量中获取私钥,但是当尝试运行 deploy.py 脚本时:
brownie run scripts/deploy.py --network rinkeby
我收到以下错误:
Running 'scripts/deploy.py::main'...
File "brownie/_cli/run.py", line 50, in main
return_value, frame = run(
File "brownie/project/scripts.py", line 103, in run
return_value = f_locals[method_name](*args, **kwargs)
File "./scripts/deploy.py", line 10, in main
deploy_fund_me()
File "./scripts/deploy.py", line 5, in deploy_fund_me
account = get_account()
File "./scripts/helpful_scripts.py", line 19, in get_account
return accounts.add(config["wallets"]["from_key"])
File "brownie/network/account.py", line 142, in add
w3account = web3.eth.account.from_key(private_key)
File "eth_utils/decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "eth_account/account.py", line 250, in from_key
key = self._parsePrivateKey(private_key)
File "eth_utils/decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "eth_account/account.py", line 776, in _parsePrivateKey
raise ValueError(
ValueError: The private key must be exactly 32 bytes long, instead of 0 bytes.
关于此错误的一些背景信息:我在与 brownie-config.yml 相同的目录中有一个 .env 文件,其中包含以下内容:
export PRIVATE_KEY={my_private_key}
其中 {my_private_key} 是我的十六进制实际私钥。
brownie-config.yml 看起来像这样:
dependencies:
# - <organization/repo>@<version>
- smartcontractkit/chainlink-brownie-contracts@1.1.1
compiler:
solc:
remappings:
- '@chainlink=smartcontractkit/chainlink-brownie-contracts@1.1.1'
dotenv: .env
wallets:
from_key: ${PRIVATE_KEY}
据我了解,brownie 应该使用 .env 文件将密钥导出为环境变量,然后将其放入某个 brownie 配置文件(可能是在编译代码时?),当合约部署时,brownie 会在其中查找它。如果我在执行代码之前手动导出此环境变量,它也不会改变任何内容。
PS:似乎导致错误的功能:
def get_account():
if network.show_active() == "development":
return accounts[0]
else:
return accounts.add(config["wallets"]["from_key"])