Skip to content

Commit

Permalink
Add computeAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Jul 10, 2024
1 parent df908a7 commit 9e47f77
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Hash.php
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));
}
}
14 changes: 14 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ public static function stripZeroPrefix($value)
return $value;
}

/**
* removeLeadingZero
*
* @param string $value
* @return string
*/
public static function removeLeadingZero($value)
{
if (!is_string($value)) {
throw new InvalidArgumentException('The value to removeLeadingZero function must be string.');
}
return preg_replace('/^0+/', '', $value);
}

/**
* isNegative
*
Expand Down
51 changes: 51 additions & 0 deletions test/unit/HashTest.php
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);
}
}
14 changes: 14 additions & 0 deletions test/unit/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ public function testIsZeroPrefixed()
$isPrefixed = Utils::isZeroPrefixed(new stdClass);
}

/**
* testRemoveLeadingZero
*
* @return void
*/
public function testRemoveLeadingZero()
{
$result = Utils::removeLeadingZero('01234');
$this->assertEquals('1234', $result);

$this->expectException(InvalidArgumentException::class);
$result = Utils::removeLeadingZero(new stdClass);
}

/**
* testStripZeroPrefix
*
Expand Down

0 comments on commit 9e47f77

Please sign in to comment.