From 5c3dc038adc177a348b20e31627ef69c214299e9 Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Thu, 15 Mar 2018 01:17:42 +0100 Subject: [PATCH] bump bitcoin-php to 0.0.35 --- composer.json | 4 +- src/Address/AddressReaderBase.php | 46 -------- src/Address/Base32AddressInterface.php | 15 --- src/Address/BitcoinAddressReader.php | 81 ------------- src/Address/BitcoinCashAddressReader.php | 123 -------------------- src/Address/CashAddress.php | 103 ---------------- src/Bitcoin/BIP32Key.php | 2 +- src/BlocktrailSDK.php | 44 +++---- src/Network/AbstractBitcoinCash.php | 41 ------- src/Network/BitcoinCash.php | 16 --- src/Network/BitcoinCashNetworkInterface.php | 11 -- src/Network/BitcoinCashRegtest.php | 16 --- src/Network/BitcoinCashTestnet.php | 16 --- src/OutputsNormalizer.php | 8 +- src/TransactionBuilder.php | 8 +- src/Wallet.php | 23 ++-- src/WalletInterface.php | 4 +- src/WalletSweeper.php | 43 +++---- src/WalletV1.php | 9 +- src/WalletV2.php | 9 +- src/WalletV3.php | 9 +- tests/BitcoinCashAddressTest.php | 26 ++--- tests/Network/BitcoinCashTest.php | 66 +---------- tests/OutputsNormalizerTest.php | 20 ++-- tests/WalletTest.php | 46 +++++--- 25 files changed, 137 insertions(+), 652 deletions(-) delete mode 100644 src/Address/AddressReaderBase.php delete mode 100644 src/Address/Base32AddressInterface.php delete mode 100644 src/Address/BitcoinAddressReader.php delete mode 100644 src/Address/BitcoinCashAddressReader.php delete mode 100644 src/Address/CashAddress.php delete mode 100644 src/Network/AbstractBitcoinCash.php delete mode 100644 src/Network/BitcoinCash.php delete mode 100644 src/Network/BitcoinCashNetworkInterface.php delete mode 100644 src/Network/BitcoinCashRegtest.php delete mode 100644 src/Network/BitcoinCashTestnet.php diff --git a/composer.json b/composer.json index 8b60641..1c20b3e 100644 --- a/composer.json +++ b/composer.json @@ -33,10 +33,12 @@ "ext-dom": "*", "ext-curl": "*", - "bitwasp/bitcoin": "v0.0.34.1", + "bitwasp/bitcoin": "v0.0.35", + "btccom/bitwasp-bitcoin-bch-addon": "v0.0.2", "btccom/justencrypt": "v0.2.0", "btccom/cashaddress": "v0.0.3", "mdanter/ecc": "v0.4.*", + "symfony/http-foundation": "^2.7|^3.0|^4.0", "guzzlehttp/guzzle": "6.*", "99designs/http-signatures-guzzlehttp": "2.0.*", diff --git a/src/Address/AddressReaderBase.php b/src/Address/AddressReaderBase.php deleted file mode 100644 index a5f003d..0000000 --- a/src/Address/AddressReaderBase.php +++ /dev/null @@ -1,46 +0,0 @@ -slice(0, 1)->getHex(); - - if ($prefixByte === $network->getP2shByte()) { - return new ScriptHashAddress($data->slice(1)); - } else if ($prefixByte === $network->getAddressByte()) { - return new PayToPubKeyHashAddress($data->slice(1)); - } - } catch (\Exception $e) { - } - return null; - } - - /** - * @param $strAddress - * @param NetworkInterface|null $network - * @return AddressInterface - */ - abstract public function fromString($strAddress, NetworkInterface $network = null); - - /** - * @param ScriptInterface $script - * @return AddressInterface - */ - abstract public function fromOutputScript(ScriptInterface $script); -} diff --git a/src/Address/Base32AddressInterface.php b/src/Address/Base32AddressInterface.php deleted file mode 100644 index 6528255..0000000 --- a/src/Address/Base32AddressInterface.php +++ /dev/null @@ -1,15 +0,0 @@ -isWitness($wp)) { - /** @var WitnessProgram $wp */ - return new SegwitAddress($wp); - } - - $decode = (new OutputClassifier())->decode($outputScript); - switch ($decode->getType()) { - case ScriptType::P2PKH: - /** @var BufferInterface $solution */ - return new PayToPubKeyHashAddress($decode->getSolution()); - case ScriptType::P2SH: - /** @var BufferInterface $solution */ - return new ScriptHashAddress($decode->getSolution()); - default: - throw new \RuntimeException('Script type is not associated with an address'); - } - } - - /** - * @param string $strAddress - * @param NetworkInterface|null $network - * @return Base58AddressInterface|SegwitAddress|null - * @throws BlocktrailSDKException - */ - public function fromString($strAddress, NetworkInterface $network = null) { - $network = $network ?: Bitcoin::getNetwork(); - - if (($base58Address = $this->readBase58($strAddress, $network))) { - return $base58Address; - } - - if (($bech32Address = $this->readSegwitAddress($strAddress, $network))) { - return $bech32Address; - } - - throw new BlocktrailSDKException("Address not recognized"); - } -} diff --git a/src/Address/BitcoinCashAddressReader.php b/src/Address/BitcoinCashAddressReader.php deleted file mode 100644 index 06c1bbf..0000000 --- a/src/Address/BitcoinCashAddressReader.php +++ /dev/null @@ -1,123 +0,0 @@ -useNewCashAddress = (bool) $useNewCashAddress; - } - - /** - * @param string $strAddress - * @param BitcoinCashNetworkInterface $network - * @return CashAddress|null - */ - protected function readCashAddress($strAddress, BitcoinCashNetworkInterface $network) { - try { - list ($prefix, $scriptType, $hash) = \CashAddr\CashAddress::decode($strAddress); - if ($prefix !== $network->getCashAddressPrefix()) { - return null; - } - if (!($scriptType === ScriptType::P2PKH || $scriptType === ScriptType::P2SH)) { - return null; - } - - return new CashAddress($scriptType, new Buffer($hash, 20)); - } catch (\Exception $e) { - // continue on - } - - try { - list ($prefix, $scriptType, $hash) = \CashAddr\CashAddress::decode( - sprintf("%s:%s", $network->getCashAddressPrefix(), $strAddress) - ); - - if ($prefix !== $network->getCashAddressPrefix()) { - return null; - } - if (!($scriptType === ScriptType::P2PKH || $scriptType === ScriptType::P2SH)) { - return null; - } - - return new CashAddress($scriptType, new Buffer($hash, 20)); - } catch (\Exception $e) { - // continue on - } - - return null; - } - - /** - * @param string $strAddress - * @param NetworkInterface|null $network - * @return Base58AddressInterface|CashAddress - * @throws BlocktrailSDKException - */ - public function fromString($strAddress, NetworkInterface $network = null) { - $network = $network ?: Bitcoin::getNetwork(); - - if (($base58Address = $this->readBase58($strAddress, $network))) { - return $base58Address; - } - - if ($this->useNewCashAddress && $network instanceof BitcoinCashNetworkInterface) { - if (($base32Address = $this->readCashAddress($strAddress, $network))) { - return $base32Address; - } - } - - throw new BlocktrailSDKException("Address not recognized"); - } - - /** - * @param ScriptInterface $script - * @return Base58AddressInterface|CashAddress - */ - public function fromOutputScript(ScriptInterface $script) { - $decode = (new OutputClassifier())->decode($script); - - switch ($decode->getType()) { - case ScriptType::P2PKH: - /** @var BufferInterface $solution */ - if ($this->useNewCashAddress) { - return new CashAddress(ScriptType::P2PKH, $decode->getSolution()); - } else { - return new PayToPubKeyHashAddress($decode->getSolution()); - } - break; - case ScriptType::P2SH: - /** @var BufferInterface $solution */ - if ($this->useNewCashAddress) { - return new CashAddress(ScriptType::P2SH, $decode->getSolution()); - } else { - return new ScriptHashAddress($decode->getSolution()); - } - break; - default: - throw new \RuntimeException('Script type is not associated with an address'); - } - } -} diff --git a/src/Address/CashAddress.php b/src/Address/CashAddress.php deleted file mode 100644 index 1875f0a..0000000 --- a/src/Address/CashAddress.php +++ /dev/null @@ -1,103 +0,0 @@ -type = $type; - - parent::__construct($hash); - } - - /** - * @param BitcoinCashNetworkInterface $network - * @return string - */ - public function getPrefix(BitcoinCashNetworkInterface $network) { - return $network->getCashAddressPrefix(); - } - - /** - * @return string - */ - public function getType() { - return $this->type; - } - - /** - * @param NetworkInterface|null $network - * @return string - * @throws BlocktrailSDKException - * @throws \CashAddr\Exception\Base32Exception - * @throws \CashAddr\Exception\CashAddressException - */ - public function getAddress(NetworkInterface $network = null) { - if (null === $network) { - $network = Bitcoin::getNetwork(); - } - - if (!($network instanceof BitcoinCashNetworkInterface)) { - throw new BlocktrailSDKException("Invalid network - must implement BitcoinCashNetworkInterface"); - } - - return \CashAddr\CashAddress::encode( - $network->getCashAddressPrefix(), - $this->type, - $this->hash->getBinary() - ); - } - - /** - * @return PayToPubKeyHashAddress|ScriptHashAddress - */ - public function getLegacyAddress() { - if ($this->type === ScriptType::P2PKH) { - return new PayToPubKeyHashAddress($this->hash); - } else { - return new ScriptHashAddress($this->hash); - } - } - - /** - * @return \BitWasp\Bitcoin\Script\ScriptInterface - */ - public function getScriptPubKey() { - if ($this->type === ScriptType::P2PKH) { - return ScriptFactory::scriptPubKey()->p2pkh($this->hash); - } else { - return ScriptFactory::scriptPubKey()->p2sh($this->hash); - } - } -} diff --git a/src/Bitcoin/BIP32Key.php b/src/Bitcoin/BIP32Key.php index 1cb7ba3..6d39e70 100644 --- a/src/Bitcoin/BIP32Key.php +++ b/src/Bitcoin/BIP32Key.php @@ -148,7 +148,7 @@ public function buildKey($path) { } if ($toPublic) { - $key = $key->toPublic(); + $key = $key->withoutPrivateKey(); } $this->derivations[$originalPath] = BIP32Key::create($key, $originalPath); diff --git a/src/BlocktrailSDK.php b/src/BlocktrailSDK.php index 7fe53c5..2cccd36 100644 --- a/src/BlocktrailSDK.php +++ b/src/BlocktrailSDK.php @@ -2,6 +2,10 @@ namespace Blocktrail\SDK; +use Btccom\BitcoinCash\Address\AddressCreator as BitcoinCashAddressCreator; +use Btccom\BitcoinCash\Address\CashAddress; +use Btccom\BitcoinCash\Network\NetworkFactory as BitcoinCashNetworkFactory; +use BitWasp\Bitcoin\Address\AddressCreator as BitcoinAddressCreator; use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress; use BitWasp\Bitcoin\Bitcoin; use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer; @@ -14,22 +18,16 @@ use BitWasp\Bitcoin\MessageSigner\SignedMessage; use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator; use BitWasp\Bitcoin\Mnemonic\MnemonicFactory; -use BitWasp\Bitcoin\Network\NetworkFactory; +use BitWasp\Bitcoin\Network\NetworkFactory as BitcoinNetworkFactory; use BitWasp\Bitcoin\Transaction\TransactionFactory; use BitWasp\Buffertools\Buffer; use BitWasp\Buffertools\BufferInterface; use Blocktrail\CryptoJSAES\CryptoJSAES; -use Blocktrail\SDK\Address\AddressReaderBase; -use Blocktrail\SDK\Address\BitcoinAddressReader; -use Blocktrail\SDK\Address\BitcoinCashAddressReader; -use Blocktrail\SDK\Address\CashAddress; +use BitWasp\Bitcoin\Address\BaseAddressCreator; use Blocktrail\SDK\Bitcoin\BIP32Key; use Blocktrail\SDK\Connection\RestClient; use Blocktrail\SDK\Exceptions\BlocktrailSDKException; -use Blocktrail\SDK\Network\BitcoinCash; use Blocktrail\SDK\Connection\RestClientInterface; -use Blocktrail\SDK\Network\BitcoinCashRegtest; -use Blocktrail\SDK\Network\BitcoinCashTestnet; use Btccom\JustEncrypt\Encryption; use Btccom\JustEncrypt\EncryptionMnemonic; use Btccom\JustEncrypt\KeyDerivation; @@ -102,19 +100,19 @@ protected function setBitcoinLibMagicBytes($network, $testnet, $regtest) { if ($network === "bitcoin") { if ($regtest) { - $useNetwork = NetworkFactory::bitcoinRegtest(); + $useNetwork = BitcoinNetworkFactory::bitcoinRegtest(); } else if ($testnet) { - $useNetwork = NetworkFactory::bitcoinTestnet(); + $useNetwork = BitcoinNetworkFactory::bitcoinTestnet(); } else { - $useNetwork = NetworkFactory::bitcoin(); + $useNetwork = BitcoinNetworkFactory::bitcoin(); } } else if ($network === "bitcoincash") { if ($regtest) { - $useNetwork = new BitcoinCashRegtest(); + $useNetwork = BitcoinCashNetworkFactory::bitcoinCashRegtest(); } else if ($testnet) { - $useNetwork = new BitcoinCashTestnet(); + $useNetwork = BitcoinCashNetworkFactory::bitcoinCashTestnet(); } else { - $useNetwork = new BitcoinCash(); + $useNetwork = BitcoinCashNetworkFactory::bitcoinCash(); } } @@ -655,11 +653,11 @@ protected function createNewWalletV1($options) { } } else { $backupPrivateKey = HierarchicalKeyFactory::fromEntropy((new Bip39SeedGenerator())->getSeed($backupMnemonic, "")); - $backupPublicKey = BIP32Key::create($backupPrivateKey->toPublic(), "M"); + $backupPublicKey = BIP32Key::create($backupPrivateKey->withoutPrivateKey(), "M"); } // create a checksum of our private key which we'll later use to verify we used the right password - $checksum = $primaryPrivateKey->getPublicKey()->getAddress()->getAddress(); + $checksum = $primaryPrivateKey->getAddress(new BitcoinAddressCreator())->getAddress(); $addressReader = $this->makeAddressReader($options); // send the public keys to the server to store them @@ -780,7 +778,7 @@ protected function createNewWalletV2($options) { } // create a checksum of our private key which we'll later use to verify we used the right password - $checksum = $options['primary_private_key']->publicKey()->getAddress()->getAddress(); + $checksum = $options['primary_private_key']->key()->getAddress(new BitcoinAddressCreator())->getAddress(); $addressReader = $this->makeAddressReader($options); // send the public keys and encrypted data to server @@ -925,7 +923,7 @@ protected function createNewWalletV3($options) { } // create a checksum of our private key which we'll later use to verify we used the right password - $checksum = $options['primary_private_key']->publicKey()->getAddress()->getAddress(); + $checksum = $options['primary_private_key']->key()->getAddress(new BitcoinAddressCreator())->getAddress(); $addressReader = $this->makeAddressReader($options); // send the public keys and encrypted data to server @@ -1064,6 +1062,7 @@ public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKe 'segwit' => $segwit, ]; $this->verifyPublicOnly($data); + $response = $this->client->post("wallet", null, $data, RestClient::AUTH_HTTP_SIG); return self::jsonDecode($response->body(), true); } @@ -1124,7 +1123,7 @@ public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { /** * @param array $options - * @return AddressReaderBase + * @return BaseAddressCreator */ private function makeAddressReader(array $options) { if ($this->network == "bitcoincash") { @@ -1132,9 +1131,9 @@ private function makeAddressReader(array $options) { if (array_key_exists("use_cashaddress", $options) && $options['use_cashaddress']) { $useCashAddress = true; } - return new BitcoinCashAddressReader($useCashAddress); + return new BitcoinCashAddressCreator($useCashAddress); } else { - return new BitcoinAddressReader(); + return new BitcoinAddressCreator(); } } @@ -1767,7 +1766,8 @@ public function verifyMessage($message, $address, $signature) { // $this->client->post("verify_message", null, ['message' => $message, 'address' => $address, 'signature' => $signature])['result']; $adapter = Bitcoin::getEcAdapter(); - $addr = \BitWasp\Bitcoin\Address\AddressFactory::fromString($address); + $addressCreator = new BitcoinAddressCreator(); + $addr = $addressCreator->fromString($address); if (!$addr instanceof PayToPubKeyHashAddress) { throw new \RuntimeException('Can only verify a message with a pay-to-pubkey-hash address'); } diff --git a/src/Network/AbstractBitcoinCash.php b/src/Network/AbstractBitcoinCash.php deleted file mode 100644 index edb8f68..0000000 --- a/src/Network/AbstractBitcoinCash.php +++ /dev/null @@ -1,41 +0,0 @@ -getAddressByte(), - $base->getP2shByte(), - $base->getPrivByte(), - $base->isTestnet() - ); - - $this->setHDPrivByte($base->getHDPrivByte()); - $this->setHDPubByte($base->getHDPubByte()); - $this->setNetMagicBytes($base->getNetMagicBytes()); - $this->cashAddressPrefix = $cashAddressPrefix; - } - - /** - * @return string - */ - public function getCashAddressPrefix() { - return $this->cashAddressPrefix; - } -} diff --git a/src/Network/BitcoinCash.php b/src/Network/BitcoinCash.php deleted file mode 100644 index bc8543d..0000000 --- a/src/Network/BitcoinCash.php +++ /dev/null @@ -1,16 +0,0 @@ -reader = $reader; } diff --git a/src/TransactionBuilder.php b/src/TransactionBuilder.php index 5c00824..57509fb 100644 --- a/src/TransactionBuilder.php +++ b/src/TransactionBuilder.php @@ -3,10 +3,10 @@ namespace Blocktrail\SDK; use BitWasp\Bitcoin\Address\AddressInterface; +use BitWasp\Bitcoin\Address\BaseAddressCreator; use BitWasp\Bitcoin\Script\ScriptFactory; use BitWasp\Bitcoin\Script\ScriptInterface; use BitWasp\Buffertools\Buffer; -use Blocktrail\SDK\Address\AddressReaderBase; use Blocktrail\SDK\Exceptions\BlocktrailSDKException; /** @@ -38,7 +38,7 @@ class TransactionBuilder { private $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL; /** - * @var AddressReaderBase + * @var BaseAddressCreator */ private $addressReader; @@ -49,9 +49,9 @@ class TransactionBuilder { /** * TransactionBuilder constructor. - * @param AddressReaderBase $addressReader + * @param BaseAddressCreator $addressReader */ - public function __construct(AddressReaderBase $addressReader) { + public function __construct(BaseAddressCreator $addressReader) { $this->addressReader = $addressReader; $this->outputNormalizer = new OutputsNormalizer($this->addressReader); } diff --git a/src/Wallet.php b/src/Wallet.php index 6ec1f5c..4459ec1 100644 --- a/src/Wallet.php +++ b/src/Wallet.php @@ -2,7 +2,11 @@ namespace Blocktrail\SDK; +use Btccom\BitcoinCash\Transaction\SignatureHash\SigHash as BchSigHash; +use Btccom\BitcoinCash\Transaction\Factory\Checker\CheckerCreator as BchCheckerCreator; +use BitWasp\Bitcoin\Address\AddressCreator; use BitWasp\Bitcoin\Address\Base58AddressInterface; +use BitWasp\Bitcoin\Address\BaseAddressCreator; use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress; use BitWasp\Bitcoin\Address\ScriptHashAddress; use BitWasp\Bitcoin\Bitcoin; @@ -21,8 +25,7 @@ use BitWasp\Bitcoin\Transaction\Transaction; use BitWasp\Bitcoin\Transaction\TransactionInterface; use BitWasp\Buffertools\Buffer; -use Blocktrail\SDK\Address\AddressReaderBase; -use Blocktrail\SDK\Address\CashAddress; +use Btccom\BitcoinCash\Address\CashAddress; use Blocktrail\SDK\Bitcoin\BIP32Key; use Blocktrail\SDK\Bitcoin\BIP32Path; use Blocktrail\SDK\Exceptions\BlocktrailSDKException; @@ -155,7 +158,7 @@ abstract class Wallet implements WalletInterface { protected $changeIndex; /** - * @var AddressReaderBase + * @var BaseAddressCreator */ protected $addressReader; @@ -178,7 +181,7 @@ abstract class Wallet implements WalletInterface { * @param string $checksum * @throws BlocktrailSDKException */ - public function __construct(BlocktrailSDKInterface $sdk, $identifier, array $primaryPublicKeys, $backupPublicKey, array $blocktrailPublicKeys, $keyIndex, $network, $testnet, $segwit, AddressReaderBase $addressReader, $checksum) { + public function __construct(BlocktrailSDKInterface $sdk, $identifier, array $primaryPublicKeys, $backupPublicKey, array $blocktrailPublicKeys, $keyIndex, $network, $testnet, $segwit, BaseAddressCreator $addressReader, $checksum) { $this->sdk = $sdk; $this->identifier = $identifier; @@ -214,7 +217,7 @@ public function __construct(BlocktrailSDKInterface $sdk, $identifier, array $pri } /** - * @return AddressReaderBase + * @return BaseAddressCreator */ public function getAddressReader() { return $this->addressReader; @@ -1065,7 +1068,8 @@ protected function determineChange($utxos, $outputs, $fee) { * @throws \Exception */ protected function signTransaction(Transaction $tx, array $signInfo) { - $signer = new Signer($tx, Bitcoin::getEcAdapter()); + $adapter = Bitcoin::getEcAdapter(); + $signer = new Signer($tx, $adapter); assert(Util::all(function ($signInfo) { return $signInfo instanceof SignInfo; @@ -1073,8 +1077,8 @@ protected function signTransaction(Transaction $tx, array $signInfo) { $sigHash = SigHash::ALL; if ($this->network === "bitcoincash") { - $sigHash |= SigHash::BITCOINCASH; - $signer->redeemBitcoinCash(true); + $sigHash |= BchSigHash::BITCOINCASH; + $signer->setCheckerCreator(BchCheckerCreator::fromEcAdapter($adapter)); } foreach ($signInfo as $idx => $info) { @@ -1195,8 +1199,7 @@ public function deleteWallet($force = false) { protected function createChecksumVerificationSignature() { $privKey = $this->primaryPrivateKey->key(); - $pubKey = $this->primaryPrivateKey->publicKey(); - $address = $pubKey->getAddress()->getAddress(); + $address = $this->primaryPrivateKey->key()->getAddress(new AddressCreator())->getAddress(); $signer = new MessageSigner(Bitcoin::getEcAdapter()); $signed = $signer->sign($address, $privKey->getPrivateKey()); diff --git a/src/WalletInterface.php b/src/WalletInterface.php index 33cb239..dee4329 100644 --- a/src/WalletInterface.php +++ b/src/WalletInterface.php @@ -2,7 +2,7 @@ namespace Blocktrail\SDK; -use Blocktrail\SDK\Address\AddressReaderBase; +use BitWasp\Bitcoin\Address\BaseAddressCreator; use Blocktrail\SDK\Bitcoin\BIP32Key; use Blocktrail\SDK\Bitcoin\BIP32Path; use Blocktrail\SDK\Exceptions\BlocktrailSDKException; @@ -33,7 +33,7 @@ public function getIdentifier(); public function getBackupKey(); /** - * @return AddressReaderBase + * @return BaseAddressCreator */ public function getAddressReader(); diff --git a/src/WalletSweeper.php b/src/WalletSweeper.php index 09ff444..b8abad3 100644 --- a/src/WalletSweeper.php +++ b/src/WalletSweeper.php @@ -2,10 +2,16 @@ namespace Blocktrail\SDK; -use BitWasp\Bitcoin\Address\AddressFactory; +use Btccom\BitcoinCash\Address\CashAddress; +use Btccom\BitcoinCash\Address\AddressCreator as BitcoinCashAddressCreator; +use Btccom\BitcoinCash\Network\NetworkFactory as BitcoinCashNetworkFactory; +use Btccom\BitcoinCash\Transaction\Factory\Checker\CheckerCreator as BchCheckerCreator; +use Btccom\BitcoinCash\Transaction\SignatureHash\SigHash as BchSigHash; +use BitWasp\Bitcoin\Address\AddressCreator as BitcoinAddressCreator; +use BitWasp\Bitcoin\Address\BaseAddressCreator; use BitWasp\Bitcoin\Bitcoin; use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory; -use BitWasp\Bitcoin\Network\NetworkFactory; +use BitWasp\Bitcoin\Network\NetworkFactory as BitcoinNetworkFactory; use BitWasp\Bitcoin\Script\P2shScript; use BitWasp\Bitcoin\Script\ScriptFactory; use BitWasp\Bitcoin\Script\WitnessScript; @@ -18,15 +24,9 @@ use BitWasp\Bitcoin\Transaction\TransactionOutput; use BitWasp\Buffertools\Buffer; use BitWasp\Buffertools\BufferInterface; -use Blocktrail\SDK\Address\AddressReaderBase; -use Blocktrail\SDK\Address\BitcoinAddressReader; -use Blocktrail\SDK\Address\BitcoinCashAddressReader; -use Blocktrail\SDK\Address\CashAddress; use Blocktrail\SDK\Bitcoin\BIP32Key; use Blocktrail\SDK\Bitcoin\BIP32Path; use Blocktrail\SDK\Exceptions\BlocktrailSDKException; -use Blocktrail\SDK\Network\BitcoinCash; -use Blocktrail\SDK\Network\BitcoinCashRegtest; abstract class WalletSweeper { @@ -73,7 +73,7 @@ abstract class WalletSweeper { protected $sweepData; /** - * @var AddressReaderBase + * @var BaseAddressCreator */ protected $addressReader; @@ -101,6 +101,7 @@ public function __construct(BufferInterface $primarySeed, BufferInterface $backu "use_cashaddress" => false, ]); + //create BIP32 keys for the Blocktrail public keys foreach ($blocktrailPublicKeys as $blocktrailKey) { $this->blocktrailPublicKeys[$blocktrailKey['keyIndex']] = BlocktrailSDK::normalizeBIP32Key([$blocktrailKey['pubkey'], $blocktrailKey['path']]); @@ -114,7 +115,7 @@ public function __construct(BufferInterface $primarySeed, BufferInterface $backu /** * @param array $options - * @return AddressReaderBase + * @return BaseAddressCreator */ private function makeAddressReader(array $options) { if ($this->network == "bitcoincash") { @@ -122,9 +123,9 @@ private function makeAddressReader(array $options) { if (array_key_exists("use_cashaddress", $options) && $options['use_cashaddress']) { $useCashAddress = true; } - return new BitcoinCashAddressReader($useCashAddress); + return new BitcoinCashAddressCreator($useCashAddress); } else { - return new BitcoinAddressReader(); + return new BitcoinAddressCreator(); } } @@ -138,19 +139,19 @@ protected function setBitcoinLibMagicBytes($network, $testnet, $regtest) { assert($network == "bitcoin" || $network == "bitcoincash"); if ($network === "bitcoin") { if ($regtest) { - $useNetwork = NetworkFactory::bitcoinRegtest(); + $useNetwork = BitcoinNetworkFactory::bitcoinRegtest(); } else if ($testnet) { - $useNetwork = NetworkFactory::bitcoinTestnet(); + $useNetwork = BitcoinNetworkFactory::bitcoinTestnet(); } else { - $useNetwork = NetworkFactory::bitcoin(); + $useNetwork = BitcoinNetworkFactory::bitcoin(); } } else if ($network === "bitcoincash") { if ($regtest) { - $useNetwork = new BitcoinCashRegtest(); + $useNetwork = BitcoinCashNetworkFactory::bitcoinCashRegtest(); } else if ($testnet) { - $useNetwork = new BitcoinCashTestnet(); + $useNetwork = BitcoinCashNetworkFactory::bitcoinCashTestnet(); } else { - $useNetwork = new BitcoinCash(); + $useNetwork = BitcoinCashNetworkFactory::bitcoinCash(); } } @@ -380,7 +381,7 @@ protected function createTransaction($destinationAddress) { $utxo['hash'], $utxo['index'], $utxo['value'], - AddressFactory::fromString($address), + $this->addressReader->fromString($address), ScriptFactory::fromHex($utxo['script_hex']), $data['path'], $data['redeem'], @@ -435,8 +436,8 @@ protected function signTransaction(TransactionInterface $tx, array $signInfo) { $sigHash = SigHash::ALL; if ($this->network === "bitcoincash") { - $sigHash |= SigHash::BITCOINCASH; - $signer->redeemBitcoinCash(true); + $sigHash |= BchSigHash::BITCOINCASH; + $signer->setCheckerCreator(BchCheckerCreator::fromEcAdapter(Bitcoin::getEcAdapter())); } assert(Util::all(function ($signInfo) { diff --git a/src/WalletV1.php b/src/WalletV1.php index 78e6bf2..9eb7bce 100644 --- a/src/WalletV1.php +++ b/src/WalletV1.php @@ -2,10 +2,11 @@ namespace Blocktrail\SDK; +use BitWasp\Bitcoin\Address\AddressCreator; +use BitWasp\Bitcoin\Address\BaseAddressCreator; use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKey; use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory; use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator; -use Blocktrail\SDK\Address\AddressReaderBase; use Blocktrail\SDK\Bitcoin\BIP32Key; use Blocktrail\SDK\Exceptions\BlocktrailSDKException; use Blocktrail\SDK\Exceptions\NotImplementedException; @@ -30,11 +31,11 @@ class WalletV1 extends Wallet { * @param string $network * @param bool $testnet * @param bool $segwit - * @param AddressReaderBase $addressReader + * @param BaseAddressCreator $addressReader * @param string $checksum * @throws BlocktrailSDKException */ - public function __construct(BlocktrailSDKInterface $sdk, $identifier, $primaryMnemonic, array $primaryPublicKeys, $backupPublicKey, array $blocktrailPublicKeys, $keyIndex, $network, $testnet, $segwit, AddressReaderBase $addressReader, $checksum) { + public function __construct(BlocktrailSDKInterface $sdk, $identifier, $primaryMnemonic, array $primaryPublicKeys, $backupPublicKey, array $blocktrailPublicKeys, $keyIndex, $network, $testnet, $segwit, BaseAddressCreator $addressReader, $checksum) { $this->primaryMnemonic = $primaryMnemonic; parent::__construct($sdk, $identifier, $primaryPublicKeys, $backupPublicKey, $blocktrailPublicKeys, $keyIndex, $network, $testnet, $segwit, $addressReader, $checksum); @@ -80,7 +81,7 @@ public function unlock($options, callable $fn = null) { $this->primaryPrivateKey = BIP32Key::create($primaryPrivateKey, "m"); // create checksum (address) of the primary privatekey to compare to the stored checksum - $checksum = $this->primaryPrivateKey->key()->getPublicKey()->getAddress()->getAddress(); + $checksum = $this->primaryPrivateKey->key()->getAddress(new AddressCreator())->getAddress(); if ($checksum != $this->checksum) { throw new \Exception("Checksum [{$checksum}] does not match [{$this->checksum}], most likely due to incorrect password"); } diff --git a/src/WalletV2.php b/src/WalletV2.php index 769c770..6d2f63a 100644 --- a/src/WalletV2.php +++ b/src/WalletV2.php @@ -2,12 +2,13 @@ namespace Blocktrail\SDK; +use BitWasp\Bitcoin\Address\AddressCreator; +use BitWasp\Bitcoin\Address\BaseAddressCreator; use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKey; use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory; use BitWasp\Bitcoin\Mnemonic\MnemonicFactory; use BitWasp\Buffertools\Buffer; use Blocktrail\CryptoJSAES\CryptoJSAES; -use Blocktrail\SDK\Address\AddressReaderBase; use Blocktrail\SDK\Bitcoin\BIP32Key; use Blocktrail\SDK\Exceptions\BlocktrailSDKException; use Blocktrail\SDK\Exceptions\WalletDecryptException; @@ -35,10 +36,10 @@ class WalletV2 extends Wallet { * @param bool $testnet * @param bool $segwit * @param string $checksum - * @param AddressReaderBase $addressReader + * @param BaseAddressCreator $addressReader * @throws BlocktrailSDKException */ - public function __construct(BlocktrailSDKInterface $sdk, $identifier, $encryptedPrimarySeed, $encryptedSecret, $primaryPublicKeys, $backupPublicKey, $blocktrailPublicKeys, $keyIndex, $network, $testnet, $segwit, AddressReaderBase $addressReader, $checksum) { + public function __construct(BlocktrailSDKInterface $sdk, $identifier, $encryptedPrimarySeed, $encryptedSecret, $primaryPublicKeys, $backupPublicKey, $blocktrailPublicKeys, $keyIndex, $network, $testnet, $segwit, BaseAddressCreator $addressReader, $checksum) { $this->encryptedPrimarySeed = $encryptedPrimarySeed; $this->encryptedSecret = $encryptedSecret; @@ -98,7 +99,7 @@ public function unlock($options, callable $fn = null) { $this->primaryPrivateKey = $primaryPrivateKey instanceof BIP32Key ? $primaryPrivateKey : BIP32Key::create($primaryPrivateKey, "m"); // create checksum (address) of the primary privatekey to compare to the stored checksum - $checksum = $this->primaryPrivateKey->publicKey()->getAddress()->getAddress(); + $checksum = $this->primaryPrivateKey->key()->getAddress(new AddressCreator())->getAddress(); if ($checksum != $this->checksum) { throw new \Exception("Checksum [{$checksum}] does not match [{$this->checksum}], most likely due to incorrect password"); } diff --git a/src/WalletV3.php b/src/WalletV3.php index 6f210ed..93b44ea 100644 --- a/src/WalletV3.php +++ b/src/WalletV3.php @@ -2,10 +2,11 @@ namespace Blocktrail\SDK; +use BitWasp\Bitcoin\Address\AddressCreator; +use BitWasp\Bitcoin\Address\BaseAddressCreator; use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory; use BitWasp\Buffertools\Buffer; use BitWasp\Buffertools\BufferInterface; -use Blocktrail\SDK\Address\AddressReaderBase; use Blocktrail\SDK\Bitcoin\BIP32Key; use Blocktrail\SDK\Exceptions\BlocktrailSDKException; use Blocktrail\SDK\Exceptions\WalletDecryptException; @@ -47,11 +48,11 @@ class WalletV3 extends Wallet * @param string $network * @param bool $testnet * @param bool $segwit - * @param AddressReaderBase $addressReader + * @param BaseAddressCreator $addressReader * @param string $checksum * @throws BlocktrailSDKException */ - public function __construct(BlocktrailSDKInterface $sdk, $identifier, $encryptedPrimarySeed, $encryptedSecret, $primaryPublicKeys, $backupPublicKey, $blocktrailPublicKeys, $keyIndex, $network, $testnet, $segwit, AddressReaderBase $addressReader, $checksum) { + public function __construct(BlocktrailSDKInterface $sdk, $identifier, $encryptedPrimarySeed, $encryptedSecret, $primaryPublicKeys, $backupPublicKey, $blocktrailPublicKeys, $keyIndex, $network, $testnet, $segwit, BaseAddressCreator $addressReader, $checksum) { if ($encryptedPrimarySeed !== null && !($encryptedPrimarySeed instanceof Buffer)) { throw new \InvalidArgumentException('Encrypted Primary Seed must be a Buffer or null'); } @@ -107,7 +108,7 @@ public function unlock($options, callable $fn = null) { $this->primaryPrivateKey = BIP32Key::create(HierarchicalKeyFactory::fromEntropy($this->primarySeed), "m"); // create checksum (address) of the primary privatekey to compare to the stored checksum - $checksum = $this->primaryPrivateKey->publicKey()->getAddress()->getAddress(); + $checksum = $this->primaryPrivateKey->key()->getAddress(new AddressCreator())->getAddress(); if ($checksum != $this->checksum) { throw new \Exception("Checksum [{$checksum}] does not match [{$this->checksum}], most likely due to incorrect password"); } diff --git a/tests/BitcoinCashAddressTest.php b/tests/BitcoinCashAddressTest.php index 0f2e743..4846dd3 100644 --- a/tests/BitcoinCashAddressTest.php +++ b/tests/BitcoinCashAddressTest.php @@ -4,30 +4,26 @@ use BitWasp\Bitcoin\Address\ScriptHashAddress; -use Blocktrail\SDK\Address\BitcoinCashAddressReader; -use Blocktrail\SDK\Address\CashAddress; -use Blocktrail\SDK\Network\BitcoinCashTestnet; +use Btccom\BitcoinCash\Address\AddressCreator as BitcoinCashAddressCreator; +use Btccom\BitcoinCash\Network\Networks\BitcoinCash; +use Btccom\BitcoinCash\Network\Networks\BitcoinCashTestnet; +use Btccom\BitcoinCash\Address\CashAddress; use Blocktrail\SDK\Exceptions\BlocktrailSDKException; -use Blocktrail\SDK\Network\BitcoinCash; class BitcoinCashAddressTest extends BlocktrailTestCase { - /** - * @expectedException \Blocktrail\SDK\Exceptions\BlocktrailSDKException - * @expectedExceptionMessage Address not recognized - */ public function testShortCashAddress() { $bch = new BitcoinCash(); $tbch = new BitcoinCashTestnet(); $address = "bchtest:ppm2qsznhks23z7629mms6s4cwef74vcwvhanqgjxu"; $short = "ppm2qsznhks23z7629mms6s4cwef74vcwvhanqgjxu"; - $reader = new BitcoinCashAddressReader(true); - $this->assertEquals($address, $reader->fromString($address, $bch)->getAddress($bch)); - $this->assertEquals($address, $reader->fromString($short, $bch)->getAddress($bch)); + $reader = new BitcoinCashAddressCreator(true); + $this->assertEquals($address, $reader->fromString($address, $tbch)->getAddress($tbch)); + $this->assertEquals($address, $reader->fromString($short, $tbch)->getAddress($tbch)); - $this->setExpectedException(BlocktrailSDKException::class, "Address not recognized"); - $reader->fromString($short, $tbch); + $this->setExpectedException(\BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException::class, "Address not recognized"); + $reader->fromString($short, $bch); } public function testInitializeWithDefaultFormat() { @@ -40,7 +36,7 @@ public function testInitializeWithDefaultFormat() { ]); $legacyAddress = "2N44ThNe8NXHyv4bsX8AoVCXquBRW94Ls7W"; - $this->assertInstanceOf(BitcoinCashAddressReader::class, $legacyAddressWallet->getAddressReader()); + $this->assertInstanceOf(BitcoinCashAddressCreator::class, $legacyAddressWallet->getAddressReader()); $this->assertInstanceOf(ScriptHashAddress::class, $legacyAddressWallet->getAddressReader()->fromString($legacyAddress, $tbcc)); $cashAddress = "bchtest:ppm2qsznhks23z7629mms6s4cwef74vcwvhanqgjxu"; @@ -50,7 +46,7 @@ public function testInitializeWithDefaultFormat() { "use_cashaddress" => true, ]); - $this->assertInstanceOf(BitcoinCashAddressReader::class, $newAddressWallet->getAddressReader()); + $this->assertInstanceOf(BitcoinCashAddressCreator::class, $newAddressWallet->getAddressReader()); $this->assertInstanceOf(CashAddress::class, $newAddressWallet->getAddressReader()->fromString($cashAddress, $tbcc)); $convertedLegacy = $client->getLegacyBitcoinCashAddress($cashAddress); diff --git a/tests/Network/BitcoinCashTest.php b/tests/Network/BitcoinCashTest.php index 3c97215..dd8b31c 100644 --- a/tests/Network/BitcoinCashTest.php +++ b/tests/Network/BitcoinCashTest.php @@ -2,70 +2,12 @@ namespace Blocktrail\SDK\Tests\Network; -use BitWasp\Bitcoin\Network\NetworkFactory; use BitWasp\Buffertools\Buffer; -use Blocktrail\SDK\Address\CashAddress; -use Blocktrail\SDK\Network\BitcoinCash; -use Blocktrail\SDK\Network\BitcoinCashTestnet; +use Btccom\BitcoinCash\Address\CashAddress; +use Btccom\BitcoinCash\Network\NetworkFactory as BchNetworkFactory; class BitcoinCashTest extends \PHPUnit_Framework_TestCase { - public function testnetProvider() - { - return [ - [true, "bchtest",], - [false, "bitcoincash",], - ]; - } - - /** - * @param bool $testnet - * @dataProvider testnetProvider - */ - public function testBitcoinCash($testnet, $cashAddrPrefix) - { - if ($testnet) { - $network = new BitcoinCashTestnet(); - } else { - $network = new BitcoinCash(); - } - - $this->assertEquals($testnet, $network->isTestnet()); - $this->assertEquals($cashAddrPrefix, $network->getCashAddressPrefix()); - - if ($testnet) { - $cmp = NetworkFactory::bitcoinTestnet(); - } else { - $cmp = NetworkFactory::bitcoin(); - } - - $this->assertEquals($cmp->getAddressByte(), $network->getAddressByte()); - $this->assertEquals($cmp->getP2shByte(), $network->getP2shByte()); - $this->assertEquals($cmp->getPrivByte(), $network->getPrivByte()); - $this->assertEquals($cmp->isTestnet(), $network->isTestnet()); - $this->assertEquals($cmp->getNetMagicBytes(), $network->getNetMagicBytes()); - $this->assertEquals($cmp->getHDPrivByte(), $network->getHDPrivByte()); - $this->assertEquals($cmp->getHDPubByte(), $network->getHDPubByte()); - } - - /** - * @param $testnet - * @throws \Exception - * @dataProvider testnetProvider - * @expectedException \Exception - * @expectedExceptionMessage No bech32 prefix for segwit addresses set - */ - public function testNoBech32($testnet) - { - if ($testnet) { - $network = new BitcoinCashTestnet(); - } else { - $network = new BitcoinCash(); - } - - $network->getSegwitBech32Prefix(); - } - public function getCashAddressFixture() { return [ @@ -89,9 +31,9 @@ public function getCashAddressFixture() public function testCashAddress($testnet, $type, $hashHex, $expected) { if ($testnet) { - $network = new BitcoinCashTestnet(); + $network = BchNetworkFactory::bitcoinCashTestnet(); } else { - $network = new BitcoinCash(); + $network = BchNetworkFactory::bitcoinCash(); } $hash = Buffer::hex($hashHex); diff --git a/tests/OutputsNormalizerTest.php b/tests/OutputsNormalizerTest.php index 5d5ed4d..3677f84 100644 --- a/tests/OutputsNormalizerTest.php +++ b/tests/OutputsNormalizerTest.php @@ -2,13 +2,12 @@ namespace Blocktrail\SDK\Tests; -use BitWasp\Bitcoin\Network\NetworkFactory; -use Blocktrail\SDK\Address\BitcoinAddressReader; -use Blocktrail\SDK\Address\BitcoinCashAddressReader; +use BitWasp\Bitcoin\Network\NetworkFactory as BitcoinNetworkFactory; +use Btccom\BitcoinCash\Network\NetworkFactory as BitcoinCashNetworkFactory; +use BitWasp\Bitcoin\Address\AddressCreator as BitcoinAddressCreator; +use Btccom\BitcoinCash\Address\AddressCreator as BitcoinCashAddressCreator; use Blocktrail\SDK\Blocktrail; use Blocktrail\SDK\Exceptions\BlocktrailSDKException; -use Blocktrail\SDK\Network\BitcoinCash; -use Blocktrail\SDK\Network\BitcoinCashTestnet; use Blocktrail\SDK\OutputsNormalizer; class OutputsNormalizerTest extends BlocktrailTestCase @@ -17,18 +16,18 @@ private function loadAddressReader($network, $testnet) { switch ($network) { case "BTC": if ($testnet) { - return [NetworkFactory::bitcoinTestnet(), new BitcoinAddressReader()]; + return [BitcoinNetworkFactory::bitcoinTestnet(), new BitcoinAddressCreator()]; } - return [NetworkFactory::bitcoin(), new BitcoinAddressReader()]; + return [BitcoinNetworkFactory::bitcoin(), new BitcoinAddressCreator()]; break; case "BCC": if ($testnet) { - $network = new BitcoinCashTestnet(); + $network = BitcoinCashNetworkFactory::bitcoinCashTestnet(); } else { - $network = new BitcoinCash(); + $network = BitcoinCashNetworkFactory::bitcoinCash(); } - return [$network, new BitcoinCashAddressReader(true)]; + return [$network, new BitcoinCashAddressCreator(true)]; break; default: throw new \RuntimeException("Unknown network"); @@ -209,7 +208,6 @@ public function getFixtures() { public function testOutputsNormalizer($network, $testnet, array $input, array $expected) { list ($network, $addressReader) = $this->loadAddressReader($network, $testnet); - $normalizer = new OutputsNormalizer($addressReader); $outputs = $normalizer->normalize($input, $network); diff --git a/tests/WalletTest.php b/tests/WalletTest.php index 30ec7d9..c836d7d 100644 --- a/tests/WalletTest.php +++ b/tests/WalletTest.php @@ -4,7 +4,7 @@ \error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT); -use BitWasp\Bitcoin\Address\AddressFactory; +use BitWasp\Bitcoin\Address\AddressCreator; use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress; use BitWasp\Bitcoin\Address\ScriptHashAddress; use BitWasp\Bitcoin\Address\SegwitAddress; @@ -21,7 +21,7 @@ use BitWasp\Buffertools\Buffer; use Blocktrail\CryptoJSAES\CryptoJSAES; -use Blocktrail\SDK\Address\BitcoinAddressReader; +use BitWasp\Bitcoin\Address\AddressCreator as BitcoinAddressCreator; use Blocktrail\SDK\Bitcoin\BIP32Key; use Blocktrail\SDK\Blocktrail; use Blocktrail\SDK\BlocktrailSDK; @@ -130,7 +130,7 @@ protected function _createTestWallet(BlocktrailSDKInterface $client, $identifier $testnet = true; - $checksum = $primaryPrivateKey->publicKey()->getAddress()->getAddress(); + $checksum = $primaryPrivateKey->key()->getAddress(new BitcoinAddressCreator())->getAddress(); $result = $client->storeNewWalletV2( $identifier, @@ -159,7 +159,7 @@ protected function _createTestWallet(BlocktrailSDKInterface $client, $identifier 'bitcoin', $testnet, false, - new BitcoinAddressReader(), + new BitcoinAddressCreator(), $checksum ); @@ -221,7 +221,8 @@ public function testCreateWallet() { list($path, $address) = $wallet->getNewAddressPair(); $this->assertTrue(strpos($path, "M/9999'/0/") === 0); - $this->assertEquals($address, AddressFactory::fromString($address)->getAddress()); + $addrCreator = new AddressCreator(); + $this->assertEquals($address, $addrCreator->fromString($address)->getAddress()); } @@ -348,7 +349,7 @@ public function testSegwitWalletTransaction() $this->assertTrue($segwitwallet->isSegwit()); list ($path, $address) = $segwitwallet->getNewAddressPair(); - $addrObj = AddressFactory::fromString($address); + $addrObj = $segwitwallet->getAddressReader()->fromString($address); // Send back to unittest-transaction-sw @@ -666,7 +667,7 @@ public function testWalletTransaction() { list($path, $address) = $wallet->getNewAddressPair(); $this->assertTrue(strpos($path, "M/9999'/0/") === 0); - $this->assertTrue(AddressFactory::fromString($address)->getAddress() == $address); + $this->assertTrue($wallet->getAddressReader()->fromString($address)->getAddress() == $address); $value = BlocktrailSDK::toSatoshi(0.0002); $txHash = $wallet->pay([$address => $value,], null, false, true, Wallet::FEE_STRATEGY_BASE_FEE); @@ -783,7 +784,8 @@ public function testWalletTransactionWithoutMnemonics() { list($path, $address) = $wallet->getNewAddressPair(); $this->assertTrue(strpos($path, "M/9999'/0/") === 0); - $this->assertTrue(AddressFactory::fromString($address)->getAddress() == $address); + $addrCreator = new AddressCreator(); + $this->assertTrue($addrCreator->fromString($address)->getAddress() == $address); $value = BlocktrailSDK::toSatoshi(0.0002); $txHash = $wallet->pay([ @@ -997,7 +999,7 @@ public function testNewBlankWithoutMnemonicsWalletV2() { $identifier = $this->getRandomTestIdentifier(); $primaryPrivateKey = BIP32Key::create(HierarchicalKeyFactory::generateMasterKey(), 'm'); - $backupPublicKey = BIP32Key::create(HierarchicalKeyFactory::generateMasterKey()->toPublic(), 'M'); + $backupPublicKey = BIP32Key::create(HierarchicalKeyFactory::generateMasterKey()->withoutPrivateKey(), 'M'); /** * @var $wallet \Blocktrail\SDK\Wallet @@ -1352,10 +1354,11 @@ public function testSegwitBuildTx() { $this->assertEquals($expectfee, $fee); // assert the input(s) + $addrCreator = new AddressCreator(); $this->assertEquals(1, count($tx->getInputs())); $this->assertEquals($txid, $tx->getInput(0)->getOutPoint()->getTxId()->getHex()); $this->assertEquals(0, $tx->getInput(0)->getOutPoint()->getVout()); - $this->assertEquals($address, AddressFactory::fromOutputScript($signInfo[0]->output->getScript())->getAddress()); + $this->assertEquals($address, $addrCreator->fromOutputScript($signInfo[0]->output->getScript())->getAddress()); $this->assertEquals($scriptPubKey, $signInfo[0]->output->getScript()->getHex()); $this->assertEquals($value, $signInfo[0]->output->getValue()); $this->assertEquals($path, $signInfo[0]->path); @@ -1370,7 +1373,7 @@ public function testSegwitBuildTx() { // assert the output(s) $this->assertEquals(1, count($tx->getOutputs())); - $this->assertEquals("2N6DJMnoS3xaxpCSDRMULgneCghA1dKJBmT", AddressFactory::fromOutputScript($tx->getOutput(0)->getScript())->getAddress()); + $this->assertEquals("2N6DJMnoS3xaxpCSDRMULgneCghA1dKJBmT", $addrCreator->fromOutputScript($tx->getOutput(0)->getScript())->getAddress()); $this->assertEquals($outValue, $tx->getOutput(0)->getValue()); } @@ -1430,10 +1433,11 @@ public function testBuildTx() { $this->assertEquals(BlocktrailSDK::toSatoshi(0.0001), $fee); // assert the input(s) + $addrCreator = new AddressCreator(); $this->assertEquals(2, count($tx->getInputs())); $this->assertEquals("0d8703ab259b03a757e37f3cdba7fc4543e8d47f7cc3556e46c0aeef6f5e832b", $tx->getInput(0)->getOutPoint()->getTxId()->getHex()); $this->assertEquals(0, $tx->getInput(0)->getOutPoint()->getVout()); - $this->assertEquals("2N9os1eAZXrWwKWgo7ppDRsY778PyxbScYH", AddressFactory::fromOutputScript($signInfo[0]->output->getScript())->getAddress()); + $this->assertEquals("2N9os1eAZXrWwKWgo7ppDRsY778PyxbScYH", $addrCreator->fromOutputScript($signInfo[0]->output->getScript())->getAddress()); $this->assertEquals("a914b5ae3a9950fa66efa4aab2c21ce4a4275e7c95b487", $signInfo[0]->output->getScript()->getHex()); $this->assertEquals(10000, $signInfo[0]->output->getValue()); $this->assertEquals("M/9999'/0/5", $signInfo[0]->path); @@ -1444,7 +1448,7 @@ public function testBuildTx() { $this->assertEquals("be837cd8f04911f3ee10d010823a26665980f7bb6c9ed307d798cb968ca00128", $tx->getInput(1)->getOutPoint()->getTxId()->getHex()); $this->assertEquals(0, $tx->getInput(1)->getOutPoint()->getVout()); - $this->assertEquals("2NBV4sxQMYNyBbUeZkmPTZYtpdmKcuZ4Cyw", AddressFactory::fromOutputScript($signInfo[1]->output->getScript())->getAddress()); + $this->assertEquals("2NBV4sxQMYNyBbUeZkmPTZYtpdmKcuZ4Cyw", $addrCreator->fromOutputScript($signInfo[1]->output->getScript())->getAddress()); $this->assertEquals("a914c8107bd24bae2c521a5a9f56c9b72e047eafa1f587", $signInfo[1]->output->getScript()->getHex()); $this->assertEquals(100000, $signInfo[1]->output->getValue()); $this->assertEquals("M/9999'/0/12", $signInfo[1]->path); @@ -1455,7 +1459,7 @@ public function testBuildTx() { // assert the output(s) $this->assertEquals(1, count($tx->getOutputs())); - $this->assertEquals("2N7C5Jn1LasbEK9mvHetBYXaDnQACXkarJe", AddressFactory::fromOutputScript($tx->getOutput(0)->getScript())->getAddress()); + $this->assertEquals("2N7C5Jn1LasbEK9mvHetBYXaDnQACXkarJe", $addrCreator->fromOutputScript($tx->getOutput(0)->getScript())->getAddress()); $this->assertEquals(100000, $tx->getOutput(0)->getValue()); /* @@ -1537,7 +1541,8 @@ public function testBuildTx() { $this->assertEquals(BlocktrailSDK::toSatoshi(0.9999), $outputTotal); $this->assertEquals(BlocktrailSDK::toSatoshi(0.0001), $fee); $this->assertEquals(14, count($tx->getOutputs())); - $this->assertEquals("2N6DJMnoS3xaxpCSDRMULgneCghA1dKJBmT", AddressFactory::fromOutputScript($tx->getOutput(13)->getScript())->getAddress()); + $addrCreator = new AddressCreator(); + $this->assertEquals("2N6DJMnoS3xaxpCSDRMULgneCghA1dKJBmT", $addrCreator->fromOutputScript($tx->getOutput(13)->getScript())->getAddress()); $this->assertEquals(99860000, $tx->getOutput(13)->getValue()); /* @@ -1605,7 +1610,8 @@ public function testBuildTx() { $this->assertEquals(BlocktrailSDK::toSatoshi(0.9999), $outputTotal); $this->assertEquals(BlocktrailSDK::toSatoshi(0.0001), $fee); $this->assertEquals(20, count($tx->getOutputs())); - $this->assertEquals("2N6DJMnoS3xaxpCSDRMULgneCghA1dKJBmT", AddressFactory::fromOutputScript($tx->getOutput(19)->getScript())->getAddress()); + $addrCreator = new AddressCreator(); + $this->assertEquals("2N6DJMnoS3xaxpCSDRMULgneCghA1dKJBmT", $addrCreator->fromOutputScript($tx->getOutput(19)->getScript())->getAddress()); $this->assertEquals(BlocktrailSDK::toSatoshi(0.9980), $tx->getOutput(19)->getValue()); /* @@ -1678,7 +1684,8 @@ public function testBuildTx() { $this->assertEquals(BlocktrailSDK::toSatoshi(0.0002), $fee); $this->assertEquals(22, count($tx->getOutputs())); $change = $tx->getOutput(21); - $this->assertEquals("2N6DJMnoS3xaxpCSDRMULgneCghA1dKJBmT", AddressFactory::fromOutputScript($change->getScript())->getAddress()); + $addrCreator = new AddressCreator(); + $this->assertEquals("2N6DJMnoS3xaxpCSDRMULgneCghA1dKJBmT", $addrCreator->fromOutputScript($change->getScript())->getAddress()); $this->assertEquals(BlocktrailSDK::toSatoshi(0.9977), $change->getValue()); /* @@ -1897,8 +1904,9 @@ public function testBuildTx() { $this->assertEquals(BlocktrailSDK::toSatoshi(0.0019), $outputTotal); $this->assertEquals(BlocktrailSDK::toSatoshi(0.0001), $fee); - $this->assertEquals("2NAUFsSps9S2mEnhaWZoaufwyuCaVPUv8op", AddressFactory::fromOutputScript($tx->getOutput(0)->getScript())->getAddress()); - $this->assertEquals("2NAUFsSps9S2mEnhaWZoaufwyuCaVPUv8op", AddressFactory::fromOutputScript($tx->getOutput(1)->getScript())->getAddress()); + $addrCreator = new AddressCreator(); + $this->assertEquals("2NAUFsSps9S2mEnhaWZoaufwyuCaVPUv8op", $addrCreator->fromOutputScript($tx->getOutput(0)->getScript())->getAddress()); + $this->assertEquals("2NAUFsSps9S2mEnhaWZoaufwyuCaVPUv8op", $addrCreator->fromOutputScript($tx->getOutput(1)->getScript())->getAddress()); } protected function getTx(BlocktrailSDK $client, $txId, $retries = 3) {