-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* This file is part of starknet.php package. | ||
* | ||
* (c) Kuan-Cheng,Lai <[email protected]> | ||
* | ||
* @author Peter Lai <[email protected]> | ||
* @license MIT | ||
*/ | ||
|
||
namespace StarkNet; | ||
|
||
use BN\BN; | ||
use StarkNet\Utils; | ||
use StarkNet\Crypto\FastPedersenHash; | ||
|
||
class Hash | ||
{ | ||
const CONTRACT_ADDRESS_PREFIX = '523065374597054866729014270389667305596563390979550329787219'; | ||
|
||
public static function L2_ADDRESS_UPPER_BOUND() | ||
{ | ||
// 2**251 - 256 | ||
return new BN('3618502788666131106986593281521497120414687020801267626233049500247285300992'); | ||
} | ||
|
||
/** | ||
* getSelectorFromName | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public static function getSelectorFromName($name) | ||
{ | ||
return Utils::keccak($name); | ||
} | ||
|
||
/** | ||
* computeAddress | ||
* | ||
* @param mixed $classHash | ||
* @param mixed $constructorData | ||
* @param mixed $salt | ||
* @param mixed $deployerAddress | ||
* @return string | ||
*/ | ||
public static function computeAddress($classHash, $constructorData, $salt, $deployerAddress = 0) | ||
{ | ||
$constructorDataHash = FastPedersenHash::computeHashOnElements($constructorData); | ||
$rawAddress = FastPedersenHash::computeHashOnElements([ | ||
self::CONTRACT_ADDRESS_PREFIX, | ||
$deployerAddress, | ||
$salt, | ||
$classHash, | ||
$constructorDataHash | ||
]); | ||
return Utils::removeLeadingZero($rawAddress->mod(self::L2_ADDRESS_UPPER_BOUND())->toString(16)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Test\Unit; | ||
|
||
use InvalidArgumentException; | ||
use stdClass; | ||
use Test\TestCase; | ||
use phpseclib\Math\BigInteger as BigNumber; | ||
use StarkNet\Hash; | ||
|
||
class HashTest extends TestCase | ||
{ | ||
/** | ||
* setUp | ||
* | ||
* @return void | ||
*/ | ||
// public function setUp(): void | ||
// { | ||
// parent::setUp(); | ||
// } | ||
|
||
/** | ||
* testComputeAddress | ||
* | ||
* @return void | ||
*/ | ||
public function testComputeAddress() | ||
{ | ||
$result = Hash::computeAddress( | ||
'951442054899045155353616354734460058868858519055082696003992725251069061570', | ||
[ | ||
21, | ||
37 | ||
], | ||
1111 | ||
); | ||
$this->assertEquals('30018328ec720e778744bfeec50d93bad01215f53b7a09e7c8b724157c2d947', $result); | ||
|
||
$result = Hash::computeAddress( | ||
'951442054899045155353616354734460058868858519055082696003992725251069061570', | ||
[ | ||
21, | ||
37 | ||
], | ||
1111, | ||
1234 | ||
); | ||
$this->assertEquals('707c2720b9dc5fb42ff24eeef04ddff509fcf2f86371cbf9a344de8638bd4a8', $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters