From 9abc542ffef1baece5ce575d91319ba9aafdb8eb Mon Sep 17 00:00:00 2001 From: Dimitris Frangiadakis Date: Sat, 9 Feb 2019 21:24:26 +0200 Subject: [PATCH 1/9] Making sure signature is recoverable, test passes, not sure why it doesn't work yet --- .idea/workspace.xml | 105 ++++++++++++++++++++++++++------------------ src/IconService.php | 29 ++++++++++-- util/res.json | 16 +++++++ 3 files changed, 104 insertions(+), 46 deletions(-) create mode 100644 util/res.json diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 92e0780..9400c77 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,7 @@ - - - + - - - - - - - - - - - - @@ -38,20 +24,27 @@ + + + + + + + - + - + - - + + @@ -59,6 +52,15 @@ + + + + + + + + + @@ -66,6 +68,11 @@ + + + hex2 + + @@ -116,7 +124,7 @@ - + @@ -199,18 +210,18 @@ - + - - + @@ -230,42 +241,11 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -298,8 +278,18 @@ - - + + + + + + + + + + + + diff --git a/README.md b/README.md index a42899d..94e14c8 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,13 @@ Already fully or partially supports the following JSON-RPC functions: * icx_getTransactionResult * icx_getTransactionByHash * ise_getStatus +* icx_sendTransaction --Partial support, can only send ICX for now There is also some wallet support, but is not yet stable for production. TODO -------- -* icx_sendTransaction Usage:.. From 1ddc0f03a1d3998a0829a38eff64dcfabf53d3ac Mon Sep 17 00:00:00 2001 From: Dimitris Frangiadakis Date: Sun, 10 Feb 2019 13:43:01 +0200 Subject: [PATCH 5/9] Added messaging, call, install and update contract functionality --- .idea/workspace.xml | 119 ++++++------ src/IconService.php | 385 +++++++++++++++++++++++++++++++++++++- tests/IconServiceTest.php | 98 +++++++++- util/res.json | 16 -- 4 files changed, 534 insertions(+), 84 deletions(-) delete mode 100644 util/res.json diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b0e85dc..5c3eaa4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,10 @@ - + + + + @@ -92,9 +75,9 @@ @@ -135,9 +118,9 @@ - - + + + + + - @@ -198,18 +188,19 @@ - + + - - + - + @@ -230,7 +221,7 @@ - + @@ -265,13 +256,17 @@ - + + - - + + + + + @@ -279,16 +274,10 @@ - - - - - - - - + + @@ -297,8 +286,8 @@ - - + + diff --git a/src/IconService.php b/src/IconService.php index f55525c..a66b1ab 100644 --- a/src/IconService.php +++ b/src/IconService.php @@ -21,10 +21,15 @@ class IconService */ //Mainnet //private $icon_service_URL = 'https://ctz.solidwallet.io/api/v3'; + //Yeouido + //private $icon_service_URL = "https://bicon.net.solidwallet.io/api/v3"; private $version = "0x3"; - private $icon_service_URL = "https://bicon.net.solidwallet.io/api/v3"; + private $icon_service_URL; + public function __construct($url){ + $this->icon_service_URL = $url; + } /** * icx_getLastBlock * @@ -454,7 +459,7 @@ public function send($from, $to, $value, $stepLimit, string $privateKey, $nid = return json_decode($result); } - public function callSCORE($from, $to, $value, $stepLimit, string $privateKey, string $method, array $params, $nid = '0x1') + public function callSCORE($from, $to, $stepLimit, string $privateKey, string $method, array $params, $nid = '0x1') { //Create transaction table $data = array( @@ -465,7 +470,6 @@ public function callSCORE($from, $to, $value, $stepLimit, string $privateKey, st "version" => $this->version, "from" => $from, "to" => $to, - "value" => $value, "stepLimit" => $stepLimit, "timestamp" => $this->getBase64TimestampInMilliseconds(), "nid" => $nid, diff --git a/tests/IconServiceTest.php b/tests/IconServiceTest.php index f9be4e2..7161959 100644 --- a/tests/IconServiceTest.php +++ b/tests/IconServiceTest.php @@ -19,9 +19,14 @@ class IconServiceTest extends TestCase * any typo before you even use this library in a real project. * */ - /*public function testIsThereAnySyntaxError() + //Mainnet + private $icon_service_URL_main = 'https://ctz.solidwallet.io/api/v3'; + //Yeouido + private $icon_service_URL_yeouido = "https://bicon.net.solidwallet.io/api/v3"; + + public function testIsThereAnySyntaxError() { - $var = new mitsosf\IconSDK\IconService; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_main); $this->assertTrue(is_object($var)); unset($var); } @@ -30,7 +35,7 @@ class IconServiceTest extends TestCase //TODO Check if request is made properly, error doesn't mean that test should fail public function test_icx_getLastBlock() { - $var = new mitsosf\IconSDK\IconService; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_main); $this->assertTrue(!isset($var->icx_getLastBlock()->error)); unset($var); } @@ -40,7 +45,7 @@ public function test_icx_getLastBlock() //TODO Check if request is made properly, error doesn't mean that test should fail public function test_icx_getBlockByHeight() { - $var = new mitsosf\IconSDK\IconService; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_main); $height = "0x3"; $this->assertTrue(!isset($var->icx_getBlockByHeight($height)->error)); unset($var); @@ -50,8 +55,8 @@ public function test_icx_getBlockByHeight() //TODO Check if request is made properly, error doesn't mean that test should fail public function test_icx_getBlockByHash() { - $var = new mitsosf\IconSDK\IconService; - $hash = "0xb9c1edf5ad1c5d698e008d02ad12a90eb923839b2c690a993ed43102524b4f71"; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_yeouido); + $hash = "0x8611088a8e121b7148ffaa9e53076bb84295868fc6bad267a811000d522061af"; //Yeouido $this->assertTrue(!isset($var->icx_getBlockByHash($hash)->error)); unset($var); } @@ -60,7 +65,7 @@ public function test_icx_getBlockByHash() //TODO Check if request is made properly, error doesn't mean that test should fail public function test_icx_call() { - $var = new mitsosf\IconSDK\IconService; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_main); $from = "hx70e8eeb5d23ab18a828ec95f769db6d953e5f0fd"; $score = "cx9ab3078e72c8d9017194d17b34b1a47b661945ca"; @@ -77,7 +82,7 @@ public function test_icx_call() //TODO Check if request is made properly, error doesn't mean that test should fail public function test_icx_getBalance() { - $var = new mitsosf\IconSDK\IconService; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_main); $address = "hx70e8eeb5d23ab18a828ec95f769db6d953e5f0fd"; $this->assertTrue(!isset($var->icx_getBalance($address)->error)); @@ -88,7 +93,7 @@ public function test_icx_getBalance() //TODO Check if request is made properly, error doesn't mean that test should fail public function test_icx_getScoreApi() { - $var = new mitsosf\IconSDK\IconService; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_main); $address = "cx9ab3078e72c8d9017194d17b34b1a47b661945ca"; $this->assertTrue(!isset($var->icx_getScoreApi($address)->error)); @@ -97,31 +102,34 @@ public function test_icx_getScoreApi() public function test_icx_getTotalSupply() { - $var = new mitsosf\IconSDK\IconService; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_main); $this->assertTrue(!isset($var->icx_getTotalSupply()->error)); unset($var); } - public function test_icx_getTransactionResult(){ - $var = new mitsosf\IconSDK\IconService; + public function test_icx_getTransactionResult() + { + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_main); $txHash = "0xb89690b7598e07c286db87f05c1ee4cfc1cf915bf061007ac3404a42dc4979e9"; $this->assertTrue(!isset($var->icx_getTransactionResult($txHash)->error)); unset($var); } - public function test_icx_getTransactionByHash(){ - $var = new mitsosf\IconSDK\IconService; + public function test_icx_getTransactionByHash() + { + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_main); $txHash = "0xb89690b7598e07c286db87f05c1ee4cfc1cf915bf061007ac3404a42dc4979e9"; $this->assertTrue(!isset($var->icx_getTransactionByHash($txHash)->error)); unset($var); } - public function test_ise_getStatus(){ - $var = new mitsosf\IconSDK\IconService; + public function test_ise_getStatus() + { + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_main); $keys = ['lastBlock']; $this->assertTrue(!isset($var->ise_getStatus($keys)->error)); @@ -129,23 +137,22 @@ public function test_ise_getStatus(){ } //Not working for now - /* - public function test_debug_estimateStep(){ - $var = new mitsosf\IconSDK\IconService; - $from = "hxc4193cda4a75526bf50896ec242d6713bb6b02a3"; - $to = "hxaa36c3e67d51f993a900fd5acf8b1eb5029c5dfd"; - $timestamp = "0x5c42da6830136"; - $value = "0xde0b6b3a7640000"; + /* public function test_debug_estimateStep(){ + $var = new mitsosf\IconSDK\IconService; - fwrite(STDERR, print_r($var->debug_estimateStep($from, $to, $timestamp, $value), TRUE)); - $this->assertTrue(!isset($var->debug_estimateStep(from, $to, $timestamp, $value)->error)); - unset($var); - } + $from = "hxc4193cda4a75526bf50896ec242d6713bb6b02a3"; + $to = "hxaa36c3e67d51f993a900fd5acf8b1eb5029c5dfd"; + $timestamp = "0x5c42da6830136"; + $value = "0xde0b6b3a7640000"; + + $this->assertTrue(!isset($var->debug_estimateStep($from, $to, $timestamp, $value)->error)); + unset($var); + }*/ public function test_send() { - $var = new mitsosf\IconSDK\IconService; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_yeouido); $private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key $from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; @@ -153,82 +160,77 @@ public function test_send() $value = "0x2386f26fc10000"; // = 0.01 ICX $stepLimit = "0x186a0"; // = 100000 steps $nid = "0x3"; // YEOUIDO network - $test = $var->send($from, $to, $value, $stepLimit, $private_key, $nid); - var_dump($test); - - unset($var); - - } - - public function test_callSCORE() - { - //TODO properly test with contract - $var = new mitsosf\IconSDK\IconService; - - $private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key - $from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; - $to = "cxf8689d6c4c8f333651469fdea2ac59a18f6c242d"; - $stepLimit = "0x186a0"; // = 100000 steps - $nid = "0x3"; // YEOUIDO network - $method = "tranfer"; - $params = array( - "to" => "hxmyAss", - "value" => "0x1231" - ); - $test = $var->callSCORE($from, $to, $stepLimit, $private_key, $method, $params, $nid); - var_dump($test); - unset($var); - - } - - public function test_installSCORE() - { - //TODO properly test with contract - $var = new mitsosf\IconSDK\IconService; - - $private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key - $from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; - $value = "0x2386f26fc10000"; // = 0.01 ICX - $stepLimit = "0x186a0"; // = 100000 steps - $nid = "0x3"; // YEOUIDO network - $score = "0xtestScoreData"; - $params = array( - "name" => "TestTokenn", - "symbol" => "tst", - "decimals" => "0x12" //18 - ); - $test = $var->installSCORE($from, $value, $stepLimit, $private_key, $score, $params, $nid); - var_dump($test); - - unset($var); - - } - - public function test_updateSCORE() - { - //TODO properly test with contract - $var = new mitsosf\IconSDK\IconService; - - $private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key - $from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; - $to = "hxf8689d6c4c8f333651469fdea2ac59a18f6c242d";s - $stepLimit = "0x186a0"; // = 100000 steps - $nid = "0x3"; // YEOUIDO network - $score = "0xtestScoreData"; - $params = array( - "amount" => "0x123" - ); - $test = $var->updateSCORE($from, $to, $stepLimit, $private_key, $score, $params, $nid); - var_dump($test); - - unset($var); - - }*/ + $this->assertTrue(!isset($var->send($from, $to, $value, $stepLimit, $private_key, $nid)->error)); + + unset($var); + + } + //Commenting out until I find a contract to test against + /* public function test_callSCORE() + { + //TODO properly test with contract + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_yeouido); + + $private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key + $from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; + $to = "cxf8689d6c4c8f333651469fdea2ac59a18f6c242d"; + $stepLimit = "0x186a0"; // = 100000 steps + $nid = "0x3"; // YEOUIDO network + $method = "tranfer"; + $params = array( + "to" => "hxmyAss", + "value" => "0x1231" + ); + + $this->assertTrue(!isset($var->callSCORE($from, $to, $stepLimit, $private_key, $method, $params, $nid)->error)); + unset($var); + } + + public function test_installSCORE() + { + //TODO properly test with contract + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_yeouido); + + $private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key + $from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; + $stepLimit = "0x186a0"; // = 100000 steps + $nid = "0x3"; // YEOUIDO network + $score = "0xtestScoreData"; + $params = array( + "name" => "TestTokenn", + "symbol" => "tst", + "decimals" => "0x12" //18 + ); + + $this->assertTrue(!isset($var->installSCORE($from, $stepLimit, $private_key, $score, $params, $nid)->error)); + unset($var); + + } + + public function test_updateSCORE() + { + //TODO properly test with contract + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_yeouido); + + $private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key + $from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; + $to = "hxf8689d6c4c8f333651469fdea2ac59a18f6c242d"; + $stepLimit = "0x186a0"; // = 100000 steps + $nid = "0x3"; // YEOUIDO network + $score = "0xtestScoreData"; + $params = array( + "amount" => "0x123" + ); + + $this->assertTrue(!isset($var->updateSCORE($from, $to, $stepLimit, $private_key, $score, $params, $nid)->error)); + unset($var); + + }*/ public function test_message() { - $var = new mitsosf\IconSDK\IconService; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_yeouido); $private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key $from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; @@ -238,11 +240,9 @@ public function test_message() $stepLimit = "0xfffff"; // = 100000 steps $nid = "0x3"; // YEOUIDO network - $test = $var->message($from, $to, $stepLimit, $private_key, $message, $nid); - var_dump($test); - + $this->assertTrue(!isset($var->message($from, $to, $stepLimit, $private_key, $message, "0x0", $nid)->error)); unset($var); - //TODO Assert stuff + } } From 896d3c2f184fc7eaa9d4200f3f89af269fa0d67c Mon Sep 17 00:00:00 2001 From: Dimitris Frangiadakis Date: Sun, 10 Feb 2019 16:47:13 +0200 Subject: [PATCH 7/9] Wrote tests for all wallet functions. Also renamed sme functions and restructured wallet files --- .idea/workspace.xml | 98 +++++++++++++++++--------------------------- src/Wallet.php | 17 +++----- tests/WalletTest.php | 28 ++++++++++--- 3 files changed, 64 insertions(+), 79 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 700d658..d18e00c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,8 @@ - - - + + - - + @@ -220,27 +213,6 @@ - - - - - - - - - - - - - - - - - - - - - - + - - + + + + + - - + + + + + + - + - - + + - + - - - - - + + - + - - + + diff --git a/src/Wallet.php b/src/Wallet.php index 0946873..800df6f 100644 --- a/src/Wallet.php +++ b/src/Wallet.php @@ -59,22 +59,15 @@ public function getPublicKey($private_key){ } $publicKey = $ec->keyFromPrivate($private_key)->getPublic(false, 'hex'); - if (!$this->isPublicKey(substr($publicKey,2))){ - throw new \Exception('Invalid public key'); - } - - $address = $this->pubKeyToAddress($publicKey); - //TODO check if valid address - return $address; + return substr($publicKey,2); } - static function pubKeyToAddress($pubkey) { - return "hx" . substr(hash('sha3-256', substr(hex2bin($pubkey),1)),-40); - //return "hx" . substr(Keccak::hash(substr(hex2bin($pubkey),1), 256),-40); + public function pubKeyToAddress($publicKey) { + return "hx" . substr(hash('sha3-256', hex2bin($publicKey)),-40); } - private function isPrivateKey($key) + public function isPrivateKey($key) { $length = 64; if (strlen($key)!== $length){ @@ -88,7 +81,7 @@ private function isPrivateKey($key) return true; } - private function isPublicKey($key){ + public function isPublicKey($key){ $length = 128; if (strlen($key)!== $length){ return false; diff --git a/tests/WalletTest.php b/tests/WalletTest.php index 9faee7d..f95bc10 100644 --- a/tests/WalletTest.php +++ b/tests/WalletTest.php @@ -11,22 +11,38 @@ */ class WalletTest extends TestCase { - public function test_privateKeyGeneration() + public $private_key="3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; + public $public_key="c818b3d2ddeb6f29aaba7a85f113e057fb6ad3c522710d9831ef9501d477dff4c29b5585ce412edaf6702faa13b8ab78fcf166b853dcbaf3fed7eefbec0461ce"; + public $public_address ="hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; + + public function test_create() + { + $var = new mitsosf\IconSDK\Wallet; + + $key = $var->create(); + $this->assertTrue(strlen($key) === 64); + $this->assertTrue(ctype_xdigit($key)); + unset($var); + } + + public function test_getPublicKey() { $var = new mitsosf\IconSDK\Wallet; - $var->create(); + $key = $var->getPublicKey($this->private_key); + $this->assertTrue(strlen($key) === 128); + $this->assertTrue(ctype_xdigit($key)); + $this->assertSame($this->public_key, $key); unset($var); } - public function test_publicKeyGeneration(){ + public function test_pubkeyToAddress(){ $var = new mitsosf\IconSDK\Wallet; $privateKey = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; $expectedAddress = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; - $test = $var->getPublicKey($privateKey); - var_dump($test); - //TODO Assert stuff + $derived = $var->pubKeyToAddress($var->getPublicKey($privateKey)); + $this->assertSame($expectedAddress, $derived); unset($var); } From 5e710d038dd00739a2a386e21b50447f43594dc8 Mon Sep 17 00:00:00 2001 From: Dimitris Frangiadakis Date: Fri, 21 Jun 2019 22:35:42 +0300 Subject: [PATCH 8/9] Refactored IconService.php and added some Wallet tests --- README.md | 14 +++--- src/IconService.php | 63 +++++++++++++------------- src/Wallet.php | 95 +++++++++++++++++++++++++-------------- tests/IconServiceTest.php | 4 +- tests/WalletTest.php | 23 +++++++++- 5 files changed, 124 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 94e14c8..34b3588 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ ICON SDK for PHP (Unofficial) ========================= This is an unofficial SDK to communicate with the ICON JSON-RPC server, built for PHP. -Disclaimer: Still not ready to be used in production +Disclaimer: I cannot guarantee optimal performance of this software. +It is provided as is and without any assurances. Use it at your own risk. Features -------- @@ -20,13 +21,16 @@ Already fully or partially supports the following JSON-RPC functions: * ise_getStatus * icx_sendTransaction --Partial support, can only send ICX for now -There is also some wallet support, but is not yet stable for production. +There is also some wallet support, see Wallet.php file for more info. +More detailed documentation coming soon. TODO -------- -Usage:.. - - +* Usage:.. +* More tests, especially on the wallet part +* Debug and p-rep functions seem not to be working on ICON's part atm. +Will promptly update when they become available. +* Refactoring diff --git a/src/IconService.php b/src/IconService.php index a66b1ab..f18cbaa 100644 --- a/src/IconService.php +++ b/src/IconService.php @@ -3,14 +3,8 @@ namespace mitsosf\IconSDK; use Elliptic\EC; -use Elliptic\Utils; /** - * A sample class - * - * Use this section to define what this class is doing, the PHPDocumentator will use this - * to automatically generate an API documentation using this information. - * * @author Dimitris Frangiadakis */ class IconService @@ -30,6 +24,7 @@ class IconService public function __construct($url){ $this->icon_service_URL = $url; } + /** * icx_getLastBlock * @@ -54,7 +49,7 @@ public function icx_getLastBlock() 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -90,7 +85,7 @@ public function icx_getBlockByHeight($height) 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -126,7 +121,7 @@ public function icx_getBlockByHash($hash) 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -171,7 +166,7 @@ public function icx_call($from, $score, $method, $params) 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -207,7 +202,7 @@ public function icx_getBalance($address) 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -243,7 +238,7 @@ public function icx_getScoreApi($address) 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -274,7 +269,7 @@ public function icx_getTotalSupply() 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -310,7 +305,7 @@ public function icx_getTransactionResult($txHash) 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -346,7 +341,7 @@ public function icx_getTransactionByHash($txHash) 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -382,7 +377,7 @@ public function ise_getStatus($keys) 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -453,7 +448,7 @@ public function send($from, $to, $value, $stepLimit, string $privateKey, $nid = 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -551,7 +546,7 @@ public function callSCORE($from, $to, $stepLimit, string $privateKey, string $me 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -650,7 +645,7 @@ public function installSCORE($from, $stepLimit, string $privateKey, string $scor 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -749,7 +744,7 @@ public function updateSCORE($from, $to, $stepLimit, string $privateKey, string $ 'Content-Type: application/json' )); - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -815,17 +810,7 @@ public function message($from, $to, $stepLimit, string $privateKey, string $mess //Add signature to transaction data $data["params"]["signature"] = $transaction_signature; - //Send request to RPC - $data_string = json_encode($data); - $ch = curl_init($this->icon_service_URL); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); - curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, array( - 'Content-Type: application/json' - )); - - $result = curl_exec($ch); + $result = $this->sendRequest($data); //Return as object return json_decode($result); @@ -845,7 +830,7 @@ public function message($from, $to, $stepLimit, string $privateKey, string $mess * @param string $nonce An arbitrary number used to prevent transaction hash collision eg.0x1 * @return string */ - /* +/* //TODO make it work for contracts as well public function debug_estimateStep($from, $to, $timestamp, $value = "0", $nid = "0x1", $nonce = "0x1") { @@ -879,6 +864,20 @@ public function debug_estimateStep($from, $to, $timestamp, $value = "0", $nid = return json_decode($result); }*/ + private function sendRequest($data){ + //Send request to RPC + $data_string = json_encode($data); + $ch = curl_init($this->icon_service_URL); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json' + )); + + return curl_exec($ch); + } + private function getBase64TimestampInMilliseconds() { $milliseconds = round(microtime(true) * 1000000); diff --git a/src/Wallet.php b/src/Wallet.php index 800df6f..bf173c4 100644 --- a/src/Wallet.php +++ b/src/Wallet.php @@ -3,43 +3,39 @@ namespace mitsosf\IconSDK; use Elliptic\EC; -use GMP; -use http\Exception; -use kornrunner\Keccak; -use Mdanter\Ecc\EccFactory; -use function Sodium\add; +use Exception; +/** + * @property string private_key + * @property string public_key + * @property string public_address + */ class Wallet { private $private_key; private $public_key; + private $public_address; - function __construct($privateKey = null, $publicKey = null) + function __construct($privateKey = null) { - if (is_null($privateKey) && is_null($publicKey)) { - return "Both a private and a public key cannot be supplied to the constructor."; + if (is_null($privateKey)) { // Generate wallet + $this->private_key = $this->create(); + } else { + $this->private_key = $privateKey; } - - if (!is_null($privateKey) && !is_null($publicKey)) { - return "Both a private and a public key cannot be supplied to the constructor."; - } - - if (is_null($privateKey )){ - - } - + $this->public_key = $this->getPublicKeyFromPrivate($this->private_key); + $this->public_address = $this->pubKeyToAddress($this->public_key); } - public function create(){ - - + public function create() + { $characters = '0123456789abcdef'; $charactersLength = strlen($characters); $private_key = ''; for ($i = 0; $i < 64; $i++) { try { $private_key .= $characters[random_int(0, $charactersLength - 1)]; - } catch (\Exception $e) { + } catch (Exception $e) { return $e->getMessage(); } } @@ -50,47 +46,78 @@ public function create(){ /** * @param $private_key * @return bool - * @throws \Exception + * @throws Exception */ - public function getPublicKey($private_key){ + public function getPublicKeyFromPrivate($private_key) + { $ec = new EC('secp256k1'); - if (!$this->isPrivateKey($private_key)){ - throw new \Exception('Invalid private key'); + if (!$this->isPrivateKey($private_key)) { + throw new Exception('Invalid private key'); } $publicKey = $ec->keyFromPrivate($private_key)->getPublic(false, 'hex'); - return substr($publicKey,2); + return substr($publicKey, 2); } - public function pubKeyToAddress($publicKey) { - return "hx" . substr(hash('sha3-256', hex2bin($publicKey)),-40); + public function pubKeyToAddress($publicKey) + { + return "hx" . substr(hash('sha3-256', hex2bin($publicKey)), -40); } public function isPrivateKey($key) { $length = 64; - if (strlen($key)!== $length){ + if (strlen($key) !== $length) { return false; } - if (!ctype_xdigit($key)){ + if (!ctype_xdigit($key)) { return false; } return true; } - public function isPublicKey($key){ + public function isPublicKey($key) + { $length = 128; - if (strlen($key)!== $length){ + if (strlen($key) !== $length) { return false; } - if (!ctype_xdigit($key)){ + if (!ctype_xdigit($key)) { return false; } return true; - } + } + + /** + * @return string + */ + public function getPrivateKey(): string + { + return $this->private_key; + } + + /** + * @return string + */ + public function getPublicKey(): string + { + return $this->public_key; + } + + /** + * @return string + */ + public function getPublicAddress(): string + { + return $this->public_address; + } + + + + } \ No newline at end of file diff --git a/tests/IconServiceTest.php b/tests/IconServiceTest.php index 7161959..1b22d60 100644 --- a/tests/IconServiceTest.php +++ b/tests/IconServiceTest.php @@ -139,7 +139,7 @@ public function test_ise_getStatus() //Not working for now /* public function test_debug_estimateStep(){ - $var = new mitsosf\IconSDK\IconService; + $var = new mitsosf\IconSDK\IconService($this->icon_service_URL_yeouido); $from = "hxc4193cda4a75526bf50896ec242d6713bb6b02a3"; $to = "hxaa36c3e67d51f993a900fd5acf8b1eb5029c5dfd"; @@ -235,7 +235,7 @@ public function test_message() $private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key $from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; $to = "hxf8689d6c4c8f333651469fdea2ac59a18f6c242d"; - //Lorem ipsum for message + //Test message $message = "Testing the Messaging system"; $stepLimit = "0xfffff"; // = 100000 steps $nid = "0x3"; // YEOUIDO network diff --git a/tests/WalletTest.php b/tests/WalletTest.php index f95bc10..4f5bdd4 100644 --- a/tests/WalletTest.php +++ b/tests/WalletTest.php @@ -15,6 +15,25 @@ class WalletTest extends TestCase public $public_key="c818b3d2ddeb6f29aaba7a85f113e057fb6ad3c522710d9831ef9501d477dff4c29b5585ce412edaf6702faa13b8ab78fcf166b853dcbaf3fed7eefbec0461ce"; public $public_address ="hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; + public function test_construct(){ + $var = new mitsosf\IconSDK\Wallet; + + $privateKey = $var->getPrivateKey(); + $publicKey = $var->getPublicKeyFromPrivate($privateKey); + $publicAddress = $var->pubKeyToAddress($publicKey); + + $this->assertTrue($var->getPublicKey() === $publicKey); + $this->assertTrue($var->getPublicAddress() === $publicAddress); + unset($var); + + $var = new mitsosf\IconSDK\Wallet('d4b34071dc52970d8631674d2e5db510527be5ae08f47cc8212d05f8b0d7db5d'); + + $this->assertTrue($var->getPrivateKey() === 'd4b34071dc52970d8631674d2e5db510527be5ae08f47cc8212d05f8b0d7db5d'); + $this->assertTrue($var->getPublicKey() === '9cf951e78c718208084bc9964a410064f78f724b15f39da67e26f3aed7450048c4e2aa8df3a0fa60eaa513a51740f2dff88dbf01c6bd9a17bb45eb907868542b'); + $this->assertTrue($var->getPublicAddress() === 'hx6365b257f4bc4697fac88862dbe0f6e1a263e6c6'); + unset($var); + } + public function test_create() { $var = new mitsosf\IconSDK\Wallet; @@ -29,7 +48,7 @@ public function test_getPublicKey() { $var = new mitsosf\IconSDK\Wallet; - $key = $var->getPublicKey($this->private_key); + $key = $var->getPublicKeyFromPrivate($this->private_key); $this->assertTrue(strlen($key) === 128); $this->assertTrue(ctype_xdigit($key)); $this->assertSame($this->public_key, $key); @@ -41,7 +60,7 @@ public function test_pubkeyToAddress(){ $privateKey = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; $expectedAddress = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; - $derived = $var->pubKeyToAddress($var->getPublicKey($privateKey)); + $derived = $var->pubKeyToAddress($var->getPublicKeyFromPrivate($privateKey)); $this->assertSame($expectedAddress, $derived); unset($var); From 943c99529b2e13fa4467fdcc3ab9ff199afd1a95 Mon Sep 17 00:00:00 2001 From: Dimitris Frangiadakis Date: Fri, 21 Jun 2019 22:38:50 +0300 Subject: [PATCH 9/9] Cleaned up extra files --- .idea/encodings.xml | 4 - .idea/icon-sdk-php.iml | 43 ------- .idea/misc.xml | 6 - .idea/modules.xml | 8 -- .idea/php.xml | 46 ------- .idea/vcs.xml | 6 - .idea/workspace.xml | 271 ----------------------------------------- 7 files changed, 384 deletions(-) delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/icon-sdk-php.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/php.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 15a15b2..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/icon-sdk-php.iml b/.idea/icon-sdk-php.iml deleted file mode 100644 index 1712bae..0000000 --- a/.idea/icon-sdk-php.iml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 28a804d..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 9fd4ea8..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml deleted file mode 100644 index 08d2ef0..0000000 --- a/.idea/php.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index d18e00c..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,271 +0,0 @@ - - - - - - - - - - $PROJECT_DIR$/composer.json - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - hex2 - getBase - $version - callSCO - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -