├── .coveralls.yml ├── .github └── workflows │ ├── static-analysis.yml │ └── tests.yml ├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── infection.json.dist ├── phpunit.xml.dist ├── src └── Ethereum │ ├── EIP1559Transaction.php │ └── Transaction.php └── test ├── EIP1559TransactionTest.php └── TransactionTest.php /.coveralls.yml: -------------------------------------------------------------------------------- 1 | service_name: travis-ci 2 | coverage_clover: build/logs/clover.xml 3 | json_path: coveralls-upload.json 4 | -------------------------------------------------------------------------------- /.github/workflows/static-analysis.yml: -------------------------------------------------------------------------------- 1 | name: Static Analysis (informative) 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | phpstan: 10 | name: PHPStan 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - uses: shivammathur/setup-php@v2 15 | with: 16 | php-version: 8.1 17 | coverage: none 18 | 19 | - run: composer install --no-progress --prefer-dist 20 | - run: composer require phpstan/phpstan --no-progress --dev 21 | - run: vendor/bin/phpstan analyse src/ 22 | continue-on-error: true # is only informative 23 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | 7 | linux: 8 | name: Test on Linux 9 | runs-on: ubuntu-20.04 10 | strategy: 11 | fail-fast: false 12 | matrix: 13 | php-version: ['8.1', '8.2', '8.3'] 14 | 15 | steps: 16 | - uses: actions/checkout@v2 17 | with: 18 | fetch-depth: 1 19 | 20 | - run: php${{ matrix.php-version }} -v 21 | - run: php${{ matrix.php-version }} -m 22 | - run: composer -V 23 | - run: composer install --no-progress 24 | - run: php${{ matrix.php-version }} vendor/bin/phpunit 25 | 26 | windows: 27 | name: Test on Windows 28 | defaults: 29 | run: 30 | shell: cmd 31 | runs-on: windows-latest 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | php-version: ['8.1', '8.2'] 36 | arch: [x64] 37 | ts: [nts] 38 | 39 | steps: 40 | - name: Setup PHP 41 | id: setup-php 42 | uses: cmb69/setup-php-sdk@v0.7 43 | with: 44 | version: ${{matrix.php-version}} 45 | arch: ${{matrix.arch}} 46 | ts: ${{matrix.ts}} 47 | - uses: actions/checkout@v2 48 | with: 49 | fetch-depth: 1 50 | 51 | - run: php -v 52 | - run: echo extension=gmp>>C:\tools\php\php.ini 53 | - run: php -m 54 | - run: composer -V 55 | - run: composer install --no-progress 56 | - run: php vendor/bin/phpunit 57 | 58 | code_coverage: 59 | name: Code Coverage 60 | runs-on: ubuntu-latest 61 | steps: 62 | - uses: actions/checkout@v2 63 | - uses: shivammathur/setup-php@v2 64 | with: 65 | php-version: 8.1 66 | coverage: none 67 | 68 | - run: composer install --no-progress 69 | - run: mkdir -p build/logs 70 | - run: phpdbg -qrr vendor/bin/phpunit 71 | - run: wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.3/php-coveralls.phar 72 | - env: 73 | COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} 74 | run: php php-coveralls.phar --verbose 75 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.phar 2 | /vendor/ 3 | composer.lock 4 | 5 | # Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control 6 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 7 | # composer.lock 8 | nbproject/ 9 | build/ 10 | .phpunit.result.cache 11 | infection.log 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Boris Momčilović 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-ethereum-offline-tx [![Tests](https://github.com/kornrunner/php-ethereum-offline-tx/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/kornrunner/php-ethereum-offline-tx/actions/workflows/tests.yml) [![Coverage Status](https://coveralls.io/repos/github/kornrunner/php-ethereum-offline-raw-tx/badge.svg?branch=master)](https://coveralls.io/github/kornrunner/php-ethereum-offline-raw-tx?branch=master) [![Latest Stable Version](https://poser.pugx.org/kornrunner/ethereum-offline-raw-tx/v/stable)](https://packagist.org/packages/kornrunner/ethereum-offline-raw-tx) 2 | 3 | Pure PHP Ethereum Offline Raw Transaction Signer 4 | 5 | Ethereum raw transaction hash offline in PHP 6 | 7 | ## Installation 8 | 9 | ```sh 10 | $ composer require kornrunner/ethereum-offline-raw-tx 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```php 16 | use kornrunner\Ethereum\Transaction; 17 | 18 | $nonce = '04'; 19 | $gasPrice = '03f5476a00'; 20 | $gasLimit = '027f4b'; 21 | $to = '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72'; 22 | $value = '2a45907d1bef7c00'; 23 | 24 | $privateKey = 'b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898'; 25 | 26 | $transaction = new Transaction ($nonce, $gasPrice, $gasLimit, $to, $value); 27 | $transaction->getRaw ($privateKey); 28 | // f86d048503f5476a0083027f4b941a8c8adfbe1c59e8b58cc0d515f07b7225f51c72882a45907d1bef7c00801ba0e68be766b40702e6d9c419f53d5e053c937eda36f0e973074d174027439e2b5da0790df3e4d0294f92d69104454cd96005e21095efd5f2970c2829736ca39195d8 29 | ``` 30 | 31 | With different `chainId` 32 | 33 | ```php 34 | use kornrunner\Ethereum\Transaction; 35 | 36 | $nonce = '04'; 37 | $gasPrice = '03f5476a00'; 38 | $gasLimit = '027f4b'; 39 | $to = '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72'; 40 | $value = '2a45907d1bef7c00'; 41 | $chainId = 1; 42 | 43 | $privateKey = 'b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898'; 44 | 45 | $transaction = new Transaction ($nonce, $gasPrice, $gasLimit, $to, $value); 46 | $transaction->getRaw ($privateKey, $chainId); 47 | // f86d048503f5476a0083027f4b941a8c8adfbe1c59e8b58cc0d515f07b7225f51c72882a45907d1bef7c008025a0db4efcc22a7d9b2cab180ce37f81959412594798cb9af7c419abb6323763cdd5a0631a0c47d27e5b6e3906a419de2d732e290b73ead4172d8598ce4799c13bda69 48 | ``` 49 | 50 | Create a EIP1559 transaction 51 | 52 | ```php 53 | use kornrunner\Ethereum\EIP1559Transaction; 54 | 55 | $nonce = '02'; 56 | $maxPriorityFeePerGas = 'b2d05e00'; 57 | $maxFeePerGas = '6fc23ac00'; 58 | $gasLimit = '5208'; 59 | $to = '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72'; 60 | $value = '29a2241af62c0000'; 61 | $data = ''; 62 | $chainId = 1; 63 | 64 | $privateKey = 'b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898'; 65 | 66 | $transaction = new EIP1559Transaction($nonce, $maxPriorityFeePerGas, $maxFeePerGas, $gasLimit, $to, $value, $data); 67 | 68 | $rawTx = $transaction->getRaw($privateKey, $chainId); 69 | // 02f873010284b2d05e008506fc23ac00825208941a8c8adfbe1c59e8b58cc0d515f07b7225f51c728829a2241af62c000080c080a0dd32dc794af9a9085d6772c40656fc156a577570c6fd32f2a2d4126673373919a066cf1859672a9e6fdbb00ebd230bcfec66cac1c99f5c83598ecae6025d0e91f4 70 | ``` 71 | 72 | ## Crypto 73 | 74 | [![Ethereum](https://user-images.githubusercontent.com/725986/61891022-0d0c7f00-af09-11e9-829f-096c039bbbfa.png) 0x9c7b7a00972121fb843af7af74526d7eb585b171][Ethereum] 75 | 76 | [Ethereum]: https://etherscan.io/address/0x9c7b7a00972121fb843af7af74526d7eb585b171 "Donate with Ethereum" 77 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kornrunner/ethereum-offline-raw-tx", 3 | "description": "Pure PHP Ethereum Offline Raw Transaction Signer", 4 | "keywords": ["ethereum", "offline", "transaction", "eth", "tx"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Boris Momcilovic", 9 | "homepage": "https://github.com/kornrunner/php-ethereum-offline-tx" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=8.1", 14 | "kornrunner/keccak": "^1.0", 15 | "kornrunner/secp256k1": "^0.3", 16 | "kornrunner/rlp": "^0.3" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "kornrunner\\": "src" 21 | } 22 | }, 23 | "autoload-dev": { 24 | "psr-4": { 25 | "kornrunner\\": "test" 26 | } 27 | }, 28 | "require-dev": { 29 | "phpunit/phpunit": "^9" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /infection.json.dist: -------------------------------------------------------------------------------- 1 | { 2 | "timeout": 10, 3 | "source": { 4 | "directories": [ 5 | "src" 6 | ] 7 | }, 8 | "logs": { 9 | "text": "infection.log" 10 | }, 11 | "mutators": { 12 | "@default": true 13 | } 14 | } -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | src 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | test 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/Ethereum/EIP1559Transaction.php: -------------------------------------------------------------------------------- 1 | nonce = $nonce; 30 | $this->maxPriorityFeePerGas = $maxPriorityFeePerGas; 31 | $this->maxFeePerGas = $maxFeePerGas; 32 | $this->gasLimit = $gasLimit; 33 | $this->to = $to; 34 | $this->value = $value; 35 | $this->data = $data; 36 | $this->accessList = []; 37 | } 38 | 39 | public function getInput(): array 40 | { 41 | return [ 42 | 'chainId' => $this->chainId, 43 | 'nonce' => $this->nonce, 44 | 'maxPriorityFeePerGas' => $this->maxPriorityFeePerGas, 45 | 'maxFeePerGas' => $this->maxFeePerGas, 46 | 'gasLimit' => $this->gasLimit, 47 | 'to' => $this->to, 48 | 'value' => $this->value, 49 | 'data' => $this->data, 50 | 'accessList' => $this->accessList, 51 | 'v' => $this->v, 52 | 'r' => $this->r, 53 | 's' => $this->s, 54 | ]; 55 | } 56 | 57 | public function getRaw(string $privateKey, int $chainId = 0): string 58 | { 59 | if ($chainId < 0) { 60 | throw new RuntimeException('ChainID must be positive'); 61 | } 62 | 63 | $this->chainId = dechex($chainId); 64 | $this->v = ''; 65 | $this->r = ''; 66 | $this->s = ''; 67 | 68 | if (strlen($privateKey) != 64) { 69 | throw new RuntimeException('Incorrect private key'); 70 | } 71 | 72 | $this->sign($privateKey, $chainId); 73 | 74 | return $this->serialize(); 75 | } 76 | 77 | private function serialize(): string 78 | { 79 | return $this->txType . $this->RLPencode($this->getInput()); 80 | } 81 | 82 | public function getUnsigned(int $chainId = 0): string 83 | { 84 | if ($chainId < 0) { 85 | throw new RuntimeException('ChainID must be positive'); 86 | } 87 | 88 | $this->chainId = dechex($chainId); 89 | 90 | $input = $this->getInput(); 91 | 92 | unset($input['v']); 93 | unset($input['r']); 94 | unset($input['s']); 95 | 96 | return $this->txType . $this->RLPencode($input); 97 | } 98 | 99 | private function sign(string $privateKey, int $chainId = 0): void 100 | { 101 | $hash = $this->hash($chainId); 102 | 103 | $secp256k1 = new Secp256k1(); 104 | /** 105 | * @var Signature 106 | */ 107 | $signed = $secp256k1->sign($hash, $privateKey); 108 | $this->r = $this->hexup(gmp_strval($signed->getR(), 16)); 109 | $this->s = $this->hexup(gmp_strval($signed->getS(), 16)); 110 | $this->v = dechex((int)$signed->getRecoveryParam()); 111 | } 112 | 113 | public function hash(int $chainId = 0): string 114 | { 115 | if ($chainId < 0) { 116 | throw new RuntimeException('ChainID must be positive'); 117 | } 118 | 119 | $this->chainId = dechex($chainId); 120 | $input = $this->getInput(); 121 | 122 | unset($input['v']); 123 | unset($input['r']); 124 | unset($input['s']); 125 | 126 | $encoded = $this->RLPencode($input); 127 | 128 | return Keccak::hash(hex2bin($this->txType . $encoded), 256); 129 | } 130 | 131 | private function RLPencode(array $input): string 132 | { 133 | $rlp = new RLP; 134 | 135 | $data = []; 136 | foreach ($input as $item) { 137 | if (is_array($item)) { 138 | $data[] = $item; 139 | continue; 140 | } 141 | 142 | $value = strpos($item, '0x') !== false ? substr($item, strlen('0x')) : $item; 143 | $data[] = $value ? '0x' . $this->hexup($value) : ''; 144 | } 145 | return $rlp->encode($data); 146 | } 147 | 148 | private function hexup(string $value): string 149 | { 150 | return strlen($value) % 2 === 0 ? $value : "0{$value}"; 151 | } 152 | 153 | } 154 | -------------------------------------------------------------------------------- /src/Ethereum/Transaction.php: -------------------------------------------------------------------------------- 1 | nonce = $nonce; 25 | $this->gasPrice = $gasPrice; 26 | $this->gasLimit = $gasLimit; 27 | $this->to = $to; 28 | $this->value = $value; 29 | $this->data = $data; 30 | } 31 | 32 | public function getInput(): array 33 | { 34 | return [ 35 | 'nonce' => $this->nonce, 36 | 'gasPrice' => $this->gasPrice, 37 | 'gasLimit' => $this->gasLimit, 38 | 'to' => $this->to, 39 | 'value' => $this->value, 40 | 'data' => $this->data, 41 | 'v' => $this->v, 42 | 'r' => $this->r, 43 | 's' => $this->s, 44 | ]; 45 | } 46 | 47 | public function getRaw(string $privateKey, int $chainId = 0): string 48 | { 49 | if ($chainId < 0) { 50 | throw new RuntimeException('ChainID must be positive'); 51 | } 52 | 53 | $this->v = ''; 54 | $this->r = ''; 55 | $this->s = ''; 56 | 57 | if (strlen($privateKey) != 64) { 58 | throw new RuntimeException('Incorrect private key'); 59 | } 60 | 61 | $this->sign($privateKey, $chainId); 62 | 63 | return $this->serialize(); 64 | } 65 | 66 | private function serialize(): string 67 | { 68 | return $this->RLPencode($this->getInput()); 69 | } 70 | 71 | public function getUnsigned(int $chainId = 0): string 72 | { 73 | $input = $this->getInput(); 74 | 75 | if ($chainId > 0) { 76 | $input['v'] = dechex($chainId); 77 | $input['r'] = ''; 78 | $input['s'] = ''; 79 | } else { 80 | unset($input['v']); 81 | unset($input['r']); 82 | unset($input['s']); 83 | } 84 | 85 | return $this->RLPencode($input); 86 | } 87 | 88 | private function sign(string $privateKey, int $chainId = 0): void 89 | { 90 | $hash = $this->hash($chainId); 91 | 92 | $secp256k1 = new Secp256k1(); 93 | /** 94 | * @var Signature 95 | */ 96 | $signed = $secp256k1->sign($hash, $privateKey); 97 | 98 | $this->r = $this->hexup(gmp_strval($signed->getR(), 16)); 99 | $this->s = $this->hexup(gmp_strval($signed->getS(), 16)); 100 | $this->v = dechex ((int) $signed->getRecoveryParam () + 27 + ($chainId ? $chainId * 2 + 8 : 0)); 101 | } 102 | 103 | public function hash(int $chainId = 0): string 104 | { 105 | $input = $this->getInput(); 106 | 107 | if ($chainId > 0) { 108 | $input['v'] = dechex($chainId); 109 | $input['r'] = ''; 110 | $input['s'] = ''; 111 | } else { 112 | unset($input['v']); 113 | unset($input['r']); 114 | unset($input['s']); 115 | } 116 | 117 | $encoded = $this->RLPencode($input); 118 | 119 | return Keccak::hash(hex2bin($encoded), 256); 120 | } 121 | 122 | private function RLPencode(array $input): string 123 | { 124 | $rlp = new RLP; 125 | 126 | $data = []; 127 | foreach ($input as $item) { 128 | $value = strpos ($item, '0x') !== false ? substr ($item, strlen ('0x')) : $item; 129 | $data[] = $value ? '0x' . $this->hexup($value) : ''; 130 | } 131 | 132 | return $rlp->encode($data); 133 | } 134 | 135 | private function hexup(string $value): string 136 | { 137 | return strlen ($value) % 2 === 0 ? $value : "0{$value}"; 138 | } 139 | 140 | } 141 | -------------------------------------------------------------------------------- /test/EIP1559TransactionTest.php: -------------------------------------------------------------------------------- 1 | assertSame($expect, $transaction->getInput()); 19 | } 20 | 21 | public static function input (): array { 22 | return [ 23 | [ 24 | ['chainId' => null, 'nonce' => '', 'maxPriorityFeePerGas' => '', 'maxFeePerGas' => '', 'gasLimit' => '', 'to' => '', 'value' => '', 'data' => '', 'accessList' => [], 'v' => '', 'r' => '', 's' => ''], 25 | '', '', '', '', '', '' 26 | ], 27 | [ 28 | ['chainId' => null, 'nonce' => '04', 'maxPriorityFeePerGas' => '03f5476a00', 'maxFeePerGas' => '027f4b', 'gasLimit' => '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', 'to' => '2a45907d1bef7c00', 'value' => '', 'data' => '', 'accessList' => [], 'v' => '', 'r' => '', 's' => ''], 29 | '04', '03f5476a00', '027f4b', '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', '2a45907d1bef7c00', '' 30 | ], 31 | ]; 32 | } 33 | 34 | /** 35 | * @dataProvider getTransactionData 36 | */ 37 | public function testGetRaw($txid, $unsigned, $signed, $privateKey, $chainId, $nonce, $maxPriorityFeePerGas, $maxFeePerGas, $gasLimit, $to, $value, $data) 38 | { 39 | $transaction = new EIP1559Transaction($nonce, $maxPriorityFeePerGas, $maxFeePerGas, $gasLimit, $to, $value, $data); 40 | $this->assertSame($signed, $transaction->getRaw($privateKey, $chainId)); 41 | } 42 | 43 | /** 44 | * @dataProvider getTransactionData 45 | */ 46 | public function testGetUnsigned($txid, $unsigned, $signed, $privateKey, $chainId, $nonce, $maxPriorityFeePerGas, $maxFeePerGas, $gasLimit, $to, $value, $data) 47 | { 48 | $transaction = new EIP1559Transaction($nonce, $maxPriorityFeePerGas, $maxFeePerGas, $gasLimit, $to, $value, $data); 49 | $this->assertSame($unsigned, $transaction->getUnsigned($chainId)); 50 | } 51 | 52 | /** 53 | * @dataProvider getTransactionData 54 | */ 55 | public function testHash($txid, $unsigned, $signed, $privateKey, $chainId, $nonce, $maxPriorityFeePerGas, $maxFeePerGas, $gasLimit, $to, $value, $data) 56 | { 57 | $transaction = new EIP1559Transaction($nonce, $maxPriorityFeePerGas, $maxFeePerGas, $gasLimit, $to, $value, $data); 58 | $this->assertSame($txid, $transaction->hash($chainId)); 59 | } 60 | 61 | public static function getTransactionData(): array 62 | { 63 | return [ 64 | [ 65 | '4fa4b9bd743ee70bb1dc109cadaa5985d5e9461e793405261daeff7bb6419865', //txid 66 | '02f0010284b2d05e008506fc23ac00825208941a8c8adfbe1c59e8b58cc0d515f07b7225f51c728829a2241af62c000080c0', //unsigned 67 | '02f873010284b2d05e008506fc23ac00825208941a8c8adfbe1c59e8b58cc0d515f07b7225f51c728829a2241af62c000080c080a0dd32dc794af9a9085d6772c40656fc156a577570c6fd32f2a2d4126673373919a066cf1859672a9e6fdbb00ebd230bcfec66cac1c99f5c83598ecae6025d0e91f4', 68 | 'b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898', 1, '02', 'b2d05e00', '6fc23ac00', '5208', '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', '29a2241af62c0000', '' 69 | ], 70 | [ 71 | 'f470e5422f3dc01bca43f818f03de3eea876328f8a7d1c9c4fe05410f6fc3bb6', //txid 72 | '02f0380284b2d05e008506fc23ac00825208941a8c8adfbe1c59e8b58cc0d515f07b7225f51c728829a2241af62c000080c0', //unsigned 73 | '02f873380284b2d05e008506fc23ac00825208941a8c8adfbe1c59e8b58cc0d515f07b7225f51c728829a2241af62c000080c080a0b8ec74b95d0d8ebe4b4c7e9d5a8af16e06a7d636d20b2c632633a2abe3a0f5cca005971b1f3ec9b498d8d7b17b312477d0b1fd2232ca4b82e9c34fc43d33e5f196', 74 | 'b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898', 56, '02', 'b2d05e00', '6fc23ac00', '5208', '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', '29a2241af62c0000', '' 75 | ], 76 | [ 77 | '950b552d191d26ea7b2dbaf53e59b2f82177b1924f8ac06715a0fe0f058f1c20', //txid 78 | '02f4380284b2d05e008506fc23ac00825208941a8c8adfbe1c59e8b58cc0d515f07b7225f51c728829a2241af62c00008401232213c0', //unsigned 79 | '02f877380284b2d05e008506fc23ac00825208941a8c8adfbe1c59e8b58cc0d515f07b7225f51c728829a2241af62c00008401232213c001a05bc0caa25dd8e23adf3f79f8dbe1237e0f22980a3b15f00bbe9f5a19dd23a70ca002e9a353c9b155b0e7e796b5f966bc6dd53deafea9990398a069d93278e50644', 80 | 'b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898', 56, '02', 'b2d05e00', '6fc23ac00', '5208', '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', '29a2241af62c0000', '1232213' 81 | ] 82 | ]; 83 | } 84 | 85 | public function testGetRawBadChainId() 86 | { 87 | $this->expectException(RuntimeException::class); 88 | $transaction = new EIP1559Transaction('04', '03f5476a00', '027f4b', '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', '2a45907d1bef7c00', ''); 89 | $transaction->getRaw ('b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898', -1); 90 | } 91 | 92 | public function testBadPrivateKey() 93 | { 94 | $this->expectException(RuntimeException::class); 95 | $this->expectExceptionMessage('Incorrect private key'); 96 | $transaction = new EIP1559Transaction(); 97 | $transaction->getRaw(''); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /test/TransactionTest.php: -------------------------------------------------------------------------------- 1 | assertSame($expect, $transaction->getInput()); 19 | } 20 | 21 | public static function input(): array 22 | { 23 | return [ 24 | [ 25 | ['nonce' => '', 'gasPrice' => '', 'gasLimit' => '', 'to' => '', 'value' => '', 'data' => '', 'v' => '', 'r' => '', 's' => ''], 26 | '', '', '', '', '', '' 27 | ], 28 | [ 29 | ['nonce' => '04', 'gasPrice' => '03f5476a00', 'gasLimit' => '027f4b', 'to' => '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', 'value' => '2a45907d1bef7c00', 'data' => '', 'v' => '', 'r' => '', 's' => ''], 30 | '04', '03f5476a00', '027f4b', '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', '2a45907d1bef7c00', '' 31 | ], 32 | ]; 33 | } 34 | 35 | /** 36 | * @dataProvider getTransactionData 37 | */ 38 | public function testGetRaw($txid, $unsigned, $signed, $privateKey, $chainId, $nonce, $gasPrice, $gasLimit, $to, $value, $data) 39 | { 40 | $transaction = new Transaction($nonce, $gasPrice, $gasLimit, $to, $value, $data); 41 | $this->assertSame($signed, $transaction->getRaw($privateKey, $chainId)); 42 | } 43 | 44 | /** 45 | * @dataProvider getTransactionData 46 | */ 47 | public function testGetUnsigned($txid, $unsigned, $signed, $privateKey, $chainId, $nonce, $gasPrice, $gasLimit, $to, $value, $data) 48 | { 49 | $transaction = new Transaction($nonce, $gasPrice, $gasLimit, $to, $value, $data); 50 | $this->assertSame($unsigned, $transaction->getUnsigned($chainId)); 51 | } 52 | 53 | /** 54 | * @dataProvider getTransactionData 55 | */ 56 | public function testHash($txid, $unsigned, $signed, $privateKey, $chainId, $nonce, $gasPrice, $gasLimit, $to, $value, $data) 57 | { 58 | $transaction = new Transaction($nonce, $gasPrice, $gasLimit, $to, $value, $data); 59 | $this->assertSame($txid, $transaction->hash($chainId)); 60 | } 61 | 62 | public static function getTransactionData(): array 63 | { 64 | return [ 65 | [ 66 | 'a0c5887430a600aac139fc269a354b0e5d4c700c3e177ea2c227502367224e9c', //txid 67 | 'ea048503f5476a0083027f4b941a8c8adfbe1c59e8b58cc0d515f07b7225f51c72882a45907d1bef7c0080', //unsigned 68 | 'f86d048503f5476a0083027f4b941a8c8adfbe1c59e8b58cc0d515f07b7225f51c72882a45907d1bef7c00801ba0e68be766b40702e6d9c419f53d5e053c937eda36f0e973074d174027439e2b5da0790df3e4d0294f92d69104454cd96005e21095efd5f2970c2829736ca39195d8', 69 | 'b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898', 0, '04', '03f5476a00', '027f4b', '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', '2a45907d1bef7c00', '' 70 | ], 71 | [ 72 | 'db734dc3a2da9bee8baf350f7d6e6f2dc593b06534a7721385cdb588431b6136', //txid 73 | 'ed048503f5476a0083027f4b941a8c8adfbe1c59e8b58cc0d515f07b7225f51c72882a45907d1bef7c0080018080', //unsigned 74 | 'f86d048503f5476a0083027f4b941a8c8adfbe1c59e8b58cc0d515f07b7225f51c72882a45907d1bef7c008025a0db4efcc22a7d9b2cab180ce37f81959412594798cb9af7c419abb6323763cdd5a0631a0c47d27e5b6e3906a419de2d732e290b73ead4172d8598ce4799c13bda69', 75 | 'b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898', 1, '04', '03f5476a00', '027f4b', '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', '2a45907d1bef7c00', '' 76 | ], 77 | [ 78 | '8e5a5404792108371948015b678cbe6a10d0cf40c9969eeb04829a6714170ff6', //txid 79 | 'ed048503f5476a0083027f4b941a8c8adfbe1c59e8b58cc0d515f07b7225f51c72882a45907d1bef7c0080388080', //unsigned 80 | 'f86e048503f5476a0083027f4b941a8c8adfbe1c59e8b58cc0d515f07b7225f51c72882a45907d1bef7c00808193a05c206269d6d902591b5b37ec821be99f78f1375191b8a8fb5a9e7bd87cb33999a027ca8262e92e3a5d7d2682d0394587bd7362250f4313767aa1a656c075d09911', 81 | 'b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898', 56, '04', '03f5476a00', '027f4b', '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', '2a45907d1bef7c00', '' 82 | ], 83 | ]; 84 | } 85 | 86 | public function testGetRawBadChainId() 87 | { 88 | $this->expectException(RuntimeException::class); 89 | $transaction = new Transaction('04', '03f5476a00', '027f4b', '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72', '2a45907d1bef7c00', ''); 90 | $transaction->getRaw('b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898', -1); 91 | } 92 | 93 | public function testBadPrivateKey() 94 | { 95 | $this->expectException(RuntimeException::class); 96 | $this->expectExceptionMessage('Incorrect private key'); 97 | $transaction = new Transaction(); 98 | $transaction->getRaw(''); 99 | } 100 | } 101 | --------------------------------------------------------------------------------