added new db datablocks and its update feature
This commit is contained in:
parent
a0864b2409
commit
498025f079
@ -14,12 +14,17 @@
|
|||||||
|
|
||||||
<div id="output_div"></div>
|
<div id="output_div"></div>
|
||||||
|
|
||||||
|
<h5>Indexed DB</h5>
|
||||||
<div class="box">
|
<div class="box">
|
||||||
<label for="ask_flo_addr">Enter Your FLO Address: </label>
|
<label for="ask_flo_addr">Enter Your FLO Address: </label>
|
||||||
<input type="text" id="ask_flo_addr" class="form-control">
|
<input type="text" id="ask_flo_addr" class="form-control">
|
||||||
|
|
||||||
<input type="button" id="ask_flo_addr_btn" value="Submit">
|
<input type="button" id="ask_flo_addr_btn" value="Submit">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<h5>Sync</h5>
|
||||||
|
<div id="syncNodes"></div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- SHA1 -->
|
<!-- SHA1 -->
|
||||||
@ -7724,7 +7729,6 @@
|
|||||||
|
|
||||||
var privateKeyArr = key.getBitcoinPrivateKeyByteArray(privateKeyHex);
|
var privateKeyArr = key.getBitcoinPrivateKeyByteArray(privateKeyHex);
|
||||||
privateKey = BigInteger.fromByteArrayUnsigned(privateKeyArr);
|
privateKey = BigInteger.fromByteArrayUnsigned(privateKeyArr);
|
||||||
console.log(privateKey);
|
|
||||||
|
|
||||||
var messageHash = Crypto.SHA256(msg);
|
var messageHash = Crypto.SHA256(msg);
|
||||||
|
|
||||||
@ -7755,17 +7759,13 @@
|
|||||||
<script>
|
<script>
|
||||||
var Rpc = localbitcoinplusplus.rpc = function () {
|
var Rpc = localbitcoinplusplus.rpc = function () {
|
||||||
this.rpc_req_id;
|
this.rpc_req_id;
|
||||||
this.valid_job = ["trade_buy", "trade_sell"];
|
this.valid_job = ["trade_buy", "trade_sell", "sync"];
|
||||||
}
|
}
|
||||||
Rpc.prototype = {
|
Rpc.prototype = {
|
||||||
|
|
||||||
send_rpc(method, ...params) {
|
send_rpc(method, ...params) {
|
||||||
var request = new JSON_RPC.Request(method, params);
|
var request = new JSON_RPC.Request(method, params);
|
||||||
console.log(request);
|
|
||||||
|
|
||||||
var id = request.id;
|
var id = request.id;
|
||||||
console.log(id);
|
|
||||||
|
|
||||||
this.rpc_req_id = id;
|
this.rpc_req_id = id;
|
||||||
return request.toString();
|
return request.toString();
|
||||||
},
|
},
|
||||||
@ -7777,16 +7777,17 @@
|
|||||||
|
|
||||||
if (typeof params != undefined && typeof method != undefined) {
|
if (typeof params != undefined && typeof method != undefined) {
|
||||||
request.response = {};
|
request.response = {};
|
||||||
|
var Trade = new localbitcoinplusplus.trade();
|
||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "trade_buy":
|
case "trade_buy":
|
||||||
var Trade = new localbitcoinplusplus.trade();
|
|
||||||
request.response = Trade.trade_buy(...request.params);
|
request.response = Trade.trade_buy(...request.params);
|
||||||
break;
|
break;
|
||||||
case "trade_sell":
|
case "trade_sell":
|
||||||
var Trade = new localbitcoinplusplus.trade();
|
|
||||||
request.response = Trade.trade_sell(...request.params);
|
request.response = Trade.trade_sell(...request.params);
|
||||||
break;
|
break;
|
||||||
|
case "sync":
|
||||||
|
|
||||||
default:
|
default:
|
||||||
alert("Unknown method called for execution.");
|
alert("Unknown method called for execution.");
|
||||||
break;
|
break;
|
||||||
@ -8180,6 +8181,14 @@
|
|||||||
websocket.send(message);
|
websocket.send(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function SendToSpecificAddress(to, message) {
|
||||||
|
writeToScreen("SENT: " + message);
|
||||||
|
websocket.send(JSON.stringify({
|
||||||
|
to: to,
|
||||||
|
data: message
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
function writeToScreen(message) {
|
function writeToScreen(message) {
|
||||||
var pre = document.createElement("p");
|
var pre = document.createElement("p");
|
||||||
pre.style.wordWrap = "break-word";
|
pre.style.wordWrap = "break-word";
|
||||||
@ -8220,8 +8229,17 @@
|
|||||||
myAddressTrustLevel: 1
|
myAddressTrustLevel: 1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let dataBlock = {
|
||||||
|
version: 1,
|
||||||
|
data: {},
|
||||||
|
blockOwnerFLOAddress: "",
|
||||||
|
blockhash: "",
|
||||||
|
blockSignature: ""
|
||||||
|
}
|
||||||
|
|
||||||
var db;
|
var db;
|
||||||
var request = window.indexedDB.open("localbitcoinDB", 1);
|
const DBName = "localbitcoinDB";
|
||||||
|
var request = window.indexedDB.open(DBName, 3);
|
||||||
|
|
||||||
request.onerror = function (event) {
|
request.onerror = function (event) {
|
||||||
//https://stackoverflow.com/questions/13972385/invalidstateerror-while-opening-indexeddb-in-firefox
|
//https://stackoverflow.com/questions/13972385/invalidstateerror-while-opening-indexeddb-in-firefox
|
||||||
@ -8237,18 +8255,25 @@
|
|||||||
|
|
||||||
request.onupgradeneeded = function (event) {
|
request.onupgradeneeded = function (event) {
|
||||||
var db = event.target.result;
|
var db = event.target.result;
|
||||||
if (!db.objectStoreNames.contains('person')) {
|
if (!db.objectStoreNames.contains('localbitcoinUser')) {
|
||||||
var objectStore = db.createObjectStore("localbitcoinUser", {
|
var objectStore = db.createObjectStore("localbitcoinUser", {
|
||||||
keyPath: "id"
|
keyPath: "id"
|
||||||
});
|
});
|
||||||
objectStore.add(localbitcoinplusplusObj);
|
objectStore.put(localbitcoinplusplusObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!db.objectStoreNames.contains('datablocks')) {
|
||||||
|
var objectStore = db.createObjectStore("datablocks", {
|
||||||
|
keyPath: "version"
|
||||||
|
});
|
||||||
|
objectStore.put(dataBlock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function readDB(callback) {
|
function readDB(tablename, id, callback) {
|
||||||
var transaction = db.transaction(["localbitcoinUser"]);
|
var transaction = db.transaction([tablename]);
|
||||||
var objectStore = transaction.objectStore("localbitcoinUser");
|
var objectStore = transaction.objectStore(tablename);
|
||||||
var request = objectStore.get("00-01");
|
var request = objectStore.get(id);
|
||||||
|
|
||||||
request.onerror = function (event) {
|
request.onerror = function (event) {
|
||||||
alert("Unable to retrieve daa from database!");
|
alert("Unable to retrieve daa from database!");
|
||||||
@ -8264,8 +8289,8 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function readAllDB() {
|
function readAllDB(tablename) {
|
||||||
var objectStore = db.transaction("localbitcoinUser").objectStore("localbitcoinUser");
|
var objectStore = db.transaction(tablename).objectStore(tablename);
|
||||||
|
|
||||||
objectStore.openCursor().onsuccess = function (event) {
|
objectStore.openCursor().onsuccess = function (event) {
|
||||||
var cursor = event.target.result;
|
var cursor = event.target.result;
|
||||||
@ -8278,10 +8303,10 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function addDB(localbitcoinplusplusObj) {
|
function addDB(tablename, dbObject) {
|
||||||
var request = db.transaction(["localbitcoinUser"], "readwrite")
|
var request = db.transaction([tablename], "readwrite")
|
||||||
.objectStore("localbitcoinUser")
|
.objectStore(tablename)
|
||||||
.add(localbitcoinplusplusObj);
|
.add(dbObject);
|
||||||
|
|
||||||
request.onsuccess = function (event) {
|
request.onsuccess = function (event) {
|
||||||
alert("Data has been added to your database.");
|
alert("Data has been added to your database.");
|
||||||
@ -8292,10 +8317,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateinDB(Obj, key) {
|
function updateinDB(tablename, Obj, key) {
|
||||||
|
|
||||||
var request = db.transaction(["localbitcoinUser"], "readwrite")
|
var request = db.transaction([tablename], "readwrite")
|
||||||
.objectStore("localbitcoinUser")
|
.objectStore(tablename)
|
||||||
.put(Obj);
|
.put(Obj);
|
||||||
|
|
||||||
request.onsuccess = function (event) {
|
request.onsuccess = function (event) {
|
||||||
@ -8307,9 +8332,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeinDB(id) {
|
function removeinDB(tablename, id) {
|
||||||
var request = db.transaction(["localbitcoinUser"], "readwrite")
|
var request = db.transaction([tablename], "readwrite")
|
||||||
.objectStore("localbitcoinUser")
|
.objectStore(tablename)
|
||||||
.delete(id);
|
.delete(id);
|
||||||
|
|
||||||
request.onsuccess = function (event) {
|
request.onsuccess = function (event) {
|
||||||
@ -8324,11 +8349,15 @@
|
|||||||
var RM_RPC = new localbitcoinplusplus.rpc;
|
var RM_RPC = new localbitcoinplusplus.rpc;
|
||||||
|
|
||||||
//Test: fetch flo comment
|
//Test: fetch flo comment
|
||||||
var rm_configs = localbitcoinplusplus.actions.fetch_configs(function (...fetch_configs_res) {
|
try {
|
||||||
let local_btc_configs = fetch_configs_res[0];
|
var rm_configs = localbitcoinplusplus.actions.fetch_configs(function (...fetch_configs_res) {
|
||||||
RM_TRADE.super_nodes_array = local_btc_configs.superNodesArray;
|
let local_btc_configs = fetch_configs_res[0];
|
||||||
RM_TRADE.valid_assets = local_btc_configs.validAssetsArray;
|
RM_TRADE.super_nodes_array = local_btc_configs.superNodesArray;
|
||||||
});
|
RM_TRADE.valid_assets = local_btc_configs.validAssetsArray;
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to fetch configurations: ${error}`);
|
||||||
|
}
|
||||||
|
|
||||||
//Test: Trade functionality
|
//Test: Trade functionality
|
||||||
var trade_btn = document.createElement("button");
|
var trade_btn = document.createElement("button");
|
||||||
@ -8343,10 +8372,6 @@
|
|||||||
|
|
||||||
<!-- Database operations -->
|
<!-- Database operations -->
|
||||||
<script>
|
<script>
|
||||||
// Nodes Database
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// localbitcoinUser Databse
|
// localbitcoinUser Databse
|
||||||
window.onload = function () {
|
window.onload = function () {
|
||||||
let ask_flo_addr_btn = document.getElementById('ask_flo_addr_btn');
|
let ask_flo_addr_btn = document.getElementById('ask_flo_addr_btn');
|
||||||
@ -8359,8 +8384,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
readDB(function (idbData) {
|
readDB("localbitcoinUser", "00-01", function (idbData) {
|
||||||
//console.log(idbData);
|
console.log(idbData);
|
||||||
if (typeof idbData.myLocalFLOPrivateKey == undefined || idbData.myLocalFLOPrivateKey
|
if (typeof idbData.myLocalFLOPrivateKey == undefined || idbData.myLocalFLOPrivateKey
|
||||||
.trim() == '') {
|
.trim() == '') {
|
||||||
let user_pvt_key = prompt("Please Enter your private key");
|
let user_pvt_key = prompt("Please Enter your private key");
|
||||||
@ -8378,7 +8403,7 @@
|
|||||||
localbitcoinplusplusObj.mySelfdeclaredBalanceBitcoin = 0;
|
localbitcoinplusplusObj.mySelfdeclaredBalanceBitcoin = 0;
|
||||||
localbitcoinplusplusObj.mySelfDeclaredBalanceINR = 0;
|
localbitcoinplusplusObj.mySelfDeclaredBalanceINR = 0;
|
||||||
|
|
||||||
updateinDB(localbitcoinplusplusObj, "00-01");
|
updateinDB("localbitcoinUser", localbitcoinplusplusObj, "00-01");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -8386,10 +8411,19 @@
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"ERROR: Failed to initialise the database. You are unable to trade at the moment."
|
"ERROR: Failed to initialise the localbitcoinUser database. You are unable to trade at the moment."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// datablocks database
|
||||||
|
try {
|
||||||
|
readDB("datablocks", "", function(blockData) {
|
||||||
|
console.log(blockData);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error("ERROR: Failed to initialise the datablocks database. You are unable to trade at the moment.");
|
||||||
|
}
|
||||||
|
|
||||||
// Further operation here
|
// Further operation here
|
||||||
console.log("Hello! You are doing great.");
|
console.log("Hello! You are doing great.");
|
||||||
|
|
||||||
@ -8397,6 +8431,53 @@
|
|||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Sync Nodes Database -->
|
||||||
|
<script>
|
||||||
|
window.onload = function() {
|
||||||
|
let syncButton = document.createElement('button');
|
||||||
|
let syncButtonText = document.createTextNode('Sync');
|
||||||
|
syncButton.appendChild(syncButtonText);
|
||||||
|
let syncNodes = document.getElementById("syncNodes");
|
||||||
|
syncNodes.appendChild(syncButton);
|
||||||
|
|
||||||
|
let blockVersion = Math.floor(Math.random(1,100)*100);
|
||||||
|
let blockOwnerFLOAddress = "olVxhjhaIedlpafshT";
|
||||||
|
let data = {
|
||||||
|
sample: "lorem ipsum..."
|
||||||
|
};
|
||||||
|
if (Object.keys(data).length === 0 && data.constructor === Object) {
|
||||||
|
throw new Error("Data cannot be empty.");
|
||||||
|
}
|
||||||
|
let datastr = JSON.stringify(data);
|
||||||
|
let blockHashMsg = `${blockVersion}${blockOwnerFLOAddress}${datastr}`;
|
||||||
|
let blockHash = Crypto.SHA256(blockHashMsg);
|
||||||
|
let blockSignature = RM_WALLET.sign(blockHash);
|
||||||
|
|
||||||
|
let updatedDataBlock = {
|
||||||
|
version: blockVersion,
|
||||||
|
data: data,
|
||||||
|
blockOwnerFLOAddress: blockOwnerFLOAddress,
|
||||||
|
blockhash: blockHash,
|
||||||
|
blockSignature: blockSignature
|
||||||
|
}
|
||||||
|
|
||||||
|
syncButton.onclick = function() {
|
||||||
|
try {
|
||||||
|
updateinDB("datablocks", updatedDataBlock, "");
|
||||||
|
try {
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error("ERROR: Failed to broadcast to other nodes.");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error("ERROR: Failed to update the datablocks database. You are unable to trade at the moment.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
Loading…
Reference in New Issue
Block a user