diff --git a/src/Identities/LegacyAddress.php b/src/Identities/LegacyAddress.php new file mode 100644 index 0000000..a1a55a8 --- /dev/null +++ b/src/Identities/LegacyAddress.php @@ -0,0 +1,46 @@ +getBinary(); + + return Base58::encodeCheck(new Buffer($seed)); + } + + public static function fromPrivateKey(PrivateKey $privateKey, int $pubKeyHash): string + { + return static::fromPublicKey($privateKey->publicKey, $pubKeyHash); + } + + public static function validate(string $address, int $pubKeyHash): bool + { + try { + $decoded = Base58::decodeCheck($address); + + if ($decoded->getSize() !== 21) { + return false; + } + + return ord($decoded->getBinary()[0]) === $pubKeyHash; + } catch (\Exception) { + return false; + } + } +} diff --git a/tests/Unit/Identities/LegacyAddressTest.php b/tests/Unit/Identities/LegacyAddressTest.php new file mode 100644 index 0000000..9922dca --- /dev/null +++ b/tests/Unit/Identities/LegacyAddressTest.php @@ -0,0 +1,64 @@ +getFixture('legacy-identity'); + + $actual = LegacyAddress::fromPassphrase($fixture['passphrase'], $fixture['pubKeyHash']); + + expect($actual)->toBe($fixture['data']['address']); +}); + +it('should get the address from public key', function () { + $fixture = $this->getFixture('legacy-identity'); + + $actual = LegacyAddress::fromPublicKey($fixture['data']['publicKey'], $fixture['pubKeyHash']); + + expect($actual)->toBe($fixture['data']['address']); +}); + +it('should get the address from private key', function () { + $fixture = $this->getFixture('legacy-identity'); + + $privateKey = PrivateKey::fromHex($fixture['data']['privateKey']); + + $actual = LegacyAddress::fromPrivateKey($privateKey, $fixture['pubKeyHash']); + + expect($actual)->toBe($fixture['data']['address']); +}); + +it('should validate the address', function () { + $fixture = $this->getFixture('legacy-identity'); + + $actual = LegacyAddress::validate($fixture['data']['address'], $fixture['pubKeyHash']); + + expect($actual)->toBeTrue(); +}); + +it('should fail to validate address with incorrect pubKeyHash', function () { + $fixture = $this->getFixture('legacy-identity'); + + $actual = LegacyAddress::validate($fixture['data']['address'], 32); + + expect($actual)->toBeFalse(); +}); + +it('should fail to validate an invalid address', function () { + $fixture = $this->getFixture('legacy-identity'); + + $actual = LegacyAddress::validate('D2WFnqYDRiFkSf4ezzWRt3jCsUp2sRmDMifwd', $fixture['pubKeyHash']); + + expect($actual)->toBeFalse(); +}); + +it('should fail to validate address with decoding error', function () { + $actual = LegacyAddress::validate('invalid', 30); + + expect($actual)->toBeFalse(); +}); diff --git a/tests/fixtures/legacy-identity.json b/tests/fixtures/legacy-identity.json new file mode 100644 index 0000000..aa9e2fe --- /dev/null +++ b/tests/fixtures/legacy-identity.json @@ -0,0 +1,9 @@ +{ + "data": { + "publicKey": "02a7c5ca78f6abbced169cb883aec3ffc0a0950affc0de575fb211873b5846e668", + "privateKey": "c7a0df6e1c42268946af49af28c49c6da64419f0203fa970b6e9be9f85a44875", + "address": "D6WFwqYDRiFkSf4ezzWRt3jCsUp2sRmDMi" + }, + "passphrase": "enact busy minimum fantasy endless shoot reduce few inject ostrich snow promote", + "pubKeyHash": 30 +}