Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/Identities/LegacyAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace ArkEcosystem\Crypto\Identities;

use ArkEcosystem\Crypto\Binary\UnsignedInteger\Writer;
use BitWasp\Bitcoin\Base58;
use BitWasp\Bitcoin\Crypto\Hash;
use BitWasp\Buffertools\Buffer;

class LegacyAddress
{
public static function fromPassphrase(string $passphrase, int $pubKeyHash): string
{
return static::fromPrivateKey(PrivateKey::fromPassphrase($passphrase), $pubKeyHash);
}

public static function fromPublicKey(string $publicKey, int $pubKeyHash): string
{
$ripemd160 = Hash::ripemd160(new Buffer(hex2bin($publicKey)));
$seed = Writer::bit8($pubKeyHash).$ripemd160->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;
}
}
}
64 changes: 64 additions & 0 deletions tests/Unit/Identities/LegacyAddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace ArkEcosystem\Tests\Crypto\Unit\Identities;

use ArkEcosystem\Crypto\Identities\LegacyAddress;
use ArkEcosystem\Crypto\Identities\PrivateKey;

it('should get the address from passphrase', function () {
$fixture = $this->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();
});
9 changes: 9 additions & 0 deletions tests/fixtures/legacy-identity.json
Original file line number Diff line number Diff line change
@@ -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
}
Loading