Bitcoin Forum
September 24, 2026, 08:16:06 PM *
News: Latest Bitcoin Core release: 31.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [PHP] Get address from raw transaction  (Read 1173 times)
noize941 (OP)
Newbie
*
Offline

Activity: 16
Merit: 0


View Profile
March 20, 2015, 03:01:10 PM
Last edit: March 20, 2015, 03:16:40 PM by noize941
 #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:
<?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;
}
}
}
?>
DeathAndTaxes
Donator
Legendary
*
Offline

Activity: 1218
Merit: 1342


Gerald Davis


View Profile
March 20, 2015, 04:04:06 PM
 #2

Can you provide an example where it failed?  Include the expected output and the actual output.
Taras
Legendary
*
Offline

Activity: 1386
Merit: 1063


Please do not PM me loan requests!


View Profile WWW
March 21, 2015, 06: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.
Snagglebone
Member
**
Offline

Activity: 87
Merit: 10


View Profile
March 22, 2015, 11: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.
soowein
Newbie
*
Offline

Activity: 42
Merit: 0


View Profile
March 25, 2015, 08: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 !
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!