MessageSigner

Overview

The MessageSigner is a singleton instance exported from @btc-vision/transaction that provides a unified API for cryptographic message signing and verification. It supports Schnorr signatures, Taproot-tweaked Schnorr signatures, and quantum-resistant ML-DSA (FIPS 204) signatures.

Its most important feature is a set of Auto methods that transparently detect whether the code is running in a browser (with the OP_WALLET extension) or on a backend server, and delegate signing accordingly.

The full source code is available on GitHub at MessageSigner.ts.

Import

import { MessageSigner, EcKeyPair } from '@btc-vision/transaction';
import { networks, toHex } from '@btc-vision/bitcoin';

Interfaces

SignedMessage
MLDSASignedMessage

Instance Methods

isOPWalletAvailable()

Auto Methods (Recommended)

The Auto methods are the primary recommended API for signing messages. They unify browser and backend signing behind a single call, making your code portable across environments. When keypair is undefined or omitted, the method attempts browser signing via OP_WALLET. When keypair is provided, it signs locally using the keypair directly.

signMessageAuto()
tweakAndSignMessageAuto()
signMLDSAMessageAuto()

Non-Auto Methods (Backend Only)

These methods sign directly with a provided keypair. They do not check for OP_WALLET and will throw errors if a valid keypair is not provided. Use these only when you are certain you are running in a backend environment with access to private keys.

signMessage()
tweakAndSignMessage()
signMLDSAMessage()

Browser Wallet Methods

These methods interact directly with the OP_WALLET browser extension. They return null when OP_WALLET is not available (instead of throwing), making them safe to call speculatively. The Auto methods use these internally.

trySignSchnorrWithOPWallet()
trySignMLDSAWithOPWallet()
verifyMLDSAWithOPWallet()
getMLDSAPublicKeyFromOPWallet()

Verification Methods

All verification methods are synchronous and work in both browser and backend environments. They do not require private keys.

verifySignature()
tweakAndVerifySignature()
verifyMLDSASignature()

Utility Methods

sha256()