Merge branch 'avishkarabhishek786-master'

Adding flosend.html
This commit is contained in:
Abhishek Sinha 2018-10-22 14:37:42 +05:30
commit e5e5989d6b
2 changed files with 3894 additions and 151 deletions

3565
supernode/flosend.html Normal file

File diff suppressed because it is too large Load Diff

View File

@ -165,6 +165,157 @@
})();
}
</script>
<!-- SHA1 -->
<script type="text/javascript">
//Adding SHA1 to fix basic PKBDF2
/*
* Crypto-JS v2.5.4
* http://code.google.com/p/crypto-js/
* (c) 2009-2012 by Jeff Mott. All rights reserved.
* http://code.google.com/p/crypto-js/wiki/License
*/
(function () {
// Shortcuts
var C = Crypto,
util = C.util,
charenc = C.charenc,
UTF8 = charenc.UTF8,
Binary = charenc.Binary;
// Public API
var SHA1 = C.SHA1 = function (message, options) {
var digestbytes = util.wordsToBytes(SHA1._sha1(message));
return options && options.asBytes ? digestbytes :
options && options.asString ? Binary.bytesToString(digestbytes) :
util.bytesToHex(digestbytes);
};
// The core
SHA1._sha1 = function (message) {
// Convert to byte array
if (message.constructor == String) message = UTF8.stringToBytes(message);
/* else, assume byte array already */
var m = util.bytesToWords(message),
l = message.length * 8,
w = [],
H0 = 1732584193,
H1 = -271733879,
H2 = -1732584194,
H3 = 271733878,
H4 = -1009589776;
// Padding
m[l >> 5] |= 0x80 << (24 - l % 32);
m[((l + 64 >>> 9) << 4) + 15] = l;
for (var i = 0; i < m.length; i += 16) {
var a = H0,
b = H1,
c = H2,
d = H3,
e = H4;
for (var j = 0; j < 80; j++) {
if (j < 16) w[j] = m[i + j];
else {
var n = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16];
w[j] = (n << 1) | (n >>> 31);
}
var t = ((H0 << 5) | (H0 >>> 27)) + H4 + (w[j] >>> 0) + (
j < 20 ? (H1 & H2 | ~H1 & H3) + 1518500249 :
j < 40 ? (H1 ^ H2 ^ H3) + 1859775393 :
j < 60 ? (H1 & H2 | H1 & H3 | H2 & H3) - 1894007588 :
(H1 ^ H2 ^ H3) - 899497514);
H4 = H3;
H3 = H2;
H2 = (H1 << 30) | (H1 >>> 2);
H1 = H0;
H0 = t;
}
H0 += a;
H1 += b;
H2 += c;
H3 += d;
H4 += e;
}
return [H0, H1, H2, H3, H4];
};
// Package private blocksize
SHA1._blocksize = 16;
SHA1._digestsize = 20;
})();
</script>
<!-- HMAC -->
<script type="text/javascript">
//Added to make PKBDF2 work
/*
* Crypto-JS v2.5.4
* http://code.google.com/p/crypto-js/
* (c) 2009-2012 by Jeff Mott. All rights reserved.
* http://code.google.com/p/crypto-js/wiki/License
*/
(function () {
// Shortcuts
var C = Crypto,
util = C.util,
charenc = C.charenc,
UTF8 = charenc.UTF8,
Binary = charenc.Binary;
C.HMAC = function (hasher, message, key, options) {
// Convert to byte arrays
if (message.constructor == String) message = UTF8.stringToBytes(message);
if (key.constructor == String) key = UTF8.stringToBytes(key);
/* else, assume byte arrays already */
// Allow arbitrary length keys
if (key.length > hasher._blocksize * 4)
key = hasher(key, {
asBytes: true
});
// XOR keys with pad constants
var okey = key.slice(0),
ikey = key.slice(0);
for (var i = 0; i < hasher._blocksize * 4; i++) {
okey[i] ^= 0x5C;
ikey[i] ^= 0x36;
}
var hmacbytes = hasher(okey.concat(hasher(ikey.concat(message), {
asBytes: true
})), {
asBytes: true
});
return options && options.asBytes ? hmacbytes :
options && options.asString ? Binary.bytesToString(hmacbytes) :
util.bytesToHex(hmacbytes);
};
})();
</script>
<script type="text/javascript">
/*!
* Crypto-JS v2.5.4 SHA256.js
@ -6872,15 +7023,17 @@
})();
</script>
<!-- Ranchi Mall localbitcoinplusplus Code starts -->
<script type="text/javascript">
var ninja = {
var localbitcoinplusplus = {
wallets: {},
trade: {},
keys_management: {},
rpc:{}
rpc: {},
rm_configs: {},
flocha: "https://livenet.flocha.in"
};
ninja.privateKey = {
localbitcoinplusplus.privateKey = {
isPrivateKey: function (key) {
return (
Bitcoin.ECKey.isWalletImportFormat(key) ||
@ -6924,18 +7077,18 @@
try {
hex = Bitcoin.Base58.decode(base58Encrypted);
} catch (e) {
callback(new Error(ninja.translator.get("detailalertnotvalidprivatekey")));
callback(new Error(localbitcoinplusplus.translator.get("detailalertnotvalidprivatekey")));
return;
}
// 43 bytes: 2 bytes prefix, 37 bytes payload, 4 bytes checksum
if (hex.length != 43) {
callback(new Error(ninja.translator.get("detailalertnotvalidprivatekey")));
callback(new Error(localbitcoinplusplus.translator.get("detailalertnotvalidprivatekey")));
return;
}
// first byte is always 0x01
else if (hex[0] != 0x01) {
callback(new Error(ninja.translator.get("detailalertnotvalidprivatekey")));
callback(new Error(localbitcoinplusplus.translator.get("detailalertnotvalidprivatekey")));
return;
}
@ -6945,7 +7098,7 @@
if (checksum[0] != expChecksum[0] || checksum[1] != expChecksum[1] || checksum[2] !=
expChecksum[2] ||
checksum[3] != expChecksum[3]) {
callback(new Error(ninja.translator.get("detailalertnotvalidprivatekey")));
callback(new Error(localbitcoinplusplus.translator.get("detailalertnotvalidprivatekey")));
return;
}
@ -6960,7 +7113,7 @@
}
// key should NOT use compression
else if (hex[2] != 0xc0) {
callback(new Error(ninja.translator.get("detailalertnotvalidprivatekey")));
callback(new Error(localbitcoinplusplus.translator.get("detailalertnotvalidprivatekey")));
return;
}
}
@ -6970,11 +7123,11 @@
isCompPoint = (hex[2] & 0x20) != 0;
hasLotSeq = (hex[2] & 0x04) != 0;
if ((hex[2] & 0x24) != hex[2]) {
callback(new Error(ninja.translator.get("detailalertnotvalidprivatekey")));
callback(new Error(localbitcoinplusplus.translator.get("detailalertnotvalidprivatekey")));
return;
}
} else {
callback(new Error(ninja.translator.get("detailalertnotvalidprivatekey")));
callback(new Error(localbitcoinplusplus.translator.get("detailalertnotvalidprivatekey")));
return;
}
@ -6992,7 +7145,8 @@
if (checksum[0] != hex[3] || checksum[1] != hex[4] || checksum[2] != hex[5] || checksum[
3] !=
hex[6]) {
callback(new Error(ninja.translator.get("bip38alertincorrectpassphrase"))); // callback using closure
callback(new Error(localbitcoinplusplus.translator.get(
"bip38alertincorrectpassphrase"))); // callback using closure
return;
}
callback(tmpkey.getBitcoinPrivateKeyByteArray()); // callback using closure
@ -7162,7 +7316,8 @@
// address using either compressed or uncompressed public key methodology (specify which methodology is used
// inside flagbyte). This is the generated Bitcoin address, call it generatedaddress.
var ec = EllipticCurve.getSECCurveByName("secp256k1").getCurve();
var generatedPoint = ec.decodePointHex(ninja.publicKey.getHexFromByteArray(passpoint));
var generatedPoint = ec.decodePointHex(localbitcoinplusplus.publicKey.getHexFromByteArray(
passpoint));
var generatedBytes = generatedPoint.multiply(BigInteger.fromByteArrayUnsigned(factorB)).getEncoded(
compressed);
var generatedAddress = (new Bitcoin.Address(Bitcoin.Util.sha256ripe160(generatedBytes))).toString();
@ -7202,11 +7357,12 @@
}
};
ninja.publicKey = {
localbitcoinplusplus.publicKey = {
isPublicKeyHexFormat: function (key) {
key = key.toString();
return ninja.publicKey.isUncompressedPublicKeyHexFormat(key) || ninja.publicKey.isCompressedPublicKeyHexFormat(
key);
return localbitcoinplusplus.publicKey.isUncompressedPublicKeyHexFormat(key) ||
localbitcoinplusplus.publicKey.isCompressedPublicKeyHexFormat(
key);
},
// 130 characters [0-9A-F] starts with 04
isUncompressedPublicKeyHexFormat: function (key) {
@ -7255,102 +7411,91 @@
var ecparams = EllipticCurve.getSECCurveByName("secp256k1");
var ecPoint = ecparams.getCurve().decodePointHex(pubKeyHexComp);
var pubByteArray = ecPoint.getEncoded(0);
var pubHexUncompressed = ninja.publicKey.getHexFromByteArray(pubByteArray);
var pubHexUncompressed = localbitcoinplusplus.publicKey.getHexFromByteArray(pubByteArray);
return pubHexUncompressed;
}
};
</script>
<!-- Keys Object Operations (Generate, Sign and Verify) -->
<script>
(function (wallets) {
var signing = wallets.flowallet = {
ecparams: EllipticCurve.getSECCurveByName("secp256k1"),
generateFloKeys: function () {
var privateKey = Bitcoin.ECDSA.getBigRandom(EllipticCurve.getSECCurveByName("secp256k1")
.getN());
console.log(privateKey);
var key = new Bitcoin.ECKey(privateKey);
key.setCompressed(true);
var privateKeyHex = key.getBitcoinHexFormat();
var publicKeyHex = ninja.publicKey.getHexFromByteArray(key.getPubPoint().getEncoded(1))
.toString().toUpperCase();
console.log("privateKeyHex: ", privateKeyHex);
console.log("publicKeyHex: ", publicKeyHex);
},
sign: function (msg, privateKeyHex) {
var key = new Bitcoin.ECKey(privateKeyHex);
key.setCompressed(true);
var privateKeyArr = key.getBitcoinPrivateKeyByteArray(privateKeyHex);
privateKey = BigInteger.fromByteArrayUnsigned(privateKeyArr);
console.log(privateKey);
var messageHash = Crypto.SHA256(msg);
var messageHashBigInteger = new BigInteger(messageHash);
var messageSign = Bitcoin.ECDSA.sign(messageHashBigInteger, privateKey);
var sighex = Crypto.util.bytesToHex(messageSign);
return sighex;
},
verify: function (msg, signatureHex, publicKeyHex) {
var msgHash = Crypto.SHA256(msg);
var messageHashBigInteger = new BigInteger(msgHash);
var sigBytes = Crypto.util.hexToBytes(signatureHex);
var signature = Bitcoin.ECDSA.parseSig(sigBytes);
var publicKeyPoint = this.ecparams.getCurve().decodePointHex(publicKeyHex);
var verify = Bitcoin.ECDSA.verifyRaw(messageHashBigInteger, signature.r, signature.s,
publicKeyPoint);
return verify;
}
}
})(ninja.wallets)
</script>
<script>
var Keys = ninja.keys_management = function manageKeys(keys) {
var wallets = localbitcoinplusplus.wallets = function (wallets) {
//3354E12F0D2737A16B30B46D66CEDB9C7C0CD35F22CFCCE9828318DDBB73C0F6 // private key
this.master_node = "038E48297353843614190D9C36D1F6565D68CB24C01A66DC7432BD91D979F7AC68"; // pub key
}
this.rm_flo_addr = "FSTDEb1ygMSwKB65uMYrrzjhdcCpXkt1uH";
};
wallets.prototype = {
ecparams: EllipticCurve.getSECCurveByName("secp256k1"),
Keys.prototype.foo = {
bar: "lorem ipsum",
nominateAsTrustedKey: function () {
console.log("ok");
generateFloKeys: function () {
var privateKey = Bitcoin.ECDSA.getBigRandom(EllipticCurve.getSECCurveByName("secp256k1")
.getN());
console.log(privateKey);
var key = new Bitcoin.ECKey(privateKey);
key.setCompressed(true);
var privateKeyHex = key.getBitcoinHexFormat();
var publicKeyHex = localbitcoinplusplus.publicKey.getHexFromByteArray(key.getPubPoint().getEncoded(
1))
.toString().toUpperCase();
console.log("privateKeyHex: ", privateKeyHex);
console.log("publicKeyHex: ", publicKeyHex);
},
sign: function (msg, privateKeyHex) {
var key = new Bitcoin.ECKey(privateKeyHex);
key.setCompressed(true);
var privateKeyArr = key.getBitcoinPrivateKeyByteArray(privateKeyHex);
privateKey = BigInteger.fromByteArrayUnsigned(privateKeyArr);
console.log(privateKey);
var messageHash = Crypto.SHA256(msg);
var messageHashBigInteger = new BigInteger(messageHash);
var messageSign = Bitcoin.ECDSA.sign(messageHashBigInteger, privateKey);
var sighex = Crypto.util.bytesToHex(messageSign);
return sighex;
},
verify: function (msg, signatureHex, publicKeyHex) {
var msgHash = Crypto.SHA256(msg);
var messageHashBigInteger = new BigInteger(msgHash);
var sigBytes = Crypto.util.hexToBytes(signatureHex);
var signature = Bitcoin.ECDSA.parseSig(sigBytes);
var publicKeyPoint = this.ecparams.getCurve().decodePointHex(publicKeyHex);
var verify = Bitcoin.ECDSA.verifyRaw(messageHashBigInteger, signature.r, signature.s,
publicKeyPoint);
return verify;
}
}
</script>
<!-- RPC Object -->
<script>
/*RPC Class*/
var Rpc = ninja.rpc = function() {
var Rpc = localbitcoinplusplus.rpc = function () {
this.rpc_req_id;
this.valid_job = ["trade_buy", "trade_sell"];
}
Rpc.prototype = {
//get rpc_req_id() {return this.rpc_req_id;},
send_rpc(method, ...params) {
var request = new JSON_RPC.Request(method, params);
console.log(request);
var id = request.id;
this.rpc_req_id = id;
return request.toString();
return request.toString();
//return request;
},
},
receive_rpc_response(request) {
var request = JSON.parse(request);
@ -7359,22 +7504,22 @@
if (typeof params != undefined && typeof method != undefined) {
request.response = {};
switch (method) {
case "trade_buy":
var Trade = new ninja.trade();
var Trade = new localbitcoinplusplus.trade();
request.response = Trade.trade_buy(...request.params);
break;
case "trade_sell":
var Trade = new ninja.trade();
var Trade = new localbitcoinplusplus.trade();
request.response = Trade.trade_sell(...request.params);
break;
break;
default:
alert("Unknown method called for execution.");
break;
}
}
//request.response = request.method(); // if successful
return request.toString(); // return to client
},
@ -7392,9 +7537,10 @@
}
</script>
<!-- Trade Object -->
<script>
/*Initiate trade level with 0*/
var Trade = ninja.trade = function processTrade() {
var Trade = localbitcoinplusplus.trade = function processTrade() {
//this.op_code = null;
this.errors = [];
this.level = 0; // default
@ -7404,75 +7550,88 @@
this.valid_product = ["BTC", "INR"];
this.currency = null;
this.valid_currencies = ["BTC", "INR"],
this.buy_price = null;
this.buy_price = null;
this.buyer_public_key = null;
this.buyer_key_signature = null;
this.order_validator_public_key = null;
this.rpc_job = null;
this.floAddress = null;
this.rpc_job = null;
this.floAddress = null;
}
Trade.prototype = {
get trustLevel() {return this.level;},
get trustLevel() {
return this.level;
},
set trustLevel(level) {
if (typeof level === "number" && level === parseInt(level, 10) && level > 0 && level < 6) {
this.level = level;
}
},
validate_order(order_type, product, currency, buy_price, buyer_public_key, buyer_key_signature, order_validator_public_key) {
if(this.valid_order_type.indexOf(order_type) >= 0) {
validate_order(order_type, product, currency, buy_price, buyer_public_key, buyer_key_signature,
order_validator_public_key) {
if (this.valid_order_type.indexOf(order_type) >= 0) {
this.order_type = order_type;
} else {
this.errors.push("Inavlid trade type value.");
}
if(this.valid_product.indexOf(product)>=0) {
if (this.valid_product.indexOf(product) >= 0) {
this.product = product;
} else {
this.errors.push("Invalid product.");
}
if(this.valid_currencies.indexOf(currency)>=0) {
}
if (this.valid_currencies.indexOf(currency) >= 0) {
this.currency = currency;
} else {
this.errors.push("Invalid currency.");
}
if(typeof buy_price == "number" && buy_price > 0) {
}
if (typeof buy_price == "number" && buy_price > 0) {
this.buy_price = buy_price;
} else {
this.errors.push("Invalid buying price. Please place a valid buy amount.");
}
if(buyer_public_key.length > 0) {
}
if (buyer_public_key.length > 0) {
this.buyer_public_key = buyer_public_key;
} else {
this.errors.push("Invalid Buyer's public key.");
}
if(buyer_key_signature.length > 0) {
if (buyer_key_signature.length > 0) {
this.buyer_key_signature = buyer_key_signature;
} else {
this.errors.push("Invalid Buyer's key signature.");
}
if(order_validator_public_key.length > 0) {
if (order_validator_public_key.length > 0) {
this.order_validator_public_key = order_validator_public_key;
} else {
this.errors.push("Invalid Validator's key signature.");
}
if(this.errors.length > 0) {
if (this.errors.length > 0) {
return this.errors;
}
return true;
},
place_order(order_type, product, currency, buy_price, buyer_public_key, buyer_key_signature, order_validator_public_key) {
var is_valid_order = this.validate_order(order_type, product, currency, buy_price, buyer_public_key, buyer_key_signature, order_validator_public_key);
if(is_valid_order === true) {
var orderRPC = new ninja.rpc();
this.rpc_job = 'trade_'+this.order_type;
return orderRPC.send_rpc(this.rpc_job, {"order_type":this.order_type, "product":this.product, "currency":this.currency, "buy_price":this.buy_price, "buyer_public_key":this.buyer_public_key, "buyer_key_signature":this.buyer_key_signature, "order_validator_public_key":this.order_validator_public_key});
} else if(is_valid_order == "object") {
place_order(order_type, product, currency, buy_price, buyer_public_key, buyer_key_signature,
order_validator_public_key) {
var is_valid_order = this.validate_order(order_type, product, currency, buy_price, buyer_public_key,
buyer_key_signature, order_validator_public_key);
if (is_valid_order === true) {
var orderRPC = new localbitcoinplusplus.rpc();
this.rpc_job = 'trade_' + this.order_type;
return orderRPC.send_rpc(this.rpc_job, {
"order_type": this.order_type,
"product": this.product,
"currency": this.currency,
"buy_price": this.buy_price,
"buyer_public_key": this.buyer_public_key,
"buyer_key_signature": this.buyer_key_signature,
"order_validator_public_key": this.order_validator_public_key
});
} else if (is_valid_order == "object") {
var err;
for(err=0; err<is_valid_order.length; err++) {
for (err = 0; err < is_valid_order.length; err++) {
alert(is_valid_order[err]);
}
}
return false;
}
},
@ -7484,11 +7643,13 @@
console.log("sell is called");
},
/*Parse Flo Blockchain comments*/
parse_flo_comments() {
if (this.floAddress == null) { return false; }
parse_flo_comments(callback) {
if (this.floAddress == null) {
return false;
}
var request = new XMLHttpRequest();
request.open('GET', `https://livenet.flocha.in/api/txs/?address=${this.floAddress}`, true);
request.open('GET', `${localbitcoinplusplus.flocha}/api/txs/?address=${this.floAddress}`, true);
request.onload = function () {
// Begin accessing JSON data here
@ -7497,7 +7658,8 @@
if (request.status >= 200 && request.status < 400) {
data.txs.forEach(tx => {
console.log(tx.floData);
});
callback(tx.floData);
});
} else {
console.log('error');
}
@ -7507,9 +7669,8 @@
}
</script>
<!-- JSON RPC Library Starts -->
<script language="javascript" type="text/javascript">
/* JSON RPC Library Starts */
var JSON_RPC = {};
var id = 0,
@ -7725,39 +7886,39 @@
function onMessage(evt) {
console.log(evt);
var response = evt.data;
var response = evt.data;
var res_pos = response.indexOf('{');
if (res_pos>=0) {
if (res_pos >= 0) {
var res = response.substr(res_pos);
try {
var res_obj = JSON.parse(res);
console.log(res_obj);
if (typeof res_obj.method !== undefined) {
var orderRPC = new ninja.rpc();
var orderRPC = new localbitcoinplusplus.rpc();
switch (res_obj.method) {
case "trade_buy":
var response_from_sever = orderRPC.receive_rpc_response(JSON.stringify(res_obj));
//orderRPC.parse_server_rpc_response(response_from_sever);
//doSend(response_from_sever); // send response to client
var response_from_sever = orderRPC.receive_rpc_response(JSON.stringify(res_obj));
//orderRPC.parse_server_rpc_response(response_from_sever);
//doSend(response_from_sever); // send response to client
break;
case "trade_sell":
var response_from_sever = orderRPC.receive_rpc_response(JSON.stringify(res_obj));
doSend(response_from_sever); // send response to client
var response_from_sever = orderRPC.receive_rpc_response(JSON.stringify(res_obj));
doSend(response_from_sever); // send response to client
break;
default:
break;
}
}
}
} catch (error) {
console.error(error);
return;
}
}
}
}
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
}
@ -7782,16 +7943,33 @@
/* Websocket Code Ends Here*/
</script>
<script>
var trade = document.createElement("button");
trade.innerText = "Trade";
trade.onclick = function() {
var trade = new ninja.trade;
var buytrade = trade.place_order("buy", "BTC", "INR", 10000.00, "buyer_public_key", "buyer_key_signature", "order_validator_public_key");
doSend(buytrade);
//doSend(JSON.stringify(buytrade));
}
document.getElementById("output").appendChild(trade);
<script>
(function () {
var RM_WALLET = new localbitcoinplusplus.wallets;
var RM_TRADE = new localbitcoinplusplus.trade;
var RM_RPC = new localbitcoinplusplus.rpc;
// Test: fetch flo comment
RM_TRADE.floAddress = RM_WALLET.rm_flo_addr;
var fetch_configs = RM_TRADE.parse_flo_comments(function (floData) {
console.log(floData);
});
// 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").appendChild(trade_btn);
})()
</script>
<script>
</script>
</body>