} // Gather private keys $x = 1; $privkeys = array(); while (1) { $var = 'verify_private_key' . $x; if (!isset($_POST[$var])) { break; } $privkeys[] = trim($_POST[$var]); $x++; } // Get public keys $rows = DB::query("SELECT * FROM coin_wallets_keys WHERE wallet_id = %d", $wallet_id); foreach ($rows as $row) { $public_keys[] = trim($enc->decrypt($row['public_key'])); } // Verify the keys $num = 1; $ok = true; $no_keys = array(); foreach ($public_keys as $public_key) { // Go through private keys $found = false; foreach ($privkeys as $private_key) { if ($public_key == $b32->extended_private_to_public($private_key)) { $found = true; break; } } if ($found === false) {
if ($input_amount >= $send_amount) { break; } // Get address row if (!($addr_row = DB::queryFirstRow("SELECT * FROM coin_addresses WHERE address = %s", $row['address']))) { continue; } // Get keyindexes 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
public function address_to_sigscript($address) { // Initialize $enc = new encrypt(); // Get address if (!($addr_row = DB::queryFirstRow("SELECT * FROM coin_addresses WHERE address = %s", $address))) { trigger_error("Address does not exist, {$address}", E_USER_ERROR); } // Get wallet if (!($wallet = DB::queryFirstRow("SELECT * FROM coin_wallets WHERE id = %d", $addr_row['wallet_id']))) { trigger_error("Wallet does not exist, ID# {$addr_row['wallet_id']}", E_USER_ERROR); } // Multisig if ($wallet['address_type'] == 'multisig') { // Go through addresses $public_keys = array(); $rows = DB::query("SELECT * FROM coin_addresses_multisig WHERE address = %s ORDER BY id", $address); foreach ($rows as $row) { $keyindex = $addr_row['is_change_address'] . '/' . $row['address_num']; $ext_pubkey = trim($enc->decrypt(DB::queryFirstField("SELECT public_key FROM coin_wallets_keys WHERE id = %d", $row['key_id']))); $child_pubkey = $this->build_key($ext_pubkey, $keyindex)[0]; $public_keys[] = $this->import($child_pubkey)['key']; } // Create redeem script $scriptsig = $this->create_redeem_script($wallet['sigs_required'], $public_keys); // Standard } else { $decode_address = $this->base58_decode($address); $scriptsig = '76a914' . substr($decode_address, 2, 40) . '88ac'; } // Return return $scriptsig; }
public function gather_inputs($wallet_id, $amount, $privkeys = array()) { // Initialize global $config; $bip32 = new bip32(); $enc = new encrypt(); // Get wallet if (!($wallet = DB::queryFirstRow("SELECT * FROM coin_wallets WHERE id = %d", $wallet_id))) { trigger_error("Wallet does not exist, ID# {$wallet_id}", E_USER_ERROR); } // Go through inputs $inputs = array(); $input_amount = 0; $rows = DB::query("SELECT * FROM coin_inputs WHERE is_spent = 0 AND is_confirmed = 1 ORDER BY id"); foreach ($rows as $row) { if ($input_amount >= $amount) { break; } // Get address row if (!($addr_row = DB::queryFirstRow("SELECT * FROM coin_addresses WHERE address = %s", $row['address']))) { continue; } // Multisig address if ($wallet['address_type'] == 'multisig') { // Go through addresses $keys = array(); $public_keys = array(); $arows = DB::query("SELECT * FROM coin_addresses_multisig WHERE address = %s ORDER BY id", $row['address']); foreach ($arows as $arow) { // Get public key $keyindex = $addr_row['is_change_address'] . '/' . $arow['address_num']; $ext_pubkey = trim($enc->decrypt(DB::queryFirstField("SELECT public_key FROM coin_wallets_keys WHERE id = %d", $arow['key_id']))); $child_pubkey = $bip32->build_key($ext_pubkey, $keyindex)[0]; $import = $bip32->import($child_pubkey); $public_keys[] = $import['key']; // Go through private keys foreach ($privkeys as $privkey) { // Get child key $child_privkey = $bip32->build_key($privkey, $keyindex)[0]; $chk_pubkey = $bip32->extended_private_to_public($child_privkey); if ($chk_pubkey != $child_pubkey) { continue; } // Validate privkey if (!in_array($child_privkey, $keys)) { $keys[] = $child_privkey; } } } if (count($keys) < $wallet['sigs_required']) { continue; } // Add to inputs $vars = array('input_id' => $row['id'], 'txid' => $row['txid'], 'vout' => $row['vout'], 'amount' => $row['amount'], 'scriptsig' => $bip32->create_redeem_script($wallet['sigs_required'], $public_keys), 'public_keys' => $public_keys, 'privkeys' => $keys); array_push($inputs, $vars); // Standard address } else { // Get private key $keyindex = $addr_row['is_change_address'] . '/' . $addr_row['address_num']; $privkey = $bip32->build_key($privkeys[0], $keyindex)[0]; // Get script sig $decode_address = $bip32->base58_decode($row['address']); $scriptsig = '76a914' . substr($decode_address, 2, 40) . '88ac'; // Get public key $public_key = DB::queryFirstField("SELECT public_key FROM coin_wallets_keys WHERE wallet_id = %d ORDER BY id LIMIT 0,1", $wallet_id); $public_key = trim($enc->decrypt($public_key)); $child_pubkey = $bip32->build_key($public_key, $keyindex)[0]; // Validate key $chk_pubkey = $bip32->extended_private_to_public($privkey); if ($chk_pubkey != $child_pubkey) { continue; } // Add to inputs $vars = array('input_id' => $row['id'], 'txid' => $row['txid'], 'vout' => $row['vout'], 'amount' => $row['amount'], 'scriptsig' => $scriptsig, 'public_keys' => array($public_key), 'privkeys' => array($privkey)); array_push($inputs, $vars); } // Add to amounts $input_amount += $row['amount']; $amount += $config['btc_txfee']; } // Check amount if ($input_amount < $amount) { return false; } // Return return $inputs; }