} else { $vout_id = $db->insert($voSql, $voFlds); } } else { $voSql = "update transactions_vouts set transaction_id = ?, txid = ?, " . "value = ?, n = ?, reqSigs = ?, type = ?, hexgz = ? where vout_id = ?"; $voFlds[0] = $voFlds[0] . 'i'; $voFlds[] = $vout_id; if ($outputType == 'sketchy') { $voFlds[count($voFlds) - 2] = ""; $db->update($voSql, $voFlds); } else { $db->update($voSql, $voFlds); } } if ($outputType !== 'sketchy') { $address = AddressFactory::getAssociatedAddress($output->getScript(), $network); //echo $address,"\n"; $address_id = getAddressId($address); $aisql = "insert into transactions_vouts_addresses (vout_id, address_id) values (?, ?)"; $db->insert($aisql, ['ii', $vout_id, $address_id]); } } /* * * inputs * */ $inputs = $tx->getInputs(); for ($i = 0; $i < $inputs->count(); $i++) { $input = $inputs->getInput($i); $inputVout = $input->getVout();
public function parseBTCTransaction($raw_transaction_hex, $is_counterparty = false) { if (!$raw_transaction_hex) { throw new Exception("Transaction hex was empty", 1); } $transaction = TransactionFactory::fromHex($raw_transaction_hex); $out = []; // inputs $inputs = $transaction->getInputs(); $out['inputs'] = []; foreach ($inputs as $input) { // build the ASM $asm = ""; $opcodes = $input->getScript()->getOpCodes(); foreach ($input->getScript()->getScriptParser()->decode() as $op) { // $asm .= $opcodes->getOp($op->getOp()); if ($op->isPush()) { $item = $op->getData()->getHex(); } else { $item = $opcodes->getOp($op->getOp()); } $asm = ltrim($asm . " " . $item); } // extract the address $address = null; // address decoding not implemented yet // $classifier = new InputClassifier($script); // if ($classifier->isPayToPublicKeyHash()) { // $decoded = $script->getScriptParser()->decode(); // $hex_buffer = $decoded[1]->getData(); // $public_key = PublicKeyFactory::fromHex($hex_buffer); // $address = $public_key->getAddress()->getAddress(); // } else if ($classifier->isPayToScriptHash()) { // $decoded = $script->getScriptParser()->decode(); // $hex_buffer = $decoded[count($decoded)-1]->getData(); // $script = ScriptFactory::fromHex($hex_buffer); // $sh_address = new ScriptHashAddress($script->getScriptHash()); // $address = $sh_address->getAddress(); // } $out['inputs'][] = ['txid' => $input->getTransactionId(), 'n' => $input->getVout(), 'asm' => $asm]; } // txid $out['txid'] = $transaction->getTxId()->getHex(); // destination $output_offset = 0; $outputs = $transaction->getOutputs(); $destination = AddressFactory::getAssociatedAddress($outputs[$output_offset]->getScript()); $out['destination'] = $destination; $out['btc_amount'] = $outputs[0]->getValue(); if ($is_counterparty) { // OP_RETURN ++$output_offset; $obfuscated_op_return_hex = $outputs[$output_offset]->getScript()->getScriptParser()->decode()[1]->getData()->getHex(); $hex = $this->arc4decrypt($transaction->getInput(0)->getTransactionId(), $obfuscated_op_return_hex); $counterparty_data = $this->parseTransactionData(hex2bin(substr($hex, 16))); $out = $counterparty_data + $out; $out['btc_dust_size'] = $outputs[0]->getValue(); } // change ++$output_offset; $change = []; for ($i = $output_offset; $i < count($outputs); $i++) { $output = $outputs[$i]; $change[] = [AddressFactory::getAssociatedAddress($outputs[$i]->getScript()), $output->getValue()]; } $out['change'] = $change; // sum all outputs $sum_out = 0; foreach ($outputs as $output) { $sum_out += $output->getValue(); } $out['sum_out'] = $sum_out; return $out; }