python
复制代码
# 单次运行
import binascii
import sha3
from ecdsa import SigningKey, SECP256k1
priv = SigningKey.generate(curve=SECP256k1) # 生成私钥
pub = priv.get_verifying_key() # 生成公钥
keccak = sha3.keccak_256()
keccak.update(pub.to_string()) # keccak_256哈希运算
address = "0x" + keccak.hexdigest()[24:]
priv_key = binascii.hexlify(priv.to_string())
pub_key = binascii.hexlify(pub.to_string())
print("Private key: " + priv_key.decode())
print("Public key: " + pub_key.decode())
print("Address: " + address)
# 生成的以太坊生成账户地址是 私钥 -> 公钥 ->账户地址,
# 以太坊账户地址使用Secp256k1椭圆曲线得到私钥、公钥,得到公钥后,
# 对公钥做Keccak-256哈希运算,然后取最后的40位16进制字符,
# 得到的就是以太坊账户地址。
# 使用循环进行生成然后写入文本保存
import binascii
import sha3
from ecdsa import SigningKey, SECP256k1
for i in range(1,2):
priv = SigningKey.generate(curve=SECP256k1) # 生成私钥
pub = priv.get_verifying_key() # 生成公钥
keccak = sha3.keccak_256()
keccak.update(pub.to_string()) # keccak_256哈希运算
address = "0x" + keccak.hexdigest()[24:]
priv_key = binascii.hexlify(priv.to_string())
pub_key = binascii.hexlify(pub.to_string())
f = open('address.txt', 'a+', encoding='utf-8')
Private_key = ('Private key: ' + priv_key.decode() + '\n')
Pub_key = ("Public key: " + pub_key.decode() + '\n')
Produce_Address = ("Address: " + address + '\n'+'\n'+'\n')
f.write(Private_key + Pub_key + Produce_Address)
f.close()