$x = 1; $privkeys = array(); while (1) { $var = 'private_key' . $x; if (!isset($_POST[$var])) { break; } $privkeys[] = $_POST[$var]; $x++; } // Get total inputs $balance = DB::queryFirstField("SELECT sum(amount) FROM coin_inputs WHERE wallet_id = %d AND is_spent = 0", $_POST['wallet_id']); $total_inputs = DB::queryFirstField("SELECT count(*) FROM coin_inputs WHERE wallet_id = %d AND is_spent = 0", $_POST['wallet_id']); $balance -= $total_inputs * $config['btc_txfee']; // Generate address from new wallet $address = $bip32->generate_address($new_wallet_id); $outputs = array($address => $balance); // Gather all unspent inputs $client = new rawtx(); $inputs = $client->gather_inputs($_POST['wallet_id'], $balance, $privkeys); // Create transaction $transaction = $client->create_transaction($_POST['wallet_id'], $inputs, $outputs); // Sign transaction $signed_tx = $client->sign_transaction($transaction, $inputs); // Send transaction $client = new transaction(); $client->send_transaction($signed_tx); // Update wallets DB::query("UPDATE coin_wallets SET status = 'inactive' WHERE id = %d", $_POST['wallet_id']); DB::query("UPDATE coin_inputs SET is_spent = 1 WHERE wallet_id = %d", $_POST['wallet_id']); // User message
// Set vars $vars = array('input_id' => $row['id'], 'amount' => $row['amount'], 'txid' => $row['txid'], 'vout' => $row['vout'], 'sigscript' => $sigscript, 'keyindex' => $keyindexes); array_push($json['inputs'], $vars); } // Gather outputs $rows = DB::query("SELECT * FROM coin_sends WHERE status = 'pending' ORDER BY id"); foreach ($rows as $row) { // Gather recipients $recipients = array(); $arows = DB::query("SELECT * FROM coin_sends_addresses WHERE send_id = %d", $row['id']); foreach ($arows as $arow) { $vars = array('amount' => $arow['amount'], 'address' => $arow['address']); array_push($recipients, $vars); } // Get change address $change_address = $bip32->generate_address($wrow['id'], 0, 1); $change_sigscript = $bip32->address_to_sigscript($change_address); // Get key indexes of change address $change_keyindexes = array(); $addr_rows = DB::query("SELECT * FROM coin_addresses_multisig WHERE address = %s", $change_address); foreach ($addr_rows as $addr_row) { $change_keyindexes[] = '1/' . $addr_row['address_num']; } // Set vars $vars = array('output_id' => $arow['id'], 'recipients' => $recipients, 'change_keyindex' => $change_keyindexes, 'change_sigscript' => $change_sigscript); array_push($json['outputs'], $vars); } // Send file header("Content-disposition: attachment; filename=\"tx.json\""); header("Content-type: text/json"); echo json_encode($json);
$template->add_message('Invalid amount specified, ' . $_POST['amount'], 'error'); } elseif (!$_POST['amount'] > 0) { $template->add_message('Amount must be greater than 0.', 'error'); } // Add invoice, if no errors if ($template->has_errors != 1) { // Get amounts if ($_POST['currency'] == 'fiat') { $amount = $_POST['amount']; $amount_btc = $amount / $config['exchange_rate']; } else { $amount_btc = $_POST['amount']; $amount = $amount_btc * $config['exchange_rate']; } // Generate payment address $address = $bip32->generate_address($_POST['wallet_id'], $user_row['id']); DB::query("UPDATE coin_addresses SET is_used = 1 WHERE address = %s", $address); // Add new invoice DB::insert('invoices', array('wallet_id' => $_POST['wallet_id'], 'userid' => $user_row['id'], 'currency' => $_POST['currency'], 'amount' => $amount, 'amount_btc' => $amount_btc, 'payment_address' => $address, 'note' => $_POST['note'], 'process_note' => '')); $invoice_id = DB::insertId(); // Send notifications send_notifications('invoice_created', $invoice_id); // User message $template->add_message("Successfully generated a new pending invoice for user, {$_POST['username']}"); } // Process invoices } elseif (isset($_POST['submit']) && $_POST['submit'] == 'Process Checked Invoices') { // Process $ids = get_chk('invoice_id'); foreach ($ids as $id) { if (!$id > 0) {
public function create_transaction($wallet_id, $inputs, $outputs) { // Initialize global $config; $p2sh_byte = TESTNET == 1 ? 'c4' : '05'; $bip32 = new bip32(); // Start variables $transaction = array('01000000', str_pad(count($inputs), 2, 0, STR_PAD_LEFT)); // Gather inputs $input_amount = floatval(0); $txfee = 0; foreach ($inputs as $row) { $transaction[] = bin2hex(strrev(hex2bin($row['txid']))) . bin2hex(pack('V', $row['vout'])); $transaction[] = '00'; $transaction[] = ''; $transaction[] = 'ffffffff'; $input_amount += floatval($row['amount']) - floatval($config['btc_txfee']); $txfee += $config['btc_txfee']; } // Get amounts $output_amount = floatval(array_sum(array_values($outputs))); // Check for change if (round($input_amount, 8) > round($output_amount, 8)) { $change_amount = round($input_amount, 8) - round($output_amount, 8); $change_address = $bip32->generate_address($wallet_id, 0, 1); $outputs[$change_address] = round($change_amount, 8, PHP_ROUND_HALF_DOWN); } // Gather outputs $transaction[] = str_pad(count($outputs), 2, 0, STR_PAD_LEFT); foreach ($outputs as $address => $amount) { // Get script sig $decode_address = $bip32->base58_decode($address); $version = substr($decode_address, 0, 2); $scriptsig = $version == $p2sh_byte ? 'a914' . substr($decode_address, 2, 40) . '87' : '76a914' . substr($decode_address, 2, 40) . '88ac'; // Get amount $amount = $this->dec_to_bytes($amount * 100000000.0, 8); $amount = $this->flip_byte_order($amount); // Add transaction vars $transaction[] = $amount; $transaction[] = dechex(strlen(hex2bin($scriptsig))); $transaction[] = $scriptsig; } // Return $transaction[] = '00000000'; return $transaction; }