From af758d1d0e07319eda079eaec075d2d5e9d9c5bf Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Wed, 27 Nov 2019 11:17:48 +0000 Subject: [PATCH] redesign state balances overview --- src/Controller/DashboardController.php | 4 +- src/Controller/StateBalancesController.php | 31 +- .../TransactionSendCoinsController.php | 5 + src/Model/Table/StateUsersTable.php | 19 + src/Model/Table/TransactionCreationsTable.php | 5 + src/Model/Transactions/Transaction.php | 5 +- src/Model/Transactions/TransactionBase.php | 2 +- src/Model/Transactions/TransactionBody.php | 42 +- .../Transactions/TransactionCreation.php | 9 +- src/Template/Layout/frontend.ctp | 4 +- src/Template/Layout/frontend_ripple.ctp | 130 + src/Template/StateBalances/overview.ctp | 85 + src/Template/TransactionCreations/create.ctp | 1 + ...actionJsonRequestHandlerControllerTest.php | 5 + webroot/css/loginServer/style.css | 8973 +++++++++++++++++ webroot/css/materialdesignicons.min.css | 2 + webroot/favicon.ico | Bin 15086 -> 4286 bytes webroot/fonts/Roboto/Roboto-Black.ttf | Bin 0 -> 171480 bytes webroot/fonts/Roboto/Roboto-Bold.ttf | Bin 0 -> 170760 bytes webroot/fonts/Roboto/Roboto-Light.ttf | Bin 0 -> 170420 bytes webroot/fonts/Roboto/Roboto-Medium.ttf | Bin 0 -> 172064 bytes webroot/fonts/Roboto/Roboto-Regular.ttf | Bin 0 -> 171676 bytes webroot/fonts/Roboto/Roboto-Thin.ttf | Bin 0 -> 171904 bytes webroot/fonts/materialdesignicons-webfont.eot | Bin 0 -> 531696 bytes webroot/fonts/materialdesignicons-webfont.svg | 1 + webroot/fonts/materialdesignicons-webfont.ttf | Bin 0 -> 531476 bytes .../fonts/materialdesignicons-webfont.woff | Bin 0 -> 256404 bytes .../fonts/materialdesignicons-webfont.woff2 | Bin 0 -> 188316 bytes webroot/img/50x50.png | Bin 0 -> 182 bytes webroot/img/logo_half.webp | Bin 0 -> 2490 bytes webroot/img/logo_schrift.webp | Bin 0 -> 21314 bytes webroot/img/logo_schrift_half.webp | Bin 0 -> 12190 bytes webroot/js/basic.js | 75 + webroot/js/empty | 0 34 files changed, 9384 insertions(+), 14 deletions(-) create mode 100644 src/Template/Layout/frontend_ripple.ctp create mode 100644 webroot/css/loginServer/style.css create mode 100644 webroot/css/materialdesignicons.min.css create mode 100644 webroot/fonts/Roboto/Roboto-Black.ttf create mode 100644 webroot/fonts/Roboto/Roboto-Bold.ttf create mode 100644 webroot/fonts/Roboto/Roboto-Light.ttf create mode 100644 webroot/fonts/Roboto/Roboto-Medium.ttf create mode 100644 webroot/fonts/Roboto/Roboto-Regular.ttf create mode 100644 webroot/fonts/Roboto/Roboto-Thin.ttf create mode 100644 webroot/fonts/materialdesignicons-webfont.eot create mode 100644 webroot/fonts/materialdesignicons-webfont.svg create mode 100644 webroot/fonts/materialdesignicons-webfont.ttf create mode 100644 webroot/fonts/materialdesignicons-webfont.woff create mode 100644 webroot/fonts/materialdesignicons-webfont.woff2 create mode 100644 webroot/img/50x50.png create mode 100644 webroot/img/logo_half.webp create mode 100644 webroot/img/logo_schrift.webp create mode 100644 webroot/img/logo_schrift_half.webp create mode 100644 webroot/js/basic.js delete mode 100644 webroot/js/empty diff --git a/src/Controller/DashboardController.php b/src/Controller/DashboardController.php index 94aa66a7a..f7ac266ea 100644 --- a/src/Controller/DashboardController.php +++ b/src/Controller/DashboardController.php @@ -2,8 +2,8 @@ namespace App\Controller; use App\Controller\AppController; -use Cake\Routing\Router; -use Cake\ORM\TableRegistry; +//use Cake\Routing\Router; +//use Cake\ORM\TableRegistry; /** * StateUsers Controller diff --git a/src/Controller/StateBalancesController.php b/src/Controller/StateBalancesController.php index 9a3eb6e29..44911201b 100644 --- a/src/Controller/StateBalancesController.php +++ b/src/Controller/StateBalancesController.php @@ -1,6 +1,7 @@ viewBuilder()->setLayout('frontend'); + $this->viewBuilder()->setLayout('frontend_ripple'); + $session = $this->getRequest()->getSession(); + $user = $session->read('StateUser'); +// var_dump($user); + if(!$user) { + //return $this->redirect(Router::url('/', true) . 'account/', 303); + $result = $this->requestLogin(); + if($result !== true) { + return $result; + } + $user = $session->read('StateUser'); + } + + $creationsTable = TableRegistry::getTableLocator()->get('TransactionCreations'); + $creationTransactions = $creationsTable + ->find('all') + ->where(['state_user_id' => $user['id']]) + ->contain(['Transactions']); + + $transferTable = TableRegistry::getTableLocator()->get('TransactionSendCoins'); + $transferTransactions = $transferTable + ->find('all') + ->where(['OR' => ['state_user_id' => $user['id'], 'receiver_user_id' => $user['id']]]) + ->contain(['Transactions']); + + $transactions = []; + foreach($creationTransactions as $creation) { + + } $this->set('timeUsed', microtime(true) - $startTime); } diff --git a/src/Controller/TransactionSendCoinsController.php b/src/Controller/TransactionSendCoinsController.php index 491f6ddb5..e845dceee 100644 --- a/src/Controller/TransactionSendCoinsController.php +++ b/src/Controller/TransactionSendCoinsController.php @@ -92,6 +92,11 @@ class TransactionSendCoinsController extends AppController $receiverUsers = $this->TransactionSendCoins->ReceiverUsers->find('list', ['limit' => 200]); $this->set(compact('transactionSendCoin', 'transactions', 'stateUsers', 'receiverUsers')); } + + public function create() + { + + } /** * Delete method diff --git a/src/Model/Table/StateUsersTable.php b/src/Model/Table/StateUsersTable.php index fe18766e9..a28162d6a 100644 --- a/src/Model/Table/StateUsersTable.php +++ b/src/Model/Table/StateUsersTable.php @@ -61,6 +61,10 @@ class StateUsersTable extends Table $this->hasMany('TransactionSendCoins', [ 'foreignKey' => 'state_user_id' ]); + $this->hasMany('TransactionReceiveCoins', [ + 'className' => 'TransactionSendCoins', + 'foreignKey' => 'receiver_user_id' + ]); } /** @@ -96,4 +100,19 @@ class StateUsersTable extends Table return $rules; } + + public function getReceiverProposal() { + $stateUsers = $this->find('all'); + $receiverProposal = []; + foreach($stateUsers as $stateUser) { + $name = $stateUser->email; + $keyHex = bin2hex(stream_get_contents($stateUser->public_key)); + if($name === NULL) { + $name = $stateUser->first_name . ' ' . $stateUser->last_name; + } + array_push($receiverProposal, ['name' => $name, 'key' => $keyHex]); + //$stateUser->public_key + } + return $receiverProposal; + } } diff --git a/src/Model/Table/TransactionCreationsTable.php b/src/Model/Table/TransactionCreationsTable.php index 8f0afc158..70ab54071 100644 --- a/src/Model/Table/TransactionCreationsTable.php +++ b/src/Model/Table/TransactionCreationsTable.php @@ -45,6 +45,10 @@ class TransactionCreationsTable extends Table 'foreignKey' => 'state_user_id', 'joinType' => 'INNER' ]); + $this->belongsTo('StateUsers', [ + 'foreignKey' => 'receiver_user_id', + 'joinType' => 'INNER' + ]); } /** @@ -82,6 +86,7 @@ class TransactionCreationsTable extends Table { $rules->add($rules->existsIn(['transaction_id'], 'Transactions')); $rules->add($rules->existsIn(['state_user_id'], 'StateUsers')); + $rules->add($rules->existsIn(['receiver_user_id'], 'StateUsers')); return $rules; } diff --git a/src/Model/Transactions/Transaction.php b/src/Model/Transactions/Transaction.php index 1ae38ffbc..a75c6ddbd 100644 --- a/src/Model/Transactions/Transaction.php +++ b/src/Model/Transactions/Transaction.php @@ -101,7 +101,7 @@ class Transaction extends TransactionBase { $connection->begin(); //id transaction_id signature pubkey - if (!$this->mTransactionBody->save($this->getFirstPublic())) { + if (!$this->mTransactionBody->save($this->getFirstPublic(), $this->mProtoTransaction->getSigMap())) { $this->addErrors($this->mTransactionBody->getErrors()); $connection->rollback(); return false; @@ -117,7 +117,6 @@ class Transaction extends TransactionBase { $signatureEntitys = []; foreach($sigPairs as $sigPair) { $signatureEntity = $transactionsSignaturesTable->newEntity(); - $signatureEntity->transaction_id = $transactionId; $signatureEntity->signature = $sigPair->getEd25519(); $signatureEntity->pubkey = $sigPair->getPubKey(); @@ -136,6 +135,8 @@ class Transaction extends TransactionBase { return false; } + + $connection->commit(); return true; } diff --git a/src/Model/Transactions/TransactionBase.php b/src/Model/Transactions/TransactionBase.php index 4b1ba210e..859389e33 100644 --- a/src/Model/Transactions/TransactionBase.php +++ b/src/Model/Transactions/TransactionBase.php @@ -51,7 +51,7 @@ class TransactionBase { //debug($stateBalanceQuery); if($stateBalanceQuery->count() > 0) { - $stateBalanceEntry = $stateBalanceEntry->first(); + $stateBalanceEntry = $stateBalanceQuery->first(); $stateBalanceEntry->amount += $newAmountCent; } else { $stateBalanceEntry = $stateBalancesTable->newEntity(); diff --git a/src/Model/Transactions/TransactionBody.php b/src/Model/Transactions/TransactionBody.php index 96a8009d5..ce66fe1b5 100644 --- a/src/Model/Transactions/TransactionBody.php +++ b/src/Model/Transactions/TransactionBody.php @@ -68,7 +68,7 @@ class TransactionBody extends TransactionBase { return $this->mProtoTransactionBody->getData(); } - public function save($firstPublic) { + public function save($firstPublic, $sigMap) { $transactionsTable = TableRegistry::getTableLocator()->get('transactions'); $transactionEntity = $transactionsTable->newEntity(); @@ -87,7 +87,45 @@ class TransactionBody extends TransactionBase { $this->addError('TransactionBody::save', 'error saving transaction with: ' . json_encode($transactionEntity->getError())); return false; } - return true; + $previousTxHash = null; + if($this->mTransactionID > 1) { + try { + $previousTransaction = $transactionsTable->get($this->mTransactionID - 1, [ + 'contain' => false, + 'fields' => ['tx_hash'] + ]); + } catch(Cake\Datasource\Exception\RecordNotFoundException $ex) { + $this->addError('TransactionBody::save', 'previous transaction (with id ' . ($this->mTransactionID-1) . ' not found'); + return false; + } + if(!$previousTransaction) { + // shouldn't occur + $this->addError('TransactionBody::save', 'previous transaction (with id ' . ($this->mTransactionID-1) . ' not found'); + return false; + } + $previousTxHash = $previousTransaction->tx_hash; + } + $transactionEntity->received = $transactionsTable->get($transactionEntity->id, ['contain' => false, 'fields' => ['received']])->received; + + // calculate tx hash + // previous tx hash + id + received + sigMap as string + // Sodium use for the generichash function BLAKE2b today (11.11.2019), mabye change in the future + $state = \Sodium\crypto_generichash_init(); + //echo "prev hash: $previousTxHash\n"; + if($previousTxHash != null) { + \Sodium\crypto_generichash_update($state, stream_get_contents($previousTxHash)); + } + //echo "id: " . $transactionEntity->id . "\n"; + \Sodium\crypto_generichash_update($state, strval($transactionEntity->id)); + //echo "received: " . $transactionEntity->received; + \Sodium\crypto_generichash_update($state, $transactionEntity->received->i18nFormat('yyyy-MM-dd HH:mm:ss')); + \Sodium\crypto_generichash_update($state, $sigMap->serializeToString()); + $transactionEntity->tx_hash = \Sodium\crypto_generichash_final($state); + if ($transactionsTable->save($transactionEntity)) { + return true; + } + $this->addError('TransactionBody::save', 'error saving transaction with: ' . json_encode($transactionEntity->getError())); + return false; } public function getTransactionID() { diff --git a/src/Model/Transactions/TransactionCreation.php b/src/Model/Transactions/TransactionCreation.php index 1538f9366..073ae2bf7 100644 --- a/src/Model/Transactions/TransactionCreation.php +++ b/src/Model/Transactions/TransactionCreation.php @@ -73,12 +73,13 @@ class TransactionCreation extends TransactionBase { $transactionCreationEntity->transaction_id = $transaction_id; // state user id - $state_user_id = $this->getStateUserId($firstPublic); - if(!$state_user_id) { + //$state_user_id = $this->getStateUserId($firstPublic); + $receiverUser = $this->getStateUserId($this->getReceiverPublic()); + if(!$receiverUser) { $this->addError('TransactionCreation::save', 'couldn\'t get state user id'); return false; } - $transactionCreationEntity->state_user_id = $state_user_id; + $transactionCreationEntity->state_user_id = $receiverUser; $transactionCreationEntity->amount = $this->getAmount(); $transactionCreationEntity->ident_hash = $this->getIdentHash(); @@ -86,7 +87,7 @@ class TransactionCreation extends TransactionBase { $this->addError('TransactionCreation::save', 'error saving transactionCreation with errors: ' . json_encode($transactionCreationEntity->getErrors())); return false; } - $receiverUser = $this->getStateUserId($this->getReceiverPublic()); + // update state balance if(!$this->updateStateBalance($receiverUser, $this->getAmount())) { return false; diff --git a/src/Template/Layout/frontend.ctp b/src/Template/Layout/frontend.ctp index 53384ea05..e23da0062 100644 --- a/src/Template/Layout/frontend.ctp +++ b/src/Template/Layout/frontend.ctp @@ -1,6 +1,6 @@

Community Server in Entwicklung

-

Alpha 0.5.0

+

Alpha 0.6.0