pu369com

python生成比特币bitcoin私钥公钥地址

BTC突破70500美元,续刷历史新高之际,俺是即没钱,也没资格交易;更不想高位接盘。只好写个代码吧:

from bitcoin import *
 
# 私钥
my_private_key = random_key() 
# 公钥
my_public_key = privtopub(my_private_key)
# 地址
my_bitcoin_address = pubtoaddr(my_public_key)
print(f"Private Key: {my_private_key}")
print(f"Public Key: {my_public_key}")
print(f"Address: {my_bitcoin_address}")

试了几次,上述代码生成的地址都是以 1 开头的。(补充:要得到类似的私钥和地址,也可以直接从 https://www.bitaddress.org/ 上生成,并且这个网页是单文件,可以另存为本地文件,然后离线使用)

 那么要生成以3 开头 或以bc1开头的地址怎么办呢?

文心一言提示用bit库,但给出的代码却不能运行

参考bit库文档写出如下代码,生成了以3开头的地址:

from bit import Key
 
# 私钥
my_private_key = Key()
# 公钥
#You will also never use this directly.
#This value is only used internally to derive your address and
#is needed in the construction of every transaction.
my_public_key = my_private_key.public_key
# 地址
# my_bitcoin_address = my_private_key.address
my_bitcoin_address = my_private_key.segwit_address
print(f"Private Key: {my_private_key}")
print(f"Public Key: {my_public_key}")
print(f"Address: {my_bitcoin_address}")

另外,要生成 多重签名的隔离见证地址,文心一言提示用 bitcoinlib库,然而给出的代码仍不可用

于是在pypi上搜索一下,找到比较新的库  bitcoin-utils    https://pypi.org/project/bitcoin-utils/

再找了一个 https://github.hscsec.cn/    ,从上面搜索   bitcoin-utils  并下载BitcoinUtilities.pdf 但是English挺费劲,

参考Sample,以下代码生成的还是1开头的地址:

# Copyright (C) 2018-2022 The python-bitcoin-utils developers
#
# This file is part of python-bitcoin-utils
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of python-bitcoin-utils, including this file, may be copied,
# modified, propagated, or distributed except according to the terms contained
# in the LICENSE file.


from bitcoinutils.setup import setup
from bitcoinutils.keys import PrivateKey, PublicKey


def main():
    # always remember to setup the network
    setup("mainnet")

    # create a private key (deterministically)
    priv = PrivateKey(secret_exponent=1)

    # compressed is the default
    print("\nPrivate key WIF:", priv.to_wif(compressed=True))

    # could also instantiate from existing WIF key
    # priv = PrivateKey.from_wif('KwDiBf89qGgbjEhKnhxjUh7LrciVRzI3qYjgd9m7Rfu73SvHnOwn')

    # get the public key
    pub = priv.get_public_key()

    # compressed is the default
    print("Public key:", pub.to_hex(compressed=True))

    # get address from public key
    address = pub.get_address()

    # print the address and hash160 - default is compressed address
    print("Address:", address.to_string())
    print("Hash160:", address.to_hash160())

    print("\n--------------------------------------\n")

    # sign a message with the private key and verify it
    message = "The test!"
    signature = priv.sign_message(message)
    assert signature is not None
    print("The message to sign:", message)
    print("The signature is:", signature)

    if PublicKey.verify_message(address.to_string(), signature, message):
        print("The signature is valid!")
    else:
        print("The signature is NOT valid!")


if __name__ == "__main__":
    main()

引用一下别人的内容:===============================

Mainnet, testnet, regtest——它们是啥意思?

Mainnet(主网)是作为官方版本使用的网络,所有真实的交易都发生在这个网络上

Testnet(测试网),一个与主网具有几乎相同规则的网络(一些操作码在mainnet上是禁止的,而在Testnet上取消了这个限制)。

Regtest是一个私有的区块链,它具有与testnet相同的规则和地址格式,但是没有要连接到的全局p2p网络。

由于mainnet币有价值,而testnet/regtest币没有价值,所以它们由不同的前缀来区分。Mainnet地址以“1”、“3”或“bc1”开头,testnet/regtest地址以“m”、“n”、“2”或“tb1”开头。记住,testnet/regtest地址没有值,所以了解它们之间的区别很重要!钱包可以检测出其中的差别,但也有一些情况下,人们也会被人用testnet的币忽悠!

我们将使用regtest,因为我们可以轻松地创建自己的区块链,并且可以使用一个方便的命令在不挖掘硬件的情况下挖掘块。

图形化的比特币核心客户端Bitcoin-qt也能在视觉上区分不同的网络,以帮助您识别其运行的模式。当您第一次安装Bitcoin -qt时,它将运行在使用熟悉的橙色比特币符号的mainnet上。Testnet使用绿色,regtest使用蓝色。

=============更多内容请参考原文 https://www.elecfans.com/blockchain/894596.html  ======================

试着生成取bc1开头的地址,将上面代码中设为 mainnet ,即: setup("mainnet")  ,而不是 Testnet 或  Regtest

然后,将  address = pub.get_address()  这句改为:address = pub.get_segwit_address()

可以生成bc1开头的地址了,但代码中address.to_hash160()这句就不能用了,提示: AttributeError: 'P2wpkhAddress' object has no attribute 'to_hash160'

 同时运行结果也变为:The signature is NOT valid!

 

参考:https://blog.csdn.net/iCloudEnd/article/details/93675389

https://blog.csdn.net/wypeng2010/article/details/89332833

https://pypi.org/project/bit/

https://ofek.dev/bit/

https://www.elecfans.com/blockchain/894596.html

https://zhuanlan.zhihu.com/p/673293772

https://zhuanlan.zhihu.com/p/34085839

https://blog.csdn.net/vohyeah/article/details/80704478

https://www.blockchain.com/explorer/assets/BTC

posted on 2024-03-11 18:43  pu369com  阅读(772)  评论(0编辑  收藏  举报

导航