/** * @param NetworkInterface|null $network * @return string */ public function getAddress(NetworkInterface $network = null) { $network = $network ?: Bitcoin::getNetwork(); $witnessByte = dechex($this->witnessVersion); $witnessByte = strlen($witnessByte) % 2 == 0 ? $witnessByte : '0' . $witnessByte; $payload = Buffer::hex($this->getPrefixByte($network) . $witnessByte . "00" . $this->getHash()); return Base58::encodeCheck($payload); }
/** * @param $wif * @return PrivateKey * @throws InvalidPrivateKey * @throws Base58ChecksumFailure */ public function parse($wif) { $payload = Base58::decodeCheck($wif)->slice(1); $size = $payload->getSize(); if (!in_array($size, [32, 33])) { throw new InvalidPrivateKey("Private key should be always be 32 or 33 bytes (depending on if it's compressed)"); } return $this->hexSerializer->parse($payload->slice(0, 32))->setCompressed($size === 33); }
/** * @param string $wif * @return PrivateKeyInterface * @throws InvalidPrivateKey * @throws Base58ChecksumFailure */ public function parse($wif) { $payload = Base58::decodeCheck($wif)->slice(1); $size = $payload->getSize(); if (33 === $size) { $compressed = true; $payload = $payload->slice(0, 32); } else { if (32 === $size) { $compressed = false; } else { throw new InvalidPrivateKey("Private key should be always be 32 or 33 bytes (depending on if it's compressed)"); } } return PrivateKeyFactory::fromInt($payload->getInt(), $compressed); }
/** * @param string $base58 * @return HierarchicalKey * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange * @throws \Exception */ public function parse($base58) { $payload = Base58::decodeCheck($base58); return $this->hexSerializer->parse($payload); }
<?php use BitWasp\Bitcoin\Base58; require __DIR__ . "/../vendor/autoload.php"; if (!isset($argv[1])) { die('Must provide base58 data to decode'); } $data = $argv[1]; try { $text = Base58::decode($data)->getHex(); } catch (\Exception $e) { $text = 'Invalid base58 data'; } echo $text . PHP_EOL;
/** * @param ScriptInterface $script * @param NetworkInterface $network * @return String * @throws \RuntimeException */ public static function getAssociatedAddress(ScriptInterface $script, NetworkInterface $network = null) { $classifier = new OutputClassifier($script); $network = $network ?: Bitcoin::getNetwork(); try { if ($classifier->isPayToPublicKey()) { $address = PublicKeyFactory::fromHex($script->getScriptParser()->decode()[0]->getData())->getAddress(); } else { $address = self::fromOutputScript($script); } return Base58::encodeCheck(Buffer::hex($network->getAddressByte() . $address->getHash(), 21)); } catch (\Exception $e) { throw new \RuntimeException('No address associated with this script type'); } }
/** * @param NetworkInterface|null $network * @return string */ public function getAddress(NetworkInterface $network = null) { $network = $network ?: Bitcoin::getNetwork(); $payload = Buffer::hex($this->getPrefixByte($network) . $this->getHash()); return Base58::encodeCheck($payload); }