|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use ArkEcosystem\Crypto\Utils\TransactionTypeIdentifier; |
| 6 | + |
| 7 | +function loadMethodIdentifiers(string $path): array |
| 8 | +{ |
| 9 | + $json = file_get_contents($path); |
| 10 | + |
| 11 | + expect($json)->not->toBeFalse(); |
| 12 | + |
| 13 | + $decoded = json_decode($json, true); |
| 14 | + |
| 15 | + expect($decoded)->toBeArray(); |
| 16 | + expect($decoded)->toHaveKey('methodIdentifiers'); |
| 17 | + |
| 18 | + return $decoded['methodIdentifiers']; |
| 19 | +} |
| 20 | + |
| 21 | +beforeEach(function () { |
| 22 | + $this->consensusMethods = loadMethodIdentifiers(dirname(__DIR__, 3).'/src/Utils/Abi/json/Abi.Consensus.json'); |
| 23 | + $this->multipaymentMethods = loadMethodIdentifiers(dirname(__DIR__, 3).'/src/Utils/Abi/json/Abi.Multipayment.json'); |
| 24 | + $this->usernamesMethods = loadMethodIdentifiers(dirname(__DIR__, 3).'/src/Utils/Abi/json/Abi.Usernames.json'); |
| 25 | +}); |
| 26 | + |
| 27 | +it('identifies transfer by empty payload', function () { |
| 28 | + expect(TransactionTypeIdentifier::isTransfer(''))->toBeTrue(); |
| 29 | + expect(TransactionTypeIdentifier::isTransfer('0x'))->toBeFalse(); |
| 30 | + expect(TransactionTypeIdentifier::isTransfer('12345678'))->toBeFalse(); |
| 31 | +}); |
| 32 | + |
| 33 | +it('identifies vote signature', function () { |
| 34 | + $signature = $this->consensusMethods['vote(address)']; |
| 35 | + |
| 36 | + expect(TransactionTypeIdentifier::isVote($signature))->toBeTrue(); |
| 37 | + expect(TransactionTypeIdentifier::isVote('0x'.$signature))->toBeTrue(); |
| 38 | + expect(TransactionTypeIdentifier::isVote('1234567'))->toBeFalse(); |
| 39 | +}); |
| 40 | + |
| 41 | +it('identifies unvote signature', function () { |
| 42 | + $signature = $this->consensusMethods['unvote()']; |
| 43 | + |
| 44 | + expect(TransactionTypeIdentifier::isUnvote($signature))->toBeTrue(); |
| 45 | + expect(TransactionTypeIdentifier::isUnvote('0x'.$signature))->toBeTrue(); |
| 46 | + expect(TransactionTypeIdentifier::isUnvote('1234567'))->toBeFalse(); |
| 47 | +}); |
| 48 | + |
| 49 | +it('identifies multipayment signature', function () { |
| 50 | + $signature = $this->multipaymentMethods['pay(address[],uint256[])']; |
| 51 | + |
| 52 | + expect(TransactionTypeIdentifier::isMultiPayment($signature))->toBeTrue(); |
| 53 | + expect(TransactionTypeIdentifier::isMultiPayment('0x'.$signature))->toBeTrue(); |
| 54 | + expect(TransactionTypeIdentifier::isMultiPayment('1234567'))->toBeFalse(); |
| 55 | +}); |
| 56 | + |
| 57 | +it('identifies username registration signature', function () { |
| 58 | + $signature = $this->usernamesMethods['registerUsername(string)']; |
| 59 | + |
| 60 | + expect(TransactionTypeIdentifier::isUsernameRegistration($signature))->toBeTrue(); |
| 61 | + expect(TransactionTypeIdentifier::isUsernameRegistration('0x'.$signature))->toBeTrue(); |
| 62 | + expect(TransactionTypeIdentifier::isUsernameRegistration('1234567'))->toBeFalse(); |
| 63 | +}); |
| 64 | + |
| 65 | +it('identifies username resignation signature', function () { |
| 66 | + $signature = $this->usernamesMethods['resignUsername()']; |
| 67 | + |
| 68 | + expect(TransactionTypeIdentifier::isUsernameResignation($signature))->toBeTrue(); |
| 69 | + expect(TransactionTypeIdentifier::isUsernameResignation('0x'.$signature))->toBeTrue(); |
| 70 | + expect(TransactionTypeIdentifier::isUsernameResignation('1234567'))->toBeFalse(); |
| 71 | +}); |
| 72 | + |
| 73 | +it('identifies validator registration signature', function () { |
| 74 | + $signature = $this->consensusMethods['registerValidator(bytes)']; |
| 75 | + |
| 76 | + expect(TransactionTypeIdentifier::isValidatorRegistration($signature))->toBeTrue(); |
| 77 | + expect(TransactionTypeIdentifier::isValidatorRegistration('0x'.$signature))->toBeTrue(); |
| 78 | + expect(TransactionTypeIdentifier::isValidatorRegistration('1234567'))->toBeFalse(); |
| 79 | +}); |
| 80 | + |
| 81 | +it('identifies validator resignation signature', function () { |
| 82 | + $signature = $this->consensusMethods['resignValidator()']; |
| 83 | + |
| 84 | + expect(TransactionTypeIdentifier::isValidatorResignation($signature))->toBeTrue(); |
| 85 | + expect(TransactionTypeIdentifier::isValidatorResignation('0x'.$signature))->toBeTrue(); |
| 86 | + expect(TransactionTypeIdentifier::isValidatorResignation('1234567'))->toBeFalse(); |
| 87 | +}); |
| 88 | + |
| 89 | +it('identifies update validator signature', function () { |
| 90 | + $signature = $this->consensusMethods['updateValidator(bytes)']; |
| 91 | + |
| 92 | + expect(TransactionTypeIdentifier::isUpdateValidator($signature))->toBeTrue(); |
| 93 | + expect(TransactionTypeIdentifier::isUpdateValidator('0x'.$signature))->toBeTrue(); |
| 94 | + expect(TransactionTypeIdentifier::isUpdateValidator('1234567'))->toBeFalse(); |
| 95 | +}); |
| 96 | + |
| 97 | +it('identifies token transfer payloads', function () { |
| 98 | + expect(TransactionTypeIdentifier::isTokenTransfer('0xa9059cbb000000000000000000000000a5cc0bfeb09742c5e4c610f2ebaab82eb142ca10000000000000000000000000000000000000009bd2ffdd71438a49e803314000'))->toBeTrue(); |
| 99 | + expect(TransactionTypeIdentifier::isTokenTransfer('0x'.str_repeat('0', 64)))->toBeFalse(); |
| 100 | + expect(TransactionTypeIdentifier::isTokenTransfer('1234567'))->toBeFalse(); |
| 101 | +}); |
0 commit comments