Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] BC Break: NetworkParams in Wallet and Network in other places #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3,6 +3,7 @@
namespace Blocktrail\SDK;

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

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

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

protected $identifier;

protected $backupInfo;
Expand All @@ -45,7 +51,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 @@ -61,6 +67,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 @@ -77,7 +84,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(HierarchicalKeyFactory::fromExtended($key[0]), $key[1]);
$key = $key instanceof BIP32Key ? $key : BIP32Key::create(HierarchicalKeyFactory::fromExtended($key[0], $this->network), $key[1]);

$qrCode = new QrCode();
$qrCode
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