2

I'm building an app and one of my features is being able to scan a bitcoin private key and then send a portion of its funds to another address. How can I do something like this in javascript or php? Do I need to run my own Bitcoin server?

Is there an example script I can use where I can just plug in a private key?

1 Answer 1

2

You can either use your own bitcoind server or you can use a 3rd party API service.

Let's do a few examples of what you are asking.
I'll use short fake addresses and private keys as examples to make things easy.

PHP with a bitcoind server

<?php
require("easybitcoin.php");
$bitcoin = new Bitcoin("username", "somepassword");

//add the private key (label, key)
$import = bitcoin->importprivkey("5KL3cN..SOME_PRIVATE_KEY", "Satoshi1");
?>    

What we did here was import a private key and give it a specific unique label. This part might take a while as your node will need to rescan as you added a new private key and it needs to check for transactions.

Once the node is rescanned you can send the funds.

<?
require("easybitcoin.php");
$bitcoin = new Bitcoin("username", "somepassword");

//send the funds from the Satoshi1 account/label
$send = $bitcoin->$bitcoin->sendfrom("Satoshi1","1receivingAddress", 0.5);
echo $send ? $send : "Oops an error: ".$bitcoin->error;
?>    

What we did above was use the sendfrom() JSON RPC command to send 0.5BTC from the account/label Satoshi1 to the 1receivingAddress. If successful it will respond with the transaction ID, or if an error it will print out the error.

This is just the code, you'll still need to set your VPS with bitcoind or you can run a node on your home computer and experiment locally using something like xampp.

PHP with a 3rd party API

Here's an example with blockchain.info wallet API. You can import the private key using their create wallet utility, and then later send from that specific address.

$private = "5KL3cN..SOME_PRIVATE_KEY";
$url = "http://127.0.0.1:3030/merchant/YOUR-GUID/create?password=YOUR-PASSWORD&api_code=YOUR-API-CODE&priv=".$private;
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //change to true if your host can verify SSL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
curl_setopt($ch, CURLOPT_URL, $url);
$ccc = curl_exec($ch);
$json = json_decode($ccc, true);
echo "<pre>";
var_dump($json);
echo "</pre>";

Then once created you can send from that specific new address that was imported.

$from = "1fromAddress"; 
$to = "1receivingAddress";
$url = "http://127.0.0.1:3030/merchant/YOUR-GUID/payment?password=YOUR-PASSWORD&api_code=YOUR-API-CODE&from=".$from."&amount=1000000&to=".$to;
//amounts are in satoshi so 1000000 is 0.01BTC
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
curl_setopt($ch, CURLOPT_URL, $url);
$ccc = curl_exec($ch);
$json = json_decode($ccc, true);
echo "<pre>";
var_dump($json);
echo "</pre>";

If you need help setting up bitcoind or blockchain.info's wallet API I suggest you check out my tutorials on youtube. The hardest part with the 3rd party route is getting an API key; I hear blockchain.info has been strict with these lately.

bitcoind server method: https://www.youtube.com/watch?v=cMM-t7azzJE
blockchain.info 3rd party method: https://www.youtube.com/watch?v=X8jsaf4sEgs

4
  • im assuming this is the version i would need to download and run? bitcoin.org/en/download -- i just want to make sure. thanks for the answer! Sep 25, 2016 at 22:33
  • Also can this be done with other wallets that doesnt require downloading the entire blockchain like bitcoin core? Sep 25, 2016 at 22:34
  • The bitcoind version would require you to download and run bitcoin core. You can prune it though, if you are concerned with hard-drive space and still have full wallet functionality.
    – m1xolyd1an
    Sep 26, 2016 at 2:12
  • but the thing is i dont want it to store the private keys. is there a service for such a thing? or I would just have to remove it from these apps manually? What command would one use then? Oct 11, 2016 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.