Skip to content

Commit

Permalink
Add ethSigToPrivate
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Jul 8, 2024
1 parent 20cb146 commit b4cf498
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/Crypto/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,19 @@ public static function getStarkKey ($privateKey)
$publicKey = self::getPublicKey($privateKey, true, '');
return '0x' . preg_replace('/^0+/', '', $publicKey->getX()->toString(16));
}

/**
* ethSigToPrivate
*
* @param string $sig
* @return string
*/
public static function ethSigToPrivate ($sig)
{
$sig = Utils::stripZeroPrefix($sig);
if (strlen($sig) !== 130) {
throw new Exception('Wrong ethereum signature');
}
return self::grindKey(substr($sig, 0, 64));
}
}
16 changes: 13 additions & 3 deletions test/unit/KeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class KeyTest extends TestCase
*/
public function testGrindKey()
{

$result = Key::grindKey('86F3E7293141F20A8BAFF320E8EE4ACCB9D4A4BF2B4D295E8CEE784DB46E0519');
$this->assertEquals('05c8c8683596c732541a59e03007b2d30dbbbb873556fe65b5fb63c16688f941', $result);

Expand All @@ -33,7 +32,6 @@ public function testGrindKey()
*/
public function testGetPublicKey()
{

$result = Key::getPublicKey('2dccce1da22003777062ee0870e9881b460a8b7eca276870f57c601f182136c');
$this->assertEquals('0400499f65ae2f71d5298d2d88823b2e5e19596a71aac1984710479e406a00243904745865467631492cf6ecc433a3cf4ecc580d698097d6b738ad8f3da7c4d66c', $result);
}
Expand All @@ -45,11 +43,23 @@ public function testGetPublicKey()
*/
public function testGetStarkKey()
{

$result = Key::getStarkKey('0x178047D3869489C055D7EA54C014FFB834A069C9595186ABE04EA4D1223A03F');
$this->assertEquals('0x1895a6a77ae14e7987b9cb51329a5adfb17bd8e7c638f92d6892d76e51cebcf', $result);

$result = Key::getStarkKey('0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279');
$this->assertEquals('0x33f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745', $result);
}

/**
* testEthSigToPrivate
*
* @return void
*/
public function testEthSigToPrivate()
{
$sig = '0x21fbf0696d5e0aa2ef41a2b4ffb623bcaf070461d61cf7251c74161f82fec3a43' .
'70854bc0a34b3ab487c1bc021cd318c734c51ae29374f2beb0e6f2dd49b4bf41c';
$result = Key::ethSigToPrivate($sig);
$this->assertEquals('0766f11e90cd7c7b43085b56da35c781f8c067ac0d578eabdceebc4886435bda', $result);
}
}

0 comments on commit b4cf498

Please sign in to comment.