public function signMessage($address) { $input = Input::all(); $output = array(); if (!isset($input['message']) or trim($input['message']) == '') { $output['error'] = 'Message required'; $output['result'] = false; return new Response($output, 400); } $get = PaymentAddress::where('uuid', $address)->orWhere('address', $address)->first(); $found = false; if (!$get) { $output['error'] = 'Bitcoin address does not belong to server'; $output['result'] = false; return new Response($output, 400); } $address = $get->address; $address_generator = app('Tokenly\\BitcoinAddressLib\\BitcoinAddressGenerator'); $lib = new BitcoinLib(); $priv_key = $address_generator->WIFPrivateKey($get->private_key_token); $priv_key = BitcoinLib::WIF_to_private_key($priv_key); $sign = $priv_key; try { $sign = $lib->signMessage($input['message'], $priv_key); } catch (Exception $e) { $sign = false; } if (!$sign) { $output['error'] = 'Error signing message'; $output['result'] = false; return new Response($output, 500); } $output['result'] = $sign; return new Response($output); }
/** * Private Keys To Wallet * * This function accepts $wallet - a reference to an array containing * wallet info, indexed by hash160 of expected address. * It will attempt to add each key to this wallet, as well as all the * details that could be needed later on: public key, uncompressed key, * address, an indicator for address compression. Type is always set * to pubkeyhash for private key entries in the wallet. * * @param array $wallet * @param array $wifs * @param string $magic_byte */ public static function private_keys_to_wallet(&$wallet, array $wifs, $magic_byte = null) { $magic_byte = BitcoinLib::magicByte($magic_byte); if (count($wifs) > 0) { foreach ($wifs as $wif) { if (is_array($wif) && isset($wif['key'], $wif['is_compressed'])) { $key = $wif; } else { $key = BitcoinLib::WIF_to_private_key($wif); } $pubkey = BitcoinLib::private_key_to_public_key($key['key'], $key['is_compressed']); if ($pubkey !== false) { $pk_hash = BitcoinLib::hash160($pubkey); if ($key['is_compressed'] == true) { $uncompressed_key = BitcoinLib::decompress_public_key($pubkey); $uncompressed_key = $uncompressed_key['public_key']; } else { $uncompressed_key = $pubkey; } $wallet[$pk_hash] = array('type' => 'pubkeyhash', 'private_key' => $key['key'], 'public_key' => $pubkey, 'uncompressed_key' => $uncompressed_key, 'is_compressed' => $key['is_compressed'], 'address' => BitcoinLib::hash160_to_address($pk_hash, $magic_byte)); } } } }
public function testSignMessageDataSetAgainstRPC() { if (!getenv('BITCOINLIB_TEST_AGAINST_RPC')) { return $this->markTestSkipped("Not testing against RPC"); } // special case, when undefined we do 1, otherwise we do ENV * 5 (50 on travis) $cnt = getenv('BITCOINLIB_EXTENSIVE_TESTING') ? getenv('BITCOINLIB_EXTENSIVE_TESTING') * 5 : 1; $rpcHost = getenv('BITCOINLIB_RPC_HOST') ?: '127.0.0.1'; $rpcUser = getenv('BITCOINLIB_RPC_USER') ?: 'bitcoinrpc'; $rpcPassword = getenv('BITCOINLIB_RPC_PASSWORD') ?: '6Wk1SYL7JmPYoUeWjYRSdqij4xrM5rGBvC4kbJipLVJK'; $rpc = new Jsonrpcclient(['url' => "http://{$rpcUser}:{$rpcPassword}@{$rpcHost}:8332"]); if ($rpc->getinfo() == null) { $this->fail("Can't connect to bitcoind"); } $data = json_decode(file_get_contents(__DIR__ . "/data/signverify.json"), true); $data = array_map(function ($k) use($data) { return $data[$k]; }, (array) array_rand($data, $cnt)); foreach ($data as $row) { $privKey = BitcoinLib::WIF_to_private_key($row['wif']); $signature = BitcoinLib::signMessage($row['address'], $privKey); $this->assertTrue(!!$signature); $this->assertTrue(BitcoinLib::verifyMessage($row['address'], $signature, $row['address'])); $this->assertTrue($rpc->verifymessage($row['address'], $signature, $row['address'])); } }
public function testGetPrivKeyWif() { $cnt = (getenv('BITCOINLIB_EXTENSIVE_TESTING') ?: 1) * 5; for ($i = 0; $i < $cnt; $i++) { $hex = (string) str_pad(bin2hex(mcrypt_create_iv(32, \MCRYPT_DEV_URANDOM)), 64, '0', STR_PAD_LEFT); // create private key and WIF $wif = BitcoinLib::private_key_to_WIF($hex, FALSE, $this->addressVersion); $key = BitcoinLib::WIF_to_private_key($wif); $this->assertTrue($key['key'] == $hex); // create private key and WIF, without specifying address version $wif = BitcoinLib::private_key_to_WIF($hex, FALSE); $key = BitcoinLib::WIF_to_private_key($wif); $this->assertTrue($key['key'] == $hex); } }