Skip to content

Commit

Permalink
Merge pull request #7 from sc0Vu/master
Browse files Browse the repository at this point in the history
 Add string as param in __construct
  • Loading branch information
sc0Vu authored May 25, 2018
2 parents f1ff3c4 + 5eb6706 commit 063108a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ $transaction = new Transaction([
'chainId' => 1,
'data' => '0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675'
]);

// hex encoded transaction
$transaction = new Transaction('0xf86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83');
```

Sign a transaction:
Expand Down
39 changes: 30 additions & 9 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions test/unit/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down

0 comments on commit 063108a

Please sign in to comment.