<p>No MVP PHP code, but some bits that could help.</p>

<blockquote>
  <ol>
  <li>verify the private key</li>
  </ol>
</blockquote>

<p>Assuming you want a private key to be in the secp256k1 ECDSA range. You could check if the key is any number between <code>0x1</code> and  <code>0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4140</code>. Take a look in the bitcoin wiki entry to <a href="https://en.bitcoin.it/wiki/Private_key#Range_of_valid_ECDSA_private_keys" rel="nofollow noreferrer">private keys about the key range</a>.</p>

<blockquote>
  <ol start="2">
  <li>Get the public address using that private key</li>
  </ol>
</blockquote>

<p>The library <a href="https://github.com/BitcoinPHP/BitcoinECDSA.php" rel="nofollow noreferrer">BitcoinECDSA.php</a> could help you out here. Especially</p>

<pre><code>$bitcoinECDSA-&gt;setPrivateKey($privateKey);
$bitcoinECDSA-&gt;getAddress(); 
</code></pre>

<blockquote>
  <p>3 + 4.Check the balance using an external api and sweep if balance > 0</p>
</blockquote>

<p>This step can be easily combined. If you want to rely on a third party you could use the <a href="https://github.com/BlockIo/block_io-php" rel="nofollow noreferrer">Block.io PHP API and Libary</a>. They have an <a href="https://github.com/BlockIo/block_io-php/blob/master/examples/sweeper.php" rel="nofollow noreferrer">example PHP code</a> for sweeping. </p>

CC BY-SA 4.0
2018-07-09T18:14:02.583Z
FYI, this needs the PHP  **gmp** module.
Patoshi パトシ
CC BY-SA 4.0
2018-07-05T14:59:11.217Z
2018-07-05T14:59:11.217Z
0xb10c
<p>Is there a PHP script where it validates a bitcoin private key and also gets the public address from the key?</p>

<p>My goal was to:</p>

<ol>
<li>verify the private key</li>
<li>Get the public address using that private key</li>
<li>Check the balance using an external api</li>
<li>Sweep if balance > 0</li>
</ol>

<p>I know I can test it by sending some bitcoins to it, or use electrum to attempt the sweep, but let's say I have 100 private keys I need to verify, how would I go about that?</p>

CC BY-SA 4.0
2018-07-05T14:31:47.750Z
What do you define as a **valid** private key in step 1.? When the key is in the secp256k1 ECDSA range? That would be _nearly_ all  256-bit numbers. (https://en.bitcoin.it/wiki/Private_key#Range_of_valid_ECDSA_private_keys)
0xb10c
CC BY-SA 4.0
2018-07-05T14:59:46.757Z
What format do your private keys have?
0xb10c
CC BY-SA 4.0
2018-07-10T14:48:40.523Z
Question title edit: How do I empty the accounts of 100 people who's private keys I've hacked
JBaczuk
<private-key><public-key><php><sweep>
Sweeping a bitcoin private key using PHP
Toxnyc
# Sweeping a bitcoin private key using PHP

(Asked by: username_0 on 2018-07-02)

Is there a PHP script where it validates a bitcoin private key and also gets the public address from the key?

My goal was to:

1. verify the private key
2. Get the public address using that private key
3. Check the balance using an external api
4. Sweep if balance > 0

I know I can test it by sending some bitcoins to it, or use electrum to attempt the sweep, but let's say I have 100 private keys I need to verify, how would I go about that?

**Tags:** private-key, public-key, php, sweep

> **Comments:**
> **username_1**: What do you define as a **valid** private key in step 1.? When the key is in the secp256k1 ECDSA range? That would be _nearly_ all  256-bit numbers. (https://en.bitcoin.it/wiki/Private_key#Range_of_valid_ECDSA_private_keys)  
> **username_1**: What format do your private keys have?  
> **username_2**: Question title edit: How do I empty the accounts of 100 people who's private keys I've hacked  

---

## 1 answers

### Answer by username_1 on 2018-07-05. Score: 2 | Accepted answer

No MVP PHP code, but some bits that could help.


verify the private key


Assuming you want a private key to be in the secp256k1 ECDSA range. You could check if the key is any number between 0x1 and  0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4140. Take a look in the bitcoin wiki entry to private keys about the key range.


Get the public address using that private key


The library BitcoinECDSA.php could help you out here. Especially
$bitcoinECDSA->setPrivateKey($privateKey);
$bitcoinECDSA->getAddress(); 


3 + 4.Check the balance using an external api and sweep if balance > 0

This step can be easily combined. If you want to rely on a third party you could use the Block.io PHP API and Libary. They have an example PHP code for sweeping.

> **Comments:**
> **username_3**: FYI, this needs the PHP  **gmp** module.  

---

