Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add default contract addresses #143

Merged
merged 10 commits into from
Dec 17, 2024
Merged
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
12 changes: 12 additions & 0 deletions src/Enums/ContractAddresses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace ArkEcosystem\Crypto\Enums;

enum ContractAddresses: string
{
case CONSENSUS = '0x535B3D7A252fa034Ed71F0C53ec0C6F784cB64E1';
case MULTIPAYMENT = '0x83769BeEB7e5405ef0B7dc3C66C43E3a51A6d27f';
case USERNAMES = '0x2c1DE3b4Dbb4aDebEbB5dcECAe825bE2a9fc6eb6';
}
2 changes: 0 additions & 2 deletions src/Identities/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ArkEcosystem\Crypto\Identities;

use ArkEcosystem\Crypto\Networks\AbstractNetwork;
use ArkEcosystem\Crypto\Utils\Address as AddressUtils;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PrivateKey as EccPrivateKey;
use Elliptic\EC;
Expand Down Expand Up @@ -91,7 +90,6 @@ public static function fromPrivateKey(EccPrivateKey $privateKey): string
* Validate the given address.
*
* @param string $address
* @param AbstractNetwork|int|null $network
*
* @return bool
*/
Expand Down
8 changes: 3 additions & 5 deletions src/Identities/PrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ArkEcosystem\Crypto\Identities;

use ArkEcosystem\Crypto\Networks\AbstractNetwork;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PrivateKey as EcPrivateKey;
Expand Down Expand Up @@ -53,18 +52,17 @@ public static function fromHex($privateKey): EcPrivateKey
/**
* Derive the private key for the given WIF.
*
* @param string $wif
* @param AbstractNetwork|null $network
* @param string $wif
*
* @return EcPrivateKey
*/
public static function fromWif(string $wif, AbstractNetwork $network = null): EcPrivateKey
public static function fromWif(string $wif): EcPrivateKey
{
return (new PrivateKeyFactory(
EcAdapterFactory::getPhpEcc(
Bitcoin::getMath(),
Bitcoin::getGenerator()
)
))->fromWif($wif, $network);
))->fromWif($wif);
}
}
9 changes: 3 additions & 6 deletions src/Identities/WIF.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

namespace ArkEcosystem\Crypto\Identities;

use ArkEcosystem\Crypto\Networks\AbstractNetwork;

class WIF
{
/**
* Derive the WIF from the given passphrase.
*
* @param string $passphrase
* @param AbstractNetwork|null $network
* @param string $passphrase
*
* @return string
*/
public static function fromPassphrase(string $passphrase, AbstractNetwork $network = null): string
public static function fromPassphrase(string $passphrase): string
{
return PrivateKey::fromPassphrase($passphrase)->toWif($network);
return PrivateKey::fromPassphrase($passphrase)->toWif();
}
}
45 changes: 0 additions & 45 deletions src/Networks/Testnet.php

This file was deleted.

8 changes: 8 additions & 0 deletions src/Transactions/Builder/UnvoteBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace ArkEcosystem\Crypto\Transactions\Builder;

use ArkEcosystem\Crypto\Enums\ContractAddresses;
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;
use ArkEcosystem\Crypto\Transactions\Types\Unvote;

class UnvoteBuilder extends AbstractTransactionBuilder
{
public function __construct(?array $data = null)
{
parent::__construct($data);

$this->recipientAddress(ContractAddresses::CONSENSUS->value);
}

protected function getTransactionInstance(?array $data = []): AbstractTransaction
{
return new Unvote($data);
Expand Down
8 changes: 8 additions & 0 deletions src/Transactions/Builder/UsernameRegistrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

namespace ArkEcosystem\Crypto\Transactions\Builder;

use ArkEcosystem\Crypto\Enums\ContractAddresses;
use ArkEcosystem\Crypto\Helpers;
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;
use ArkEcosystem\Crypto\Transactions\Types\UsernameRegistration;

class UsernameRegistrationBuilder extends AbstractTransactionBuilder
{
public function __construct(?array $data = null)
{
parent::__construct($data);

$this->recipientAddress(ContractAddresses::USERNAMES->value);
}

public function username(string $username): self
{
Helpers::isValidUsername($username);
Expand Down
8 changes: 8 additions & 0 deletions src/Transactions/Builder/UsernameResignationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace ArkEcosystem\Crypto\Transactions\Builder;

use ArkEcosystem\Crypto\Enums\ContractAddresses;
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;
use ArkEcosystem\Crypto\Transactions\Types\UsernameResignation;

class UsernameResignationBuilder extends AbstractTransactionBuilder
{
public function __construct(?array $data = null)
{
parent::__construct($data);

$this->recipientAddress(ContractAddresses::USERNAMES->value);
}

protected function getTransactionInstance(?array $data = []): AbstractTransaction
{
return new UsernameResignation($data);
Expand Down
8 changes: 8 additions & 0 deletions src/Transactions/Builder/ValidatorRegistrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace ArkEcosystem\Crypto\Transactions\Builder;

use ArkEcosystem\Crypto\Enums\ContractAddresses;
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorRegistration;

class ValidatorRegistrationBuilder extends AbstractTransactionBuilder
{
public function __construct(?array $data = null)
{
parent::__construct($data);

$this->recipientAddress(ContractAddresses::CONSENSUS->value);
}

public function validatorPublicKey(string $validatorPublicKey): self
{
$this->transaction->data['validatorPublicKey'] = $validatorPublicKey;
Expand Down
8 changes: 8 additions & 0 deletions src/Transactions/Builder/ValidatorResignationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace ArkEcosystem\Crypto\Transactions\Builder;

use ArkEcosystem\Crypto\Enums\ContractAddresses;
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorResignation;

class ValidatorResignationBuilder extends AbstractTransactionBuilder
{
public function __construct(?array $data = null)
{
parent::__construct($data);

$this->recipientAddress(ContractAddresses::CONSENSUS->value);
}

protected function getTransactionInstance(?array $data = []): AbstractTransaction
{
return new ValidatorResignation($data);
Expand Down
8 changes: 8 additions & 0 deletions src/Transactions/Builder/VoteBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace ArkEcosystem\Crypto\Transactions\Builder;

use ArkEcosystem\Crypto\Enums\ContractAddresses;
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;
use ArkEcosystem\Crypto\Transactions\Types\Vote;

class VoteBuilder extends AbstractTransactionBuilder
{
public function __construct(?array $data = null)
{
parent::__construct($data);

$this->recipientAddress(ContractAddresses::CONSENSUS->value);
}

public function vote(string $vote): self
{
$this->transaction->data['vote'] = $vote;
Expand Down
2 changes: 1 addition & 1 deletion src/Transactions/Deserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function guessTransactionFromData(array $data): AbstractTransaction
$payloadData = $this->decodePayload($data);

if ($payloadData === null) {
return new EvmCall();
return new Transfer($data);
}

$functionName = $payloadData['functionName'];
Expand Down
30 changes: 15 additions & 15 deletions src/Transactions/Types/AbstractTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@ public function __construct(?array $data = null)

abstract public function getPayload(): string;

public function decodePayload(array $data): ?array
{
if (! isset($data['data'])) {
return null;
}

$payload = $data['data'];

if ($payload === '') {
return null;
}

return (new AbiDecoder())->decodeFunctionData($payload);
}

public function refreshPayloadData(): static
{
$this->data['data'] = ltrim($this->getPayload(), '0x');
Expand Down Expand Up @@ -185,6 +170,21 @@ public function hash(bool $skipSignature): BufferInterface
return TransactionHasher::toHash($hashData, $skipSignature);
}

protected function decodePayload(array $data): ?array
{
if (! isset($data['data'])) {
return null;
}

$payload = $data['data'];

if ($payload === '') {
return null;
}

return (new AbiDecoder())->decodeFunctionData($payload);
}

private function getSignature(): CompactSignatureInterface
{
$ecAdapter = EcAdapterFactory::getPhpEcc(
Expand Down
2 changes: 0 additions & 2 deletions src/Utils/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace ArkEcosystem\Crypto\Utils;

use ArkEcosystem\Crypto\ByteBuffer\ByteBuffer;
use ArkEcosystem\Crypto\Networks\AbstractNetwork;
use BitWasp\Buffertools\Buffer;
use kornrunner\Keccak;

Expand All @@ -15,7 +14,6 @@ class Address
* Validate the given address.
*
* @param string $address
* @param AbstractNetwork|int|null $network
*
* @return bool
*/
Expand Down
22 changes: 0 additions & 22 deletions tests/Unit/Networks/TestnetTest.php

This file was deleted.

1 change: 0 additions & 1 deletion tests/Unit/Transactions/Builder/UnvoteBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function it_should_sign_it_with_a_passphrase()
->nonce($fixture['data']['nonce'])
->network($fixture['data']['network'])
->gasLimit($fixture['data']['gasLimit'])
->recipientAddress($fixture['data']['recipientAddress'])
->sign($this->passphrase);

$this->assertSame($fixture['serialized'], $builder->transaction->serialize()->getHex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function it_should_sign_it_with_a_passphrase()
->nonce($fixture['data']['nonce'])
->network($fixture['data']['network'])
->gasLimit($fixture['data']['gasLimit'])
->recipientAddress($fixture['data']['recipientAddress'])
->username('php')
->sign($this->passphrase);

Expand All @@ -47,7 +46,6 @@ public function it_should_throw_exception_for_invalid_username()
->nonce($fixture['data']['nonce'])
->network($fixture['data']['network'])
->gasLimit($fixture['data']['gasLimit'])
->recipientAddress($fixture['data']['recipientAddress'])
->username('this_is_a_very_long_username_that_is_invalid')
->sign($this->passphrase);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function it_should_sign_it_with_a_passphrase()
->nonce($fixture['data']['nonce'])
->network($fixture['data']['network'])
->gasLimit($fixture['data']['gasLimit'])
->recipientAddress($fixture['data']['recipientAddress'])
->sign($this->passphrase);

$this->assertSame($fixture['serialized'], $builder->transaction->serialize()->getHex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function it_should_sign_it_with_a_passphrase()
->nonce($fixture['data']['nonce'])
->network($fixture['data']['network'])
->gasLimit($fixture['data']['gasLimit'])
->validatorPublicKey('a08058db53e2665c84a40f5152e76dd2b652125a6079130d4c315e728bcf4dd1dfb44ac26e82302331d61977d3141118')
->recipientAddress($fixture['data']['recipientAddress'])
->validatorPublicKey('954f46d6097a1d314e900e66e11e0dad0a57cd03e04ec99f0dedd1c765dcb11e6d7fa02e22cf40f9ee23d9cc1c0624bd')
->sign($this->passphrase);

$this->assertSame($fixture['serialized'], $builder->transaction->serialize()->getHex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function it_should_sign_it_with_a_passphrase()
->nonce($fixture['data']['nonce'])
->network($fixture['data']['network'])
->gasLimit($fixture['data']['gasLimit'])
->recipientAddress($fixture['data']['recipientAddress'])
->sign($this->passphrase);

$this->assertSame($fixture['serialized'], $builder->transaction->serialize()->getHex());
Expand Down
Loading
Loading