was a bit bored and cannot sleep (hope there are no major mistakes
![Shocked](https://bitcointalk.org/Smileys/default/shocked.gif)
)...
had not tested the code, usage with caution and on your own risk!i think the most difficult part is done ^.-
guess bitspill uses importprivkey and this is no option if i get the task right
todo:
- generating public address from given private key needs to be added
- may the script transform into a function
- ... aaand errorhandling!
i'm posting the code public, so everyone can blame me, can improve the code and benefit from it and one can get the bounty for finishin' the work :x
requirements:
- bitcoind/bitcoin-qt >= 0.7
- php >= 5.2.0
<?php
// guess this part is already done ^.^
$require_once("oneoftheserpclibs");
$rpc=new jsonRPCClient(...);
//
// -- user inputs?
$privatekey="";
$publickey=""; // replacing later to generate publickey from privatekey
$sweep="";
// -- user inputs end
// -- settings
$fee_per_kb=0.0001;
$value_min=0; // mabe change from 0 to 0.00005430 due to 0.8.2?
// -- settings end
// -- main part
$unspent_data=json_decode(file_get_contents("http://blockchain.info/unspent?active=".$publickey),true); // you have to get the unspent data from somewhere, rescan is no option, so pull from somewhere else, in this case, blockchain.info
$txs=array();
$txs_s=array();
$value=0.0;
foreach($unspent_data AS $k=>$v) // now parse the unspent data and prepare to send ;)
{
$txs[]=array("txid"=>$v['tx_hash'],"vout"=>$v['tx_output_n']);
$txs_s[]=array("txid"=>$v['tx_hash'],"vout"=>$v['tx_output_n'],"scriptPubKey"=>$v['script']);
$value+=($v['value']*1e8);
}
$fee=$fee_per_kb*ceil((count($txs)*181+74)/1024); // very simple calculating size and fee, should be enough in this case
$value-=$fee;
$recipients=array($sweep=>$value);
if($value>$value_min&&!empty($txs)) // only checking if real sending value is greater then 0/min and if inputs are given before creating the transaction, sign' and sending
{
$rawtransaction=$rpc->createrawtransaction($txs,$recipients);
$signed_transaction=$rpc->signrawtransaction($rawtransaction,$txs_s,array($privatekey));
if($signed_transaction['complete']==1)
{
$rpc->sendrawtransaction($signed_transaction['hex']);
}
}
// -- main part end
?>
edit: few typos