Skip to content

Commit

Permalink
Add hashPersonalMessage method
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Aug 17, 2018
1 parent ceb622c commit fbe03f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,16 @@ public function ecsign(string $privateKey, string $message)

return $signature;
}

/**
* hasPersonalMessage
*
* @param string $message
* @return string
*/
public function hashPersonalMessage(string $message)
{
$prefix = sprintf("\x19Ethereum Signed Message:\n%d", mb_strlen($message));
return $this->sha3($prefix . $message);
}
}
13 changes: 13 additions & 0 deletions test/unit/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,17 @@ public function testEcsign()
$this->assertEquals('67cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83', $signature->s->toString(16));
$this->assertEquals(35, $signature->recoveryParam);
}

/**
* testHashPersonalMessage
*
* @return void
*/
public function testHashPersonalMessage()
{
$util = new Util;
$hashedMessage = $util->hashPersonalMessage('Hello world');

$this->assertEquals('8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede', $hashedMessage);
}
}

0 comments on commit fbe03f0

Please sign in to comment.