Skip to content

Commit

Permalink
BC Break: NetworkParams in Wallet and Network in other places
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kerin committed Nov 24, 2017
1 parent 2f4595a commit 4809295
Show file tree
Hide file tree
Showing 21 changed files with 452 additions and 280 deletions.
4 changes: 2 additions & 2 deletions examples/custom_tx_payment_api_example_force_from.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
// add UTXOs to txbuilder
foreach ($utxos['data'] as $utxo) {
$scriptPubKey = ScriptFactory::fromHex($utxo['script_hex']);
$address = AddressFactory::fromString($utxo['address']);
$path = $wallet->getPathForAddress($address->getAddress());
$address = $utxo['address'];
$path = $wallet->getPathForAddress($address);
$scripts = $wallet->getWalletScriptByPath($path);
$redeemScript = $scripts->getRedeemScript();
$witnessScript = null;
Expand Down
4 changes: 2 additions & 2 deletions examples/wallet_cpfp.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@
// add UTXOs to txbuilder
foreach ($utxos as $utxo) {
$scriptPubKey = ScriptFactory::fromHex($utxo['scriptpubkey_hex']);
$address = AddressFactory::fromString($utxo['address']);
$path = $wallet->getPathForAddress($address->getAddress());
$address = $utxo['address'];
$path = $wallet->getPathForAddress($address);
$scripts = $wallet->getWalletScriptByPath($path);
$redeemScript = $scripts->getRedeemScript();
$witnessScript = null;
Expand Down
11 changes: 9 additions & 2 deletions src/BackupGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Blocktrail\SDK;

use BitWasp\Bitcoin\Network\NetworkInterface;
use Blocktrail\SDK\Bitcoin\BIP32Key;
use Endroid\QrCode\QrCode;

Expand All @@ -26,6 +27,11 @@ class BackupGenerator {
*/
protected $blocktrailPubKeyQRs = [];

/**
* @var NetworkInterface
*/
protected $network;

protected $identifier;

protected $backupInfo;
Expand All @@ -44,7 +50,7 @@ class BackupGenerator {
* @param array $extra
* @param null $options
*/
public function __construct($identifier, $backupInfo, $extra = null, $options = null) {
public function __construct(NetworkInterface $network, $identifier, $backupInfo, $extra = null, $options = null) {
/*
* if DOMPDF is not already loaded we have to do it
* they require a config file to be loaded, no autoloading :/
Expand All @@ -60,6 +66,7 @@ public function __construct($identifier, $backupInfo, $extra = null, $options =
//set the fonts path
$this->fontsPath = dirname(__FILE__) . '/../resources/fonts';

$this->network = $network;
$this->identifier = $identifier;
$this->backupInfo = $backupInfo;
$this->extra = $extra ?: [];
Expand All @@ -76,7 +83,7 @@ protected function processBlocktrailPubKeys() {

//create QR codes for each blocktrail pub key
foreach ($this->backupInfo['blocktrail_public_keys'] as $keyIndex => $key) {
$key = $key instanceof BIP32Key ? $key : BIP32Key::create($key);
$key = $key instanceof BIP32Key ? $key : BIP32Key::create($this->network, $key);
$qrCode = new QrCode();
$qrCode
->setText($key->key())
Expand Down
46 changes: 36 additions & 10 deletions src/Bitcoin/BIP32Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Blocktrail\SDK\Bitcoin;

use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PublicKey;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKey;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory;
use BitWasp\Bitcoin\Network\NetworkInterface;

/**
* Class BIP32Key
Expand All @@ -22,6 +23,11 @@ class BIP32Key {
*/
private $path;

/**
* @var NetworkInterface
*/
private $network;

/**
* @var string|null
*/
Expand All @@ -33,13 +39,15 @@ class BIP32Key {
private $derivations = [];

/**
* @param HierarchicalKey $key
* @param string|null $path
* @param HierarchicalKey $key
* @param NetworkInterface $network
* @param string|null $path
* @throws \Exception
*/
public function __construct(HierarchicalKey $key, $path = null) {
public function __construct(NetworkInterface $network, HierarchicalKey $key, $path = null) {
$this->key = $key;
$this->path = BIP32Path::path($path);
$this->network = $network;

return;

Expand All @@ -57,12 +65,30 @@ public function __construct(HierarchicalKey $key, $path = null) {
/**
* static method to initialize class
*
* @param HierarchicalKey $key
* @param string|null $path
* @param NetworkInterface $network
* @param HierarchicalKey $key
* @param string|null $path
* @return BIP32Key
*/
public static function create(NetworkInterface $network, HierarchicalKey $key, $path = null) {
return new BIP32Key($network, $key, $path);
}

/**
* @param NetworkInterface $network
* @param string $key
* @param string|null $path
* @return BIP32Key
*/
public static function create(HierarchicalKey $key, $path = null) {
return new BIP32Key($key, $path);
public static function fromString(NetworkInterface $network, $key, $path = null) {
return static::create($network, HierarchicalKeyFactory::fromExtended($key, $network), $path);
}

/**
* @return NetworkInterface
*/
public function network() {
return $this->network;
}

/**
Expand Down Expand Up @@ -113,7 +139,7 @@ public function bip32Path() {
}

public function tuple() {
return [$this->key->toExtendedKey(), (string)$this->path];
return [$this->key->toExtendedKey($this->network), (string)$this->path];
}

/**
Expand Down Expand Up @@ -151,7 +177,7 @@ public function buildKey($path) {
$key = $key->toPublic();
}

$this->derivations[$originalPath] = BIP32Key::create($key, $originalPath);
$this->derivations[$originalPath] = BIP32Key::create($this->network, $key, $originalPath);
}

return $this->derivations[$originalPath];
Expand Down
Loading

0 comments on commit 4809295

Please sign in to comment.