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 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); }
/** * create checksum to verify ownership of the master primary key * * @return string[] [address, signature] */ protected function createChecksumVerificationSignature() { $import = BIP32::import($this->primaryPrivateKey->key()); $public = $this->primaryPrivateKey->publicKey(); $address = BitcoinLib::public_key_to_address($public, $import['version']); return [$address, BitcoinLib::signMessage($address, $import)]; }