Skip to content

Commit 0945c24

Browse files
committed
Finalizing this project MVP and ready for showcase
1 parent bf6d58b commit 0945c24

19 files changed

+232
-105
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
<version>1.65</version>
4040
</dependency>
4141

42+
<!-- https://mvnrepository.com/artifact/org.json/json -->
43+
<dependency>
44+
<groupId>org.json</groupId>
45+
<artifactId>json</artifactId>
46+
<version>20200518</version>
47+
</dependency>
48+
4249
<!-- Spring Boot Dependencies -->
4350

4451
<dependency>

src/main/java/blockmatrix/controllers/Blockmatrix_RestController.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ public class Blockmatrix_RestController {
5959
* === Blockchain Data (Explorer) ===
6060
* ==================================
6161
*/
62-
63-
6462

6563

6664
@RequestMapping(path = "/get_blockmatrix_blocks")
6765
public String getBlockMatrixBlocks() {
68-
6966
return new GsonBuilder().setPrettyPrinting().create().toJson(new_blockMatrix.getAllBlocks());
7067
}
7168

@@ -80,8 +77,14 @@ public String getBlockMatrixBlocks() {
8077
*/
8178

8279
@RequestMapping(path = "/get_block_data")
83-
public String getBlockData(@RequestParam(value = "num", required = true, defaultValue = "1") int blockNumber) {
84-
return new_blockMatrix.getBlockData(blockNumber);
80+
public String getBlockData(@RequestParam(value = "block_num", required = true, defaultValue = "1") int blockNumber) {
81+
return new GsonBuilder().setPrettyPrinting().create().toJson(new_blockMatrix.getBlockData(blockNumber));
82+
83+
}
84+
85+
@RequestMapping(path = "/get_transaction")
86+
public String getTransactionId(@RequestParam(value = "tx_id", required = true, defaultValue = "1") String txHashId) {
87+
return new GsonBuilder().setPrettyPrinting().create().toJson(new_blockMatrix.getTransaction(txHashId));
8588
}
8689

8790
/**
@@ -93,8 +96,8 @@ public String getBlockData(@RequestParam(value = "num", required = true, default
9396
*/
9497

9598
@RequestMapping(path = "/get_block_transactions")
96-
public String getBlockTransactions(@RequestParam(value = "num", required = true, defaultValue = "1") int blockNumber) {
97-
return new_blockMatrix.getBlockTransactions(blockNumber).toArray().toString();
99+
public String getBlockTransactions(@RequestParam(value = "block_num", required = true, defaultValue = "1") int blockNumber) {
100+
return new GsonBuilder().setPrettyPrinting().create().toJson(new_blockMatrix.getBlockTransactions(blockNumber));
98101
}
99102

100103
/**
@@ -128,9 +131,8 @@ public ResponseEntity<Object> getBlockMatrix() {
128131
*/
129132

130133
@RequestMapping(path = "/get_blockmatrix_transactions")
131-
public String getBlockMatrixTX() {
132-
133-
return new GsonBuilder().setPrettyPrinting().create().toJson(new_blockMatrix.getAllTransactions());
134+
public ArrayList<String> getBlockMatrixTX() {
135+
return new_blockMatrix.getAllStringTransactions();
134136
}
135137

136138
// ====================
@@ -158,7 +160,6 @@ public ResponseEntity<Object> accessWallet(@RequestParam(value = "pub_key", requ
158160
* _____
159161
* @return JSON (HashMap)
160162
*/
161-
162163
@RequestMapping(path = "/generate_new_wallet")
163164
public String generate_new_wallet() {
164165
Wallet new_wallet = new Wallet();
@@ -175,7 +176,6 @@ public String generate_new_wallet() {
175176
* @param msg
176177
*
177178
*/
178-
179179
@RequestMapping(path = "/send_funds")
180180
@ResponseStatus(HttpStatus.CREATED)
181181
public ResponseEntity<String> send_funds(@RequestParam(value = "funds", required = true) float value,

src/main/java/blockmatrix/controllers/Template_Controller.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public String wallet(Model model) {
8686
model.addAttribute("transactions", wallet_genesis.getUTXOs());
8787
model.addAttribute("pub_address", StringUtil.getStringFromKey(wallet_genesis.getPublicKey()));
8888
model.addAttribute("priv_address", StringUtil.getStringFromKey(wallet_genesis.getPrivateKey()));
89+
model.addAttribute("bm_verify", new_blockMatrix.is_Matrix_Valid().toString());
8990
model.addAttribute("wallet_txs", wallet_genesis.getTransactions());
9091
return "html/wallet_ui";
9192
}

src/main/java/blockmatrix/model/Block.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.ArrayList;
44
import java.util.Date;
55

6+
import com.google.gson.Gson;
7+
68
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
810
import org.springframework.stereotype.Component;
@@ -118,7 +120,15 @@ public void print_Block_Transactions() {
118120
System.out.println(t.toString());
119121
count++;
120122
}
121-
122123
}
123124

125+
public ArrayList<String> getStringTx() {
126+
ArrayList<String> list = new ArrayList<>();
127+
128+
for (Transaction tx : transactions) {
129+
list.add(tx.toString());
130+
}
131+
132+
return list;
133+
}
124134
}

src/main/java/blockmatrix/model/BlockMatrix.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ public ArrayList<Integer> getBlocksWithModifiedData() {
345345
public HashMap<String, Transaction_Output> getUTXOs() { return UTXOs; }
346346
public Set<URL> getList() {return Collections.unmodifiableSet(this.nodesList); }
347347

348+
public Transaction getTransaction(String tx_id) {
349+
350+
for(Transaction tx : getAllTransactions()) {
351+
if (tx.getTransactionId().equals(tx_id)) {
352+
return tx;
353+
}
354+
}
355+
return null;
356+
}
357+
348358
public ArrayList<Transaction> getAllTransactions() {
349359

350360
ArrayList<Transaction> list = new ArrayList<>();
@@ -353,10 +363,22 @@ public ArrayList<Transaction> getAllTransactions() {
353363
// Loop through each block in the network
354364
while(i-1 < inputCount) {
355365
list.addAll(getBlock(i).getTransactions());
356-
// System.out.println(list);
366+
System.out.println(list.toString());
357367
i++;
358368
}
369+
return list;
370+
}
359371

372+
public ArrayList<String> getAllStringTransactions() {
373+
ArrayList<String> list = new ArrayList<>();
374+
int i = 1;
375+
376+
// Loop through each block in the network
377+
while(i-1 < inputCount) {
378+
list.addAll(getBlock(i).getStringTx());
379+
System.out.println(list.toString());
380+
i++;
381+
}
360382
return list;
361383
}
362384

@@ -370,7 +392,6 @@ public ArrayList<Block> getAllBlocks() {
370392
block_list.add(getBlock(i));
371393
i++;
372394
}
373-
374395
return block_list;
375396
}
376397

src/main/java/blockmatrix/model/Transaction.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.ArrayList;
66
import java.util.Arrays;
77

8+
import org.json.JSONObject;
9+
810
import blockmatrix.helpers.StringUtil;
911

1012
/**
@@ -172,20 +174,15 @@ public void modifyTX(String new_info){
172174

173175
@Override
174176
public String toString() {
175-
StringBuilder sb = new StringBuilder();
176-
sb.append("\n Transaction Details");
177-
sb.append("\n transactionId: ");
178-
sb.append(transactionId);
179-
sb.append("\n sender: ");
180-
sb.append(sender);
181-
sb.append("\n recipient: ");
182-
sb.append(recipient);
183-
sb.append("\n value: ");
184-
sb.append(value);
185-
sb.append("\n info: ");
186-
sb.append(info);
187-
sb.append("\n signature: ");
188-
sb.append(Arrays.toString(signature));
189-
return sb.toString();
177+
String sb = new JSONObject()
178+
.put("transactionId", transactionId)
179+
.put("sender", StringUtil.getStringFromKey(sender))
180+
.put("recipient", StringUtil.getStringFromKey(recipient))
181+
.put("value", value)
182+
.put("info", info)
183+
.put("signature", Arrays.toString(signature))
184+
.toString();
185+
System.out.println(sb);
186+
return sb;
190187
}
191188
}

src/main/resources/static/js/main.js

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
var path_url = document.URL;
66

77
if (path_url.includes('/block_explorer')) {
8-
8+
99
// -------------------------
1010
// Page UI/UX Changes
1111
// -------------------------
1212

13-
document.getElementsByTagName('footer')[0].getElementsByTagName('a')[4].style.color = '#00A3FF'
13+
document.getElementsByTagName('footer')[0].getElementsByTagName('a')[1].style.color = '#00A3FF'
1414

1515
// -------------------------
1616
// Event Listeners
1717
// -------------------------
1818

1919
document.getElementById('simple_view_btn').addEventListener('click', switchLayout)
2020
document.getElementById('visual_view_btn').addEventListener('click', switchLayout)
21+
document.getElementById('search_matrix').addEventListener('click', searchData)
2122

2223
var btn_query_arr = document.getElementsByClassName('_btn_query')
2324

@@ -42,7 +43,15 @@ if (path_url.includes('/block_explorer')) {
4243
// -------------------------
4344

4445
document.getElementById('_btn_div_action_txt').innerHTML = `<p> hello </p>`
45-
document.getElementsByTagName('footer')[0].getElementsByTagName('a')[3].style.color = '#00A3FF'
46+
document.getElementsByTagName('footer')[0].getElementsByTagName('a')[0].style.color = '#00A3FF'
47+
48+
var rows_arr = document.getElementsByTagName('tr')
49+
50+
for (let i = 0; i < rows_arr.length; i++) {
51+
if (i > 2) {
52+
rows_arr[i].classList.toggle('display_row');
53+
}
54+
}
4655

4756
// -------------------------
4857
// Event Listeners
@@ -52,16 +61,17 @@ if (path_url.includes('/block_explorer')) {
5261
document.getElementById('new_wallet').addEventListener('click', createNewWallet)
5362
document.getElementById('access_wallet').addEventListener('click', submitTX)
5463
document.getElementById('sendTx').addEventListener('click', submitTX)
64+
document.getElementById('show_more_btn').addEventListener('click', showMoreTx)
5565

5666
document.getElementById('cpy_pub_add').addEventListener('click', function () {
5767
copyClipboard(this)
5868
})
59-
69+
6070
document.getElementById('select_tx_btn').addEventListener('click', function () {
6171
clearData(this)
6272
})
6373

64-
document.getElementById('user_action_btn').addEventListener('mouseout', function() {
74+
document.getElementById('user_action_btn').addEventListener('mouseout', function () {
6575
document.getElementById('_btn_div_action_txt').style.visibility = 'hidden'
6676
})
6777

@@ -78,9 +88,26 @@ if (path_url.includes('/block_explorer')) {
7888
// Simple UI/UX Functions Triggers
7989
// -------------------------
8090

91+
function showMoreTx() {
92+
var rows_arr = document.getElementsByTagName('tr')
93+
94+
for (let i = 0; i < rows_arr.length; i++) {
95+
if (i > 2) {
96+
rows_arr[i].classList.toggle('display_row');
97+
}
98+
}
99+
}
100+
81101
function changeInputTxt(x) {
82102
var btnSelected_txt = x.getAttribute('title')
83103
document.getElementById('input_select').placeholder = btnSelected_txt
104+
if (btnSelected_txt == 'enter block number') {
105+
document.getElementsByClassName('_btn_query')[0].classList.add('selected')
106+
document.getElementsByClassName('_btn_query')[1].classList.remove('selected')
107+
} else {
108+
document.getElementsByClassName('_btn_query')[0].classList.remove('selected')
109+
document.getElementsByClassName('_btn_query')[1].classList.add('selected')
110+
}
84111
}
85112

86113
function showBtnFunc(x) {
@@ -108,6 +135,36 @@ function switchLayout() {
108135
// AJAX/ASYNC Functions
109136
// -------------------------
110137

138+
async function searchData() {
139+
140+
// Validate Pressed Button for search
141+
var btn_select = document.getElementsByClassName('_btn_query')[0].classList.contains('selected')
142+
var input = document.getElementById('input_select').value
143+
144+
// True?
145+
if (btn_select) {
146+
147+
var url = new URL("/get_block_transactions", document.URL),
148+
params = {
149+
block_num: input,
150+
}
151+
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
152+
153+
window.open(url)
154+
} else {
155+
156+
var url = new URL("/get_transaction", document.URL),
157+
params = {
158+
tx_id: input,
159+
}
160+
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
161+
162+
window.open(url)
163+
}
164+
165+
166+
}
167+
111168
async function submitTX() {
112169

113170
// get the form input data:
@@ -218,7 +275,7 @@ async function getTxData() {
218275
fetch('/get_blockmatrix_transactions')
219276
.then((res) => res.text())
220277
.then((data) => {
221-
var result = JSON.parse(data)
278+
var result = JSON.parse(data).replace(/\\/g, "")
222279
document.getElementById('contents').innerHTML = ''
223280
for (var tx_data of result) {
224281
var row = document.getElementById('contents').insertRow(0);

0 commit comments

Comments
 (0)