diff --git a/community_server/src/Controller/TransactionsController.php b/community_server/src/Controller/TransactionsController.php index 4f8e04c2b..90e78b7fb 100644 --- a/community_server/src/Controller/TransactionsController.php +++ b/community_server/src/Controller/TransactionsController.php @@ -313,15 +313,20 @@ class TransactionsController extends AppController if ($this->request->is('post')) { $transaction = $this->Transactions->patchEntity($transaction, $this->request->getData()); if ($this->Transactions->save($transaction)) { - $this->Flash->success(__('The transaction has been saved.')); - - return $this->redirect(['action' => 'index']); + $result = $this->Transactions->updateTxHash($transaction, 'start decay'); + if($result === true) { + $this->Flash->success(__('The transaction has been saved.')); + return $this->redirect(['action' => 'index']); + } else { + $this->Flash->error(__('Error by saving: ' . json_encode($result))); + } } $this->Flash->error(__('The transaction could not be saved. Please, try again.')); } $stateGroups = $this->Transactions->StateGroups->find('list', ['limit' => 200]); $transactionTypes = $this->Transactions->TransactionTypes->find('list', ['limit' => 200]); - $this->set(compact('transaction', 'stateGroups', 'transactionTypes')); + $blockchainTypes = $this->Transactions->BlockchainTypes->find('list'); + $this->set(compact('transaction', 'stateGroups', 'transactionTypes', 'blockchainTypes')); } /** diff --git a/community_server/src/Model/Table/TransactionsTable.php b/community_server/src/Model/Table/TransactionsTable.php index 893482e3f..fdd437076 100644 --- a/community_server/src/Model/Table/TransactionsTable.php +++ b/community_server/src/Model/Table/TransactionsTable.php @@ -266,4 +266,58 @@ class TransactionsTable extends Table return $final_transactions; } + + public function updateTxHash($transaction, $signatureMapString) + { + $transaction_id = $transaction->id; + $previousTxHash = null; + if($transaction_id > 1) { + try { + $previousTransaction = $this + ->find('all', ['contain' => false]) + ->select(['tx_hash']) + ->where(['id' => $transaction_id - 1]) + ->first(); + /*$previousTransaction = $transactionsTable->get($this->mTransactionID - 1, [ + 'contain' => false, + 'fields' => ['tx_hash'] + ]);*/ + } catch(Cake\Datasource\Exception\RecordNotFoundException $ex) { + return ['state' => 'error', 'msg' => 'previous transaction not found', 'details' => $ex->getMessage()]; + } + if(!$previousTransaction) { + // shouldn't occur + return ['state' => 'error', 'msg' => 'previous transaction not found']; + } + $previousTxHash = $previousTransaction->tx_hash; + } + try { + //$transactionEntity->received = $transactionsTable->get($transactionEntity->id, ['contain' => false, 'fields' => ['received']])->received; + $transaction->received = $this + ->find('all', ['contain' => false]) + ->where(['id' => $transaction->id]) + ->select(['received'])->first()->received; + } catch(Cake\Datasource\Exception\RecordNotFoundException $ex) { + return ['state' => 'error', 'msg' => 'current transaction not found in db', 'details' => $ex->getMessage()]; + } + + // 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($transaction->id)); + //echo "received: " . $transactionEntity->received; + \Sodium\crypto_generichash_update($state, $transaction->received->i18nFormat('yyyy-MM-dd HH:mm:ss')); + \Sodium\crypto_generichash_update($state, $signatureMapString); + $transaction->tx_hash = \Sodium\crypto_generichash_final($state); + if ($this->save($transaction)) { + return true; + } + return ['state' => 'error', 'msg' => 'error by saving transaction', 'details' => $transaction->getErrors()]; + } } diff --git a/community_server/src/Template/Transactions/add.ctp b/community_server/src/Template/Transactions/add.ctp index 59782d944..012d82027 100644 --- a/community_server/src/Template/Transactions/add.ctp +++ b/community_server/src/Template/Transactions/add.ctp @@ -35,7 +35,8 @@ Form->control('state_group_id', ['options' => $stateGroups]); echo $this->Form->control('transaction_type_id', ['options' => $transactionTypes]); - echo $this->Form->control('received'); + echo $this->Form->control('memo', ['type' => 'textarea']); + echo $this->Form->control('blockchain_type_id', ['options' => $blockchainTypes]); ?> Form->button(__('Submit')) ?>