Skip to content

Commit

Permalink
remove HierarchicalKey::withScriptDataFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kerin authored and afk11 committed Mar 12, 2018
1 parent d261548 commit a8a46dc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
1 change: 0 additions & 1 deletion examples/slip132.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,3 @@
$serAddrKey = $serializer->serialize($btc, $addrKey);
echo "address key {$serAddrKey}\n";
echo "addr[0] {$addrKey->getAddress($addrCreator)->getAddress($btc)}\n";

12 changes: 0 additions & 12 deletions src/Key/Deterministic/HierarchicalKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,6 @@ public function withoutPrivateKey(): HierarchicalKey
return $clone;
}

/**
* @param ScriptDataFactory $factory
* @return HierarchicalKey
*/
public function withScriptFactory(ScriptDataFactory $factory)
{
$clone = clone $this;
$clone->scriptDataFactory = $factory;
$clone->scriptAndSignData = null; // we cache, don't forget to clear
return $clone;
}

/**
* @return ScriptDataFactory
*/
Expand Down
10 changes: 0 additions & 10 deletions tests/Key/Deterministic/Bip49Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,6 @@ public function testBip49WithoutPrefix()
"03a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f",
$firstKey->getPrivateKey()->getPublicKey()->getHex()
);

$slip132 = new Slip132(new KeyToScriptHelper($adapter));
$registry = new BitcoinRegistry();
$prefix = $slip132->p2shP2wpkh($registry);
$p2shP2wpkhKey = $firstKey->withScriptFactory($prefix->getScriptDataFactory());
$address = $p2shP2wpkhKey->getAddress(new AddressCreator());
$this->assertEquals(
"2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2",
$address->getAddress($tbtc)
);
}

/**
Expand Down
32 changes: 29 additions & 3 deletions tests/Key/Deterministic/HierarchicalKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@

namespace BitWasp\Bitcoin\Tests\Key\Deterministic;

use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
use BitWasp\Bitcoin\Address\AddressCreator;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKey;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory;
use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2shP2wshScriptDecorator;
use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2shScriptDecorator;
use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2wshScriptDecorator;
use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2pkhScriptDataFactory;
use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2pkScriptDataFactory;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
use BitWasp\Bitcoin\Key\PublicKeyFactory;
use BitWasp\Bitcoin\Math\Math;
use BitWasp\Bitcoin\Network\Network;
use BitWasp\Bitcoin\Network\NetworkFactory;
use BitWasp\Bitcoin\Network\NetworkInterface;
use BitWasp\Bitcoin\Network\Networks\BitcoinTestnet;
use BitWasp\Bitcoin\Tests\AbstractTestCase;
use BitWasp\Buffertools\Buffer;
Expand Down Expand Up @@ -54,8 +58,7 @@ private function compareToPrivVectors(\BitWasp\Bitcoin\Key\Deterministic\Hierarc
$this->assertSame($vectors->secret_wif, $key->getPrivateKey()->toWif($this->network));
$this->assertSame($vectors->secret_wif, $key->getPrivateKey()->toWif());

$this->assertSame($vectors->address, (new PayToPubKeyHashAddress($key->getPublicKey()->getPubKeyHash()))->getAddress($this->network));
$this->assertSame($vectors->address, (new PayToPubKeyHashAddress($key->getPublicKey()->getPubKeyHash()))->getAddress());
$this->assertSame($vectors->address, $key->getAddress(new AddressCreator())->getAddress($this->network));

$this->assertSame($vectors->xprv_b58, $key->toExtendedPrivateKey($this->network), 'correct xprv');
$this->assertSame($vectors->xprv_b58, $key->toExtendedPrivateKey(), 'correct xprv');
Expand Down Expand Up @@ -529,4 +532,27 @@ function () {
$this->assertEquals(2, $this->HK_run_count);
$this->assertEquals(gmp_strval($math->add($k, gmp_init(1)), 10), gmp_strval($child->getPrivateKey()->getSecret(), 10));
}

/**
* @dataProvider getEcAdapters
* @param EcAdapterInterface $ecAdapter
* @throws \Exception
*/
public function testExposesScriptDataFactory(EcAdapterInterface $ecAdapter)
{
$factories = [
new P2pkScriptDataFactory(),
new P2shScriptDecorator(new P2pkScriptDataFactory()),
new P2wshScriptDecorator(new P2pkScriptDataFactory()),
new P2shP2wshScriptDecorator(new P2pkScriptDataFactory()),
];

$priv = PrivateKeyFactory::fromHex('0100000001000000010000000100000001000000010000000100000001000000', true, $ecAdapter);
$chain = new Buffer('00', 32);
foreach ($factories as $factory) {
$hd = new \BitWasp\Bitcoin\Key\Deterministic\HierarchicalKey($ecAdapter, $factory, 0, 0, 0, $chain, $priv);

$this->assertSame($factory, $hd->getScriptDataFactory());
}
}
}

0 comments on commit a8a46dc

Please sign in to comment.