From 42a009b87875c1b1399cb141942dc278ea48589f Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 25 May 2018 10:02:27 +0800 Subject: [PATCH 1/2] Add string as param in __construct --- src/Transaction.php | 39 +++++++++++++++++++++++++++-------- test/unit/TransactionTest.php | 3 +++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/Transaction.php b/src/Transaction.php index cb94056..33c6206 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -79,20 +79,41 @@ class Transaction implements ArrayAccess /** * construct * - * @param array $txData + * @param array|string $txData * @return void */ - public function __construct(array $txData=[]) + public function __construct($txData=[]) { - foreach ($txData as $key => $data) { - $txKey = isset($this->map[$key]) ? $this->map[$key] : null; - - if (is_int($txKey)) { - $this->txData[$txKey] = $data; - } - } $this->rlp = new RLP; $this->secp256k1 = new EC('secp256k1'); + $tx = []; + + if (is_array($txData)) { + foreach ($txData as $key => $data) { + $txKey = isset($this->map[$key]) ? $this->map[$key] : null; + + if (is_int($txKey)) { + $tx[$txKey] = $data; + } + } + } elseif (is_string($txData)) { + if (strpos($txData, '0x') === 0) { + $txData = $this->rlp->decode($txData); + + foreach ($txData as $txKey => $data) { + if (is_int($txKey)) { + $hexData = $data->toString('hex'); + + if (strlen($hexData) > 0) { + $tx[$txKey] = '0x' . $hexData; + } else { + $tx[$txKey] = $hexData; + } + } + } + } + } + $this->txData = $tx; } /** diff --git a/test/unit/TransactionTest.php b/test/unit/TransactionTest.php index 9ad21f6..57d7382 100644 --- a/test/unit/TransactionTest.php +++ b/test/unit/TransactionTest.php @@ -217,6 +217,9 @@ public function testEIP155() ]); $this->assertEquals('daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53', $transaction->hash(false)); $this->assertEquals('f86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83', $transaction->sign('0x4646464646464646464646464646464646464646464646464646464646464646')); + + $transaction = new Transaction('0xf86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83'); + $this->assertEquals('f86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83', $transaction->serialize()->toString('hex')); } /** From 5eb67068fbadad125968342eb0405fc912089fb0 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 25 May 2018 10:04:22 +0800 Subject: [PATCH 2/2] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 0f711fe..d369663 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,9 @@ $transaction = new Transaction([ 'chainId' => 1, 'data' => '0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675' ]); + +// hex encoded transaction +$transaction = new Transaction('0xf86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83'); ``` Sign a transaction: