ECDSA cryptographic operations for Bitcoin
Provides comprehensive ECDSA functionality including deterministic signature generation (RFC 6979), signature verification, and public key recovery. All operations use the secp256k1 elliptic curve as required by Bitcoin.
- Source:
Example
// Sign a message
const privateKey = "L1vHfV6GUbMJSvFaqjnButzwq5x4ThdFaotpUgsfScwMNKjdGVuS";
const [signature, recoveryId] = ECDSA.sign(privateKey, "Hello Bitcoin!");
// Recover public key from signature
const publicKey = ECDSA.retrieve_public_key("Hello Bitcoin!", signature, recoveryId);
// Verify signature
const isValid = ECDSA.verify(signature, "Hello Bitcoin!", publicKey);
Methods
(static) retrieve_public_key(msgopt, sig, recoveryopt) → {Uint8Array}
Recovers the public key from a signature and message using the recovery ID
This function enables public key recovery without prior knowledge of the public key, which is useful for applications like Ethereum-style address recovery and signature verification workflows.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
msg |
string |
<optional> |
"Hello world" | Original message that was signed |
sig |
Uint8Array | Buffer | DER-encoded signature bytes |
||
recovery |
number |
<optional> |
0 | Recovery ID (0-3) obtained during signing |
- Source:
Throws:
-
If recovery fails or parameters are invalid
- Type
- Error
Returns:
Compressed public key (33 bytes)
- Type
- Uint8Array
Example
const message = "Hello Bitcoin!";
const [signature, recoveryId] = ECDSA.sign(privateKey, message);
const recoveredPubKey = ECDSA.retrieve_public_key(message, signature, recoveryId);
// The recovered public key should match the original
const originalPubKey = getPublicKey(privateKey_decode(privateKey), true);
console.log(Buffer.from(recoveredPubKey).equals(Buffer.from(originalPubKey))); // true
(static) sign(private_keyopt, msgopt) → {ECDSASignatureResult}
Signs a message using ECDSA with deterministic k-value generation (RFC 6979)
The signing process:
- Decodes the WIF-encoded private key
- Converts the message to a buffer
- Generates a deterministic signature using RFC 6979
- Returns both the signature and recovery ID for public key recovery
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
private_key |
string |
<optional> |
"L1vHfV6GUbMJSvFaqjnButzwq5x4ThdFaotpUgsfScwMNKjdGVuS" | WIF-encoded private key |
msg |
string |
<optional> |
"Hello world" | Message to sign (will be UTF-8 encoded) |
- Source:
Throws:
-
If private key is invalid or signing fails
- Type
- Error
Returns:
Array containing signature and recovery ID
- Type
- ECDSASignatureResult
Example
const privateKey = "L1vHfV6GUbMJSvFaqjnButzwq5x4ThdFaotpUgsfScwMNKjdGVuS";
const message = "Hello Bitcoin!";
const [signature, recoveryId] = ECDSA.sign(privateKey, message);
console.log(signature); // Uint8Array with DER-encoded signature
console.log(recoveryId); // Number 0-3 for public key recovery
(static) verify(sig, msgopt, public_key) → {boolean}
Verifies an ECDSA signature against a message using a public key
Performs cryptographic verification to ensure that the signature was created by the holder of the private key corresponding to the given public key.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
sig |
Uint8Array | Buffer | DER-encoded signature bytes |
||
msg |
string |
<optional> |
"Hello World" | Original message that was signed |
public_key |
Uint8Array | Buffer | Compressed or uncompressed public key |
- Source:
Returns:
True if signature is valid, false otherwise
- Type
- boolean
Example
const [signature, _] = ECDSA.sign(privateKey, "Hello Bitcoin!");
const publicKey = ECDSA.retrieve_public_key("Hello Bitcoin!", signature, recoveryId);
const isValid = ECDSA.verify(signature, "Hello Bitcoin!", publicKey);
console.log(isValid); // true
// Invalid signature
const isInvalid = ECDSA.verify(signature, "Different message", publicKey);
console.log(isInvalid); // false