Skip to content
Draft
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
26 changes: 21 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,26 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

- name: Install
run: composer update --no-interaction --no-suggest --ignore-platform-reqs
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.4"
extensions: mbstring, dom, fileinfo, intl, gd, imagick, bcmath, soap, zip, sqlite, pcov
coverage: pcov

- name: Unit tests
run: composer test
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.composer/cache/files
key: dependencies-composer-${{ hashFiles('composer.json') }}

- name: Install Composer dependencies
run: composer install --no-ansi --no-interaction --no-suggest --no-progress --prefer-dist --optimize-autoloader

- name: Run Tests
run: ./vendor/bin/pest --coverage --coverage-html=.coverage --coverage-clover=coverage.xml
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
phpunit.xml
vendor
clover.xml
.php_cs.cache
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"vendor/bin/php-cs-fixer fix"
],
"test": [
"./vendor/bin/pest --parallel"
"./vendor/bin/pest --coverage --min=100 --coverage-html=.coverage --coverage-clover=coverage.xml"
]
},
"minimum-stability": "dev",
Expand Down
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/ByteBuffer/Concerns/Writes/UnsignedInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function writeUInt64(int $value, int $offset = 0): self
public function writeUInt256($value, int $offset = 0): self
{
// Convert the value to a GMP object for handling large numbers
if (is_numeric($value) || is_string($value)) {
if (is_numeric($value)) {
$gmpValue = gmp_init($value);
} elseif ($value instanceof \GMP) {
$gmpValue = $value;
Expand Down
10 changes: 1 addition & 9 deletions src/Networks/AbstractNetwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@ abstract class AbstractNetwork extends Network implements NetworkInterface
* @see Network::$base58PrefixMap
*/
protected $base58PrefixMap = [
self::BASE58_WIF => 'aa', // 170
self::BASE58_WIF => 'aa', // 170
];

/**
* {@inheritdoc}
*/
public static function __callStatic(string $method, array $args)
{
return static::factory()->{$method}(...$args);
}

/**
* Create a new network instance.
*
Expand Down
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
7 changes: 7 additions & 0 deletions tests/Unit/ByteBuffer/ByteBufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@
expect($buffer->internalSize())->toBe(4 + 11);
});

it('should fill the buffer starting from a different start point', function () {
$buffer = ByteBuffer::new('hello');
$buffer->fill(11, 4);

expect($buffer->internalSize())->toBe(4 + 11);
});

it('should flip the buffer contents', function () {
$buffer = ByteBuffer::new('Hello World');
$buffer->flip();
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/ByteBuffer/Concerns/Reads/UnsignedIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@

expect($buffer->readULong())->toBe(64);
});

test('it should read uint256', function () {
// 256-bit unsigned integer (32 bytes)
$value = '1157920892373161954235709850086879078532699846656405640323232344'; // max uint256
$buffer = ByteBuffer::new(0);
$buffer->writeUInt256($value);
$buffer->position(0);

expect($buffer->readUInt256())->toBe($value);
});
32 changes: 32 additions & 0 deletions tests/Unit/ByteBuffer/Concerns/Writes/UnsignedIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,35 @@

expect($buffer->internalSize())->toBe(8);
});

test('it should write uint256', function () {
// 256-bit unsigned integer (32 bytes)
$value = '1157920892373161954235709850086879078532699846656405640323232344'; // max uint256
$buffer = ByteBuffer::new(0);
$buffer->writeUInt256($value);

expect($buffer->internalSize())->toBe(32);
});

test('it should write uint256 gmp value', function () {
// 256-bit unsigned integer (32 bytes)
$value = gmp_init('1157920892373161954235709850086879078532699846656405640323232344'); // max uint256
$buffer = ByteBuffer::new(0);
$buffer->writeUInt256($value);

expect($buffer->internalSize())->toBe(32);
});

test('it should throw exception when writing invalid uint256', function () {
// 256-bit unsigned integer (32 bytes)
$value = 'asd';
$buffer = ByteBuffer::new(0);
$buffer->writeUInt256($value);
})->throws(InvalidArgumentException::class, 'The value must be a numeric string, integer, or GMP object.');

test('it should throw exception when writing uint256 which is too long', function () {
// 256-bit unsigned integer (32 bytes)
$value = '1157920892373161954235709850086879078532699846656405640323232344444411579208923731619542357098500868790785326998466564056403232323444444';
$buffer = ByteBuffer::new(0);
$buffer->writeUInt256($value);
})->throws(InvalidArgumentException::class, 'The value must fit into 256 bits.');
24 changes: 24 additions & 0 deletions tests/Unit/Enums/AbiFunctionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use ArkEcosystem\Crypto\Enums\AbiFunction;
use ArkEcosystem\Crypto\Transactions\Types\Multipayment;
use ArkEcosystem\Crypto\Transactions\Types\Unvote;
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;

it('should get transaction class', function ($type, $class) {
expect(AbiFunction::{$type}->transactionClass())->toEqual($class);
})->with([
'Vote' => ['VOTE', Vote::class],
'Unvote' => ['UNVOTE', Unvote::class],
'ValidatorRegistration' => ['VALIDATOR_REGISTRATION', ValidatorRegistration::class],
'ValidatorResignation' => ['VALIDATOR_RESIGNATION', ValidatorResignation::class],
'UsernameRegistration' => ['USERNAME_REGISTRATION', UsernameRegistration::class],
'UsernameResignation' => ['USERNAME_RESIGNATION', UsernameResignation::class],
'Multipayment' => ['MULTIPAYMENT', Multipayment::class],
]);
14 changes: 14 additions & 0 deletions tests/Unit/Identities/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@

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

it('should validate an address', function () {
$fixture = $this->getFixture('identity');

$actual = Address::validate($fixture['data']['address']);

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

it('should return false for invalid an address', function () {
$actual = Address::validate('invalid-address');

expect($actual)->toBeFalse();
});
43 changes: 43 additions & 0 deletions tests/Unit/Transactions/DeserializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use ArkEcosystem\Crypto\Transactions\Deserializer;
use ArkEcosystem\Crypto\Transactions\Types\EvmCall;
use ArkEcosystem\Crypto\Transactions\Types\Multipayment;
use ArkEcosystem\Crypto\Transactions\Types\Transfer;
Expand Down Expand Up @@ -88,3 +89,45 @@

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

it('should use ByteBuffer::fromHex when there is no null-byte in the string', function () {
// The string does not contain a null-byte
$hexString = 'abcdef1234567890';
$deserializer = new Deserializer($hexString);

// Use reflection to access the private buffer property
$reflection = new ReflectionClass($deserializer);
$bufferProperty = $reflection->getProperty('buffer');
$bufferProperty->setAccessible(true);
$buffer = $bufferProperty->getValue($deserializer);

// The buffer should be an instance of ByteBuffer
expect($buffer)->toBeInstanceOf(ArkEcosystem\Crypto\ByteBuffer\ByteBuffer::class);

// The buffer should contain the hex string (converted to binary)
expect($buffer->toString('hex'))->toContain($hexString);
});

it('should use ByteBuffer::fromBinary when there is a null-byte in the string', function () {
// The string contains a null-byte
$binaryString = "abc\0def"; // hex: 61626300646566
$hexString = '61626300646566';
$deserializer = new Deserializer($binaryString);

// Use reflection to access the private buffer property
$reflection = new ReflectionClass($deserializer);
$bufferProperty = $reflection->getProperty('buffer');
$bufferProperty->setAccessible(true);
$buffer = $bufferProperty->getValue($deserializer);

// The buffer should be an instance of ByteBuffer
expect($buffer)->toBeInstanceOf(ArkEcosystem\Crypto\ByteBuffer\ByteBuffer::class);

// The buffer should contain the binary string
expect($buffer->toString('hex'))->toBe($hexString);
});

it('should return null if no data value in transaction data', function () {
expect(Deserializer::decodePayload([]))->toBeNull();
expect(Deserializer::decodePayload(['data' => '']))->toBeNull();
});
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
Loading
Loading