diff --git a/src/Model/Account.php b/src/Model/Account.php index f6c3087..c7197e2 100755 --- a/src/Model/Account.php +++ b/src/Model/Account.php @@ -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; @@ -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; diff --git a/tests/Unit/Model/AccountTest.php b/tests/Unit/Model/AccountTest.php new file mode 100644 index 0000000..b44822d --- /dev/null +++ b/tests/Unit/Model/AccountTest.php @@ -0,0 +1,19 @@ +assertTrue(Account::isValidAccount('GDRXE2BQUC3AZNPVFSCEZ76NJ3WWL25FYFK6RGZGIEKWE4SOOHSUJUJ6')); + + $this->assertFalse(Account::isValidAccount(null)); + $this->assertFalse(Account::isValidAccount('G123')); + } +} \ No newline at end of file