Skip to content

Commit

Permalink
Adds isPublicAddress function to Wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsosf committed Jul 29, 2019
1 parent 3605012 commit 10bba75
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
composer.lock
.DS_Store
.DS_Store
/.idea
24 changes: 22 additions & 2 deletions src/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ public function isPublicKey($key)
return true;
}

public function isPublicAddress($address)
{
$length = 42;
if (strlen($address) !== $length) {
return false;
}

$parts = explode('x', $address);
$last_part = array_pop($parts);
$first_part = $parts[0] . 'x';

if ($first_part !== 'hx' && strlen($first_part) !== $length - 40) {
return false;
}

if (!ctype_xdigit($last_part) && strlen($last_part) !== $length - 2) {
return false;
}

return true;
}

/**
* @return string
*/
Expand All @@ -118,6 +140,4 @@ public function getPublicAddress(): string
}




}
9 changes: 9 additions & 0 deletions tests/WalletTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use mitsosf\IconSDK\Wallet;
use PHPUnit\Framework\TestCase;


Expand Down Expand Up @@ -65,4 +66,12 @@ public function test_pubkeyToAddress(){

unset($var);
}

public function test_isPublicAddress(){
$var = new mitsosf\IconSDK\Wallet;

$this->assertTrue($var->isPublicAddress($this->public_address));
$this->assertFalse($var->isPublicAddress('h'.$this->public_address));
unset($var);
}
}

0 comments on commit 10bba75

Please sign in to comment.