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

fix: transaction id & serialization issues #136

Merged
merged 8 commits into from
Nov 15, 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
6 changes: 2 additions & 4 deletions src/Transactions/Builder/AbstractTransactionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ abstract class AbstractTransactionBuilder

public function __construct(?array $data = null)
{
$this->transaction = $this->getTransactionInstance();

$this->transaction->data = $data ?? [
$this->transaction = $this->getTransactionInstance($data ?? [
'value' => '0',
'senderPublicKey' => '',
'gasPrice' => '5',
'nonce' => '1',
'network' => Network::get()->pubKeyHash(),
'gasLimit' => 1_000_000,
'data' => '',
];
]);
}

public function __toString(): string
Expand Down
1 change: 1 addition & 0 deletions src/Transactions/Builder/EvmCallBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class EvmCallBuilder extends AbstractTransactionBuilder
public function payload(string $payload): self
{
$payload = ltrim($payload, '0x');

$this->transaction->data['data'] = $payload;

return $this;
Expand Down
2 changes: 2 additions & 0 deletions src/Transactions/Builder/TransferBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function value(string $value): self
{
$this->transaction->data['value'] = $value;

$this->transaction->refreshPayloadData();

return $this;
}

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

$this->transaction->refreshPayloadData();

return $this;
}

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

$this->transaction->refreshPayloadData();

return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Transactions/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private function serializeData(ByteBuffer $buffer): void
$buffer->writeUInt8(0); // No recipient
}

$payloadHex = ltrim($this->transaction->getPayload(), '0x');
$payloadHex = $this->transaction->data['data'] ?? '';

$payloadLength = strlen($payloadHex);

Expand Down
12 changes: 10 additions & 2 deletions src/Transactions/Types/AbstractTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Serializer\Signature\CompactSignatureSerializer;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\CompactSignatureInterface;
use BitWasp\Bitcoin\Crypto\Hash;
use BitWasp\Buffertools\Buffer;
use BitWasp\Buffertools\BufferInterface;

Expand All @@ -26,6 +25,8 @@ abstract class AbstractTransaction
public function __construct(?array $data = null)
{
$this->data = $data ?? [];

$this->refreshPayloadData();
}

abstract public function getPayload(): string;
Expand All @@ -45,12 +46,19 @@ public function decodePayload(array $data): ?array
return (new AbiDecoder())->decodeFunctionData($payload);
}

public function refreshPayloadData(): static
{
$this->data['data'] = ltrim($this->getPayload(), '0x');

return $this;
}

/**
* Convert the byte representation to a unique identifier.
*/
public function getId(): string
{
return Hash::sha256(Serializer::getBytes($this))->getHex();
return $this->hash(skipSignature: false)->getHex();
}

public function getBytes(bool $skipSignature = false): Buffer
Expand Down
8 changes: 6 additions & 2 deletions src/Transactions/Types/ValidatorRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ public function __construct(?array $data = [])
$payload = $this->decodePayload($data);

if ($payload !== null) {
$data['validatorPublicKey'] = $payload['args'][0];
$data['validatorPublicKey'] = ltrim($payload['args'][0], '0x');
}

parent::__construct($data);
}

public function getPayload(): string
{
return (new AbiEncoder())->encodeFunctionCall(AbiFunction::VALIDATOR_REGISTRATION->value, [$this->data['validatorPublicKey']]);
if (! array_key_exists('validatorPublicKey', $this->data)) {
return '';
}

return (new AbiEncoder())->encodeFunctionCall(AbiFunction::VALIDATOR_REGISTRATION->value, ['0x'.$this->data['validatorPublicKey']]);
}
}
4 changes: 4 additions & 0 deletions src/Transactions/Types/Vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function __construct(?array $data = [])

public function getPayload(): string
{
if (! array_key_exists('vote', $this->data)) {
return '';
}

return (new AbiEncoder())->encodeFunctionCall(AbiFunction::VOTE->value, [$this->data['vote']]);
}
}
26 changes: 13 additions & 13 deletions src/Utils/Abi.Consensus.json

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions tests/Unit/Transactions/Builder/TransferBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

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

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;
use ArkEcosystem\Tests\Crypto\TestCase;

