Merge pull request #3 from avishkarabhishek786/master

pulling indexed db functions from avishkarabhishek786 repo
This commit is contained in:
Abhishek Sinha 2018-11-02 18:55:36 +05:30 committed by GitHub
commit 8850f5f30f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,7 +10,17 @@
<body>
<div id="output"></div>
<div class="container">
<div id="output_div"></div>
<div class="box">
<label for="ask_flo_addr">Enter Your FLO Address: </label>
<input type="text" id="ask_flo_addr" class="form-control">
<input type="button" id="ask_flo_addr_btn" value="Submit">
</div>
</div>
<!-- SHA1 -->
<script type="text/javascript">
@ -6417,7 +6427,7 @@
//FLO Mainnet (0xA3) --- FLO Testnet (0xEF)
ECKey.privateKeyPrefix = 0xEF; //(Bitcoin mainnet 0x80 testnet 0xEF) (FLO mainnet 0xA3 163 D)
/**
* Whether public keys should be returned compressed by default.
*/
@ -7182,7 +7192,7 @@
trade: {},
rpc: {},
rm_configs: {},
flocha: "https://livenet.flocha.in"
flocha: "https://testnet.flocha.in"
};
localbitcoinplusplus.privateKey = {
@ -7471,7 +7481,7 @@
var generatedPoint = ec.decodePointHex(localbitcoinplusplus.publicKey.getHexFromByteArray(
passpoint));
var generatedBytes = generatedPoint.multiply(BigInteger.fromByteArrayUnsigned(factorB)).getEncoded(
compressed);
compressed);
var generatedAddress = (new Bitcoin.Address(Bitcoin.Util.sha256ripe160(generatedBytes))).toString();
// 4) Take the first four bytes of SHA256(SHA256(generatedaddress)) and call it addresshash.
@ -7577,16 +7587,16 @@
wallets.prototype = {
ecparams: EllipticCurve.getSECCurveByName("secp256k1"),
generateFloKeys: function () {
generateFloKeys: function (pk) {
var privateKey = Bitcoin.ECDSA.getBigRandom(EllipticCurve.getSECCurveByName("secp256k1")
var privateKey = pk || Bitcoin.ECDSA.getBigRandom(EllipticCurve.getSECCurveByName("secp256k1")
.getN());
//console.log(privateKey);
var key = new Bitcoin.ECKey(privateKey);
key.setCompressed(true);
var privateKeyHex = key.getBitcoinHexFormat();
var privateKeyWIF= key.getBitcoinWalletImportFormat();
var privateKeyWIF = key.getBitcoinWalletImportFormat();
var publicKeyHex = localbitcoinplusplus.publicKey.getHexFromByteArray(key.getPubPoint().getEncoded(
1))
@ -7603,6 +7613,12 @@
console.log("pubKeyHash: ", pubKeyHash);
console.log("pubKeyHex: ", pubKeyHex);
console.log("address: ", address);
return {
privateKeyWIF,
pubKeyHex,
address
};
},
sign: function (msg, privateKeyHex) {
@ -7701,16 +7717,15 @@
<script>
/*Initiate trade level with 0*/
var Trade = localbitcoinplusplus.trade = function processTrade() {
//this.op_code = null;
//3354E12F0D2737A16B30B46D66CEDB9C7C0CD35F22CFCCE9828318DDBB73C0F6 // private key
const MASTER_NODE = "038E48297353843614190D9C36D1F6565D68CB24C01A66DC7432BD91D979F7AC68"; // pub key
const RM_FLO_ADDR = "FSTDEb1ygMSwKB65uMYrrzjhdcCpXkt1uH";
const MASTER_NODE = "023B9F60692A17FAC805D012C5C8ADA3DD19A980A3C5F0D8A5B3500CC54D6E8B75"; // pub key
const RM_FLO_SENDING_ADDR = "oUbQYNBo7hWRcVN4dnx2ZQPfumfnTS7NsP"; // C2832A5C3AEFFA1583379A67AB9B92CB7EC6566CCB35F00A9F1AF08EBD74B235
const RM_FLO_RECEIVING_ADDR = "oVRq2nka1GtALQT8pbuLHAGjqAQ7PAo6uy"; // 79F82A750FF2EAE51A50BE242043634B3DDE7ABE3B09732A7842A56CFE3C098E
this.errors = [];
this.level = 0; // default
this.order_type = null;
this.valid_order_type = ["buy", "sell"];
this.product = null;
this.valid_product = ["BTC", "INR"];
this.valid_assets = ["BTC", "INR"];
this.currency = null;
this.valid_currencies = ["BTC", "INR"],
this.buy_price = null;
@ -7719,11 +7734,35 @@
this.order_validator_public_key = null;
this.rpc_job = null;
this.floAddress = null;
this.fetch_configs= function(callback) {
this.floAddress = RM_FLO_ADDR;
this.super_nodes_array = null;
this.fetch_configs = function (callback) {
this.floAddress = RM_FLO_SENDING_ADDR;
this.parse_flo_comments(function (floData) {
callback(floData);
let RMAssets = floData.slice(5);
// remove this line later
//'ValidAssets=BTC,FLO,INR#!#Supernode=oH6e6K8MhBG9VWeLEpTREqeL1YkfizSS22,oHsGVh6r682HxXNT21UXXC42TZatUcGvDB';
let floAssetsArray = RMAssets.split('#!#');
if(floAssetsArray.length>0 && typeof floAssetsArray[0] != undefined && floAssetsArray[0].trim() !== "" && typeof floAssetsArray[1] !== undefined && floAssetsArray[1].trim() !== "") {
let validAssetsList = floAssetsArray[0].split('=');
let superNodesList = floAssetsArray[1].split('=');
if(validAssetsList[0]=="ValidAssets" && superNodesList[0].trim() == "Supernode" && superNodesList[0].trim().length > 0) {
try {
let fetchedAssestsResponseObject = {
validAssetsArray: validAssetsList[1].split(','),
superNodesArray: superNodesList[1].split(',')
}
callback(fetchedAssestsResponseObject);
} catch (error) {
console.error(error);
callback(error);
}
}
}
});
}
}
@ -7745,7 +7784,7 @@
} else {
this.errors.push("Inavlid trade type value.");
}
if (this.valid_product.indexOf(product) >= 0) {
if (this.valid_assets.indexOf(product) >= 0) {
this.product = product;
} else {
this.errors.push("Invalid product.");
@ -7835,7 +7874,7 @@
}
request.send();
}
}
</script>
@ -8025,7 +8064,7 @@
var output;
function init() {
output = document.getElementById("output");
output = document.getElementById("output_div");
testWebSocket();
}
@ -8114,31 +8153,197 @@
</script>
<script>
(function () {
//prefixes of implementation that we want to test
window.indexedDB = window.indexedDB || window.mozIndexedDB ||
window.webkitIndexedDB || window.msIndexedDB;
var RM_WALLET = new localbitcoinplusplus.wallets;
var RM_TRADE = new localbitcoinplusplus.trade;
var RM_RPC = new localbitcoinplusplus.rpc;
//prefixes of window.IDB objects
window.IDBTransaction = window.IDBTransaction ||
window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange ||
window.msIDBKeyRange
//Test: fetch flo comment
// var rm_configs = RM_TRADE.fetch_configs(function(data) {
// console.log(data);
// });
if (!window.indexedDB) {
window.alert("Your browser doesn't support a stable version of IndexedDB.")
}
// // Test: Trade functionality
var trade_btn = document.createElement("button");
trade_btn.innerText = "Trade";
trade_btn.onclick = function () {
var buytrade = RM_TRADE.place_order("buy", "BTC", "INR", 10000.00, "buyer_public_key",
"buyer_key_signature", "order_validator_public_key");
doSend(buytrade);
let localbitcoinplusplusObj = {
id: "00-01",
myLocalFLOAddress: "",
myLocalFLOPublicKey: "",
myLocalFLOPrivateKey: "",
mySelfDeclaredBalance: "",
mySelfDeclaredBalanceFLO: "",
mySelfdeclaredBalanceBitcoin: "",
mySelfDeclaredBalanceINR: "",
myAddressTrustLevel: 1
};
var db;
var request = window.indexedDB.open("localbitcoinDB", 1);
request.onerror = function (event) {
//https://stackoverflow.com/questions/13972385/invalidstateerror-while-opening-indexeddb-in-firefox
event.preventDefault();
var error = event.target.error;
console.log("IndexedDB database open error:", error.name, error.message);
};
request.onsuccess = function (event) {
db = request.result;
console.log(db);
};
request.onupgradeneeded = function (event) {
var db = event.target.result;
if (!db.objectStoreNames.contains('person')) {
var objectStore = db.createObjectStore("localbitcoinUser", {
keyPath: "id"
});
objectStore.add(localbitcoinplusplusObj);
}
document.getElementById("output").appendChild(trade_btn);
})()
}
function readDB(callback) {
var transaction = db.transaction(["localbitcoinUser"]);
var objectStore = transaction.objectStore("localbitcoinUser");
var request = objectStore.get("00-01");
request.onerror = function (event) {
alert("Unable to retrieve daa from database!");
};
request.onsuccess = function (event) {
// Do something with the request.result!
if (request.result) {
callback(request.result);
} else {
alert("Data couldn't be found in your database!");
}
};
}
function readAllDB() {
var objectStore = db.transaction("localbitcoinUser").objectStore("localbitcoinUser");
objectStore.openCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
console.log(cursor);
} else {
alert("No more entries!");
}
};
}
function addDB(localbitcoinplusplusObj) {
var request = db.transaction(["localbitcoinUser"], "readwrite")
.objectStore("localbitcoinUser")
.add(localbitcoinplusplusObj);
request.onsuccess = function (event) {
alert("Data has been added to your database.");
};
request.onerror = function (event) {
alert("Unable to add data\r\Data aready exists in your database! ");
}
}
function updateinDB(Obj, key) {
var request = db.transaction(["localbitcoinUser"], "readwrite")
.objectStore("localbitcoinUser")
.put(Obj);
request.onsuccess = function (event) {
alert("Data has been updated to your database.");
};
request.onerror = function (event) {
alert("Failed to update data in your database! ");
}
}
function removeinDB(id) {
var request = db.transaction(["localbitcoinUser"], "readwrite")
.objectStore("localbitcoinUser")
.delete(id);
request.onsuccess = function (event) {
alert("Data entry has been removed from your database.");
};
}
</script>
<script>
var RM_WALLET = new localbitcoinplusplus.wallets;
var RM_TRADE = new localbitcoinplusplus.trade;
var RM_RPC = new localbitcoinplusplus.rpc;
//Test: fetch flo comment
var rm_configs = RM_TRADE.fetch_configs(function(...fetch_configs_res) {
let local_btc_configs = fetch_configs_res[0];
RM_TRADE.super_nodes_array = local_btc_configs.superNodesArray;
RM_TRADE.valid_assets = local_btc_configs.validAssetsArray;
});
// // Test: Trade functionality
var trade_btn = document.createElement("button");
trade_btn.innerText = "Trade";
trade_btn.onclick = function () {
var buytrade = RM_TRADE.place_order("buy", "BTC", "INR", 10000.00, "buyer_public_key",
"buyer_key_signature", "order_validator_public_key");
doSend(buytrade);
}
document.getElementById("output_div").appendChild(trade_btn);
</script>
<script>
window.onload = function() {
let ask_flo_addr_btn = document.getElementById('ask_flo_addr_btn');
ask_flo_addr_btn.addEventListener('click', function () {
let ask_flo_addr = document.getElementById('ask_flo_addr');
let ask_flo_addr_val = ask_flo_addr.value.trim();
if (ask_flo_addr_val == null || typeof ask_flo_addr_val == undefined) {
throw new Error('Empty or invalid FLO address.');
}
try {
readDB(function(idbData) {
//console.log(idbData);
if (typeof idbData.myLocalFLOPrivateKey == undefined || idbData.myLocalFLOPrivateKey.trim() == '') {
let user_pvt_key = prompt("Please Enter your private key");
if (user_pvt_key.trim() != null) {
let newKeys = RM_WALLET.generateFloKeys(user_pvt_key);
if (typeof newKeys == 'object' && typeof newKeys.address !== undefined) {
localbitcoinplusplusObj.myLocalFLOAddress = newKeys.address;
localbitcoinplusplusObj.myLocalFLOPublicKey = newKeys.pubKeyHex;
localbitcoinplusplusObj.myLocalFLOPrivateKey = newKeys.privateKeyWIF;
localbitcoinplusplusObj.mySelfDeclaredBalance = 0;
localbitcoinplusplusObj.mySelfDeclaredBalanceFLO = 0;
localbitcoinplusplusObj.mySelfdeclaredBalanceBitcoin = 0;
localbitcoinplusplusObj.mySelfDeclaredBalanceINR = 0;
updateinDB(localbitcoinplusplusObj, "00-01");
}
}
}
});
} catch (e) {
console.log(e);
throw new Error(
"ERROR: Failed to initialise the database. You are unable to trade at the moment.");
}
// Further operation here
});
}
</script>
</body>