unisat.signMessage(msg[, type])
Sign a message with the current account’s private key.
Parameters
msg - string: a string to signtype - string: (Optional) “ecdsa” |
“bip322-simple”. default is “ecdsa” |
Returns
Promise - string: the signature (Base64 encoded).The default signature type uses RFC6979 Deterministic ECDSA with Bitcoin’s standard message signing format.
How it works:
"Bitcoin Signed Message:\n" and double SHA256 hashed:
hash = SHA256(SHA256(varint(prefix.length) + prefix + varint(msg.length) + msg))
k, the signature uses HMAC-DRBG to deterministically generate k from the private key and message hash. This ensures:
27 + recovery_id + 4 (for compressed public key)r values value (low-S, BIP-62/BIP-146 compliant)Supported Address Types: P2PKH, P2SH-P2WPKH, P2WPKH, P2TR
BIP-322 is a generic message signing standard that works with all Bitcoin address types, including Taproot (P2TR).
How it works:
Output Format: Base64 encoded witness stack
Recommended for: Taproot addresses (P2TR) or when cross-wallet compatibility is required.
Example
// sign by ecdsa
try {
let res = await window.unisat.signMessage("abcdefghijk123456789");
console.log(res)
} catch (e) {
console.log(e);
}
> G+LrYa7T5dUMDgQduAErw+i6ebK4GqTXYVWIDM+snYk7Yc6LdPitmaqM6j+iJOeID1CsMXOJFpVopvPiHBdulkE=
// verify by ecdsa
import { verifyMessage } from "@unisat/wallet-utils";
const pubkey = "026887958bcc4cb6f8c04ea49260f0d10e312c41baf485252953b14724db552aac";
const message = "abcdefghijk123456789";
const signature = "G+LrYa7T5dUMDgQduAErw+i6ebK4GqTXYVWIDM+snYk7Yc6LdPitmaqM6j+iJOeID1CsMXOJFpVopvPiHBdulkE=";
const result = verifyMessage(pubkey,message,signature);
console.log(result);
> true
// sign by bip322-simple
try {
let res = await window.unisat.signMessage("abcdefghijk123456789","bip322-simple");
console.log(res)
} catch (e) {
console.log(e);
}
> AkcwRAIgeHUcjr0jODaR7GMM8cenWnIj0MYdGmmrpGyMoryNSkgCICzVXWrLIKKp5cFtaCTErY7FGNXTFe6kuEofl4G+Vi5wASECaIeVi8xMtvjATqSSYPDRDjEsQbr0hSUpU7FHJNtVKqw=