From 89197ed3992d74f0de76702cd82ec32d1264dad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Mom=C4=8Dilovi=C4=87?= Date: Thu, 25 Mar 2021 12:44:33 +0100 Subject: [PATCH] Address: left pad (#2) --- src/Address.php | 2 +- test/AddressTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Address.php b/src/Address.php index 1359a72..a3eff88 100644 --- a/src/Address.php +++ b/src/Address.php @@ -28,7 +28,7 @@ public function __construct(string $privateKey = '') { } public function getPrivateKey(): string { - return str_pad(gmp_strval($this->privateKey->getSecret(), 16), 64, '0'); + return str_pad(gmp_strval($this->privateKey->getSecret(), 16), 64, '0', STR_PAD_LEFT); } public function getPublicKey(): string { diff --git a/test/AddressTest.php b/test/AddressTest.php index 5039839..ccd6604 100644 --- a/test/AddressTest.php +++ b/test/AddressTest.php @@ -22,6 +22,22 @@ public function testCreateFromPrivateKey(): void { $this->assertSame('677a637ec8f0bb2c8d33c6ace08054e521bff4b5', $address->get()); $this->assertSame('5f65c9c32a4e38393b79ccf94913c1e5dbe7071d4264aad290d936c4bb2a7c0e3a71ebc855aaadd38f477320d54cd88e5133bfcf97bbf037252db4cd824ab902', $address->getPublicKey()); } + /** + * @dataProvider privateKeyPading + */ + public function testPrivateKeyPadding($key, $public): void { + $address = new Address($key); + $this->assertSame($public, $address->get()); + } + + public static function privateKeyPading(): array { + return [ + ['93262d84237f92dc8e4409062dcc9dfc8cdc211ec32b18aa073af15841cd8440', '669d9098736e33b8a0ee0470c10357b66caac548'], + ['093262d84237f92dc8e4409062dcc9dfc8cdc211ec32b18aa073af15841cd844', '2c10383ae14f59415979d7c232ca2c85b62c18a9'], + ['07a51d7d4445c567c12639ca38e4c9fc4b12f6ec9f0aab82f98c28acaae446a3', 'f81153ba99e401149c6d028eb39fd657e474e7c0'], + ['7a51d7d4445c567c12639ca38e4c9fc4b12f6ec9f0aab82f98c28acaae446a30', 'f783c3bccfcc24a3731eb25b9587bf5071aab592'], + ]; + } public function testThrowsNotHex(): void { $this->expectException(InvalidArgumentException::class);