/**
* @covers \ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder
*/
class TransferBuilderTest extends TestCase
{
/** @test */
public function it_should_sign_it_with_a_passphrase()
{
$fixture = $this->getTransactionFixture('evm_call', 'transfer');

$builder = TransferBuilder::new()
->gasPrice($fixture['data']['gasPrice'])
->nonce($fixture['data']['nonce'])
->network($fixture['data']['network'])
->gasLimit($fixture['data']['gasLimit'])
->recipientAddress($fixture['data']['recipientAddress'])
->value($fixture['data']['value'])
->sign($this->passphrase);

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

$this->assertSame($fixture['data']['id'], $builder->transaction->data['id']);

$this->assertTrue($builder->verify());
}
}
34 changes: 34 additions & 0 deletions tests/Unit/Transactions/Builder/UnvoteBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

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

use ArkEcosystem\Crypto\Transactions\Builder\UnvoteBuilder;
use ArkEcosystem\Tests\Crypto\TestCase;

/**
* @covers \ArkEcosystem\Crypto\Transactions\Builder\UnvoteBuilder
*/
class UnvoteBuilderTest extends TestCase
{
/** @test */
public function it_should_sign_it_with_a_passphrase()
{
$fixture = $this->getTransactionFixture('evm_call', 'unvote');

$builder = UnvoteBuilder::new()
->gasPrice($fixture['data']['gasPrice'])
->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());

$this->assertSame($fixture['data']['id'], $builder->transaction->data['id']);

$this->assertTrue($builder->verify());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

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

use ArkEcosystem\Crypto\Transactions\Builder\ValidatorRegistrationBuilder;
use ArkEcosystem\Tests\Crypto\TestCase;

/**
* @covers \ArkEcosystem\Crypto\Transactions\Builder\ValidatorRegistrationBuilder
*/
class ValidatorRegistrationBuilderTest extends TestCase
{
/** @test */
public function it_should_sign_it_with_a_passphrase()
{
$fixture = $this->getTransactionFixture('evm_call', 'validator-registration');

$builder = ValidatorRegistrationBuilder::new()
->gasPrice($fixture['data']['gasPrice'])
->nonce($fixture['data']['nonce'])
->network($fixture['data']['network'])
->gasLimit($fixture['data']['gasLimit'])
->validatorPublicKey('a08058db53e2665c84a40f5152e76dd2b652125a6079130d4c315e728bcf4dd1dfb44ac26e82302331d61977d3141118')
->recipientAddress($fixture['data']['recipientAddress'])
->sign($this->passphrase);

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

$this->assertSame($fixture['data']['id'], $builder->transaction->data['id']);

$this->assertTrue($builder->verify());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

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

use ArkEcosystem\Crypto\Transactions\Builder\ValidatorResignationBuilder;
use ArkEcosystem\Tests\Crypto\TestCase;

/**
* @covers \ArkEcosystem\Crypto\Transactions\Builder\ValidatorResignationBuilder
*/
class ValidatorResignationBuilderTest extends TestCase
{
/** @test */
public function it_should_sign_it_with_a_passphrase()
{
$fixture = $this->getTransactionFixture('evm_call', 'validator-resignation');

$builder = ValidatorResignationBuilder::new()
->gasPrice($fixture['data']['gasPrice'])
->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());

$this->assertSame($fixture['data']['id'], $builder->transaction->data['id']);

$this->assertTrue($builder->verify());
}
}
35 changes: 35 additions & 0 deletions tests/Unit/Transactions/Builder/VoteBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

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

use ArkEcosystem\Crypto\Transactions\Builder\VoteBuilder;
use ArkEcosystem\Tests\Crypto\TestCase;

/**
* @covers \ArkEcosystem\Crypto\Transactions\Builder\VoteBuilder
*/
class VoteBuilderTest extends TestCase
{
/** @test */
public function it_should_sign_it_with_a_passphrase()
{
$fixture = $this->getTransactionFixture('evm_call', 'vote');

$builder = VoteBuilder::new()
->gasPrice($fixture['data']['gasPrice'])
->nonce($fixture['data']['nonce'])
->network($fixture['data']['network'])
->vote('0x512F366D524157BcF734546eB29a6d687B762255')
->gasLimit($fixture['data']['gasLimit'])
->recipientAddress($fixture['data']['recipientAddress'])
->sign($this->passphrase);

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

$this->assertSame($fixture['data']['id'], $builder->transaction->data['id']);

$this->assertTrue($builder->verify());
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Transactions/DeserializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function it_should_deserialize_a_vote_signed_with_a_passphrase()

expect($transaction->data['vote'])->toEqual('0x512F366D524157BcF734546eB29a6d687B762255');

expect($transaction->data['id'])->toEqual('749744e0d689c46e37ff2993a984599eac4989a9ef0028337b335c9d43abf936');

expect($transaction)->toBeInstanceOf(Vote::class);
}

Expand Down
Loading