Sweeping a bitcoin private key using PHP

3

1

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?

Toxnyc

Posted 2018-07-02T16:37:10.857

Reputation: 293

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 2018-07-05T14:31:47.750

What format do your private keys have?0xb10c 2018-07-05T14:59:46.757

1Question title edit: How do I empty the accounts of 100 people who's private keys I've hackedJBaczuk 2018-07-10T14:48:40.523

Answers

2

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

  1. 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.

  1. 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.

0xb10c

Posted 2018-07-02T16:37:10.857

Reputation: 953

2FYI, this needs the PHP gmp module.Patoshi パトシ 2018-07-09T18:14:02.583