if ($wrow['address_type'] == 'multisig') { $keyindexes = array(); $public_keys = array(); $addr_rows = DB::query("SELECT * FROM coin_addresses_multisig WHERE address = %s ORDER BY id", $row['address']); foreach ($addr_rows as $arow) { $keyindexes[] = $addr_row['is_change_address'] . '/' . $arow['address_num']; // Get public key $ext_public_key = trim($encrypt->decrypt(DB::queryFirstField("SELECT public_key FROM coin_wallets_keys WHERE id = %d", $arow['key_id']))); $child_key = $bip32->build_key($ext_public_key, $addr_row['is_change_address'] . '/' . $arow['address_num'])[0]; $import = $bip32->import($child_key); $public_keys[] = $import['key']; } $sigscript = $bip32->create_redeem_script($wrow['sigs_required'], $public_keys); } else { $keyindexes = $addr_row['is_change_address'] . '/' . $addr_row['address_num']; $decode_address = $bip32->base58_decode($row['address']); $sigscript = '76a914' . substr($decode_address, 2, 40) . '88ac'; } // 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);
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; }