0

I am trying to sign a raw message, like "Hello World", using the Python library bitcoinlib. The goal is to generate a base64-encoded signature for the message that can be verified. However, bitcoinlib does not seem to offer a straightforward way to sign messages directly.

Here’s what I tried:

Using bitcoinlib.keys.Key: I attempted to use the key object for signing, but it doesn't directly support signing arbitrary messages. Using bitcoinlib.transactions.Transaction: This class is designed for signing Bitcoin transactions, but it doesn't support signing raw messages.

Issues:

Transaction.inputs_add() is not a valid method. Transaction.sign() does not accept message as an argument. It seems bitcoinlib is primarily designed for handling transactions, not arbitrary messages. Expected Behavior: I want to sign raw messages like "Hello World" and get a valid base64-encoded signature using bitcoinlib. Is there a better approach to achieve this with bitcoinlib, or do I need a different library?

Additional Info:

Python version: 3.11 bitcoinlib version: 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}")
1
  • 1
    Why do you want to use a bitcoin library for signing arbitrary messages? Commented Dec 12, 2024 at 18:18

2 Answers 2

0

I found that I should use 'bip322-simple' signing algorithm, if I find exact solution

Sign up to request clarification or add additional context in comments.

Comments

0

To sign a message as "Hello World" and get a valid signature, bitcoinlib has a method for you. Sign a message with: Key.signature_message()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.