StaticmagicComputes 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.
StaticsignSigns 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.
Optionaloptions: SignOptions
Optional parameters including 'segwitType' ('p2sh(p2wpkh)' or 'p2wpkh') and 'extraEntropy'.
A Buffer containing the 65-byte BIP-137 signature.
StaticverifyVerifies 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.
Drop-in replacement class for bitcoinjs-message.