p2pkh($bitcoinPrefixes), $slip132->p2shP2wpkh($bitcoinPrefixes), $slip132->p2wpkh($bitcoinPrefixes), ]) ]); $serializer = new Base58ExtendedKeySerializer(new ExtendedKeySerializer($adapter, $config)); function compute_scripthash_status($scriptpubkey_id) { $txs = db_query("SELECT DISTINCT txid, tx_height FROM txs JOIN txos ON ( txos.create_txid_id = txs.txid_id OR txos.destroy_txid_id = txs.txid_id ) JOIN txids USING ( txid_id ) WHERE scriptpubkey_id = ? ORDER BY tx_height, tx_pos", $scriptpubkey_id); $status = ''; foreach ($txs as $tx) { $status .= $tx['txid'].':'.$tx['tx_height'].':'; } //echo("status: $status\n"); return ($status == '')?'':hash('sha256', $status); } function getParsedXPUB($xpub) { global $serializer, $btc; return $serializer->parse($btc, $xpub); } function getAddressFast($parsedXPUB, $path) { global $btc, $addrCreator; return $parsedXPUB->derivePath($path)->getAddress($addrCreator)->getAddress($btc); } function getAddress($xpub, $path) { return getAddressFast(getParsedXPUB($xpub), $path); } function getScriptPubKey($address) { global $addrCreator, $btc; return $addrCreator->fromString($address, $btc)->getScriptPubKey()->getBinary(); } function calcReverseHash($data) { return bin2hex(strrev(hash('sha256',$data, true))); } function getReverseScriptHash($address) { //global $addrCreator, $btc; //$wsh = $addrCreator->fromString($address, $btc)->getScriptPubKey()->getWitnessScriptHash()->getBinary(); //return bin2hex(strrev($wsh)); return calcReverseHash(getScriptPubKey($address)); } function rpc_electrum($method) { global $electrum_server_address, $electrum_server_port; $args = func_get_args(); array_shift($args); $json_in = json_encode(array('jsonrpc' => '1.0', 'id' => true, 'method' => $method, 'params' => $args))."\n"; $socket = fsockopen($electrum_server_address, $electrum_server_port); if (!$socket) fatal("unable to connect to $electrum_server_address:$electrum_server_port"); $length = strlen($json_in); //echo("writing a message of size $length\n"); $written = fwrite($socket, $json_in); if ($written === false) fatal("error sending JSONRPC message to server"); else if ($written < $length) fatal("short write to server"); $json_out = fgets($socket); if ($json_out === false) fatal('error performing rpc'); $ret = json_decode($json_out, true); if ($ret === null) fatal("JSON encoded data cannot be decoded"); if ($ret['error'] !== null) fatal("there was an errer performing rpc"); return $ret['result']; } function rpc_bitcoin($method) { global $rpcuser, $rpcpass; $args = func_get_args(); array_shift($args); $json_in = json_encode(array('jsonrpc' => '1.0', 'id' => true, 'method' => $method, 'params' => $args)); $ch = curl_init('http://127.0.0.1:8332'); curl_setopt($ch, CURLOPT_USERPWD, $rpcuser.':'.$rpcpass); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_in); $json_out= curl_exec($ch); if ($json_out === false) fatal('error performing rpc'); $ret = json_decode($json_out, true); if ($ret === null) fatal("JSON encoded data cannot be decoded"); if ($ret['error'] !== null) fatal("there was an errer performing rpc"); return $ret['result']; } function get_address_id($address) { if ($address == '') return NULL; db_direct('LOCK TABLES addresses WRITE'); $address_id = db_single_field('SELECT address_id FROM addresses WHERE address = ?', $address); if (!$address_id) { db_exec('INSERT INTO addresses ( address ) VALUES ( ? )', $address); $address_id = db_last_insert_id(); } db_direct('UNLOCK TABLES'); return $address_id; } function get_scriptpubkey_id($scriptPubKey, $address_id) { if (!strlen($scriptPubKey)) { die("scriptPubKey is empty"); } db_direct('LOCK TABLES scriptpubkeys WRITE'); $scriptpubkey_id = db_single_field('SELECT scriptpubkey_id FROM scriptpubkeys WHERE scriptpubkey = ?', $scriptPubKey); if (!$scriptpubkey_id) { db_exec('INSERT INTO scriptpubkeys ( scriptpubkey, address_id, reverse_scriptpubkey_hash ) VALUES ( ?, ?, ? )', $scriptPubKey, $address_id, calcReverseHash($scriptPubKey)); $scriptpubkey_id = db_last_insert_id(); } db_direct('UNLOCK TABLES'); return $scriptpubkey_id; } ?>