Skip to content

Commit

Permalink
Merge pull request #39 from MinterTeam/dev
Browse files Browse the repository at this point in the history
add tests for converter, tx, wallet
  • Loading branch information
grkamil authored Dec 11, 2018
2 parents afc4d0e + 92a4a2b commit e18f802
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 26 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This is a pure PHP SDK for working with <b>Minter</b> blockchain
- [Get hash of transaction](#get-hash-of-transaction)
- [Decode Transaction](#decode-transaction)
- [Minter Check](#create-minter-check)
* [Tests](#tests)

## Installing

Expand Down Expand Up @@ -589,4 +590,12 @@ use Minter\SDK\MinterCheck;
$check = new MinterCheck('your Minter address here', 'your pass phrase');

echo $check->createProof();
```

## Tests

To run unit tests:

```bash
vendor/bin/phpunit tests
```
8 changes: 7 additions & 1 deletion src/Minter/SDK/MinterConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Minter\SDK;

use Minter\Library\Helper;
use InvalidArgumentException;

/**
* Class MinterConverter
Expand Down Expand Up @@ -39,6 +40,11 @@ public static function convertValue(string $num, string $to)
*/
public static function convertCoinName(string $symbol): string
{
return $symbol . str_repeat(chr(0), 10 - strlen($symbol));
$countOfNulls = 10 - strlen($symbol);
if($countOfNulls < 0) {
throw new InvalidArgumentException('Coin name could have no more than 10 symbols.');
}

return $symbol . str_repeat(chr(0), $countOfNulls);
}
}
64 changes: 43 additions & 21 deletions src/Minter/SDK/MinterTx.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@
use Minter\Library\ECDSA;
use Minter\Library\Helper;
use Minter\SDK\MinterCoins\{
MinterCoinTx, MinterDelegateTx, MinterMultiSendTx, MinterRedeemCheckTx, MinterSellAllCoinTx, MinterSetCandidateOffTx, MinterSetCandidateOnTx, MinterCreateCoinTx, MinterDeclareCandidacyTx, MinterSendCoinTx, MinterUnboundTx, MinterSellCoinTx, MinterBuyCoinTx
MinterCoinTx,
MinterDelegateTx,
MinterMultiSendTx,
MinterRedeemCheckTx,
MinterSellAllCoinTx,
MinterSetCandidateOffTx,
MinterSetCandidateOnTx,
MinterCreateCoinTx,
MinterDeclareCandidacyTx,
MinterSendCoinTx,
MinterUnboundTx,
MinterSellCoinTx,
MinterBuyCoinTx
};

/**
Expand Down Expand Up @@ -332,26 +344,36 @@ protected function prepareResult(array $tx): array
{
$result = [];
foreach($this->structure as $key => $field) {
if($field === 'data') {
$result[$field] = $tx[$key];
}
elseif($field === 'payload' || $field === 'serviceData') {
$result[$field] = Helper::pack2hex($tx[$key]);
}
elseif($field === 'gasCoin') {
$result[$field] = MinterConverter::convertCoinName(
Helper::pack2hex($tx[$key])
);
}
elseif($field === 'signatureData') {
$result[$field] = [
'v' => hexdec($tx[$key][0]),
'r' => $tx[$key][1],
's' => $tx[$key][2]
];
}
else {
$result[$field] = hexdec($tx[$key]);
switch ($field) {
case 'data':
$result[$field] = $tx[$key];
break;

case 'payload':
$result[$field] = Helper::pack2hex($tx[$key]);
break;

case 'serviceData':
$result[$field] = Helper::pack2hex($tx[$key]);
break;

case 'gasCoin':
$result[$field] = MinterConverter::convertCoinName(
Helper::pack2hex($tx[$key])
);
break;

case 'signatureData':
$result[$field] = [
'v' => hexdec($tx[$key][0]),
'r' => $tx[$key][1],
's' => $tx[$key][2]
];
break;

default:
$result[$field] = hexdec($tx[$key]);
break;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Minter/SDK/MinterWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public static function create(): array
$privateKey = BIP44::fromMasterSeed($seed)->derive(self::BIP44_SEED_ADDRESS_PATH)->privateKey;

$publicKey = self::privateToPublic($privateKey);

$address = self::getAddressFromPublicKey($publicKey);

return [
'seed' => $seed,
'address' => $address,
'private_key' => $privateKey,
'mnemonic' => $mnemonic,
'seed' => $seed
'public_key' => $publicKey,
'private_key' => $privateKey
];
}

Expand Down
58 changes: 58 additions & 0 deletions tests/MinterConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Minter\SDK\MinterConverter;

/**
* Class for testing MinterConverter
*/
final class MinterConverterTest extends TestCase
{
/**
* Predefined values
*/
const VALUES = [
['1', '1000000000000000000'],
['0.1', '100000000000000000'],
['10', '10000000000000000000'],
['0.0000000000002', '200000'],
['0', '0']
];

/**
* Test converting value from bip to pip.
*/
public function testConvertValueToPIP()
{
foreach (self::VALUES as $data) {
$this->assertEquals($data[1], MinterConverter::convertValue($data[0], 'pip'));
}
}

/**
* Test converting value from pip to bip.
*/
public function testConvertValueToBIP()
{
foreach (self::VALUES as $data) {
$this->assertEquals($data[0], MinterConverter::convertValue($data[1], 'bip'));
}
}

/**
* Test converting coin name.
*/
public function testConvertCoinName()
{
$this->assertEquals(
'BIP' . str_repeat(chr(0), 10 - strlen('BIP')),
MinterConverter::convertCoinName('BIP')
);

$this->assertEquals(
'BIPBIPBIPP',
MinterConverter::convertCoinName('BIPBIPBIPP')
);
}
}
2 changes: 1 addition & 1 deletion tests/MinterSellAllCoinTxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PHPUnit\Framework\TestCase;

/**
* Class for testing MinterSellAllCoinTxTest
* Class for testing MinterSellAllCoinTx
*/
final class MinterSellAllCoinTxTest extends TestCase
{
Expand Down
123 changes: 123 additions & 0 deletions tests/MinterTxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Minter\SDK\MinterTx;
use Minter\SDK\MinterWallet;

/**
* Class for testing MinterTx
*/
final class MinterTxTest extends TestCase
{
/**
* Transaction structure
*/
const TX = [
'nonce' => 1,
'gasPrice' => 1,
'gasCoin' => 'MNT',
'type' => 1,
'data' => [
'to' => 'Mx1b685a7c1e78726c48f619c497a07ed75fe00483',
'value' => '1',
'coin' => 'MNT'
],
'payload' => '',
'serviceData' => '',
'signatureType' => 1
];

/**
* Intermediate transaction representation
*/
const INTERMEDIATE_TX = [
'nonce' => 1,
'gasPrice' => 1,
'gasCoin' => 'MNT',
'type' => 1,
'data' => [
'4d4e5400000000000000',
'1b685a7c1e78726c48f619c497a07ed75fe00483',
'0de0b6b3a7640000'
],
'payload' => '',
'serviceData' => '',
'signatureType' => 1,
'signatureData' => [
'v' => 27,
'r' => '5163017775fefa4d56f71ae50a8ddf361628fddc1101365b2eb6fd9b5dbdc250',
's' => '2fbdc56b6cf963206f807e2899f05e4fac71f43c9adfd11ea6baa7585b8b8115'
]
];

/**
* Sender Minter address
*/
const SENDER_ADDRESS = 'Mx31e61a05adbd13c6b625262704bc305bf7725026';

/**
* Private key for transaction
*/
const PRIVATE_KEY = '07bc17abdcee8b971bb8723e36fe9d2523306d5ab2d683631693238e0f9df142';

/**
* Predefined valid transaction
*/
const VALID_TX = '0xf88301018a4d4e540000000000000001aae98a4d4e5400000000000000941b685a7c1e78726c48f619c497a07ed75fe00483880de0b6b3a7640000808001b845f8431ba05163017775fefa4d56f71ae50a8ddf361628fddc1101365b2eb6fd9b5dbdc250a02fbdc56b6cf963206f807e2899f05e4fac71f43c9adfd11ea6baa7585b8b8115';

/**
* Predefined valid hash
*/
const VALID_HASH = 'Mt823ded85fc5dca098836333851144f88d5c8896b';

/**
* Test signing.
*/
public function testSign()
{
$tx = new MinterTx(self::TX);
$signature = $tx->sign(self::PRIVATE_KEY);

$this->assertEquals(self::VALID_TX, $signature);
}

/**
* Test get sender address.
*/
public function testGetSenderAddress()
{
$tx = new MinterTx(self::VALID_TX);
$address = $tx->getSenderAddress(self::INTERMEDIATE_TX);

$this->assertEquals(self::SENDER_ADDRESS, $address);
}

/**
* Test recovering public key.
*/
public function testRecoverPublicKey()
{
$tx = new MinterTx(self::VALID_TX);

$this->assertEquals(
MinterWallet::privateToPublic(self::PRIVATE_KEY),
$tx->recoverPublicKey(self::INTERMEDIATE_TX)
);
}

/**
* Test get hash by transaction.
*/
public function testGetHash()
{
// test getting hash after decoding
$tx = new MinterTx(self::VALID_TX);
$this->assertEquals(self::VALID_HASH, $tx->getHash());

// test getting hash after encoding
$tx = new MinterTx(self::TX);
$tx->sign(self::PRIVATE_KEY);
$this->assertEquals(self::VALID_HASH, $tx->getHash());
}
}
Loading

0 comments on commit e18f802

Please sign in to comment.