HexPrivateKeySerializerTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace kornrunner;
  3. use kornrunner\Serializer\HexPrivateKeySerializer;
  4. use Mdanter\Ecc\Curves\CurveFactory;
  5. use Mdanter\Ecc\Curves\SecgCurve;
  6. class HexPrivateKeySerializerTest extends TestCase
  7. {
  8. protected $serializer;
  9. public function setUp()
  10. {
  11. parent::setUp();
  12. $generator = CurveFactory::getGeneratorByName(SecgCurve::NAME_SECP_256K1);
  13. $this->serializer = new HexPrivateKeySerializer($generator);
  14. }
  15. /**
  16. * @dataProvider parse
  17. */
  18. public function testParse(string $privateKey) {
  19. $key = $this->serializer->parse($privateKey);
  20. $this->assertEquals(gmp_init($privateKey, 16), $key->getSecret());
  21. }
  22. public static function parse(): array {
  23. return [
  24. ['d0459987fdde1f41e524fddbf4b646cd9d3bea7fd7d63feead3f5dfce6174a3d'],
  25. ['0xd0459987fdde1f41e524fddbf4b646cd9d3bea7fd7d63feead3f5dfce6174a3d'],
  26. ];
  27. }
  28. /**
  29. * @dataProvider serialize
  30. */
  31. public function testSerialize(string $privateKey, string $expect) {
  32. $key = $this->serializer->serialize($this->serializer->parse($privateKey));
  33. $this->assertEquals($expect, $key);
  34. }
  35. public static function serialize(): array {
  36. return [
  37. ['d0459987fdde1f41e524fddbf4b646cd9d3bea7fd7d63feead3f5dfce6174a3d', 'd0459987fdde1f41e524fddbf4b646cd9d3bea7fd7d63feead3f5dfce6174a3d'],
  38. ['0xd0459987fdde1f41e524fddbf4b646cd9d3bea7fd7d63feead3f5dfce6174a3d', 'd0459987fdde1f41e524fddbf4b646cd9d3bea7fd7d63feead3f5dfce6174a3d'],
  39. ];
  40. }
  41. }