Skip to content
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
8 changes: 7 additions & 1 deletion src/Utils/Abi/ArgumentDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ final class ArgumentDecoder

public function __construct(string $bytes)
{
$bytes = hex2bin($bytes);
try {
$bytes = hex2bin($bytes);
} catch (\Throwable $e) {
// Handle the case where hex2bin fails, e.g., invalid hex string
$bytes = false;
}

if ($bytes === false) {
$bytes = '';
}
Expand Down
13 changes: 4 additions & 9 deletions src/Utils/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,11 @@ class Message
*/
public function __construct(object $message)
{
if (property_exists($message, 'publickey')) {
$this->publicKey = $message->publickey;
} elseif (property_exists($message, 'publicKey')) {
$this->publicKey = $message->publicKey;
} elseif (property_exists($message, 'signatory')) {
$this->publicKey = $message->signatory;
} else {
if (! property_exists($message, 'publicKey')) {
throw new InvalidArgumentException('The given message did not contain a valid public key.');
}

$this->publicKey = $message->publicKey;
$this->signature = $message->signature;
$this->message = $message->message;
}
Expand Down Expand Up @@ -113,7 +108,7 @@ public static function sign(string $message, string $passphrase): self
$v = dechex($signature->getRecoveryId() + 27);

return static::new([
'publickey' => $privateKey->publicKey,
'publicKey' => $privateKey->publicKey,
'signature' => $r.$s.$v,
'message' => $message,
]);
Expand Down Expand Up @@ -144,7 +139,7 @@ public function verify(): bool
public function toArray(): array
{
return [
'publickey' => $this->publicKey,
'publicKey' => $this->publicKey,
'signature' => $this->signature,
'message' => $this->message,
];
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/Utils/Abi/ArgumentDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
expect($decoder->decodeAddress())->toBe($expected);
});

it('should decode a string', function () {
$payload = '0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000474657374';
$expected = 'test';

$decoder = new ArgumentDecoder($payload);

expect($decoder->decodeString())->toBe($expected);
});

it('should decode unsigned int', function () {
$payload = '000000000000000000000000000000000000000000000000016345785d8a0000';
$expected = '100000000000000000';
Expand Down Expand Up @@ -54,3 +63,12 @@

expect($decoder->decodeBool())->toBe($expected);
});

it('should handle issue converting hex to binary', function () {
$decoder = new ArgumentDecoder('invalid');

$reflectionProperty = new \ReflectionProperty(ArgumentDecoder::class, 'bytes');
$reflectionProperty->setAccessible(true);

expect($reflectionProperty->getValue($decoder))->toBe('');
});
17 changes: 17 additions & 0 deletions tests/Unit/Utils/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use ArkEcosystem\Crypto\ByteBuffer\ByteBuffer;
use ArkEcosystem\Crypto\Utils\Address as TestClass;

test('it should validate the address', function () {
Expand All @@ -17,3 +18,19 @@

expect($actual)->toBeFalse();
});

it('should convert to hex string', function () {
$fixture = $this->getFixture('identity');

$actual = TestClass::toBufferHexString($fixture['data']['address']);

expect($actual)->toBe(substr($fixture['data']['address'], 2));
});

it('should extract address from a byte buffer', function () {
$fixture = $this->getFixture('identity');

$actual = TestClass::fromByteBuffer(ByteBuffer::fromHex(substr($fixture['data']['address'], 2)));

expect($actual)->toBe($fixture['data']['address']);
});
8 changes: 8 additions & 0 deletions tests/Unit/Utils/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
expect($message->message)->toBe($fixture['message']);
});

test('it should throw if no public key is provided', function () {
$fixture = $this->getFixture('message-sign');

unset($fixture['publicKey']);

Message::new($fixture);
})->throws(InvalidArgumentException::class, 'The given message did not contain a valid public key.');

test('it should create a message from a string', function () {
$fixture = $this->getFixture('message-sign');

Expand Down
80 changes: 80 additions & 0 deletions tests/Unit/Utils/TransactionUtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

use ArkEcosystem\Crypto\Utils\RlpDecoder;
use ArkEcosystem\Crypto\Utils\TransactionUtils;
use Brick\Math\BigDecimal;

it('should convert a transaction to a buffer', function () {
$fixture = $this->getTransactionFixture('evm_call', 'username-resignation');

$transaction = TransactionUtils::toBuffer($fixture['data']);

expect($transaction->getHex())->toBe($fixture['serialized']);
});

it('should convert a transaction to a buffer when data starts with 0x', function () {
$fixture = $this->getTransactionFixture('evm_call', 'username-resignation');

$fixture['data']['data'] = '0x'.$fixture['data']['data'];

$transaction = TransactionUtils::toBuffer($fixture['data']);

expect($transaction->getHex())->toBe($fixture['serialized']);
});

it('should get the hash for a transaction', function () {
$fixture = $this->getTransactionFixture('evm_call', 'username-resignation');

$transaction = TransactionUtils::toHash($fixture['data']);

expect($transaction->getHex())->toBe($fixture['data']['hash']);
});

it('should handle string data starting with 0x', function () {
$fixture = $this->getTransactionFixture('evm_call', 'username-resignation');

$fixture['data']['gasPrice'] = '0x'.dechex($fixture['data']['gasPrice']);

$transaction = TransactionUtils::toBuffer($fixture['data']);

expect($transaction->getHex())->toBe($fixture['serialized']);
});

it('should handle BigDecimal value', function () {
$fixture = $this->getTransactionFixture('evm_call', 'username-resignation');

$fixture['data']['gasPrice'] = BigDecimal::of($fixture['data']['gasPrice']);

$transaction = TransactionUtils::toBuffer($fixture['data']);

expect($transaction->getHex())->toBe($fixture['serialized']);
});

it('should handle zero BigDecimal value', function () {
$fixture = $this->getTransactionFixture('evm_call', 'username-resignation');

$fixture['data']['gasPrice'] = BigDecimal::zero();

$transaction = TransactionUtils::toBuffer($fixture['data'], true);

$decoded = RlpDecoder::decode('0x'.substr($transaction->getHex(), 2));

expect($decoded[3])->toBe('0x');
});

it('should handle unknown value value', function () {
$fixture = $this->getTransactionFixture('evm_call', 'username-resignation');

$fixture['data']['gasPrice'] = 123.456;

$transaction = TransactionUtils::toBuffer($fixture['data'], true);

$decoded = RlpDecoder::decode('0x'.substr($transaction->getHex(), 2));

expect($decoded[3])->toBe('0x');
});

// toBuffer
// toHash
Loading