/** * generate a new mnemonic from some random entropy (512 bit) * * @param string $forceEntropy forced entropy instead of random entropy for testing purposes * @return string * @throws \Exception */ protected function generateNewMnemonic($forceEntropy = null) { if ($forceEntropy === null) { $entropy = BIP39::generateEntropy(512); } else { $entropy = $forceEntropy; } return BIP39::entropyToMnemonic($entropy); }
public function testGenerate() { for ($i = 0; $i < 100; $i++) { $entropy = BIP39::generateEntropy(128); $this->assertTrue(!!$entropy); $mnemonic = BIP39::entropyToMnemonic($entropy); $this->assertTrue(!!$entropy); $entropy2 = BIP39::mnemonicToEntropy($mnemonic); $this->assertTrue(!!$entropy2); $this->assertEquals($entropy, $entropy2); $bip32 = BIP32::master_key(BIP39::mnemonicToSeedHex($mnemonic, 'PASSWORD')); $this->assertTrue(!!$bip32); } for ($i = 0; $i < 100; $i++) { $entropy = BIP39::generateEntropy(256); $this->assertTrue(!!$entropy); $mnemonic = BIP39::entropyToMnemonic($entropy); $this->assertTrue(!!$entropy); $entropy2 = BIP39::mnemonicToEntropy($mnemonic); $this->assertTrue(!!$entropy2); $this->assertEquals($entropy, $entropy2); $bip32 = BIP32::master_key(BIP39::mnemonicToSeedHex($mnemonic, 'PASSWORD')); $this->assertTrue(!!$bip32); } for ($i = 0; $i < 100; $i++) { $entropy = BIP39::generateEntropy(512); $this->assertTrue(!!$entropy); $mnemonic = BIP39::entropyToMnemonic($entropy); $this->assertTrue(!!$entropy); $entropy2 = BIP39::mnemonicToEntropy($mnemonic); $this->assertTrue(!!$entropy2); $this->assertEquals($entropy, $entropy2); $bip32 = BIP32::master_key(BIP39::mnemonicToSeedHex($mnemonic, 'PASSWORD')); $this->assertTrue(!!$bip32); } }
public function testNewBlankWithoutMnemonicsWallet() { $client = $this->setupBlocktrailSDK(); $identifier = $this->getRandomTestIdentifier(); $primaryPrivateKey = BIP32::master_key(BIP39::mnemonicToSeedHex(BIP39::entropyToMnemonic(BIP39::generateEntropy(512)), "password"), 'bitcoin', true); $backupPublicKey = BIP32::extended_private_to_public(BIP32::master_key(BIP39::mnemonicToSeedHex(BIP39::entropyToMnemonic(BIP39::generateEntropy(512)), "password"), 'bitcoin', true)); /** * @var $wallet \Blocktrail\SDK\Wallet */ $e = null; try { $wallet = $client->initWallet(["identifier" => $identifier]); } catch (ObjectNotFound $e) { list($wallet, $primaryMnemonic, $backupMnemonic, $blocktrailPublicKeys) = $client->createNewWallet(["identifier" => $identifier, "primary_private_key" => $primaryPrivateKey, "backup_public_key" => $backupPublicKey, "key_index" => 9999]); } $this->assertTrue(!!$e, "New wallet with ID [{$identifier}] already exists..."); $wallet = $client->initWallet(["identifier" => $identifier, "primary_private_key" => $primaryPrivateKey]); $this->wallets[] = $wallet; // store for cleanup $this->assertEquals(0, $wallet->getBalance()[0]); $e = null; try { $wallet->pay(["2N6Fg6T74Fcv1JQ8FkPJMs8mYmbm9kitTxy" => BlocktrailSDK::toSatoshi(0.001)]); } catch (\Exception $e) { } $this->assertTrue(!!$e, "Wallet without balance is able to pay..."); }
<?php use BitWasp\BitcoinLib\BIP32; use BitWasp\BitcoinLib\BIP39\BIP39; require_once __DIR__ . '/../vendor/autoload.php'; $password = "******"; $entropy = BIP39::generateEntropy(256); $mnemonic = BIP39::entropyToMnemonic($entropy); $seed = BIP39::mnemonicToSeedHex($mnemonic, $password); unset($entropy); // ignore, forget about this, don't use it! var_dump($mnemonic); // this is what you print on a piece of paper, etc var_dump($password); // this is secret of course var_dump($seed); // this is what you use to generate a key $key = BIP32::master_key($seed); // enjoy