Skip to content

Commit

Permalink
refactor: add method to serialize transaction (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries authored Jul 30, 2024
1 parent 9ae1831 commit da97211
Show file tree
Hide file tree
Showing 38 changed files with 46 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/Transactions/Deserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function deserialize(): Transaction
$this->deserializeVendorField($transaction);

// Deserialize type specific parts
$transaction->deserialize($this->buffer);
$transaction->deserializeData($this->buffer);

$this->deserializeSignatures($transaction->data);

Expand All @@ -81,7 +81,7 @@ public function deserialize(): Transaction
*/
public function handleVersionTwo(Transaction $transaction): Transaction
{
$transaction->data['id'] = Hash::sha256(Serializer::new($transaction)->serialize())->getHex();
$transaction->data['id'] = Hash::sha256($transaction->serialize())->getHex();

return $transaction;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Transactions/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function new($transaction)

public static function getBytes(Transaction $transaction, array $options = []): Buffer
{
return self::new($transaction)->serialize($options);
return $transaction->serialize($options);
}

/**
Expand All @@ -66,7 +66,7 @@ public function serialize(array $options = []): Buffer

$this->serializeVendorField($buffer);

$typeBuffer = $this->transaction->serialize($options);
$typeBuffer = $this->transaction->serializeData($options);
$buffer->append($typeBuffer);

$this->serializeSignatures($buffer, $options);
Expand Down
4 changes: 2 additions & 2 deletions src/Transactions/Types/MultiPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MultiPayment extends Transaction
*
* @return string
*/
public function serialize(array $options = []): ByteBuffer
public function serializeData(array $options = []): ByteBuffer
{
$buffer = ByteBuffer::new(1); // initialize with size 1, will expand as we add bytes
$buffer->writeUInt16(count($this->data['asset']['payments']));
Expand All @@ -43,7 +43,7 @@ public function serialize(array $options = []): ByteBuffer
return $buffer;
}

public function deserialize(ByteBuffer $buffer): void
public function deserializeData(ByteBuffer $buffer): void
{
$this->data['asset'] = ['payments' => []];

Expand Down
4 changes: 2 additions & 2 deletions src/Transactions/Types/MultiSignatureRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
class MultiSignatureRegistration extends Transaction
{
public function serialize(array $options = []): ByteBuffer
public function serializeData(array $options = []): ByteBuffer
{
$buffer = ByteBuffer::new(1);

Expand All @@ -36,7 +36,7 @@ public function serialize(array $options = []): ByteBuffer
return $buffer;
}

public function deserialize(ByteBuffer $buffer): void
public function deserializeData(ByteBuffer $buffer): void
{
$asset = [
'multiSignature' => [
Expand Down
9 changes: 7 additions & 2 deletions src/Transactions/Types/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,17 @@ public function secondVerify(string $secondPublicKey): bool
return $this->temporarySignerVerify($transaction, $signature, $secondPublicKey);
}

public function serialize(array $options = []): Buffer
{
return Serializer::new($this)->serialize($options);
}

/**
* Perform AIP11 compliant serialization.
*
* @return ByteBuffer $buffer
*/
abstract public function serialize(array $options = []): ByteBuffer;
abstract public function serializeData(array $options = []): ByteBuffer;

/**
* Perform AIP11 compliant deserialization.
Expand All @@ -159,7 +164,7 @@ abstract public function serialize(array $options = []): ByteBuffer;
*
* @return void
*/
abstract public function deserialize(ByteBuffer $buffer): void;
abstract public function deserializeData(ByteBuffer $buffer): void;

/**
* Convert the transaction to its array representation.
Expand Down
4 changes: 2 additions & 2 deletions src/Transactions/Types/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
class Transfer extends Transaction
{
public function serialize(array $options = []): ByteBuffer
public function serializeData(array $options = []): ByteBuffer
{
$buffer = ByteBuffer::new(24);

Expand All @@ -36,7 +36,7 @@ public function serialize(array $options = []): ByteBuffer
return $buffer;
}

public function deserialize(ByteBuffer $buffer): void
public function deserializeData(ByteBuffer $buffer): void
{
$this->data['amount'] = strval($buffer->readUInt64());

Expand Down
4 changes: 2 additions & 2 deletions src/Transactions/Types/UsernameRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class UsernameRegistration extends Transaction
{
public function serialize(array $options = []): ByteBuffer
public function serializeData(array $options = []): ByteBuffer
{
$buffer = ByteBuffer::new(1);

Expand All @@ -32,7 +32,7 @@ public function serialize(array $options = []): ByteBuffer
return $buffer;
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/Transactions/Types/UsernameResignation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
*/
class UsernameResignation extends Transaction
{
public function serialize(array $options = []): ByteBuffer
public function serializeData(array $options = []): ByteBuffer
{
return ByteBuffer::new(0);
}

public function deserialize(ByteBuffer $buffer): void
public function deserializeData(ByteBuffer $buffer): void
{
}
}
4 changes: 2 additions & 2 deletions src/Transactions/Types/ValidatorRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
*/
class ValidatorRegistration extends Transaction
{
public function serialize(array $options = []): ByteBuffer
public function serializeData(array $options = []): ByteBuffer
{
$buffer = ByteBuffer::new(1);
$buffer->writeHex($this->data['asset']['validatorPublicKey']);

return $buffer;
}

public function deserialize(ByteBuffer $buffer): void
public function deserializeData(ByteBuffer $buffer): void
{
$this->data['asset'] = [
'validatorPublicKey' => $buffer->readHex(48 * 2),
Expand Down
4 changes: 2 additions & 2 deletions src/Transactions/Types/ValidatorResignation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
*/
class ValidatorResignation extends Transaction
{
public function serialize(array $options = []): ByteBuffer
public function serializeData(array $options = []): ByteBuffer
{
return ByteBuffer::new(0);
}

public function deserialize(ByteBuffer $buffer): void
public function deserializeData(ByteBuffer $buffer): void
{
}
}
4 changes: 2 additions & 2 deletions src/Transactions/Types/Vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
class Vote extends Transaction
{
public function serialize(array $options = []): ByteBuffer
public function serializeData(array $options = []): ByteBuffer
{
$buffer = ByteBuffer::new(24);

Expand All @@ -38,7 +38,7 @@ public function serialize(array $options = []): ByteBuffer
return $buffer;
}

public function deserialize(ByteBuffer $buffer): void
public function deserializeData(ByteBuffer $buffer): void
{
$voteLength = $buffer->readUInt8();

Expand Down
3 changes: 1 addition & 2 deletions tests/Concerns/Deserialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace ArkEcosystem\Tests\Crypto\Concerns;

use ArkEcosystem\Crypto\Transactions\Deserializer;
use ArkEcosystem\Crypto\Transactions\Serializer;
use Illuminate\Support\Arr;

trait Deserialize
Expand All @@ -24,7 +23,7 @@ protected function assertDeserialized(array $expected, array $keys, int $network
$actual = Deserializer::new($expected['serialized'])->deserialize();
$data = $actual->data;

$this->assertSame($expected['serialized'], Serializer::new($actual)->serialize()->getHex());
$this->assertSame($expected['serialized'], $actual->serialize()->getHex());
$this->assertSameTransactions($expected, $data, $keys);

return $actual;
Expand Down
5 changes: 1 addition & 4 deletions tests/Concerns/Serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace ArkEcosystem\Tests\Crypto\Concerns;

use ArkEcosystem\Crypto\Enums\Types;
use ArkEcosystem\Crypto\Transactions\Serializer;

trait Serialize
{
Expand All @@ -25,8 +24,6 @@ protected function assertSerialized(array $fixture): void
$transaction = new $transactionClass();
$transaction->data = $data;

$actual = Serializer::new($transaction)->serialize();

$this->assertSame($fixture['serialized'], $actual->getHex());
$this->assertSame($fixture['serialized'], $transaction->serialize()->getHex());
}
}
7 changes: 3 additions & 4 deletions tests/Unit/Transactions/Builder/MultiPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use ArkEcosystem\Crypto\Identities\PublicKey;
use ArkEcosystem\Crypto\Transactions\Builder\MultiPaymentBuilder;
use ArkEcosystem\Crypto\Transactions\Serializer;
use ArkEcosystem\Tests\Crypto\TestCase;

/**
Expand Down Expand Up @@ -55,7 +54,7 @@ public function it_should_multi_sign()

$this->assertTrue($builder->verify());

$this->assertSameSerializationMultisignature($fixture['serialized'], Serializer::new($builder->transaction)->serialize()->getHex(), 3);
$this->assertSameSerializationMultisignature($fixture['serialized'], $builder->transaction->serialize()->getHex(), 3);

$this->assertSameTransactions($fixture, $builder->transaction->data);
}
Expand Down Expand Up @@ -83,7 +82,7 @@ public function it_should_match_fixture_passphrase()
->sign($this->passphrase);

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

Expand All @@ -101,7 +100,7 @@ public function it_should_match_fixture_vendor_field_passphrase()
->sign($this->passphrase);

$this->assertTrue($builder->verify());
$this->assertSameSerialization($fixture['serialized'], Serializer::new($builder->transaction)->serialize()->getHex());
$this->assertSameSerialization($fixture['serialized'], $builder->transaction->serialize()->getHex());
$this->assertSameTransactions($fixture, $builder->transaction->data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use ArkEcosystem\Crypto\Identities\PublicKey;
use ArkEcosystem\Crypto\Transactions\Builder\MultiSignatureRegistrationBuilder;
use ArkEcosystem\Crypto\Transactions\Serializer;
use ArkEcosystem\Tests\Crypto\TestCase;

/**
Expand Down Expand Up @@ -78,7 +77,7 @@ public function it_should_match_fixture_passphrase()

$builder->sign($this->passphrase);

$serialized = Serializer::new($builder->transaction)->serialize()->getHex();
$serialized = $builder->transaction->serialize()->getHex();
$this->assertTrue($builder->verify());
$this->assertSameSerializationMultisignature($fixture['serialized'], $serialized, 3);
$this->assertSignaturesAreSerialized($serialized, $builder->transaction->data['signatures']);
Expand Down
7 changes: 3 additions & 4 deletions tests/Unit/Transactions/Builder/TransferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use ArkEcosystem\Crypto\Identities\PublicKey;
use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;
use ArkEcosystem\Crypto\Transactions\Serializer;
use ArkEcosystem\Tests\Crypto\TestCase;

/**
Expand Down Expand Up @@ -58,7 +57,7 @@ public function it_should_multi_sign()

$this->assertTrue($builder->verify());

$this->assertSameSerializationMultisignature($fixture['serialized'], Serializer::new($builder->transaction)->serialize()->getHex(), 3);
$this->assertSameSerializationMultisignature($fixture['serialized'], $builder->transaction->serialize()->getHex(), 3);

$this->assertSameTransactions($fixture, $builder->transaction->data);
}
Expand Down Expand Up @@ -91,7 +90,7 @@ public function it_should_match_fixture_passphrase()

$this->assertTrue($builder->verify());

$this->assertSameSerialization($fixture['serialized'], Serializer::new($builder->transaction)->serialize()->getHex());
$this->assertSameSerialization($fixture['serialized'], $builder->transaction->serialize()->getHex());

$this->assertSameTransactions($fixture, $builder->transaction->data);
}
Expand All @@ -111,7 +110,7 @@ public function it_should_match_fixture_vendor_field_passphrase()
->sign($this->passphrase);

$this->assertTrue($builder->verify());
$this->assertSameSerialization($fixture['serialized'], Serializer::new($builder->transaction)->serialize()->getHex());
$this->assertSameSerialization($fixture['serialized'], $builder->transaction->serialize()->getHex());
$this->assertSameTransactions($fixture, $builder->transaction->data);
}
}
5 changes: 2 additions & 3 deletions tests/Unit/Transactions/Builder/UsernameRegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

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

/**
Expand Down Expand Up @@ -55,7 +54,7 @@ public function it_should_multi_sign()

$this->assertTrue($builder->verify());

$this->assertSameSerializationMultisignature($fixture['serialized'], Serializer::new($builder->transaction)->serialize()->getHex(), 3);
$this->assertSameSerializationMultisignature($fixture['serialized'], $builder->transaction->serialize()->getHex(), 3);

$this->assertSameTransactions($fixture, $builder->transaction->data);
}
Expand Down Expand Up @@ -84,7 +83,7 @@ public function it_should_match_fixture_passphrase()
->sign($this->passphrase);

$this->assertTrue($builder->verify());
$this->assertSameSerialization($fixture['serialized'], Serializer::new($builder->transaction)->serialize()->getHex());
$this->assertSameSerialization($fixture['serialized'], $builder->transaction->serialize()->getHex(), 3);
$this->assertSameTransactions($fixture, $builder->transaction->data);
}
}
5 changes: 2 additions & 3 deletions tests/Unit/Transactions/Builder/UsernameResignationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

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

/**
Expand Down Expand Up @@ -52,7 +51,7 @@ public function it_should_multi_sign()

$this->assertTrue($builder->verify());

$this->assertSameSerializationMultisignature($fixture['serialized'], Serializer::new($builder->transaction)->serialize()->getHex(), 3);
$this->assertSameSerializationMultisignature($fixture['serialized'], $builder->transaction->serialize()->getHex(), 3);

$this->assertSameTransactions($fixture, $builder->transaction->data);
}
Expand All @@ -79,7 +78,7 @@ public function it_should_match_fixture_passphrase()
->sign($this->passphrase);

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

0 comments on commit da97211

Please sign in to comment.