Skip to content

Commit

Permalink
Added helper for Nonstandard Currency Codes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderBuzz committed Feb 6, 2024
1 parent 5ebaaba commit e8e3eea
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 3 deletions.
15 changes: 15 additions & 0 deletions examples/custom_currency_codes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require __DIR__.'/../vendor/autoload.php';

use Hardcastle\XRPL_PHP\Core\CoreUtilities;

$customCurrency = "Dropzop";

$hash = CoreUtilities::encodeCustomCurrency($customCurrency);

print_r("Currency -> Hash: " . $customCurrency . ' -> ' . $hash . PHP_EOL);

$currencyBacktest = CoreUtilities::decodeCustomCurrency($hash);

print_r("Hash -> Currency: ". $hash . ' -> ' . $currencyBacktest . PHP_EOL);
5 changes: 2 additions & 3 deletions examples/faucet-wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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());
File renamed without changes.
30 changes: 30 additions & 0 deletions src/Core/CoreUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/Polyfill.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
15 changes: 15 additions & 0 deletions tests/Core/UtilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
10 changes: 10 additions & 0 deletions tests/PolyfillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down

0 comments on commit e8e3eea

Please sign in to comment.