update transfer with new protocoL

This commit is contained in:
Dario Rekowski on RockPI 2020-10-29 11:34:15 +00:00 committed by Ulf Gebhardt
parent 26d1cfe0fe
commit 968d8d98e8
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
3 changed files with 81 additions and 112 deletions

View File

@ -146,10 +146,17 @@ class TransactionSendCoinsController extends AppController
}
$user = $session->read('StateUser');
}
$known_groups = $this->JsonRequestClient->sendRequest(json_encode([
'ask' => ['groups']
]), '/networkInfos');
$transferForm = new TransferForm();
$this->set('transferForm', $transferForm);
$this->set('timeUsed', microtime(true) - $startTime);
$this->set('groups', $known_groups['data']['data']['groups']);
$this->set('user', $user);
if ($this->request->is('post')) {
//$this->Flash->error(__('Wird zurzeit noch entwickelt!'));
@ -159,134 +166,88 @@ class TransactionSendCoinsController extends AppController
if(isset($requestData['add'])) {$mode = 'add'; }
if($transferForm->validate($requestData)) {
$receiverPubKeyHex = '';
$senderPubKeyHex = $user['public_hex'];
$amountCent = $this->GradidoNumber->parseInputNumberToCentNumber($requestData['amount']);
if(!isset($user['balance']) || $amountCent > $user['balance']) {
$this->Flash->error(__('Du hast nicht genug Geld!'));
return;
}
$receiverEmail = $requestData['email'];
if($receiverEmail === $user['email']) {
$this->Flash->error(__('Du kannst dir leider nicht selbst Geld schicken!'));
return;
}
$receiverPubkeyHex ='';
$requestAnswear = $this->JsonRequestClient->sendRequest(json_encode([
'session_id' => $session->read('session_id'),
'email' => $receiverEmail,
'ask' => ['user.pubkeyhex', 'user.disabled']
]), '/getUserInfos');
'transaction_type' => 'transfer',
'memo' => $requestData['memo'],
'amount' => $amountCent,
'target_group' => $known_groups['data']['data']['groups'][$requestData['group']],
'target_email' => $receiverEmail
]), '/createTransaction');
if('success' == $requestAnswear['state'] && 'success' == $requestAnswear['data']['state']) {
// will be allways 64 byte long, even if it is empty
$receiverPubKeyHex = $requestAnswear['data']['userData']['pubkeyhex'];
} else {
$this->addAdminError('TransactionSendCoins', 'create', $requestAnswear, $user['id']);
$this->Flash->error(__('Der Empfänger wurde nicht auf dem Login-Server gefunden, hat er sein Konto schon angelegt?'));
$this->set('timeUsed', microtime(true) - $startTime);
return;
}
if(isset($requestAnswear['data']['userData']['disabled']) && $requestAnswear['data']['userData']['disabled']) {
$this->Flash->error(__('Der Empfänger ist deaktiviert, daher können ihm zurzeit keine Gradidos gesendet werden.'));
$this->set('timeUsed', microtime(true) - $startTime);
return;
}
if(0 == ord($receiverPubKeyHex)) {
$stateUserTable = TableRegistry::getTableLocator()->get('StateUsers');
$receiverUser = $stateUserTable
->find('all')
->select(['public_key'])
->contain(false)
->where(['email' => $receiverEmail]);
if(!$receiverUser) {
$this->Flash->error(__('Diese E-Mail ist mir nicht bekannt, hat dein Empfänger denn schon ein Gradido-Konto?'));
$this->set('timeUsed', microtime(true) - $startTime);
return;
}
if(isset($receiverUser->public_key)) {
$receiverPubKeyHex = bin2hex(stream_get_contents($receiverUser->public_key));
$pendingTransactionCount = $session->read('Transactions.pending');
if($pendingTransactionCount == null) {
$pendingTransactionCount = 1;
} else {
$this->Flash->error(__('Das Konto mit der E-Mail: ' . $receiverEmail . ' wurde noch nicht aktiviert und kann noch keine GDD empfangen!'));
$pendingTransactionCount++;
}
$session->write('Transactions.pending', $pendingTransactionCount);
//echo "pending: " . $pendingTransactionCount;
if($mode === 'next') {
return $this->redirect($this->loginServerUrl . 'account/checkTransactions', 303);
} else {
$this->Flash->success(__('Transaction submitted for review.'));
}
} 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'];
if($answear_data['state'] === 'error') {
if($answear_data['msg'] === 'session_id invalid' || $answear_data['msg'] === 'session not found') {
$this->Flash->error(__('Fehler mit der Session, bitte logge dich erneut ein!'));
$this->set('timeUsed', microtime(true) - $startTime);
return;
}
} else if($answear_data['state'] === 'not found' && $answear_data['msg'] === 'receiver not found') {
$this->Flash->error(__('Der Empfänger wurde nicht auf dem Login-Server gefunden, hat er sein Konto schon angelegt?'));
$this->set('timeUsed', microtime(true) - $startTime);
return;
}
} else if($answear_data['state'] === 'disabled') {
$this->Flash->error(__('Der Empfänger ist deaktiviert, daher können ihm zurzeit keine Gradidos gesendet werden.'));
$this->set('timeUsed', microtime(true) - $startTime);
return;
} else {
$this->Flash->error(__('Unbehandelter Fehler: ') . json_encode($answear_data));
$this->set('timeUsed', microtime(true) - $startTime);
return;
}
}
//var_dump($sessionStateUser);
$builderResult = TransactionTransfer::build(
$amountCent,
$requestData['memo'],
$receiverPubKeyHex,
$senderPubKeyHex
);
if($builderResult['state'] === 'success') {
$http = new Client();
try {
$loginServer = Configure::read('LoginServer');
$url = $loginServer['host'] . ':' . $loginServer['port'];
$session_id = $session->read('session_id');
/*
*
* $response = $http->post(
* 'http://example.com/tasks',
* json_encode($data),
* ['type' => 'json']
* );
*/
$response = $http->post($url . '/checkTransaction', json_encode([
'session_id' => $session_id,
'transaction_base64' => base64_encode($builderResult['transactionBody']->serializeToString()),
'balance' => $user['balance']
]), ['type' => 'json']);
$json = $response->getJson();
if($json['state'] != 'success') {
if($json['msg'] == 'session not found') {
$session->destroy();
return $this->redirect($this->loginServerUrl . 'account', 303);
//$this->Flash->error(__('session not found, please login again'));
} else {
$this->Flash->error(__('login server return error: ' . json_encode($json)));
}
} else {
$pendingTransactionCount = $session->read('Transactions.pending');
if($pendingTransactionCount == null) {
$pendingTransactionCount = 1;
} else {
$pendingTransactionCount++;
}
$session->write('Transactions.pending', $pendingTransactionCount);
//echo "pending: " . $pendingTransactionCount;
if($mode === 'next') {
return $this->redirect($this->loginServerUrl . 'account/checkTransactions', 303);
} else {
$this->Flash->success(__('Transaction submitted for review.'));
}
}
} catch(\Exception $e) {
$msg = $e->getMessage();
$this->Flash->error(__('error http request: ') . $msg);
}
} else {
$this->Flash->error(__('No Valid Receiver Public given: ' . $receiverPubKeyHex));
}
// */
} else {
$this->Flash->error(__('Something was invalid, please try again!'));
}
}
$this->set('timeUsed', microtime(true) - $startTime);
}

