### Install Base58 Library with Composer Source: https://github.com/tuupola/base58/blob/2.x/README.md Installs the Base58 library using Composer. This command ensures you have the latest stable version compatible with PHP 7.1 or higher. ```bash $ composer require tuupola/base58 ``` -------------------------------- ### Watch for Code Changes and Run Tests Source: https://github.com/tuupola/base58/blob/2.x/CONTRIBUTING.md Shows how to set up automatic testing using 'entr' to run tests on every code change. This requires installing 'entr' via Homebrew. ```bash $ brew install entr $ make watch ``` -------------------------------- ### Example Commit Message Structure Source: https://github.com/tuupola/base58/blob/2.x/CONTRIBUTING.md Provides an example of a well-formatted Git commit message, including a concise summary line and a more detailed explanation. This structure helps in understanding the purpose and context of a commit. ```git Capitalized, short (50 chars or less) summary More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together. ``` -------------------------------- ### Install Older Version of Base58 Library Source: https://github.com/tuupola/base58/blob/2.x/README.md Installs version 1.x of the Base58 library, which supports older PHP versions (5.6 and 7.0). Use this if your project has compatibility constraints. ```bash $ composer require "tuupola/base58:^1.0" ``` -------------------------------- ### Run Tests Source: https://github.com/tuupola/base58/blob/2.x/CONTRIBUTING.md Demonstrates how to run the project's tests using the 'make test' command. This is a crucial step for ensuring code quality and stability. ```bash $ make test ``` -------------------------------- ### Running Base58 Library Tests Source: https://github.com/tuupola/base58/blob/2.x/README.md Instructions on how to run the test suite for the Base58 library. This includes commands for manual testing and setting up automatic testing with `entr` for real-time feedback during development. ```bash $ make test ``` ```bash $ brew install entr $ make watch ``` -------------------------------- ### Manual Encoder Selection for Base58 (PHP) Source: https://context7.com/tuupola/base58/llms.txt Shows how to manually select specific Base58 encoders in PHP: GMP, pure PHP, and BCMath. It demonstrates their usage for encoding and decoding, verifying that all encoders produce identical output for the same data. Requires the 'tuupola/base58' package and autoloader, and optionally 'ext-gmp' and 'bcmath' extensions. ```php Base58::BITCOIN]); $encoded = $gmp->encode($data); echo "GMP: " . $encoded . "\n"; // Output: GMP: 3vQB7B6MrGQZaxCuFg4oh } // Pure PHP encoder (no extensions required, slower) $php = new PhpEncoder(["characters" => Base58::BITCOIN]); $encoded = $php->encode($data); echo "PHP: " . $encoded . "\n"; // Output: PHP: 3vQB7B6MrGQZaxCuFg4oh // BCMath encoder (very slow, for compatibility) if (extension_loaded('bcmath')) { $bcmath = new BcmathEncoder(["characters" => Base58::BITCOIN]); $encoded = $bcmath->encode($data); echo "BCMath: " . $encoded . "\n"; // Output: BCMath: 3vQB7B6MrGQZaxCuFg4oh } // All encoders produce identical output $decoded_gmp = $gmp->decode($encoded); $decoded_php = $php->decode($encoded); echo "Consistent: " . ($decoded_gmp === $decoded_php && $decoded_php === $data ? "yes" : "no") . "\n"; // Output: Consistent: yes ``` -------------------------------- ### Basic Base58 Encoding and Decoding in PHP Source: https://github.com/tuupola/base58/blob/2.x/README.md Demonstrates the fundamental usage of the Base58 library for encoding arbitrary binary data and decoding it back. It uses the default GMP encoder if available, otherwise falls back to pure PHP. ```php use Tuupola\Base58; $base58 = new Base58; $encoded = $base58->encode(random_bytes(128)); $decoded = $base58->decode($encoded); ``` -------------------------------- ### Encode and Decode Arbitrary Binary Data with Base58 in PHP Source: https://context7.com/tuupola/base58/llms.txt Demonstrates how to encode arbitrary binary data and strings into Base58 format and decode them back to their original form using the Base58 PHP library. Requires the 'vendor/autoload.php' file for class loading. ```php encode($data); echo "Encoded: " . $encoded . "\n"; // Output: Encoded: 7KVGqvD9Mf8rYvJd5r8P7nJxY8f9xXvZg8pQ2r // Decode back to original data $decoded = $base58->decode($encoded); echo "Match: " . ($data === $decoded ? "yes" : "no") . "\n"; // Output: Match: yes // Encode string data $text = "Hello world!"; $encoded_text = $base58->encode($text); echo "Encoded text: " . $encoded_text . "\n"; // Output: Encoded text: 1LDlk6QWOejX6rPrJ $decoded_text = $base58->decode($encoded_text); echo "Decoded: " . $decoded_text . "\n"; // Output: Decoded: Hello world! ``` -------------------------------- ### Using Base58 Static Proxy in PHP Source: https://github.com/tuupola/base58/blob/2.x/README.md Provides a static proxy for the Base58 library, allowing you to use static methods for encoding and decoding without instantiating the class. This can simplify syntax in some cases. ```php use Tuupola\Base58Proxy as Base58; $encoded = Base58::encode(random_bytes(128)); $decoded = Base58::decode($encoded); ``` -------------------------------- ### Using Different Base58 Character Sets in PHP Source: https://github.com/tuupola/base58/blob/2.x/README.md Demonstrates how to use predefined character sets like GMP, Bitcoin, Flickr, Ripple, and IPFS for Base58 encoding. You can also define a custom character set. ```php use Tuupola\Base58; print Base58::GMP; print Base58::BITCOIN; print Base58::FLICKR; print Base58::RIPPLE; print Base58::IPFS; $default = new Base58(["characters" => Base58::GMP]); $bitcoin = new Base58(["characters" => Base58::BITCOIN]); print $default->encode("Hello world!"); /* 1LDlk6QWOejX6rPrJ */ print $bitcoin->encode("Hello world!"); /* 2NEpo7TZRhna7vSvL */ ``` -------------------------------- ### Handling Base58 Encoding Edge Cases (PHP) Source: https://context7.com/tuupola/base58/llms.txt Demonstrates how the Base58 library handles various edge cases in PHP, including empty strings, leading zero bytes, and integer zero encoding. It also shows error handling for invalid characters and improperly defined character sets. Requires the 'tuupola/base58' package and autoloader. ```php Base58::BITCOIN]); // Empty string $empty = $base58->encode(""); echo "Empty string: '" . $empty . "'\n"; // Output: Empty string: '' // Leading zero bytes are preserved $with_zeros = "\x00\x00\x00\x01\x02"; $encoded_zeros = $base58->encode($with_zeros); echo "With leading zeros: " . $encoded_zeros . "\n"; // Output: With leading zeros: 1116Ldp $decoded_zeros = $base58->decode($encoded_zeros); echo "Preserved: " . ($with_zeros === $decoded_zeros ? "yes" : "no") . "\n"; // Output: Preserved: yes // Integer zero $zero = $base58->encodeInteger(0); echo "Zero: '" . $zero . "'\n"; // Output: Zero: '1' // Invalid characters throw exception try { $base58->decode("invalid~data!@#"); } catch (InvalidArgumentException $e) { echo "Error: " . $e->getMessage() . "\n"; // Output: Error: Data contains invalid characters "~!@#" } // Character set must have exactly 58 unique characters try { $bad_charset = new Base58([ "characters" => "0023456789ABCD" // Too short and has duplicates ]); } catch (InvalidArgumentException $e) { echo "Charset error: " . $e->getMessage() . "\n"; // Output: Charset error: Character set must contain 58 unique characters } ``` -------------------------------- ### Static Proxy Interface for Base58 Operations (PHP) Source: https://context7.com/tuupola/base58/llms.txt Utilizes a static proxy interface for convenient Base58 encoding and decoding in PHP. Allows configuring options globally via a static property and supports integer encoding/decoding. Requires the 'tuupola/base58' package and autoloader. ```php Base58::BITCOIN, "check" => false, ]; $bitcoin_encoded = Base58::encode("Test data"); echo "Bitcoin encoded: " . $bitcoin_encoded . "\n"; // Output: Bitcoin encoded: 9MA8fRQrT4u8Zj8ZQqk // Integer encoding with static interface $number = 123456789; $encoded_int = Base58::encodeInteger($number); echo "Integer: " . $encoded_int . "\n"; // Output: Integer: 8M0kX $decoded_int = Base58::decodeInteger($encoded_int); echo "Decoded: " . $decoded_int . "\n"; // Output: Decoded: 123456789 ``` -------------------------------- ### Base58Check Encoding with Checksum Validation (PHP) Source: https://context7.com/tuupola/base58/llms.txt Demonstrates Base58Check encoding with a version byte and checksum validation in PHP. It shows how to encode data, decode it, and handle errors for invalid checksums or version mismatches. Requires the 'tuupola/base58' package and autoloader. ```php Base58::BITCOIN, "check" => true, "version" => 0x00 ]); $data = "Hello world!"; $encoded = $base58check->encode($data); echo "With checksum: " . $encoded . "\n"; // Output: With checksum: 19wWTEnNTWna86WmtFsTAr5 // Decoding validates checksum and version try { $decoded = $base58check->decode($encoded); echo "Decoded: " . $decoded . "\n"; // Output: Decoded: Hello world! } catch (RuntimeException $e) { echo "Error: " . $e->getMessage() . "\n"; } // Invalid checksum throws exception try { $invalid = "19wWTEnNTWna86WmtFsTArX"; // corrupted $base58check->decode($invalid); } catch (RuntimeException $e) { echo "Checksum error: " . $e->getMessage() . "\n"; // Output: Checksum error: Checksum "84fec52c" does not match the expected "84fec512" } // Wrong version throws exception try { $base58check_v1 = new Base58([ "characters" => Base58::BITCOIN, "check" => true, "version" => 0x01 ]); // Try to decode v0 address with v1 decoder $base58check_v1->decode("19wWTEnNTWna86WmtFsTAr5"); } catch (RuntimeException $e) { echo "Version error: " . $e->getMessage() . "\n"; // Output: Version error: Version "0" does not match the expected "1" } ``` -------------------------------- ### Base58 Integer Encoding and Decoding in PHP Source: https://github.com/tuupola/base58/blob/2.x/README.md Shows how to use the Base58 library to specifically encode and decode integers. The `encodeInteger` method converts an integer to its Base58 string representation, and `decodeInteger` converts it back. The optional `true` parameter in `decodeInteger` ensures strict validation. ```php use Tuupola\Base58; $base58 = new Base58; $integer = $base58->encodeInteger(987654321); /* 1TFvCj */ print $base58->decodeInteger("1TFvCj", true); /* 987654321 */ ``` -------------------------------- ### Base58Check Encoding and Decoding in PHP Source: https://github.com/tuupola/base58/blob/2.x/README.md Implements Base58Check encoding, commonly used in Bitcoin addresses. It includes versioning and checksum validation. Decoding will throw a `RuntimeException` if the version or checksum is invalid. ```php use Tuupola\Base58; $base58check = new Base58([ "characters" => Base58::BITCOIN, "check" => true, "version" => 0x00 ]); print $base58check->encode("Hello world!"); /* 19wWTEnNTWna86WmtFsTAr5 */ try { $base58check->decode("19wWTEnNTWna86WmtFsTArX"); } catch (RuntimeException $exception) { /* Checksum "84fec52c" does not match the expected "84fec512" */ print $exception->getMessage(); } ``` -------------------------------- ### Encode and Decode Integers with Base58 in PHP Source: https://context7.com/tuupola/base58/llms.txt Illustrates encoding integers into Base58 format and subsequently decoding them back to their original integer values using the Base58 PHP library. This function is distinct from encoding strings that represent numbers. Requires 'vendor/autoload.php'. ```php encodeInteger($number); echo "Integer encoded: " . $encoded_int . "\n"; // Output: Integer encoded: 1TFvCj // Decode back to integer $decoded_int = $base58->decodeInteger($encoded_int); echo "Decoded integer: " . $decoded_int . "\n"; // Output: Decoded integer: 987654321 // Note: encoding string vs integer yields different results $string_encoded = $base58->encode("987654321"); $integer_encoded = $base58->encodeInteger(987654321); echo "String encoded: " . $string_encoded . "\n"; // Output: String encoded: gE62MGeOBMPt echo "Integer encoded: " . $integer_encoded . "\n"; // Output: Integer encoded: 1TFvCj ``` -------------------------------- ### Distinction Between String and Integer Encoding in PHP Source: https://github.com/tuupola/base58/blob/2.x/README.md Highlights that encoding a string representation of a number and encoding the actual integer value result in different Base58 strings. This is important for data consistency. ```php use Tuupola\Base58; $base58 = new Base58; $string = $base58->encode("987654321"); /* gE62MGeOBMPt */ $integer = $base58->encodeInteger(987654321); /* 1TFvCj */ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.