Title: [PHP] Get address from raw transaction
Post by: noize941 on March 20, 2015, 03:01:10 PM
Hi, I'm working on a PHP script wrote with a friend (who's actually busy atm :-\) 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 (http://pastebin.com/RXgxun8t) <?php 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($check, 0, 8); $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, $current, 0); } return $return; } function encodeBase58($hex) { if(strlen($hex)%2 !=0 ) { die("encodeBase58: uneven number of hex characters"); } $orighex = $hex; $chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; $hex = decodeHex($hex); $return = ""; while(bccomp($hex, 0) == 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, $i, 2) == "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($script, 1); if(isset($op_code[$byte])) { $data[] = $op_code[$byte]; } else if($byte >= 0x01 && $byte <= 0x4b) { $data[] = return_bytes($script, hexdec($byte)); } else if($byte >= 0x52 && $byte <= 0x60) { $data[] = 'OP_' . ($byte - 0x52); } } return $data; } function return_bytes(&$string, $byte_count, $reverse = false) { $requested_bytes = substr($string, 0, $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($bytes, 2))); } $transaction = $this->listtransactions($setup['account'], 500, 100); 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; } } } ?>
Title: Re: [PHP] Get address from raw transaction
Post by: DeathAndTaxes on March 20, 2015, 04:04:06 PM
Can you provide an example where it failed? Include the expected output and the actual output.
Title: Re: [PHP] Get address from raw transaction
Post by: Taras on March 21, 2015, 06:50:28 PM
I've had problems getting really big numbers to base 58. I ended up just using a bitcoin node to do it.
Title: Re: [PHP] Get address from raw transaction
Post by: Snagglebone on March 22, 2015, 11:29:59 PM
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.
Title: Re: [PHP] Get address from raw transaction
Post by: soowein on March 25, 2015, 08:36:45 AM
I don't know anything about Gibhub. I can't download a wallet from it !
Please show me how to use it!
Thanks !
|