View File

@ -1,5 +1,5 @@
<?php
// in src/Form/TransferForm.php
<?php
// in src/Form/ContactForm.php
namespace App\Form;
use Cake\Form\Form;
@ -13,13 +13,14 @@ class TransferForm extends Form
{
return $schema
->addField('email', ['type' => 'string'])
->addField('group', ['type' => 'string'])
->addField('amount', ['type' => 'decimal', 'precision' => 2])
->addField('memo', ['type' =>'text', 'default' => '', 'rows' => 3, 'maxlength' => 150]);
}
function validationDefault(Validator $validator)
{
$validator->setProvider('custom', 'App\Model\Validation\TransactionValidation');
$validator->setProvider('custom', 'App\Model\Validation\TransactionValidation');
/*
$validator->add('receiver_pubkey_hex', 'length', [
'rule' => ['length', 64],
@ -34,6 +35,12 @@ class TransferForm extends Form
'rule' => 'email',
'message' => __('A valid email address is required')
])
->add('group', 'custom', [
'rule' => 'alphaNumeric',
'provider' => 'custom',
//'message' => __('Only Alpha Numeric Character allowed')
'message' => __('No HTML Tags like &gt; or &lt; please.')
])
->add('memo', 'length', [
'rule' => ['maxLength', 150],
'message' => __('The memo should contain max 150 character')
@ -54,7 +61,7 @@ class TransferForm extends Form
->allowEmptyString('receiver_pubkey_hex', null, 'create')*/
->add('amount', 'custom', [
'rule' => 'amount',
'provider' => 'custom',
'provider' => 'custom',
'message' => __('Please give a valid positive number with maximal 2 decimal places')
]);
return $validator;
@ -72,4 +79,4 @@ class TransferForm extends Form
// Send an email.
return true;
}
}
}

View File

@ -17,6 +17,7 @@ $this->loadHelper('Form', [
<div class="form-body">
<?= $this->Form->create($transferForm) ?>
<?= $this->Form->control('email', ['label' => __('Empfänger'), 'placeholder' => 'E-Mail']) ?>
<?= $this->Form->control('group', ['label' => __('Community Alias'), 'options' => $groups, 'default' => $user['group_alias']]) ?>
<?= $this->Form->control('memo', ['label' => __('Verwendungszweck'), 'rows' => 3]) ?>
<?= $this->Form->control('amount', ['label' => __('Betrag in GDD')]) ?>
<?= $this->Form->button(__('Transaktion abschließen'), ['name' => 'next', 'class' => 'form-button']) ?>