<?php // Generate master key $b32 = new bip32(); $private_key = $b32->generate_master_key(); $public_key = $b32->extended_private_to_public($private_key); // Send response $response = array('private_key' => $private_key, 'public_key' => $public_key); echo json_encode($response); exit(0);
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; }