Skip to content

Commit

Permalink
Merge pull request #5 from sc0Vu/master
Browse files Browse the repository at this point in the history
Add functions
  • Loading branch information
sc0Vu authored Aug 17, 2018
2 parents 0815348 + fbe03f0 commit 4e5ef4d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use kornrunner\Keccak;
use Elliptic\EC;
use Elliptic\EC\KeyPair;
use Elliptic\EC\Signature;

class Util
{
Expand Down Expand Up @@ -204,4 +205,45 @@ public function recoverPublicKey(string $hash, string $r, string $s, int $v)

return '0x' . $publicKey;
}

/**
* ecsign
*
* @param string $privateKey
* @param string $message
* @return \Elliptic\EC\Signature
*/
public function ecsign(string $privateKey, string $message)
{
if ($this->isHex($privateKey) === false) {
throw new InvalidArgumentException('Invalid private key format.');
}
$privateKeyLength = strlen($this->stripZero($privateKey));

if ($privateKeyLength % 2 !== 0 && $privateKeyLength !== 64) {
throw new InvalidArgumentException('Private key length was wrong.');
}
$secp256k1 = new EC('secp256k1');
$privateKey = $secp256k1->keyFromPrivate($privateKey, 'hex');
$signature = $privateKey->sign($message, [
'canonical' => true
]);
// Ethereum v is recovery param + 35
// Or recovery param + 35 + (chain id * 2)
$signature->recoveryParam += 35;

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);
}
}
29 changes: 29 additions & 0 deletions test/unit/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,33 @@ public function testRecoverPublicKey()
$this->assertEquals('0x044bc2a31265153f07e70e0bab08724e6b85e217f8cd628ceb62974247bb493382ce28cab79ad7119ee1ad3ebcdb98a16805211530ecc6cfefa1b88e6dff99232a', $util->recoverPublicKey('0xdaf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53', '0x28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276', '0x67cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83', 0));
$this->assertEquals('0x044bc2a31265153f07e70e0bab08724e6b85e217f8cd628ceb62974247bb493382ce28cab79ad7119ee1ad3ebcdb98a16805211530ecc6cfefa1b88e6dff99232a', $util->recoverPublicKey('daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53', '28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276', '67cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83', 0));
}

/**
* testEcsign
*
* @return void
*/
public function testEcsign()
{
$util = new Util;
$signature = $util->ecsign('0x4646464646464646464646464646464646464646464646464646464646464646', 'daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53');

// EIP155 test data
$this->assertEquals('28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276', $signature->r->toString(16));
$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 4e5ef4d

Please sign in to comment.