diff --git a/examples/custom_currency_codes.php b/examples/custom_currency_codes.php new file mode 100644 index 0000000..645603c --- /dev/null +++ b/examples/custom_currency_codes.php @@ -0,0 +1,15 @@ + Hash: " . $customCurrency . ' -> ' . $hash . PHP_EOL); + +$currencyBacktest = CoreUtilities::decodeCustomCurrency($hash); + +print_r("Hash -> Currency: ". $hash . ' -> ' . $currencyBacktest . PHP_EOL); \ No newline at end of file diff --git a/examples/faucet-wallet.php b/examples/faucet-wallet.php index 93cb110..555b11d 100644 --- a/examples/faucet-wallet.php +++ b/examples/faucet-wallet.php @@ -4,7 +4,7 @@ use Hardcastle\XRPL_PHP\Client\JsonRpcClient; use Hardcastle\XRPL_PHP\Wallet\Wallet; -use function XRPL_PHP\Sugar\fundWallet; +use function Hardcastle\XRPL_PHP\Sugar\fundWallet; $client = new JsonRpcClient("https://s.altnet.rippletest.net:51234"); @@ -14,5 +14,4 @@ print_r(PHP_EOL); print_r('Wallet address: ' . $wallet->getAddress() . PHP_EOL); -print_r('Wallet seed: ' . $wallet->getSeed()); - +print_r('Wallet seed: ' . $wallet->getSeed()); \ No newline at end of file diff --git a/examples/token-trustline.php b/examples/token-create.php similarity index 100% rename from examples/token-trustline.php rename to examples/token-create.php diff --git a/src/Core/CoreUtilities.php b/src/Core/CoreUtilities.php index 71d44af..c6f0926 100644 --- a/src/Core/CoreUtilities.php +++ b/src/Core/CoreUtilities.php @@ -120,6 +120,36 @@ public static function decodeSeed(string $seed): array return $_this->addressCodec->decodeSeed($seed); } + /** + * Encode Nonstandard Currency Codes as in https://xrpl.org/currency-formats.html + * + * @param string $currencyName + * @return string + * @throws Exception + */ + public static function encodeCustomCurrency(string $currencyName): string + { + $hex = str2hex($currencyName); + $rawHash = str_pad($hex, 40, "00"); + return strtoupper($rawHash); + } + + /** + * Decode Nonstandard Currency Codes as in https://xrpl.org/currency-formats.html + * + * @param string $currencyHash + * @return string + * @throws Exception Error + */ + public static function decodeCustomCurrency(string $currencyHash): string + { + if (!str_starts_with($currencyHash, "00")) { + return rtrim(hex2str($currencyHash)); + } + + throw new Exception("Invalid currency hash"); + } + /** * is not allowed to call from outside to prevent from creating multiple instances, * to use the singleton, you have to obtain the instance from Singleton::getInstance() instead diff --git a/src/Polyfill.php b/src/Polyfill.php index 4971361..c3894b1 100644 --- a/src/Polyfill.php +++ b/src/Polyfill.php @@ -30,3 +30,22 @@ function bcdechex(string $dec): string return strtoupper($r); } } + +/** + * @param string $hex + * @return string + */ +function hex2str(string $hex): string +{ + return pack('H*', $hex); +} + +/** + * @param string $str + * @return string + */ +function str2hex(string $str): string +{ + $unpacked = unpack('H*', $str); + return array_shift($unpacked); +} \ No newline at end of file diff --git a/tests/Core/UtilitiesTest.php b/tests/Core/UtilitiesTest.php index cb47dee..6647df6 100644 --- a/tests/Core/UtilitiesTest.php +++ b/tests/Core/UtilitiesTest.php @@ -15,4 +15,19 @@ public function testSingletonUniqueness(): void $this->assertInstanceOf(CoreUtilities::class, $firstCall); $this->assertSame($firstCall, $secondCall); } + + public function testEncodeCustomCurrency(): void + { + $customCurrency = "SOLO"; + $hash = CoreUtilities::encodeCustomCurrency($customCurrency); + $this->assertEquals('534F4C4F00000000000000000000000000000000', $hash); + } + + public function testDecodecustomCurrency(): void + { + $hash = '534F4C4F00000000000000000000000000000000'; + $currency = CoreUtilities::decodeCustomCurrency($hash); + $this->assertEquals('SOLO', $currency); + } + } \ No newline at end of file diff --git a/tests/PolyfillTest.php b/tests/PolyfillTest.php index c11fe50..6a52b13 100644 --- a/tests/PolyfillTest.php +++ b/tests/PolyfillTest.php @@ -28,6 +28,16 @@ public function testBcdechex() $this->assertEquals('0', bcdechex('0')); } + public function testHex2str(): void + { + $this->assertEquals('Hello World!', hex2str('48656c6c6f20576f726c6421')); + } + + public function testStr2hex(): void + { + $this->assertEquals('48656c6c6f20576f726c6421', str2hex('Hello World!')); + } + private function needsBcmathExtension() { if (!extension_loaded('bcmath')) {