j-bitcoin
Version:
Comprehensive JavaScript cryptocurrency wallet library for Bitcoin (BTC), Bitcoin Cash (BCH), and Bitcoin SV (BSV) with custodial and non-custodial wallet support, threshold signatures, and multiple address formats
958 lines (320 loc) • 16.5 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Namespace: ECDSA</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Namespace: ECDSA</h1>
<section>
<header>
<h2>ECDSA</h2>
</header>
<article>
<div class="container-overview">
<div class="description"><p>ECDSA cryptographic operations for Bitcoin</p>
<p>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.</p></div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_ECDSA_ecdsa.js.html">src/ECDSA/ecdsa.js</a>, <a href="src_ECDSA_ecdsa.js.html#line23">line 23</a>
</li></ul></dd>
</dl>
<h3>Example</h3>
<pre class="prettyprint"><code>// 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);</code></pre>
</div>
<h3 class="subsection-title">Methods</h3>
<h4 class="name" id=".retrieve_public_key"><span class="type-signature">(static) </span>retrieve_public_key<span class="signature">(msg<span class="signature-attributes">opt</span>, sig, recovery<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Uint8Array}</span></h4>
<div class="description">
<p>Recovers the public key from a signature and message using the recovery ID</p>
<p>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.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>msg</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
"Hello world"
</td>
<td class="description last"><p>Original message that was signed</p></td>
</tr>
<tr>
<td class="name"><code>sig</code></td>
<td class="type">
<span class="param-type">Uint8Array</span>
|
<span class="param-type">Buffer</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>DER-encoded signature bytes</p></td>
</tr>
<tr>
<td class="name"><code>recovery</code></td>
<td class="type">
<span class="param-type">number</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
0
</td>
<td class="description last"><p>Recovery ID (0-3) obtained during signing</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_ECDSA_ecdsa.js.html">src/ECDSA/ecdsa.js</a>, <a href="src_ECDSA_ecdsa.js.html#line118">line 118</a>
</li></ul></dd>
</dl>
<h5>Throws:</h5>
<dl>
<dt>
<div class="param-desc">
<p>If recovery fails or parameters are invalid</p>
</div>
</dt>
<dd></dd>
<dt>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Error</span>
</dd>
</dl>
</dt>
<dd></dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p>Compressed public key (33 bytes)</p>
</div>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Uint8Array</span>
</dd>
</dl>
<h5>Example</h5>
<pre class="prettyprint"><code>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</code></pre>
<h4 class="name" id=".sign"><span class="type-signature">(static) </span>sign<span class="signature">(private_key<span class="signature-attributes">opt</span>, msg<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {<a href="global.html#ECDSASignatureResult">ECDSASignatureResult</a>}</span></h4>
<div class="description">
<p>Signs a message using ECDSA with deterministic k-value generation (RFC 6979)</p>
<p>The signing process:</p>
<ol>
<li>Decodes the WIF-encoded private key</li>
<li>Converts the message to a buffer</li>
<li>Generates a deterministic signature using RFC 6979</li>
<li>Returns both the signature and recovery ID for public key recovery</li>
</ol>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>private_key</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
"L1vHfV6GUbMJSvFaqjnButzwq5x4ThdFaotpUgsfScwMNKjdGVuS"
</td>
<td class="description last"><p>WIF-encoded private key</p></td>
</tr>
<tr>
<td class="name"><code>msg</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
"Hello world"
</td>
<td class="description last"><p>Message to sign (will be UTF-8 encoded)</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_ECDSA_ecdsa.js.html">src/ECDSA/ecdsa.js</a>, <a href="src_ECDSA_ecdsa.js.html#line65">line 65</a>
</li></ul></dd>
</dl>
<h5>Throws:</h5>
<dl>
<dt>
<div class="param-desc">
<p>If private key is invalid or signing fails</p>
</div>
</dt>
<dd></dd>
<dt>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Error</span>
</dd>
</dl>
</dt>
<dd></dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p>Array containing signature and recovery ID</p>
</div>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type"><a href="global.html#ECDSASignatureResult">ECDSASignatureResult</a></span>
</dd>
</dl>
<h5>Example</h5>
<pre class="prettyprint"><code>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</code></pre>
<h4 class="name" id=".verify"><span class="type-signature">(static) </span>verify<span class="signature">(sig, msg<span class="signature-attributes">opt</span>, public_key)</span><span class="type-signature"> → {boolean}</span></h4>
<div class="description">
<p>Verifies an ECDSA signature against a message using a public key</p>
<p>Performs cryptographic verification to ensure that the signature was created
by the holder of the private key corresponding to the given public key.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>sig</code></td>
<td class="type">
<span class="param-type">Uint8Array</span>
|
<span class="param-type">Buffer</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>DER-encoded signature bytes</p></td>
</tr>
<tr>
<td class="name"><code>msg</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
"Hello World"
</td>
<td class="description last"><p>Original message that was signed</p></td>
</tr>
<tr>
<td class="name"><code>public_key</code></td>
<td class="type">
<span class="param-type">Uint8Array</span>
|
<span class="param-type">Buffer</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>Compressed or uncompressed public key</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_ECDSA_ecdsa.js.html">src/ECDSA/ecdsa.js</a>, <a href="src_ECDSA_ecdsa.js.html#line92">line 92</a>
</li></ul></dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p>True if signature is valid, false otherwise</p>
</div>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">boolean</span>
</dd>
</dl>
<h5>Example</h5>
<pre class="prettyprint"><code>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</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="AddressFormats.html">AddressFormats</a></li><li><a href="BECH32.html">BECH32</a></li><li><a href="BIP32.html">BIP32</a></li><li><a href="BIP39.html">BIP39</a></li><li><a href="CASH_ADDR.html">CASH_ADDR</a></li><li><a href="ECDSA.html">ECDSA</a></li><li><a href="KeyDecoding.html">KeyDecoding</a></li><li><a href="Signatures.html">Signatures</a></li><li><a href="ThresholdCrypto.html">ThresholdCrypto</a></li><li><a href="Utilities.html">Utilities</a></li><li><a href="Wallets.html">Wallets</a></li><li><a href="schnorr_sig.html">schnorr_sig</a></li></ul><h3>Classes</h3><ul><li><a href="Custodial_Wallet.html">Custodial_Wallet</a></li><li><a href="Non_Custodial_Wallet.html">Non_Custodial_Wallet</a></li><li><a href="Polynomial.html">Polynomial</a></li><li><a href="ThresholdSignature.html">ThresholdSignature</a></li></ul><h3>Global</h3><ul><li><a href="global.html#CHARSET">CHARSET</a></li><li><a href="global.html#FEATURES">FEATURES</a></li><li><a href="global.html#NETWORKS">NETWORKS</a></li><li><a href="global.html#address">address</a></li><li><a href="global.html#b58encode">b58encode</a></li><li><a href="global.html#base32_encode">base32_encode</a></li><li><a href="global.html#derive">derive</a></li><li><a href="global.html#fromSeed">fromSeed</a></li><li><a href="global.html#hdKey">hdKey</a></li><li><a href="global.html#legacyAddress_decode">legacyAddress_decode</a></li><li><a href="global.html#privateKey_decode">privateKey_decode</a></li><li><a href="global.html#rmd160">rmd160</a></li><li><a href="global.html#standardKey">standardKey</a></li><li><a href="global.html#table">table</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Wed Jun 04 2025 02:28:50 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>