我正在尝试使用 Python 库 bitcoinlib 签署原始消息,例如“Hello World”。目标是为可验证的消息生成 Base64 编码的签名。然而,bitcoinlib 似乎并没有提供直接签署消息的简单方法。
这是我尝试过的:
使用bitcoinlib.keys.Key:我尝试使用密钥对象进行签名,但它不直接支持对任意消息进行签名。 使用bitcoinlib.transactions.Transaction:此类设计用于签署比特币交易,但它不支持签署原始消息。
问题:
Transaction.inputs_add() 不是有效的方法。 Transaction.sign() 不接受消息作为参数。 看来 bitcoinlib 主要是为处理交易而不是任意消息而设计的。 预期行为:我想对“Hello World”之类的原始消息进行签名,并使用 bitcoinlib 获得有效的 base64 编码签名。有没有更好的方法可以使用 bitcoinlib 来实现此目的,或者我是否需要不同的库?
附加信息:
Python版本:3.11 bitcoinlib 版本:0.6.0
from bitcoinlib.keys import Key
from bitcoinlib.transactions import Transaction
class BitcoinClient:
def __init__(self, private_key_wif: str) -> None:
"""
Initialize the BitcoinClient with a WIF private key.
:param private_key_wif: The private key in Wallet Import Format (WIF).
"""
self.key = Key(import_key=private_key_wif)
self.address = self.key.address()
def get_address(self) -> str:
"""
Get the Bitcoin address associated with the private key.
:return: Bitcoin address as a string.
"""
return self.address
def sign_message(self, message: str) -> str:
"""
Sign a message using the private key.
:param message: The message to be signed.
:return: The signature as a base64-encoded string.
"""
tx = Transaction(network=self.key.network)
tx.inputs_add(self.key.address())
signature = tx.sign(self.key, message=message)
return signature
def verify_message(self, message: str, signature: str) -> bool:
"""
Verify a signed message.
:param message: The original message.
:param signature: The base64-encoded signature to verify.
:return: True if the signature is valid, False otherwise.
"""
tx = Transaction(network=self.key.network)
return tx.verify_message(self.key.address(), message=message, signature=signature)
if __name__ == "__main__":
# Expected address and signature for testing
address_from_priv = ''
# Signed message type which i should see
signed = 'AUCozKGE8DkRwCIKgJdzwIY6nQ4lYkUlFnQqry5sDaKa9rHrpIUoIyD9I+nkRA0tCf1nwlfCeqUmMl6aSDBaLwn6'
# Your WIF private key and message
private_key_wif = ""
message = "Hello world!"
# Initialize the BitcoinClient
try:
signer = BitcoinClient(private_key_wif)
# Sign the message
signature = signer.sign_message(message)
# Get the Bitcoin address
address = signer.get_address()
# Verify the signature
is_valid = signer.verify_message(message, signature)
# Output the results
print(f"Bitcoin Address: {address}")
print(f"Signature: {signature}")
print(f"Is signature valid: {is_valid}")
print(f"Is signature matching expected: {signature == signed}")
print(f"Is address matching expected: {address == address_from_priv}")
except ValueError as e:
print(f"Error: {e}")
bitcoinlib
用于签署任意消息,您将不得不使用不同的库。
from bitcoinlib.keys import Key
from bitcoinlib.transactions import Transaction
from hashlib import sha256
import base64
class BitcoinClient:
def __init__(self, private_key_wif: str) -> None:
"""
Initialize the BitcoinClient with a WIF private key.
:param private_key_wif: The private key in Wallet Import Format (WIF).
"""
self.key = Key(import_key=private_key_wif)
self.address = self.key.address()
def get_address(self) -> str:
"""
Get the Bitcoin address associated with the private key.
:return: Bitcoin address as a string.
"""
return self.address
def sign_message(self, message: str) -> str:
"""
Sign a message using the private key.
:param message: The message to be signed.
:return: The signature as a base64-encoded string.
"""
# Hash the message using SHA-256
message_hash = sha256(message.encode()).digest()
# Sign the hashed message
signature = self.key.sign(message_hash)
# Encode the signature in base64
return base64.b64encode(signature).decode()
def verify_message(self, message: str, signature: str) -> bool:
"""
Verify a signed message.
:param message: The original message.
:param signature: The base64-encoded signature to verify.
:return: True if the signature is valid, False otherwise.
"""
# Hash the message using SHA-256
message_hash = sha256(message.encode()).digest()
# Decode the signature from base64
signature_bytes = base64.b64decode(signature)
# Verify the signature against the hashed message
return self.key.verify(signature_bytes, message_hash)
if __name__ == "__main__":
# Expected address and signature for testing
address_from_priv = ''
# Signed message type which I should see
signed = 'AUCozKGE8DkRwCIKgJdzwIY6nQ4lYkUlFnQqry5sDaKa9rHrpIUoIyD9I+nkRA0tCf1nwlfCeqUmMl6aSDBaLwn6'
# Your WIF private key and message
private_key_wif = ""
message = "Hello world!"
# Initialize the BitcoinClient
try:
signer = BitcoinClient(private_key_wif)
# Sign the message
signature = signer.sign_message(message)
# Get the Bitcoin address
address = signer.get_address()
# Verify the signature
is_valid = signer.verify_message(message, signature)
# Output the results
print(f"Bitcoin Address: {address}")
print(f"Signature: {signature}")
print(f"Is signature valid: {is_valid}")
print(f"Is signature matching expected: {signature == signed}")
print(f"Is address matching expected: {address == address_from_priv}")
except ValueError as e:
print(f"Error: {e}")
另外,你的代码并不愚蠢:D