Skip to content

Commit

Permalink
refactor: fix payload issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsANameToo committed Dec 19, 2024
1 parent fa99eed commit 66693cd
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 34 deletions.
5 changes: 5 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public static function isValidUsername(string $username): bool

return true;
}

public static function removeLeadingHexZero(string $hex): string
{
return preg_replace('/^0x/', '', $hex); // using ltrim($hex, '0x') also removes leading 0s which is not desired, e.g. 0x0123 -> 123
}
}
3 changes: 2 additions & 1 deletion src/Transactions/Types/AbstractTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ArkEcosystem\Crypto\Configuration\Network;
use ArkEcosystem\Crypto\Enums\ContractAbiType;
use ArkEcosystem\Crypto\Helpers;
use ArkEcosystem\Crypto\Identities\Address;
use ArkEcosystem\Crypto\Transactions\Serializer;
use ArkEcosystem\Crypto\Utils\AbiDecoder;
Expand Down Expand Up @@ -34,7 +35,7 @@ abstract public function getPayload(): string;

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

return $this;
}
Expand Down
1 change: 1 addition & 0 deletions src/Transactions/Types/Multipayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function __construct(?array $data = [])
{
$payload = $this->decodePayload($data, ContractAbiType::MULTIPAYMENT);

// TODO: test this approach
if ($payload !== null) {
$data['pay'] = $payload['args'][0];
}
Expand Down
3 changes: 2 additions & 1 deletion src/Utils/TransactionHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ArkEcosystem\Crypto\Utils;

use ArkEcosystem\Crypto\Helpers;
use BitWasp\Bitcoin\Crypto\Hash;
use BitWasp\Buffertools\Buffer;
use BitWasp\Buffertools\BufferInterface;
Expand Down Expand Up @@ -31,7 +32,7 @@ public static function toHash(array $transaction, bool $skipSignature = false):
self::toBeArray($transaction['gasLimit']),
$recipientAddress,
self::toBeArray($transaction['value']),
isset($transaction['data']) ? hex2bin(ltrim($transaction['data'], '0x')) : '',
isset($transaction['data']) ? hex2bin(Helpers::removeLeadingHexZero($transaction['data'])) : '',
[], // accessList is unused
];

Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
*/
class HelpersTest extends TestCase
{
/** @test */
public function it_should_trim_hex_values_properly(): void
{
$this->assertEquals('0123', Helpers::removeLeadingHexZero('0x0123'));
$this->assertEquals('0123', Helpers::removeLeadingHexZero('0123'));
$this->assertEquals('1234', Helpers::removeLeadingHexZero('0x1234'));
$this->assertEquals('0000', Helpers::removeLeadingHexZero('0x0000'));
}

/**
* @test
* @dataProvider validUsernamesProvider
Expand Down
59 changes: 30 additions & 29 deletions tests/Unit/Transactions/Builder/MultipaymentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@
*/
class MultipaymentBuilderTest extends TestCase
{
// /** @test */
// public function it_should_sign_it_with_a_passphrase()
// {
// $fixture = $this->getTransactionFixture('evm_call', 'username-registration');
/** @test */
public function it_should_sign_it_with_a_passphrase()
{
$fixture = $this->getTransactionFixture('evm_call', 'multipayment');

// $builder = UsernameRegistrationBuilder::new()
// ->gasPrice($fixture['data']['gasPrice'])
// ->nonce($fixture['data']['nonce'])
// ->network($fixture['data']['network'])
// ->gasLimit($fixture['data']['gasLimit'])
// ->username('php')
// ->sign($this->passphrase);
$builder = MultipaymentBuilder::new()
->gasPrice($fixture['data']['gasPrice'])
->nonce($fixture['data']['nonce'])
->network($fixture['data']['network'])
->gasLimit($fixture['data']['gasLimit'])
->pay('0x8233F6Df6449D7655f4643D2E752DC8D2283fAd5', '1000000000000000000')
->pay('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22', '2000000000000000000')
->sign($this->passphrase);

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

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

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

/** @test */
public function it_should_handle_single_recipient()
Expand Down Expand Up @@ -58,22 +59,22 @@ public function it_should_handle_single_recipient()

// }

/* @test */
// public function it_should_handle_empty_payment()
// {
// $fixture = $this->getTransactionFixture('evm_call', 'multipayment-0');
/** @test */
public function it_should_handle_empty_payment()
{
$fixture = $this->getTransactionFixture('evm_call', 'multipayment-0');

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

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

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

// $this->assertTrue($builder->verify());
// }
$this->assertTrue($builder->verify());
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/transactions/evm_call/multipayment-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"nonce": "4",
"gasPrice": 5,
"gasLimit": 1000000,
"value": "1000000000000000000n",
"value": "1000000000000000000",
"recipientAddress": "0x83769BeEB7e5405ef0B7dc3C66C43E3a51A6d27f",
"data": "084ce7080000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000008233f6df6449d7655f4643d2e752dc8d2283fad500000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000de0b6b3a7640000",
"signature": "4368a0aa9a9ae7931ef47709a0c727a21086eff3ae695948cfe57017c556cbb82d904197fbf6a1916bd6b8233f2ade4a42c5a296d9429c00148c609fafbb097100",
"senderPublicKey": "03f25455408f9a7e6c6a056b121e68fbda98f3511d22e9ef27b0ebaf1ef9e4eabc",
"senderAddress": "0xfEAf2f24ba1205e9255d015DFaD8463c70D9A466",
"id": "abed9a416c3c4c5c503985b85ccf346560e4cc768c33233868c16cc3c7cefe8e"
},
"serialised": "1e04000000000000000500000040420f000000000000000000000000000000000000000000000000000de0b6b3a76400000183769beeb7e5405ef0b7dc3c66c43e3a51a6d27fc4000000084ce7080000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000008233f6df6449d7655f4643d2e752dc8d2283fad500000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000de0b6b3a76400004368a0aa9a9ae7931ef47709a0c727a21086eff3ae695948cfe57017c556cbb82d904197fbf6a1916bd6b8233f2ade4a42c5a296d9429c00148c609fafbb097100"
"serialized": "1e04000000000000000500000040420f000000000000000000000000000000000000000000000000000de0b6b3a76400000183769beeb7e5405ef0b7dc3c66c43e3a51a6d27fc4000000084ce7080000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000008233f6df6449d7655f4643d2e752dc8d2283fad500000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000de0b6b3a76400004368a0aa9a9ae7931ef47709a0c727a21086eff3ae695948cfe57017c556cbb82d904197fbf6a1916bd6b8233f2ade4a42c5a296d9429c00148c609fafbb097100"
}
2 changes: 1 addition & 1 deletion tests/fixtures/transactions/evm_call/multipayment.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"senderAddress": "0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22",
"id": "3dff56759f96969db1e9d04656646bdb78bf34452a83644cb994ca2bbc2d4a8d"
},
"serialised": "1e00000000000000000500000040420f0000000000000000000000000000000000000000000000000029a2241af62c00000183769beeb7e5405ef0b7dc3c66c43e3a51a6d27f04010000084ce708000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000008233f6df6449d7655f4643d2e752dc8d2283fad50000000000000000000000006f0182a0cc707b055322ccf6d4cb6a5aff1aeb2200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000001bc16d674ec80000b7073cf34ac3eb4e1429ac1ddc42603bda078e221cd3098fdc61edd162a2e03902068fc0c72d2b5dacc91c2ebd4c44f53287a5eb2ce8f888d0d0c9f3a72a600001"
"serialized": "1e00000000000000000500000040420f0000000000000000000000000000000000000000000000000029a2241af62c00000183769beeb7e5405ef0b7dc3c66c43e3a51a6d27f04010000084ce708000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000008233f6df6449d7655f4643d2e752dc8d2283fad50000000000000000000000006f0182a0cc707b055322ccf6d4cb6a5aff1aeb2200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000001bc16d674ec80000b7073cf34ac3eb4e1429ac1ddc42603bda078e221cd3098fdc61edd162a2e03902068fc0c72d2b5dacc91c2ebd4c44f53287a5eb2ce8f888d0d0c9f3a72a600001"
}

0 comments on commit 66693cd

Please sign in to comment.