Skip to content

Commit

Permalink
Added convenience method Account::isValidAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
zulucrypto committed Sep 22, 2018
1 parent c0f626f commit 57794ef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Model/Account.php
Expand Up @@ -6,6 +6,7 @@

use phpseclib\Math\BigInteger;
use ZuluCrypto\StellarSdk\Horizon\Api\HorizonResponse;
use ZuluCrypto\StellarSdk\Keypair;
use ZuluCrypto\StellarSdk\Transaction\TransactionBuilder;
use ZuluCrypto\StellarSdk\Util\MathSafety;
use ZuluCrypto\StellarSdk\XdrModel\Asset;
Expand Down Expand Up @@ -86,6 +87,27 @@ public static function fromHorizonResponse(HorizonResponse $response)
return $object;
}

/**
* Returns true if the specified account ID (G....) passes basic validity checks
*
* Note that this doesn't necessarily mean the account is funded or exists
* on the network. To check that, use the Server::getAccount() method.
*
* @param $accountId
* @return bool
*/
public static function isValidAccount($accountId)
{
// Validate that keypair passes checksum
try {
$keypair = Keypair::newFromPublicKey($accountId);
} catch (\Exception $e) {
return false;
}

return true;
}

public function __construct($id)
{
$this->id = $id;
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Model/AccountTest.php
@@ -0,0 +1,19 @@
<?php


namespace ZuluCrypto\StellarSdk\Test\Unit\Model;


use PHPUnit\Framework\TestCase;
use ZuluCrypto\StellarSdk\Model\Account;

class AccountTest extends TestCase
{
public function testIsValidAccount()
{
$this->assertTrue(Account::isValidAccount('GDRXE2BQUC3AZNPVFSCEZ76NJ3WWL25FYFK6RGZGIEKWE4SOOHSUJUJ6'));

$this->assertFalse(Account::isValidAccount(null));
$this->assertFalse(Account::isValidAccount('G123'));
}
}

0 comments on commit 57794ef

Please sign in to comment.