Author

Topic: [PHP] Get address from raw transaction (Read 1166 times)

newbie
Activity: 42
Merit: 0
March 25, 2015, 03:36:45 AM
#5
I don't know anything about Gibhub. I can't download a wallet from it !

Please show me how to use it!

Thanks !
member
Activity: 87
Merit: 10
March 22, 2015, 06:29:59 PM
#4
Did you try to write your own Base58 library or are you using some already pre-built ones?  If the numbers are really, really big, you need something like bcmath or another big number library.
legendary
Activity: 1386
Merit: 1053
Please do not PM me loan requests!
March 21, 2015, 01:50:28 PM
#3
I've had problems getting really big numbers to base 58. I ended up just using a bitcoin node to do it.
donator
Activity: 1218
Merit: 1079
Gerald Davis
March 20, 2015, 11:04:06 AM
#2
Can you provide an example where it failed?  Include the expected output and the actual output.
newbie
Activity: 16
Merit: 0
March 20, 2015, 10:01:10 AM
#1
Hi, I'm working on a PHP script wrote with a friend (who's actually busy atm  Undecided) for a next little project.. What's wrong with it? Sometimes won't read the right address from raw transactions and multisig is 100% missing because I can't understand how to get it.
Can somebody help me to add correct functions/fix actual functions? Thanks!!

Code is also available on pastebin.com
Code:
include('setup.php');
require_once 'BitcoinRPC.php';

function readTransaction($rawtx)
{
        
$details $this->getrawtransaction($rawtx["txid"], 1);
        
$vinhex $details['vin'][0]['scriptSig']['hex'];
        
$decodedscript decodescript($vinhex);
        
$address pubKeyToAddress($decodedscript[1]);
        
return $address;
}

function hash160ToAddress($hash160,$addressversion "00")
{
$hash160 $addressversion.$hash160;
$check pack("H*" $hash160);
$check hash("sha256"hash("sha256"$check true));
$check substr($check08);
$hash160 strtoupper($hash160.$check);
return encodeBase58($hash160);
}

function hash160($data)
{
$data pack("H*" $data);
return strtoupper(hash("ripemd160"hash("sha256"$data,true)));
}

function pubKeyToAddress($pubkey)
{
return hash160ToAddress(hash160($pubkey));
}

function decodeHex($hex)
{
$hex strtoupper($hex);
$chars "0123456789ABCDEF";
$return "0";
for($i 0$i strlen($hex); $i++)
{
$current = (string)strpos($chars$hex[$i]);
$return = (string)bcmul($return"16"0);
$return = (string)bcadd($return$current0);
}
return $return;
}

function encodeBase58($hex)
{
if(strlen($hex)%!=)
{
die("encodeBase58: uneven number of hex characters");
}
$orighex $hex;
$chars "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
$hex decodeHex($hex);
$return "";
while(bccomp($hex0) == 1)
{
$dv = (string)bcdiv($hex"58"0);
$rem = (integer)bcmod($hex"58");
$hex $dv;
$return $return.$chars[$rem];
}
$return strrev($return);
for($i 0$i strlen($orighex) && substr($orighex$i2) == "00"$i += 2)
{
$return "1".$return;
}
return $return;
}

function decodescript($script)
{
$op_code = array(
'00' => 'OP_FALSE''61' => 'OP_NOP''6a' => 'OP_RETURN',
'76' => 'OP_DUP''87' => 'OP_EQUAL''88' => 'OP_EQUALVERIFY',
'51' => 'OP_TRUE''a6' => 'OP_RIPEMD160''a7' => 'OP_SHA1',
'a8' => 'OP_SHA256''a9' => 'OP_HASH160''aa' => 'OP_HASH256',
'ac' => 'OP_CHECKSIG''ae' => 'OP_CHECKMULTISIG');
$data = array();
while(strlen($script) !== 0)
{
$byte return_bytes($script1);
if(isset($op_code[$byte]))
{
$data[] = $op_code[$byte];
}
else if($byte >= 0x01 && $byte <= 0x4b)
{
$data[] = return_bytes($scripthexdec($byte));
}
else if($byte >= 0x52 && $byte <= 0x60)
{
$data[] = 'OP_' . ($byte 0x52);
}
}
return $data;
}

function return_bytes(&$string$byte_count$reverse false)
{
$requested_bytes substr($string0$byte_count 2);
$string substr($string$byte_count 2);
return ($reverse == false) ? $requested_bytes flip_byte_order($requested_bytes);
}

function flip_byte_order($bytes)
{
return implode(''array_reverse(str_split($bytes2)));
}

$transaction $this->listtransactions($setup['account'], 500100);
foreach($transaction as $rawtx)
{
if($rawtx['category'] == "sent")
{
continue;
}
else
{
$query mysql_query('SELECT * FROM `received_transactions` WHERE `tx` = "'.$rawtx['txid'].'";') or die(mysql_error());
if(!mysql_fetch_assoc($query))
{
$address readTransaction($rawtx);
$input $rawtx['amount'];
$tx $rawtx['txid'];
$received date('Y-m-d H:i:s'$rawtx['time']);
$query "INSERT INTO `received_transactions` (`id`, `tx`, `address`, `input`, `received`) VALUES (null, '$tx', '$address', '$input', '$received')";
}
else
{
continue;
}
}
}
?>
Jump to: