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

feat: add username transactions #124

Merged
merged 3 commits into from
Jul 30, 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
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Configuration/Fee.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class Fee
Types::IPFS => Fees::IPFS,
Types::MULTI_PAYMENT => Fees::MULTI_PAYMENT,
Types::VALIDATOR_RESIGNATION => Fees::VALIDATOR_RESIGNATION,
Types::HTLC_LOCK => Fees::HTLC_LOCK,
Types::HTLC_CLAIM => Fees::HTLC_CLAIM,
Types::USERNAME_REGISTRATION => Fees::USERNAME_REGISTRATION,
Types::USERNAME_RESIGNATION => Fees::USERNAME_RESIGNATION,
Types::HTLC_REFUND => Fees::HTLC_REFUND,
];

Expand Down
4 changes: 2 additions & 2 deletions src/Enums/Fees.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class Fees

public const VALIDATOR_RESIGNATION = '2500000000';

public const HTLC_LOCK = '10000000';
public const USERNAME_REGISTRATION = '2500000000';

public const HTLC_CLAIM = '0';
public const USERNAME_RESIGNATION = '2500000000';

public const HTLC_REFUND = '0';
}
4 changes: 2 additions & 2 deletions src/Enums/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class Types

public const VALIDATOR_RESIGNATION = 7;

public const HTLC_LOCK = 8;
public const USERNAME_REGISTRATION = 8;

public const HTLC_CLAIM = 9;
public const USERNAME_RESIGNATION = 9;

public const HTLC_REFUND = 10;
}
2 changes: 1 addition & 1 deletion src/Transactions/Builder/HtlcLockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function vendorField(string $vendorField): self
*/
protected function getType(): int
{
return \ArkEcosystem\Crypto\Enums\Types::HTLC_LOCK;
return \ArkEcosystem\Crypto\Enums\Types::USERNAME_REGISTRATION;
}

protected function getTypeGroup(): int
Expand Down
66 changes: 66 additions & 0 deletions src/Transactions/Builder/UsernameRegistrationBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

/*
* This file is part of Ark PHP Crypto.
*
* (c) Ark Ecosystem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArkEcosystem\Crypto\Transactions\Builder;

use ArkEcosystem\Crypto\Transactions\Types\UsernameRegistration;

/**
* This is the username registration transaction class.
*/
class UsernameRegistrationBuilder extends AbstractTransactionBuilder
{
/**
* Create a new delegate registration transaction instance.
*/
public function __construct()
{
parent::__construct();

$this->transaction->data['asset'] = [];
}

/**
* Set the username to assign.
*
* @param string $username
*
* @return self
*/
public function usernameAsset(string $username): self
{
if ($username) {
$this->transaction->data['asset']['username'] = $username;
}

return $this;
}

/**
* {@inheritdoc}
*/
protected function getType(): int
{
return \ArkEcosystem\Crypto\Enums\Types::USERNAME_REGISTRATION;
}

protected function getTypeGroup(): int
{
return \ArkEcosystem\Crypto\Enums\TypeGroup::CORE;
}

protected function getTransactionInstance(): object
{
return new UsernameRegistration();
}
}
48 changes: 48 additions & 0 deletions src/Transactions/Builder/UsernameResignationBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/*
* This file is part of Ark PHP Crypto.
*
* (c) Ark Ecosystem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArkEcosystem\Crypto\Transactions\Builder;

use ArkEcosystem\Crypto\Transactions\Types\UsernameResignation;

/**
* This is the username resignation transaction class.
*/
class UsernameResignationBuilder extends AbstractTransactionBuilder
{
/**
* Create a new username resignation transaction instance.
*/
public function __construct()
{
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function getType(): int
{
return \ArkEcosystem\Crypto\Enums\Types::USERNAME_RESIGNATION;
}

protected function getTypeGroup(): int
{
return \ArkEcosystem\Crypto\Enums\TypeGroup::CORE;
}

protected function getTransactionInstance(): object
{
return new UsernameResignation();
}
}
5 changes: 2 additions & 3 deletions src/Transactions/Deserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ class Deserializer
Types\IPFS::class,
Types\MultiPayment::class,
Types\ValidatorResignation::class,
Types\HtlcLock::class,
Types\HtlcClaim::class,
Types\HtlcRefund::class,
Types\UsernameRegistration::class,
Types\UsernameResignation::class,
];

/**
Expand Down
43 changes: 43 additions & 0 deletions src/Transactions/Types/UsernameRegistration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/*
* This file is part of Ark PHP Crypto.
*
* (c) Ark Ecosystem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArkEcosystem\Crypto\Transactions\Types;

use ArkEcosystem\Crypto\ByteBuffer\ByteBuffer;

/**
* This is the serializer class.
*/
class UsernameRegistration extends Transaction
{
public function serialize(array $options = []): ByteBuffer
{
$buffer = ByteBuffer::new(1);

$username = $this->data['asset']['username'];

$buffer->writeUint8(strlen($username));
$buffer->writeString($username);

return $buffer;
}

public function deserialize(ByteBuffer $buffer): void
{
$usernameLength = $buffer->readUint8();

$this->data['asset'] = [
'username' => $buffer->readString($usernameLength),
];
}
}
31 changes: 31 additions & 0 deletions src/Transactions/Types/UsernameResignation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of Ark PHP Crypto.
*
* (c) Ark Ecosystem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArkEcosystem\Crypto\Transactions\Types;

use ArkEcosystem\Crypto\ByteBuffer\ByteBuffer;

/**
* This is the serializer class.
*/
class UsernameResignation extends Transaction
{
public function serialize(array $options = []): ByteBuffer
{
return ByteBuffer::new(0);
}

public function deserialize(ByteBuffer $buffer): void
{
}
}
10 changes: 4 additions & 6 deletions tests/Concerns/Serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
namespace ArkEcosystem\Tests\Crypto\Concerns;

use ArkEcosystem\Crypto\Transactions\Serializer;
use ArkEcosystem\Crypto\Transactions\Types\HtlcClaim;
use ArkEcosystem\Crypto\Transactions\Types\HtlcLock;
use ArkEcosystem\Crypto\Transactions\Types\HtlcRefund;
use ArkEcosystem\Crypto\Transactions\Types\IPFS;
use ArkEcosystem\Crypto\Transactions\Types\MultiPayment;
use ArkEcosystem\Crypto\Transactions\Types\MultiSignatureRegistration;
use ArkEcosystem\Crypto\Transactions\Types\SecondSignatureRegistration;
use ArkEcosystem\Crypto\Transactions\Types\Transfer;
use ArkEcosystem\Crypto\Transactions\Types\UsernameRegistration;
use ArkEcosystem\Crypto\Transactions\Types\UsernameResignation;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorRegistration;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorResignation;
use ArkEcosystem\Crypto\Transactions\Types\Vote;
Expand All @@ -37,9 +36,8 @@ trait Serialize
IPFS::class,
MultiPayment::class,
ValidatorResignation::class,
HtlcLock::class,
HtlcClaim::class,
HtlcRefund::class,
UsernameRegistration::class,
UsernameResignation::class,
];

protected function assertSerialized(array $fixture): void
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/Transactions/Builder/UsernameRegistrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/*
* This file is part of Ark PHP Crypto.
*
* (c) Ark Ecosystem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArkEcosystem\Tests\Crypto\Unit\Transactions\Builder;

use ArkEcosystem\Crypto\Transactions\Builder\UsernameRegistrationBuilder;
use ArkEcosystem\Crypto\Transactions\Serializer;
use ArkEcosystem\Tests\Crypto\TestCase;

/**
* This is the delegate registration builder test class.
*
* @author Brian Faust <[email protected]>
* @covers \ArkEcosystem\Crypto\Transactions\Builder\UsernameRegistrationBuilder
*/
class UsernameRegistrationTest extends TestCase
{
/** @test */
public function it_should_sign_it_with_a_passphrase()
{
$transaction = UsernameRegistrationBuilder::new()
->usernameAsset('alfonsobries')
->sign($this->passphrase);

$this->assertTrue($transaction->verify());
}

/** @test */
public function it_should_match_fixture_passphrase()
{
$fixture = $this->getTransactionFixture('username_registration', 'username-registration-sign');

$builder = UsernameRegistrationBuilder::new()
->withFee($fixture['data']['fee'])
->withNonce($fixture['data']['nonce'])
->withNetwork($fixture['data']['network'])
->usernameAsset($fixture['data']['asset']['username'])
->sign($this->passphrase);

$this->assertTrue($builder->verify());
$this->assertSameSerialization($fixture['serialized'], Serializer::new($builder->transaction)->serialize()->getHex());
$this->assertSameTransactions($fixture, $builder->transaction->data);
}
}
51 changes: 51 additions & 0 deletions tests/Unit/Transactions/Builder/UsernameResignationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* This file is part of Ark PHP Crypto.
*
* (c) Ark Ecosystem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArkEcosystem\Tests\Crypto\Unit\Transactions\Builder;

use ArkEcosystem\Crypto\Transactions\Builder\UsernameResignationBuilder;
use ArkEcosystem\Crypto\Transactions\Serializer;
use ArkEcosystem\Tests\Crypto\TestCase;

/**
* This is the username resignation builder test class.
*
* @covers \ArkEcosystem\Crypto\Transactions\Builder\UsernameResignationBuilder
*/
class UsernameResignationTest extends TestCase
{
/** @test */
public function it_should_sign_it_with_a_passphrase()
{
$transaction = UsernameResignationBuilder::new()
->sign($this->passphrase);

$this->assertTrue($transaction->verify());
}

/** @test */
public function it_should_match_fixture_passphrase()
{
$fixture = $this->getTransactionFixture('username_resignation', 'username-resignation-sign');

$builder = UsernameResignationBuilder::new()
->withFee($fixture['data']['fee'])
->withNonce($fixture['data']['nonce'])
->withNetwork($fixture['data']['network'])
->sign($this->passphrase);

$this->assertTrue($builder->verify());
$this->assertSameSerialization($fixture['serialized'], Serializer::new($builder->transaction)->serialize()->getHex());
$this->assertSameTransactions($fixture, $builder->transaction->data);
}
}
Loading
Loading