【web3py】批量创建eth账号

____tz_zs

创建可在eth链及其侧链上使用的账号。内容包括:远程节点、本地节点、本地密钥对等。
前言:建议使用方式三的方法,在本地生成密钥对。

方式一、远程节点

使用远程节点(如 Infura)创建帐户时,会报如下错误: web3py the method personal_newAccount does not exist/is not available。出于安全原因,远端节点不允许创建新帐户 (personal_newAccount api)。

方式二、本地节点

本地节点(例如,Geth)是由自己启动和控制的。所以它和自己直接保管密钥一样安全。
我们可以在本地运行一个节点(例如,Geth),通过本地端口连接到它,从而可以使用 personal API 来创建账号。ps:注意确保节点已正确配置为接受 personal 方法,对于 Geth, --http.api personal,eth,<etc> 或者 --ws.api personal,eth,<etc>

web3.geth.personal.new_account("")

方式三、生成本地密钥

从技术上讲,帐户的创建与以太坊节点无关,而是由密码学机制产生的。我们不需要任何连接到互联网的东西来生成帐户,只需要知道生成私钥和地址的方法,而生成的本地私钥(32 字节的数据)可以直接在任何eth链或者eth侧链上进行使用。

eth-account 模块

eth-account 模块 的方式不需要连接到以太坊节点,可创建密钥并自己管理。

from eth_account import Account
acct = Account.create(extra_entropy="tz_zs 字符串~")
print(acct.address)
print(acct.key.hex())
# print(acct.privateKey.hex())
"""
0x721ECF90cc921889B330A7819000697d7b344847
0x381aae3c954508a3a88efb82848bcfd1890c02ecd1c4e87a2f8d300e76e70289
"""

web3.eth.accounts.create

web3.eth.accounts.createeth_account 模块是调用一样的底层代码来创建帐户,不会将密钥存储在(本地)节点上(不会存储在密钥库,所以也不会在web3.eth.get_accounts()中),而是由我们自己持有和管理。

from web3 import Web3

w3 = Web3()
acct = w3.eth.account.create("tz_zs 字符串~")
print(acct.address)
print(acct.key.hex())
# print(acct.privateKey.hex())
"""
0x9D0B93CCf7169EaD6b6A38848F98F6BAb61eC232
0xa4bdb18291fcccd6f2e06ca3b72be9c995d64ca9c2791b39ed98a34be8abafdb
"""

补充说明

1、在节点托管密钥,密钥存储在web3.eth.accounts 中,并可通过 send_transaction()签名。
2、本地密钥,即自己保管密钥,使用时,在将交易和消息发送到节点之前使用 send_raw_transaction() 进行签名。

参考

New Ethereum Account with Infura
Generating Ethereum Accounts on web3.py
Why can’t I create an account?
personal_newAccount does not exist/is not available #2723
Some Common Uses for Local Private Keys
web3.eth.accounts.create method doesn’t actually create new account
eth_account
creating account in my privateTestNet instead of a Local account in web3.py
Should I use web3.eth.accounts or web3.eth.personal for account creation ?
Working with Local Private Keys

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>