Static magicComputes the "Magic Hash" of a message as defined in the Bitcoin message signing standard.
The function prefixes the message with specific bytes ("\x18Bitcoin Signed Message:\n") and the variable-length encoded message length, then performs a double SHA-256 hash (hash256) on the result. This specific hashing mechanism prevents the signature from being used as a valid transaction signature on the Bitcoin network.
The input message to be hashed (string or Buffer).
Buffer A 32-byte Buffer containing the double SHA-256 hash of the prefixed message.
Static signSigns a message with full BIP-137 support, compatible with Legacy (P2PKH), Nested Segwit (P2SH-P2WPKH), and Native Segwit (P2WPKH) addresses.
This method produces a compact signature using the secp256k1 elliptic curve. It implements deterministic signatures (RFC 6979) by default but supports additional entropy via options. The resulting signature includes a specific header byte that encodes the recovery ID and the key/address type, allowing for public key recovery during verification.
The process involves:
The message string or buffer to be signed.
The 32-byte private key buffer used for signing.
Boolean indicating if the corresponding public key is compressed.
Optional options: SignOptionsOptional parameters including 'segwitType' ('p2sh(p2wpkh)' or 'p2wpkh') and 'extraEntropy'.
A Buffer containing the 65-byte BIP-137 signature.
Static Private toConverts a Bitcoin address string into its corresponding output script buffer, attempting to detect the network automatically.
Since bitcoinjs-lib enforces strict network validation (defaulting to Mainnet), this
helper iterates through Mainnet, Testnet, and Regtest networks to successfully
parse the address. This allows the verifier to handle addresses from different
networks transparently without requiring explicit network configuration from the caller.
The Bitcoin address string to convert.
Buffer | null The output script Buffer if the address is valid on any supported network, or null if invalid.
Static verifyVerifies a signed Bitcoin message against a provided address.
This method validates signatures adhering to the BIP-137 standard. It supports automatic detection of the address type (Legacy, Nested Segwit, or Native Segwit) and the network (Mainnet, Testnet, Regtest) by analyzing the signature header and the provided address format.
The verification steps are:
The message that was signed (string or Buffer).
The Bitcoin address (Legacy, Segwit, or Bech32) that supposedly signed the message.
The Base64 encoded signature string.
boolean Returns true if the signature is valid for the given message and address, false otherwise.
Error if unexpected error is encountered (e.g., unexpected object being passed as an message)
Drop-in replacement class for bitcoinjs-message.