diff --git a/community_server/src/Controller/AppRequestsController.php b/community_server/src/Controller/AppRequestsController.php index 1b93375fc..e89a04815 100644 --- a/community_server/src/Controller/AppRequestsController.php +++ b/community_server/src/Controller/AppRequestsController.php @@ -21,9 +21,9 @@ class AppRequestsController extends AppController { parent::initialize(); $this->loadComponent('JsonRequestClient'); - $this->loadComponent('JsonRpcRequestClient'); + //$this->loadComponent('JsonRpcRequestClient'); //$this->Auth->allow(['add', 'edit']); - $this->Auth->allow(['index', 'sendCoins']); + $this->Auth->allow(['index', 'sendCoins', 'createCoins']); } @@ -52,14 +52,73 @@ class AppRequestsController extends AppController return $this->returnJson(['state' => 'error', 'msg' => 'no post or get']); } - private function checkRequiredFields($data, $fields) { + private function checkAndCopyRequiredFields($fields, &$param) { + $data = $this->request->input('json_decode'); foreach($fields as $field) { - if(!isset($data[$field])) { - return ['state' => 'error', 'msg' => 'missing field', 'details' => $field . ' not found']; + if(is_array($field)) { + $one_exist = false; + foreach($field as $oneField) { + if(isset($data[$oneField])) { + $param[$oneField] = $data[$oneField]; + $one_exist = true; + break; + } + } + if(!$one_exist) { + return ['state' => 'error', 'msg' => 'missing field of set', 'details' => $field]; + } + } else { + if(!isset($data[$field])) { + return ['state' => 'error', 'msg' => 'missing field', 'details' => $field . ' not found']; + } else { + $param[$field] = $data[$field]; + } } } return true; } + + private function rewriteKeys(&$data, $replaceKeys) + { + foreach(array_keys($replaceKeys) as $key) { + $newKey = $replaceKeys[$key]; + if(isset($data[$key])) { + $data[$newKey] = $data[$key]; + unset($data[$key]); + } + } + } + + private function parseParameterForCreateTransaction(&$param) + { + $login_request_result = $this->requestLogin(0, false); + if($login_request_result !== true) { + return $login_request_result; + } + $session = $this->getRequest()->getSession(); + $param['session_id'] = $session->read('session_id'); + $param['blockchain_type'] = $this->blockchainType; + + $this->rewriteKeys($data, ['email' => 'target_email', 'username' => 'target_username', 'pubkey' => 'target_pubkey']); + $required_fields = $this->checkAndCopyRequiredFields(['amount', ['target_email', 'target_username', 'target_pubkey']], $param); + if($required_fields !== true) { + return $required_fields; + } + + if(intval($param['amount']) <= 0) { + return ['state' => 'error', 'msg' => 'amount is invalid', 'details' => $param['amount']]; + } + + if(isset($data['memo'])) { + $param['memo'] = $data['memo']; + } + + if(isset($data['auto_sign'])) { + $param['auto_sign'] = boolval($data['auto_sign']); + } + + return true; + } public function sendCoins() { @@ -75,51 +134,90 @@ class AppRequestsController extends AppController if(!$this->request->is('post')) { return $this->returnJson(['state' => 'error', 'msg' => 'no post']); } - $data = $this->request->input('json_decode'); - $login_request_result = $this->requestLogin(0, false); - if($login_request_result !== true) { - return $this->returnJson($login_request_result); + + $params = []; + $result = $this->parseParameterForCreateTransaction($params); + if($result !== true) { + return $this->returnJson($result); } - $session = $this->getRequest()->getSession(); - $required_fields = $this->checkRequiredFields($data, ['amount', 'email']); + $required_fields = $this->checkAndCopyRequiredFields(['target_date'], $params); if($required_fields !== true) { return $this->returnJson($required_fields); } - $amount = $data['amount']; - if(intval($amount) <= 0) { - return $this->returnJson(['state' => 'error', 'msg' => 'amount is invalid', 'details' => $amount]); - } - $email = $data['email']; - if($email == '') { - return $this->returnJson(['state' => 'error', 'msg' => 'email is empty']); - } - $memo = ''; - if(isset($data['memo'])) { - $memo = $data['memo']; - } - $auto_sign = false; - if(isset($data['auto_sign'])) { - $auto_sign = boolval($data['auto_sign']); - } - $group = ''; - if(isset($data['group'])) { - $group = $data['group']; - } else { - $group = Configure::read('GroupAlias'); - } + $params['transaction_type'] = 'transfer'; + + $requestAnswear = $this->JsonRequestClient->sendRequest(json_encode($params), '/createTransaction'); - $requestAnswear = $this->JsonRequestClient->sendRequest(json_encode([ - 'session_id' => $session->read('session_id'), - 'transaction_type' => 'transfer', - 'memo' => $memo, - 'amount' => $amount, - 'target_group' => $group, - 'target_email' => $email, - 'auto_sign' => $auto_sign, - 'blockchain_type' => $this->blockchainType - ]), '/createTransaction'); if('success' == $requestAnswear['state'] && 'success' == $requestAnswear['data']['state']) { + $session = $this->getRequest()->getSession(); + $pendingTransactionCount = $session->read('Transactions.pending'); + if($pendingTransactionCount == null) { + $pendingTransactionCount = 1; + } else { + $pendingTransactionCount++; + } + $session->write('Transactions.pending', $pendingTransactionCount); + //echo "pending: " . $pendingTransactionCount; + return $this->returnJson(['state' => 'success']); + } else { + + /* + * if request contain unknown parameter format, shouldn't happen't at all + * {"state": "error", "msg": "parameter format unknown"} + * if json parsing failed + * {"state": "error", "msg": "json exception", "details":"exception text"} + * if session_id is zero or not set + * {"state": "error", "msg": "session_id invalid"} + * if session id wasn't found on login server, if server was restartet or user logged out (also per timeout, default: 15 minutes) + * {"state": "error", "msg": "session not found"} + * if session hasn't active user, shouldn't happen't at all, login-server should be checked if happen + * {"state": "code error", "msg":"user is zero"} + * if transaction type not known + * {"state": "error", "msg":"transaction_type unknown"} + * if receiver wasn't known to Login-Server + * {"state": "not found", "msg":"receiver not found"} + * if receiver account disabled, and therefor cannto receive any coins + * {"state": "disabled", "msg":"receiver is disabled"} + * if transaction was okay and will be further proccessed + * {"state":"success"} + */ + $answear_data = $requestAnswear['data']; + return $this->returnJson($answear_data); + + } + + } + + public function createCoins() + { + /* + * "session_id" : -127182, + * "email": "max.musterman@gmail.de", + * "amount": 10000000, + * "target_date":"2021-02-19T13:25:36+00:00", + * "memo":"AGE", + * "auto_sign": true + */ + if(!$this->request->is('post')) { + return $this->returnJson(['state' => 'error', 'msg' => 'no post']); + } + + $params = []; + $result = $this->parseParameterForCreateTransaction($params); + if($result !== true) { + return $this->returnJson($result); + } + $required_fields = $this->checkAndCopyRequiredFields(['target_date'], $params); + if($required_fields !== true) { + return $this->returnJson($required_fields); + } + $params['transaction_type'] = 'creation'; + + $requestAnswear = $this->JsonRequestClient->sendRequest(json_encode($params), '/createTransaction'); + + if('success' == $requestAnswear['state'] && 'success' == $requestAnswear['data']['state']) { + $session = $this->getRequest()->getSession(); $pendingTransactionCount = $session->read('Transactions.pending'); if($pendingTransactionCount == null) { $pendingTransactionCount = 1;