/**
  * Generate a buffer containing a valid key
  *
  * @param EcAdapterInterface|null $ecAdapter
  * @return \BitWasp\Buffertools\BufferInterface
  * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  */
 public static function generateSecret(EcAdapterInterface $ecAdapter = null)
 {
     $random = new Random();
     $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
     do {
         $buffer = $random->bytes(32);
     } while (!$ecAdapter->validatePrivateKey($buffer));
     return $buffer;
 }
Example #2
0
 /**
  * @return array
  */
 public function generateSet()
 {
     $random = new Random();
     $set = [];
     for ($i = 0; $i < 2; $i++) {
         $set[] = [new Ping($random->bytes(8)->getInt())];
     }
     return $set;
 }
Example #3
0
 /**
  * @param int $version
  * @param Buffer $services
  * @param int $timestamp
  * @param NetworkAddressInterface $addrRecv
  * @param NetworkAddressInterface $addrFrom
  * @param int $nonce
  * @param Buffer $userAgent
  * @param int $startHeight
  * @param bool $relay
  */
 public function __construct($version, Buffer $services, $timestamp, NetworkAddressInterface $addrRecv, NetworkAddressInterface $addrFrom, $nonce, Buffer $userAgent, $startHeight, $relay)
 {
     if ($addrRecv instanceof NetworkAddressTimestamp) {
         $addrRecv = $addrRecv->withoutTimestamp();
     }
     if ($addrFrom instanceof NetworkAddressTimestamp) {
         $addrFrom = $addrFrom->withoutTimestamp();
     }
     $random = new Random();
     $this->nonce = $random->bytes(8)->getInt();
     $this->version = $version;
     $this->services = $services;
     $this->timestamp = $timestamp;
     $this->addrRecv = $addrRecv;
     $this->nonce = $nonce;
     $this->addrFrom = $addrFrom;
     $this->userAgent = $userAgent;
     $this->startHeight = $startHeight;
     if (!is_bool($relay)) {
         throw new \InvalidArgumentException('Relay must be a boolean');
     }
     $this->relay = $relay;
 }
 /**
  * Creates a new Bip39 mnemonic string.
  *
  * @param int $entropySize
  * @return string
  * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  */
 public function create($entropySize = 512)
 {
     $random = new Random();
     $entropy = $random->bytes($entropySize / 8);
     return $this->entropyToMnemonic($entropy);
 }
Example #5
0
 /**
  * @return Ping
  * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  */
 public function ping()
 {
     return new Ping($this->random->bytes(8)->getInt());
 }
Example #6
0
 public function testNetworkSerializer()
 {
     $v = '60002';
     $services = Buffer::hex('0000000000000001');
     $time = (string) time();
     $recipient = new NetworkAddress(Buffer::hex('0000000000000001'), '10.0.0.1', '8332');
     $sender = new NetworkAddress(Buffer::hex('0000000000000001'), '10.0.0.2', '8332');
     $userAgent = new Buffer("/Satoshi:0.7.2/");
     $lastBlock = '212672';
     $random = new Random();
     $nonce = $random->bytes(8)->getInt();
     $version = new Version($v, $services, $time, $recipient, $sender, $nonce, $userAgent, $lastBlock, true);
     $net = Bitcoin::getDefaultNetwork();
     $serializer = new NetworkMessageSerializer($net);
     $serialized = $version->getNetworkMessage()->getBuffer();
     $parsed = $serializer->parse($serialized)->getPayload();
     $this->assertEquals($version, $parsed);
 }
Example #7
0
 /**
  * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  * @expectedException \RuntimeException
  * @expectedExceptionMessage
  */
 public function testInvalidBytes()
 {
     $v = '60002';
     $services = Buffer::hex('0000000000000001');
     $time = '123456789';
     $recipient = new NetworkAddress(Buffer::hex('0000000000000001'), '10.0.0.1', '8332');
     $sender = new NetworkAddress(Buffer::hex('0000000000000001'), '10.0.0.2', '8332');
     $userAgent = new Buffer("/Satoshi:0.7.2/");
     $lastBlock = '212672';
     $random = new Random();
     $nonce = $random->bytes(8)->getInt();
     $version = new Version($v, $services, $time, $recipient, $sender, $nonce, $userAgent, $lastBlock, true);
     $bitcoin = new NetworkMessageSerializer(NetworkFactory::bitcoin());
     $serialized = $version->getNetworkMessage(NetworkFactory::viacoinTestnet())->getBuffer();
     $bitcoin->parse($serialized);
 }
Example #8
-2
<?php

require_once "../vendor/autoload.php";
use BitWasp\Bitcoin\Crypto\Random\Random;
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator;
// Generate a mnemonic
$random = new Random();
$entropy = $random->bytes(64);
$bip39 = MnemonicFactory::bip39();
$seedGenerator = new Bip39SeedGenerator($bip39);
$mnemonic = $bip39->entropyToMnemonic($entropy);
// Derive a seed from mnemonic/password
$seed = $seedGenerator->getSeed($mnemonic, 'password');
echo $seed->getHex() . "\n";
$bip32 = \BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory::fromEntropy($seed);