updates and bug fixes
This commit is contained in:
parent
be8329e876
commit
ef327a3222
381
floBank.html
381
floBank.html
@ -2,7 +2,7 @@
|
|||||||
<html>
|
<html>
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<title>FLO Standard Operators</title>
|
<title>FLO Bank</title>
|
||||||
|
|
||||||
<script id="floGlobals">
|
<script id="floGlobals">
|
||||||
/* Constants for FLO blockchain operations !!Make sure to add this at begining!! */
|
/* Constants for FLO blockchain operations !!Make sure to add this at begining!! */
|
||||||
@ -13,9 +13,7 @@
|
|||||||
|
|
||||||
//Required for blockchain API operators
|
//Required for blockchain API operators
|
||||||
apiURL: {
|
apiURL: {
|
||||||
FLO: ['https://explorer.mediciland.com/', 'https://livenet.flocha.in/', 'https://flosight.duckdns.org/',
|
FLO: [ 'https://livenet.flocha.in/'],
|
||||||
'http://livenet-explorer.floexperiments.com/'
|
|
||||||
],
|
|
||||||
FLO_TEST: ['https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
|
FLO_TEST: ['https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
|
||||||
},
|
},
|
||||||
tokenURL: "https://ranchimallflo.duckdns.org/",
|
tokenURL: "https://ranchimallflo.duckdns.org/",
|
||||||
@ -30,7 +28,7 @@
|
|||||||
|
|
||||||
//for cloud apps
|
//for cloud apps
|
||||||
subAdmins: [],
|
subAdmins: [],
|
||||||
application: "TEST_MODE",
|
application: "bank",
|
||||||
appObjects: {},
|
appObjects: {},
|
||||||
generalData: {},
|
generalData: {},
|
||||||
lastVC: {}
|
lastVC: {}
|
||||||
@ -43,6 +41,12 @@
|
|||||||
//floDapps.setAppObjectStores({sampleObs1:{}, sampleObs2:{options{autoIncrement:true, keyPath:'SampleKey'}, Indexes:{sampleIndex:{}}}})
|
//floDapps.setAppObjectStores({sampleObs1:{}, sampleObs2:{options{autoIncrement:true, keyPath:'SampleKey'}, Indexes:{sampleIndex:{}}}})
|
||||||
//floDapps.setCustomPrivKeyInput( () => { FUNCTION BODY *must resolve private key* } )
|
//floDapps.setCustomPrivKeyInput( () => { FUNCTION BODY *must resolve private key* } )
|
||||||
|
|
||||||
|
floDapps.setAppObjectStores({
|
||||||
|
accounts: {},
|
||||||
|
responses: {},
|
||||||
|
requests: {},
|
||||||
|
appendix: {}
|
||||||
|
})
|
||||||
floDapps.launchStartUp().then(result => {
|
floDapps.launchStartUp().then(result => {
|
||||||
console.log(result)
|
console.log(result)
|
||||||
alert(`Welcome FLO_ID: ${myFloID}`)
|
alert(`Welcome FLO_ID: ${myFloID}`)
|
||||||
@ -6961,7 +6965,7 @@ Bitcoin.Util = {
|
|||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script id="floCrypto" version="2.0.0">
|
<script id="floCrypto" version="2.0.1">
|
||||||
/* FLO Crypto Operators*/
|
/* FLO Crypto Operators*/
|
||||||
const floCrypto = {
|
const floCrypto = {
|
||||||
|
|
||||||
@ -6970,6 +6974,8 @@ Bitcoin.Util = {
|
|||||||
|
|
||||||
ecparams: EllipticCurve.getSECCurveByName("secp256k1"),
|
ecparams: EllipticCurve.getSECCurveByName("secp256k1"),
|
||||||
|
|
||||||
|
asciiAlternatives: `‘ '\n’ '\n“ "\n” "\n– --\n— ---\n≥ >=\n≤ <=\n≠ !=\n× *\n÷ /\n← <-\n→ ->\n↔ <->\n⇒ =>\n⇐ <=\n⇔ <=>`,
|
||||||
|
|
||||||
exponent1: function() {
|
exponent1: function() {
|
||||||
return this.p.add(BigInteger.ONE).divide(BigInteger("4"))
|
return this.p.add(BigInteger.ONE).divide(BigInteger("4"))
|
||||||
},
|
},
|
||||||
@ -6995,9 +7001,8 @@ Bitcoin.Util = {
|
|||||||
// verify y value
|
// verify y value
|
||||||
let resultBigInt = y.mod(BigInteger("2"));
|
let resultBigInt = y.mod(BigInteger("2"));
|
||||||
let check = resultBigInt.toString() % 2;
|
let check = resultBigInt.toString() % 2;
|
||||||
if (prefix_modulus !== check) {
|
if (prefix_modulus !== check)
|
||||||
yDecimalValue = y.negate().mod(p).toString();
|
yDecimalValue = y.negate().mod(p).toString();
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
x: xDecimalValue,
|
x: xDecimalValue,
|
||||||
y: yDecimalValue
|
y: yDecimalValue
|
||||||
@ -7014,20 +7019,15 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
deriveSharedKeySender: function(receiverCompressedPublicKey, senderPrivateKey) {
|
deriveSharedKeySender: function(receiverCompressedPublicKey, senderPrivateKey) {
|
||||||
try {
|
let receiverPublicKeyString = this.getUncompressedPublicKey(receiverCompressedPublicKey);
|
||||||
let receiverPublicKeyString = this.getUncompressedPublicKey(receiverCompressedPublicKey);
|
var senderDerivedKey = ellipticCurveEncryption.senderSharedKeyDerivation(
|
||||||
var senderDerivedKey = ellipticCurveEncryption.senderSharedKeyDerivation(
|
receiverPublicKeyString.x, receiverPublicKeyString.y, senderPrivateKey);
|
||||||
receiverPublicKeyString.x, receiverPublicKeyString.y, senderPrivateKey);
|
return senderDerivedKey;
|
||||||
return senderDerivedKey;
|
|
||||||
} catch (error) {
|
|
||||||
return new Error(error);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
deriveReceiverSharedKey: function(senderPublicKeyString, receiverPrivateKey) {
|
deriveReceiverSharedKey: function(senderPublicKeyString, receiverPrivateKey) {
|
||||||
return ellipticCurveEncryption.receiverSharedKeyDerivation(
|
return ellipticCurveEncryption.receiverSharedKeyDerivation(
|
||||||
senderPublicKeyString.XValuePublicString, senderPublicKeyString.YValuePublicString,
|
senderPublicKeyString.XValuePublicString, senderPublicKeyString.YValuePublicString, receiverPrivateKey);
|
||||||
receiverPrivateKey);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getReceiverPublicKeyString: function(privateKey) {
|
getReceiverPublicKeyString: function(privateKey) {
|
||||||
@ -7035,14 +7035,8 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
deriveSharedKeyReceiver: function(senderPublicKeyString, receiverPrivateKey) {
|
deriveSharedKeyReceiver: function(senderPublicKeyString, receiverPrivateKey) {
|
||||||
try {
|
return ellipticCurveEncryption.receiverSharedKeyDerivation(
|
||||||
return ellipticCurveEncryption.receiverSharedKeyDerivation(senderPublicKeyString
|
senderPublicKeyString.XValuePublicString, senderPublicKeyString.YValuePublicString, receiverPrivateKey);
|
||||||
.XValuePublicString,
|
|
||||||
senderPublicKeyString.YValuePublicString, receiverPrivateKey);
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
return new Error(error);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
wifToDecimal: function(pk_wif, isPubKeyCompressed = false) {
|
wifToDecimal: function(pk_wif, isPubKeyCompressed = false) {
|
||||||
@ -7247,10 +7241,68 @@ Bitcoin.Util = {
|
|||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
validateASCII: function(string, bool = true) {
|
||||||
|
if (typeof string !== "string")
|
||||||
|
return null;
|
||||||
|
if (bool) {
|
||||||
|
let x;
|
||||||
|
for (let i = 0; i < string.length; i++) {
|
||||||
|
x = string.charCodeAt(i);
|
||||||
|
if (x < 32 || x > 127)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
let x, invalids = {};
|
||||||
|
for (let i = 0; i < string.length; i++) {
|
||||||
|
x = string.charCodeAt(i);
|
||||||
|
if (x < 32 || x > 127)
|
||||||
|
if (x in invalids)
|
||||||
|
invalids[string[i]].push(i)
|
||||||
|
else
|
||||||
|
invalids[string[i]] = [i];
|
||||||
|
}
|
||||||
|
if (Object.keys(invalids).length)
|
||||||
|
return invalids;
|
||||||
|
else
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
convertToASCII: function(string, mode = 'soft-remove') {
|
||||||
|
let chars = this.validateASCII(string, false);
|
||||||
|
if (chars === true)
|
||||||
|
return string;
|
||||||
|
else if (chars === null)
|
||||||
|
return null;
|
||||||
|
let convertor, result = string,
|
||||||
|
refAlt = {};
|
||||||
|
this.util.asciiAlternatives.split('\n').forEach(a => refAlt[a[0]] = a.slice(2));
|
||||||
|
mode = mode.toLowerCase();
|
||||||
|
if (mode === "hard-unicode")
|
||||||
|
convertor = (c) => `\\u${('000'+c.charCodeAt().toString(16)).slice(-4)}`;
|
||||||
|
else if (mode === "soft-unicode")
|
||||||
|
convertor = (c) => refAlt[c] || `\\u${('000'+c.charCodeAt().toString(16)).slice(-4)}`;
|
||||||
|
else if (mode === "hard-remove")
|
||||||
|
convertor = c => "";
|
||||||
|
else if (mode === "soft-remove")
|
||||||
|
convertor = c => refAlt[c] || "";
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
for (let c in chars)
|
||||||
|
result = result.replaceAll(c, convertor(c));
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
revertUnicode: function(string) {
|
||||||
|
return string.replace(/\\u[\dA-F]{4}/gi,
|
||||||
|
m => String.fromCharCode(parseInt(m.replace(/\\u/g, ''), 16)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<script id="floBlockchainAPI" version="2.0.1">
|
<script id="floBlockchainAPI" version="2.0.1a">
|
||||||
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
|
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
|
||||||
const floBlockchainAPI = {
|
const floBlockchainAPI = {
|
||||||
|
|
||||||
@ -7322,6 +7374,8 @@ Bitcoin.Util = {
|
|||||||
//Send Tx to blockchain
|
//Send Tx to blockchain
|
||||||
sendTx: function(senderAddr, receiverAddr, sendAmt, privKey, floData = '') {
|
sendTx: function(senderAddr, receiverAddr, sendAmt, privKey, floData = '') {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!floCrypto.validateASCII(floData))
|
||||||
|
return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
|
||||||
if (!floCrypto.validateAddr(senderAddr))
|
if (!floCrypto.validateAddr(senderAddr))
|
||||||
reject(`Invalid address : ${senderAddr}`);
|
reject(`Invalid address : ${senderAddr}`);
|
||||||
else if (!floCrypto.validateAddr(receiverAddr))
|
else if (!floCrypto.validateAddr(receiverAddr))
|
||||||
@ -7368,6 +7422,8 @@ Bitcoin.Util = {
|
|||||||
return reject(`Invalid floID`);
|
return reject(`Invalid floID`);
|
||||||
if (!floCrypto.verifyPrivKey(privKey, floID))
|
if (!floCrypto.verifyPrivKey(privKey, floID))
|
||||||
return reject("Invalid Private Key")
|
return reject("Invalid Private Key")
|
||||||
|
if (!floCrypto.validateASCII(floData))
|
||||||
|
return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
|
||||||
|
|
||||||
var trx = bitjs.transaction();
|
var trx = bitjs.transaction();
|
||||||
var utxoAmt = 0.0;
|
var utxoAmt = 0.0;
|
||||||
@ -7431,7 +7487,8 @@ Bitcoin.Util = {
|
|||||||
*/
|
*/
|
||||||
sendTxMultiple: function(senderPrivKeys, receivers, floData = '') {
|
sendTxMultiple: function(senderPrivKeys, receivers, floData = '') {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!floCrypto.validateASCII(floData))
|
||||||
|
return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
|
||||||
let senders = {},
|
let senders = {},
|
||||||
preserveRatio;
|
preserveRatio;
|
||||||
//check for argument validations
|
//check for argument validations
|
||||||
@ -7649,8 +7706,8 @@ Bitcoin.Util = {
|
|||||||
filter : custom filter funtion for floData (eg . filter: d => {return d[0] == '$'})
|
filter : custom filter funtion for floData (eg . filter: d => {return d[0] == '$'})
|
||||||
*/
|
*/
|
||||||
readData: function(addr, options = {}) {
|
readData: function(addr, options = {}) {
|
||||||
options.limit = options.limit | 0
|
options.limit = options.limit || 0
|
||||||
options.ignoreOld = options.ignoreOld | 0
|
options.ignoreOld = options.ignoreOld || 0
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.promisedAPI(`api/addrs/${addr}/txs?from=0&to=1`).then(response => {
|
this.promisedAPI(`api/addrs/${addr}/txs?from=0&to=1`).then(response => {
|
||||||
var newItems = response.totalItems - options.ignoreOld;
|
var newItems = response.totalItems - options.ignoreOld;
|
||||||
@ -7661,22 +7718,18 @@ Bitcoin.Util = {
|
|||||||
var filteredData = [];
|
var filteredData = [];
|
||||||
for (i = 0; i < (response.totalItems - options.ignoreOld) &&
|
for (i = 0; i < (response.totalItems - options.ignoreOld) &&
|
||||||
filteredData.length < options.limit; i++) {
|
filteredData.length < options.limit; i++) {
|
||||||
if (options.sentOnly && response.items[i].vin[0].addr !==
|
if (options.sentOnly && response.items[i].vin[0].addr !== addr)
|
||||||
addr)
|
|
||||||
continue;
|
continue;
|
||||||
if (options.pattern) {
|
if (options.pattern) {
|
||||||
try {
|
try {
|
||||||
let jsonContent = JSON.parse(response.items[i]
|
let jsonContent = JSON.parse(response.items[i].floData)
|
||||||
.floData)
|
if (!Object.keys(jsonContent).includes(options.pattern))
|
||||||
if (!Object.keys(jsonContent).includes(options
|
|
||||||
.pattern))
|
|
||||||
continue;
|
continue;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (options.filter && !options.filter(response.items[i]
|
if (options.filter && !options.filter(response.items[i].floData))
|
||||||
.floData))
|
|
||||||
continue;
|
continue;
|
||||||
filteredData.push(response.items[i].floData);
|
filteredData.push(response.items[i].floData);
|
||||||
}
|
}
|
||||||
@ -9320,14 +9373,19 @@ Bitcoin.Util = {
|
|||||||
<script id="bank_app" version=0.5>
|
<script id="bank_app" version=0.5>
|
||||||
const bank_app = {
|
const bank_app = {
|
||||||
|
|
||||||
launchApp: function(reqCallback, resCallback){
|
launchApp: function(reqCallback, resCallback) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
let models = ["I_b", "I_s"];
|
||||||
|
models.forEach(m => this.model[m] = this.exParser(floGlobals.settings[m]));
|
||||||
|
for (let x in floGlobals.settings)
|
||||||
|
if (x.startsWith("constant-"))
|
||||||
|
this.constants[x.substring("constant-".length)] = floGlobals.settings[x]
|
||||||
let data = ["requests", "responses", "appendix"];
|
let data = ["requests", "responses", "appendix"];
|
||||||
Promise.all(data.map(d => readAllData(o))).then(results => {
|
Promise.all(data.map(d => compactIDB.readAllData(o))).then(results => {
|
||||||
for(let i in results)
|
for (let i in results)
|
||||||
this[data[i]] = results[i];
|
this[data[i]] = results[i];
|
||||||
this.refreshData(true).then(result => {
|
this.refreshData(true).then(result => {
|
||||||
setInterval(this.refreshData, this.util.refreshTime);
|
setInterval(() => this.refreshData(), this.util.refreshTime);
|
||||||
this.readDataFromCloud(myFloID === floGlobals.adminID, reqCallback, resCallback)
|
this.readDataFromCloud(myFloID === floGlobals.adminID, reqCallback, resCallback)
|
||||||
.then(result => resolve(result))
|
.then(result => resolve(result))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
@ -9340,26 +9398,36 @@ Bitcoin.Util = {
|
|||||||
myIndex: 0,
|
myIndex: 0,
|
||||||
lastUpdate: null,
|
lastUpdate: null,
|
||||||
secInYr: new Decimal(31556952000),
|
secInYr: new Decimal(31556952000),
|
||||||
|
refreshTime: 5 * 60000, //min*millisec
|
||||||
istr: floGlobals.application + '::'
|
istr: floGlobals.application + '::'
|
||||||
},
|
},
|
||||||
|
|
||||||
|
model: {},
|
||||||
|
constants: {},
|
||||||
|
accMap: {},
|
||||||
|
accounts: [],
|
||||||
|
rates: {},
|
||||||
|
|
||||||
refreshData: function(reset = false) {
|
refreshData: function(reset = false) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.readDataFromBlockchain().then(result => {
|
this.readDataFromBlockchain().then(result => {
|
||||||
if (reset) {
|
if (reset) {
|
||||||
compactIDB.readAllData('accounts').then(accounts => {
|
compactIDB.readAllData('accounts').then(accounts => {
|
||||||
|
console.info(this)
|
||||||
this.util.lastUpdate = null;
|
this.util.lastUpdate = null;
|
||||||
this.accMap = {};
|
this.accMap = {};
|
||||||
this.accounts = [];
|
this.accounts = [];
|
||||||
this.rates = {};
|
this.rates = {};
|
||||||
for (let a in accounts)
|
for (let a in accounts)
|
||||||
parseAccount(a);
|
parseAccount(a);
|
||||||
|
this.updateAccountNetValues();
|
||||||
resolve("Data Refreshed");
|
resolve("Data Refreshed");
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
for (let a in result)
|
for (let a in result)
|
||||||
parseAccount(a);
|
parseAccount(a);
|
||||||
|
this.updateAccountNetValues();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
this.refreshData(true)
|
this.refreshData(true)
|
||||||
@ -9380,12 +9448,13 @@ Bitcoin.Util = {
|
|||||||
for (let y in d[x].message)
|
for (let y in d[x].message)
|
||||||
r[x][y] = d[x].message[y];
|
r[x][y] = d[x].message[y];
|
||||||
r[x].floID = d[x].sender;
|
r[x].floID = d[x].sender;
|
||||||
|
r[x].status = "pending";
|
||||||
this.requests[x] = r[x];
|
this.requests[x] = r[x];
|
||||||
compactIDB.addData("requests", r[x], x);
|
compactIDB.addData("requests", r[x], x);
|
||||||
}
|
}
|
||||||
this.appendix.lastRequest = Object.keys(this.requests).sort().pop();
|
this.appendix.lastRequest = Object.keys(this.requests).sort().pop();
|
||||||
compactIDB.writeData("appendix", this.appendix.lastRequest, "lastRequest");
|
compactIDB.writeData("appendix", this.appendix.lastRequest, "lastRequest");
|
||||||
if(reqCallback instanceof Function)
|
if (reqCallback instanceof Function)
|
||||||
reqCallback(r, e);
|
reqCallback(r, e);
|
||||||
}
|
}
|
||||||
if (isAdmin)
|
if (isAdmin)
|
||||||
@ -9412,12 +9481,12 @@ Bitcoin.Util = {
|
|||||||
r[x] = {}
|
r[x] = {}
|
||||||
for (let y in d[x].message)
|
for (let y in d[x].message)
|
||||||
r[x][y] = d[x].message[y];
|
r[x][y] = d[x].message[y];
|
||||||
this.response[x] = r[x];
|
this.responses[x] = r[x];
|
||||||
compactIDB.addData("responses", r[x], x);
|
compactIDB.addData("responses", r[x], x);
|
||||||
}
|
}
|
||||||
this.appendix.lastResponse = Object.keys(this.response).sort().pop();
|
this.appendix.lastResponse = Object.keys(this.responses).sort().pop();
|
||||||
compactIDB.writeData("appendix", this.appendix.lastResponse, "lastResponse");
|
compactIDB.writeData("appendix", this.appendix.lastResponse, "lastResponse");
|
||||||
if(resCallback instanceof Function)
|
if (resCallback instanceof Function)
|
||||||
resCallback(r, e);
|
resCallback(r, e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -9427,26 +9496,26 @@ Bitcoin.Util = {
|
|||||||
|
|
||||||
readDataFromBlockchain: function() {
|
readDataFromBlockchain: function() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let options = {
|
let options = {
|
||||||
sentOnly: true,
|
sentOnly: true,
|
||||||
ignoreOld: this.appendix.lastTx,
|
ignoreOld: this.appendix.lastTx,
|
||||||
filter: d => d.startsWith(this.util.istr)
|
filter: d => d.startsWith(this.util.istr)
|
||||||
}
|
}
|
||||||
floBlockchainAPI.readData(floGlobals.adminID, options).then(result => {
|
floBlockchainAPI.readData(floGlobals.adminID, options).then(result => {
|
||||||
let accounts = {},
|
let accounts = {},
|
||||||
promises = [];
|
promises = [];
|
||||||
result.data.forEach(d => {
|
result.data.forEach(d => {
|
||||||
let data = d.substring(this.util.istr.length).split('|');
|
let data = d.substring(this.util.istr.length).split('|');
|
||||||
accounts[data[1]] = data;
|
accounts[data[1]] = data;
|
||||||
promises.push(compactIDB.addData('accounts', data, data[1]))
|
promises.push(compactIDB.addData('accounts', data, data[1]))
|
||||||
});
|
});
|
||||||
Promise.all(promises).then(result => {
|
Promise.all(promises).then(results => {
|
||||||
this.appendix.lastTx = result.totalTxs
|
this.appendix.lastTx = result.totalTxs
|
||||||
compactIDB.writeData('appendix', result.totalTxs, lastTx)
|
compactIDB.writeData('appendix', result.totalTxs, "lastTx")
|
||||||
.then(result => resolve(accounts))
|
.then(result => resolve(accounts))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
}).catch(error => reject(error))
|
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
})
|
})
|
||||||
|
|
||||||
},
|
},
|
||||||
@ -9454,7 +9523,7 @@ Bitcoin.Util = {
|
|||||||
parseAccount: function(data) {
|
parseAccount: function(data) {
|
||||||
switch (data[0]) {
|
switch (data[0]) {
|
||||||
case "open-deposit":
|
case "open-deposit":
|
||||||
if (floCrypto.verifySign(`open#${data[3]}:deposit${data[4]}|${data[7]}`, data[6], data[5]))
|
if (floCrypto.verifySign(this.istr + `open#${data[3]}:deposit${data[4]}|${data[7]}`, data[6], data[5]))
|
||||||
this.addAccount(data[2], {
|
this.addAccount(data[2], {
|
||||||
floID: data[2],
|
floID: data[2],
|
||||||
type: "deposit",
|
type: "deposit",
|
||||||
@ -9465,11 +9534,11 @@ Bitcoin.Util = {
|
|||||||
}, data[1]);
|
}, data[1]);
|
||||||
break;
|
break;
|
||||||
case "close-deposit":
|
case "close-deposit":
|
||||||
if (floCrypto.verifySign(`close-deposit#${data[3]}`, data[5], data[4]))
|
if (floCrypto.verifySign(this.istr + `close-deposit#${data[3]}`, data[5], data[4]))
|
||||||
this.closeAccount(data[2], data[3], data[1]);
|
this.closeAccount(data[2], data[3], data[1]);
|
||||||
break;
|
break;
|
||||||
case "open-loan":
|
case "open-loan":
|
||||||
if (floCrypto.verifySign(`open#${data[3]}:deposit${data[4]}|${data[7]}`, data[6], data[5]))
|
if (floCrypto.verifySign(this.istr + `open#${data[3]}:loan${data[4]}`, data[6], data[5]))
|
||||||
this.addAccount(data[2], {
|
this.addAccount(data[2], {
|
||||||
floID: data[2],
|
floID: data[2],
|
||||||
type: "loan",
|
type: "loan",
|
||||||
@ -9480,7 +9549,7 @@ Bitcoin.Util = {
|
|||||||
}, data[1]);
|
}, data[1]);
|
||||||
break;
|
break;
|
||||||
case "close-loan":
|
case "close-loan":
|
||||||
if (floCrypto.verifySign(`close-loan#${data[3]}:${data[6]}`, data[5], data[4]))
|
if (floCrypto.verifySign(this.istr + `close-loan#${data[3]}:${data[6]}`, data[5], data[4]))
|
||||||
this.closeAccount(data[2], data[3], data[1]);
|
this.closeAccount(data[2], data[3], data[1]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -9526,14 +9595,32 @@ Bitcoin.Util = {
|
|||||||
resolve(response)
|
resolve(response)
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
})
|
})
|
||||||
|
},
|
||||||
|
validateToken: function(tx, sender, receiver, amount = null, token = floGlobals.token) {
|
||||||
|
return ((!sender || tx.sender === sender) &&
|
||||||
|
(!receiver || tx.receiver === receiver) &&
|
||||||
|
(amount === null || tx.amount === amount) &&
|
||||||
|
(token === tx.token));
|
||||||
|
},
|
||||||
|
verifyToken: function(txid, sender, receiver, amount = null, token = floGlobals.token) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.getTx(tx).then(txn => {
|
||||||
|
if (sender && txn.sender != sender)
|
||||||
|
reject("Incorrect sender");
|
||||||
|
else if (receiver && txn.receiver != receiver)
|
||||||
|
reject("Incorrect receiver");
|
||||||
|
else if (amount && txn.amount != amount)
|
||||||
|
reject("Incorrect amount");
|
||||||
|
else if (token != txn.token)
|
||||||
|
reject("Incorrect token")
|
||||||
|
else
|
||||||
|
resolve(txn)
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
accMap: {},
|
|
||||||
accounts: [],
|
|
||||||
rates: {},
|
|
||||||
|
|
||||||
addAccount: function(floID, account, time) {
|
addAccount: function(floID, account, time) {
|
||||||
this.updateAccountNetValues(time);
|
this.updateAccountNetValues(time);
|
||||||
account.netAmt = account.amount;
|
account.netAmt = account.amount;
|
||||||
@ -9694,10 +9781,8 @@ Bitcoin.Util = {
|
|||||||
return accounts[mappedIndex];
|
return accounts[mappedIndex];
|
||||||
},
|
},
|
||||||
|
|
||||||
parser(expression) {
|
exParser(expression) {
|
||||||
console.info(expression)
|
|
||||||
let infix = expression.replace(/\s/g, "").split(""); //tokenize
|
let infix = expression.replace(/\s/g, "").split(""); //tokenize
|
||||||
console.info(infix)
|
|
||||||
const p = ["-", "+", "*", "/", "^"]; //presedences order (low to high)
|
const p = ["-", "+", "*", "/", "^"]; //presedences order (low to high)
|
||||||
var opStack = [],
|
var opStack = [],
|
||||||
postfix = [];
|
postfix = [];
|
||||||
@ -9742,7 +9827,6 @@ Bitcoin.Util = {
|
|||||||
postfix.push(opStack.pop());
|
postfix.push(opStack.pop());
|
||||||
else
|
else
|
||||||
throw Error("Invalid expression: unpaired bracket");
|
throw Error("Invalid expression: unpaired bracket");
|
||||||
console.info(postfix)
|
|
||||||
return postfix
|
return postfix
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -9763,7 +9847,6 @@ Bitcoin.Util = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let token of model) {
|
for (let token of model) {
|
||||||
console.info(token)
|
|
||||||
if (/^[\w.]+$/.test(token))
|
if (/^[\w.]+$/.test(token))
|
||||||
result.push(token)
|
result.push(token)
|
||||||
else {
|
else {
|
||||||
@ -9787,7 +9870,6 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeDeposit: function(amount) {
|
makeDeposit: function(amount) {
|
||||||
this.updateAccountNetValues()
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
let index = this.util.myIndex + 1
|
let index = this.util.myIndex + 1
|
||||||
@ -9795,13 +9877,15 @@ Bitcoin.Util = {
|
|||||||
let bal = await this.tokenAPI.getBalance(myFloID)
|
let bal = await this.tokenAPI.getBalance(myFloID)
|
||||||
if (bal < amount)
|
if (bal < amount)
|
||||||
throw Error('Insufficiant balance')
|
throw Error('Insufficiant balance')
|
||||||
|
//refresh data and update the account net values
|
||||||
|
await this.refreshData();
|
||||||
//send token to banker floID (using floBlockchain API)
|
//send token to banker floID (using floBlockchain API)
|
||||||
let txid = await floBlockchainAPI.writeData(myFloID, `send ${amount} ${floGlobals.token}# for open-deposit`, myPrivKey, floGlobals.adminID)
|
let txid = await floBlockchainAPI.writeData(myFloID, `send ${amount} ${floGlobals.token}# for open-deposit`, myPrivKey, floGlobals.adminID)
|
||||||
let request = {
|
let request = {
|
||||||
rtype: "openDeposit",
|
rtype: "openDeposit",
|
||||||
amount: amount,
|
amount: amount,
|
||||||
txid: txid,
|
txid: txid,
|
||||||
sign: floCrypto.signData(`open#${index}:deposit${amount}|${txid}`)
|
sign: floCrypto.signData(this.istr + `open#${index}:deposit${amount}|${txid}`)
|
||||||
}
|
}
|
||||||
//send request to banker (floCloudAPI)
|
//send request to banker (floCloudAPI)
|
||||||
floCloudAPI.sendApplicationData(request, "REQUEST")
|
floCloudAPI.sendApplicationData(request, "REQUEST")
|
||||||
@ -9815,35 +9899,39 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
verifyDeposit: function(reqID) {
|
verifyDeposit: function(reqID) {
|
||||||
this.updateAccountNetValues()
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
let req = this.requests[reqID];
|
||||||
try {
|
try {
|
||||||
//verify the token tx
|
//verify the token tx
|
||||||
let req = this.getRequest(reqID)
|
|
||||||
if (req.rtype !== "openDeposit")
|
if (req.rtype !== "openDeposit")
|
||||||
throw Error('Invalid request: Request-type is not openDeposit')
|
throw Error('Invalid request: Request-type is not openDeposit')
|
||||||
|
if (req.status !== "pending")
|
||||||
|
throw Error("Request already processed");
|
||||||
let tx = await this.tokenAPI.getTx(req.txid);
|
let tx = await this.tokenAPI.getTx(req.txid);
|
||||||
if (tx.amount !== req.amount)
|
if (!this.tokenAPI.validateToken(tx, req.floID, floGlobals.adminID, req.amount))
|
||||||
throw Error("R>Invalid deposit: Token amount doesnot match")
|
throw Error("R>Invalid deposit: Invalid token transaction");
|
||||||
//verify the signature
|
//verify the signature
|
||||||
if (req.index != this.accMap[req.floID].length)
|
if (req.index != this.accMap[req.floID].length)
|
||||||
throw Error(`R>Invalid index (correct index is ${this.accMap[req.floID].length})`);
|
throw Error(`R>Invalid index (correct index is ${this.accMap[req.floID].length})`);
|
||||||
if (!floCrypto.verifySign(`open#${req.index}:deposit${req.amount}|${req.txid}`, req.sign, req.pubKey))
|
if (!floCrypto.verifySign(this.istr + `open#${req.index}:deposit${req.amount}|${req.txid}`, req.sign, req.pubKey))
|
||||||
throw Error("R>Invalid deposit signature: Re-signing required")
|
throw Error("R>Invalid signature: Re-signing required")
|
||||||
|
await this.refreshData()
|
||||||
|
let timestamp = this.util.lastUpdate;
|
||||||
//write deposit details in blockchain
|
//write deposit details in blockchain
|
||||||
let data = ["open-deposit", tx.timestamp, req.floID, req.index, req.amount, req.pubKey, req.sign, req.txid].join('|')
|
let data = ["open-deposit", timestamp, req.floID, req.index, req.amount, req.pubKey, req.sign, req.txid].join('|')
|
||||||
let txid = floBlockchainAPI.writeData(myFloID, data, myPrivKey, floGlobals.adminID)
|
let txid = floBlockchainAPI.writeData(myFloID, this.util.istr + data, myPrivKey, floGlobals.adminID)
|
||||||
let response = {
|
let response = {
|
||||||
requestID: reqID,
|
requestID: reqID,
|
||||||
rtype: "openDeposit",
|
rtype: "openDeposit",
|
||||||
status: "success",
|
status: "success",
|
||||||
txid: txid
|
txid: txid
|
||||||
}
|
}
|
||||||
|
req.status = "completed";
|
||||||
|
compactIDB.writeData("requests", req, reqID);
|
||||||
//send response to client
|
//send response to client
|
||||||
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
||||||
receiverID: clientID
|
receiverID: clientID
|
||||||
})
|
}).then(result => resolve(response))
|
||||||
.then(result => resolve(response))
|
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -9856,11 +9944,12 @@ Bitcoin.Util = {
|
|||||||
status: "failed",
|
status: "failed",
|
||||||
reason: error.message.substring(2)
|
reason: error.message.substring(2)
|
||||||
}
|
}
|
||||||
|
req.status = `failed (${response.reason})`;
|
||||||
|
compactIDB.writeData("requests", req, reqID);
|
||||||
//send rejected response to client
|
//send rejected response to client
|
||||||
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
||||||
receiverID: clientID
|
receiverID: clientID
|
||||||
})
|
}).then(result => resolve(response))
|
||||||
.then(result => resolve(response))
|
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -9868,7 +9957,6 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
withdrawDeposit: function(index) {
|
withdrawDeposit: function(index) {
|
||||||
this.updateAccountNetValues()
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
//check for validity (ie no loans outstanding)
|
//check for validity (ie no loans outstanding)
|
||||||
@ -9880,10 +9968,11 @@ Bitcoin.Util = {
|
|||||||
throw Error(`Account#${index} is not active`)
|
throw Error(`Account#${index} is not active`)
|
||||||
if (details.netTotal < acc.netAmt)
|
if (details.netTotal < acc.netAmt)
|
||||||
throw Error(`Unable to withdraw deposit: Loan amount exceeds`)
|
throw Error(`Unable to withdraw deposit: Loan amount exceeds`)
|
||||||
|
await this.refreshData()
|
||||||
let request = {
|
let request = {
|
||||||
rtype: "closeDeposit",
|
rtype: "closeDeposit",
|
||||||
index: index,
|
index: index,
|
||||||
sign: floCrypto.signData(`close-deposit#${index}`, myPrivKey)
|
sign: floCrypto.signData(this.istr + `close-deposit#${index}`, myPrivKey)
|
||||||
}
|
}
|
||||||
//send request to banker
|
//send request to banker
|
||||||
floCloudAPI.sendApplicationData(request, "REQUEST")
|
floCloudAPI.sendApplicationData(request, "REQUEST")
|
||||||
@ -9896,12 +9985,15 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
closeDeposit: function(reqID) {
|
closeDeposit: function(reqID) {
|
||||||
this.updateAccountNetValues()
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
let req = this.requests[reqID];
|
||||||
try {
|
try {
|
||||||
let req = this.getRequest(reqID)
|
await this.refreshData()
|
||||||
|
let timestamp = this.util.lastUpdate;
|
||||||
if (req.rtype !== "closeDeposit")
|
if (req.rtype !== "closeDeposit")
|
||||||
throw Error('Invalid request: Request-type is not closeDeposit')
|
throw Error('Invalid request: Request-type is not closeDeposit')
|
||||||
|
if (req.status !== "pending")
|
||||||
|
throw Error("Request already processed");
|
||||||
//check for validity (ie no loans outstanding)
|
//check for validity (ie no loans outstanding)
|
||||||
let details = this.getUserDetails(req.floID)
|
let details = this.getUserDetails(req.floID)
|
||||||
let acc = details.accounts[req.index]
|
let acc = details.accounts[req.index]
|
||||||
@ -9912,17 +10004,17 @@ Bitcoin.Util = {
|
|||||||
if (details.netTotal < acc.netAmt)
|
if (details.netTotal < acc.netAmt)
|
||||||
throw Error(`R>Unable to close deposit: Loan amount exceeds`)
|
throw Error(`R>Unable to close deposit: Loan amount exceeds`)
|
||||||
//check for signature in request
|
//check for signature in request
|
||||||
if (floCrypto.verifySign(`close-deposit#${req.index}`, req.sign, req.pubKey))
|
if (floCrypto.verifySign(this.istr + `close-deposit#${req.index}`, req.sign, req.pubKey))
|
||||||
throw Error("R>Invalid deposit signature: Re-signing required")
|
throw Error("R>Invalid signature: Re-signing required")
|
||||||
//send tokens to user and close loan (via blockchain)
|
//send tokens to user and close loan (via blockchain)
|
||||||
let ttxid = await floBlockchainAPI.writeData(myFloID, `send ${acc.netAmt} ${floGlobals.token}# for close-deposit`, myPrivKey, req.floID)
|
let ttxid = await floBlockchainAPI.writeData(myFloID, `send ${acc.netAmt} ${floGlobals.token}# for close-deposit`, myPrivKey, req.floID)
|
||||||
//verify token transfer
|
//verify token transfer
|
||||||
let tx = await this.tokenAPI.getTx(ttxid)
|
let tx = await this.tokenAPI.getTx(ttxid)
|
||||||
if (!valid(tx)) //Condition should be fixed
|
if (!this.tokenAPI.validateToken(tx, floGlobals.adminID, req.floID, acc.netAmt))
|
||||||
throw Error('Token not valid')
|
throw Error('Token not valid');
|
||||||
//write close-deposit in blockchain
|
//write close-deposit in blockchain
|
||||||
let data = ["close-deposit", timestamp, clientID, req.index, req.pubKey, req.sign, ttxid].join('|')
|
let data = ["close-deposit", timestamp, req.floID, req.index, req.pubKey, req.sign, ttxid].join('|')
|
||||||
let btxid = await floBlockchainAPI.writeData(myFloID, data, myPrivKey, floGlobals.adminID)
|
let btxid = await floBlockchainAPI.writeData(myFloID, this.util.istr + data, myPrivKey, floGlobals.adminID)
|
||||||
//send response to client
|
//send response to client
|
||||||
let response = {
|
let response = {
|
||||||
requestID: reqID,
|
requestID: reqID,
|
||||||
@ -9931,6 +10023,8 @@ Bitcoin.Util = {
|
|||||||
token: ttxid,
|
token: ttxid,
|
||||||
txid: btxid
|
txid: btxid
|
||||||
}
|
}
|
||||||
|
req.status = "completed";
|
||||||
|
compactIDB.writeData("requests", req, reqID);
|
||||||
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
||||||
receiverID: clientID
|
receiverID: clientID
|
||||||
}).then(result => resolve(response))
|
}).then(result => resolve(response))
|
||||||
@ -9945,6 +10039,8 @@ Bitcoin.Util = {
|
|||||||
status: "failed",
|
status: "failed",
|
||||||
reason: error.message.substring(2)
|
reason: error.message.substring(2)
|
||||||
}
|
}
|
||||||
|
req.status = `failed (${response.reason})`;
|
||||||
|
compactIDB.writeData("requests", req, reqID);
|
||||||
//send rejected response to client
|
//send rejected response to client
|
||||||
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
||||||
receiverID: clientID
|
receiverID: clientID
|
||||||
@ -9956,8 +10052,8 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
requestLoan: function(amount) {
|
requestLoan: function(amount) {
|
||||||
this.updateAccountNetValues()
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
await this.refreshData()
|
||||||
let index = this.util.myIndex + 1;
|
let index = this.util.myIndex + 1;
|
||||||
//check for validity of loan (ie, check if enough deposit is present)
|
//check for validity of loan (ie, check if enough deposit is present)
|
||||||
let user = this.getUserDetails(myFloID)
|
let user = this.getUserDetails(myFloID)
|
||||||
@ -9967,7 +10063,7 @@ Bitcoin.Util = {
|
|||||||
rtype: "openLoan",
|
rtype: "openLoan",
|
||||||
index: index,
|
index: index,
|
||||||
amount: amount,
|
amount: amount,
|
||||||
sign: floCrypto.signData(`open#${index}:loan${amount}`, myPrivKey)
|
sign: floCrypto.signData(this.istr + `open#${index}:loan${amount}`, myPrivKey)
|
||||||
}
|
}
|
||||||
//send request to banker
|
//send request to banker
|
||||||
floCloudAPI.sendApplicationData(request, "REQUEST")
|
floCloudAPI.sendApplicationData(request, "REQUEST")
|
||||||
@ -9977,29 +10073,32 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
sendLoan: function(reqID) {
|
sendLoan: function(reqID) {
|
||||||
this.updateAccountNetValues()
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
let req = this.requests[reqID];
|
||||||
try {
|
try {
|
||||||
let req = this.getRequest(reqID);
|
await this.refreshData()
|
||||||
|
let timestamp = this.util.lastUpdate;
|
||||||
if (req.rtype !== "openLoan")
|
if (req.rtype !== "openLoan")
|
||||||
throw Error('Invalid request: Request-type is not openLoan');
|
throw Error('Invalid request: Request-type is not openLoan');
|
||||||
|
if (req.status !== "pending")
|
||||||
|
throw Error("Request already processed");
|
||||||
//verify the signature
|
//verify the signature
|
||||||
if (req.index != this.accMap[req.floID].length)
|
if (req.index != this.accMap[req.floID].length)
|
||||||
throw Error(`R>Invalid index (correct index is ${this.accMap[req.floID].length})`);
|
throw Error(`R>Invalid index (correct index is ${this.accMap[req.floID].length})`);
|
||||||
if (!floCrypto.verifySign(`open#${req.index}:loan${req.amount}`, req.sign, req.pubKey))
|
if (!floCrypto.verifySign(this.istr + `open#${req.index}:loan${req.amount}`, req.sign, req.pubKey))
|
||||||
throw Error("R>Invalid deposit signature: Re-signing required");
|
throw Error("R>Invalid signature: Re-signing required");
|
||||||
//check for validity of loan (ie, check if enough deposit is present)
|
//check for validity of loan (ie, check if enough deposit is present)
|
||||||
let user = this.getUserDetails(req.floID)
|
let user = this.getUserDetails(req.floID)
|
||||||
if (user.netTotal <= amount)
|
if (user.netTotal <= req.amount)
|
||||||
return reject("Deposit insufficient");
|
return reject("R>Deposit insufficient");
|
||||||
//send token to user
|
//send token to user
|
||||||
ttxid = await floBlockchainAPI.writeData(myFloID, `send ${amount} ${floGlobals.token}# for open-loan`, myPrivKey, clientID);
|
ttxid = await floBlockchainAPI.writeData(myFloID, `send ${req.amount} ${floGlobals.token}# for open-loan`, myPrivKey, clientID);
|
||||||
//verify token transfer
|
//verify token transfer
|
||||||
let tx = await this.tokenAPI.getTx(ttxid)
|
let tx = await this.tokenAPI.getTx(ttxid)
|
||||||
if (!valid(tx)) //Condition should be fixed
|
if (!this.tokenAPI.validateToken(tx, floGlobals.adminID, req.floID, req.amount))
|
||||||
throw Error('Token not valid');
|
throw Error('Token not valid');
|
||||||
let data = ["open-loan", timestamp, clientID, sign, ttxid].join('|')
|
let data = ["open-loan", timestamp, req.floID, req.index, req.amount, req.pubKey, req.sign, ttxid].join('|')
|
||||||
let btxid = await floBlockchainAPI.writeData(myFloID, data, myPrivKey, floGlobals.adminID)
|
let btxid = await floBlockchainAPI.writeData(myFloID, this.util.istr + data, myPrivKey, floGlobals.adminID)
|
||||||
let response = {
|
let response = {
|
||||||
requestID: reqID,
|
requestID: reqID,
|
||||||
rtype: "openLoan",
|
rtype: "openLoan",
|
||||||
@ -10007,6 +10106,8 @@ Bitcoin.Util = {
|
|||||||
token: ttxid,
|
token: ttxid,
|
||||||
txid: btxid
|
txid: btxid
|
||||||
}
|
}
|
||||||
|
req.status = "completed";
|
||||||
|
compactIDB.writeData("requests", req, reqID);
|
||||||
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
||||||
receiverID: clientID
|
receiverID: clientID
|
||||||
}).then(result => resolve(result))
|
}).then(result => resolve(result))
|
||||||
@ -10021,6 +10122,8 @@ Bitcoin.Util = {
|
|||||||
status: "failed",
|
status: "failed",
|
||||||
reason: error.message.substring(2)
|
reason: error.message.substring(2)
|
||||||
}
|
}
|
||||||
|
req.status = `failed (${response.reason})`;
|
||||||
|
compactIDB.writeData("requests", req, reqID);
|
||||||
//send rejected response to client
|
//send rejected response to client
|
||||||
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
||||||
receiverID: clientID
|
receiverID: clientID
|
||||||
@ -10032,9 +10135,9 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
repayLoan: function(index) {
|
repayLoan: function(index) {
|
||||||
this.updateAccountNetValues()
|
return new Promise(async (resolve, reject) => {
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
try {
|
try {
|
||||||
|
this.refreshData();
|
||||||
let details = this.getUserDetails(myFloID)
|
let details = this.getUserDetails(myFloID)
|
||||||
let acc = details.accounts[index]
|
let acc = details.accounts[index]
|
||||||
if (acc.type !== "loan")
|
if (acc.type !== "loan")
|
||||||
@ -10051,7 +10154,7 @@ Bitcoin.Util = {
|
|||||||
rtype: "closeLoan",
|
rtype: "closeLoan",
|
||||||
index: index,
|
index: index,
|
||||||
txid: txid,
|
txid: txid,
|
||||||
sign: floCrypto.signData(`close-loan#${index}:${txid}`)
|
sign: floCrypto.signData(this.istr + `close-loan#${index}:${txid}`)
|
||||||
}
|
}
|
||||||
floCloudAPI.sendApplicationData(request, "REQUEST")
|
floCloudAPI.sendApplicationData(request, "REQUEST")
|
||||||
.then(result => resolve(result))
|
.then(result => resolve(result))
|
||||||
@ -10063,26 +10166,38 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
closeLoan: function(reqID) {
|
closeLoan: function(reqID) {
|
||||||
this.updateAccountNetValues()
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
let req = this.requests[reqID];
|
||||||
try {
|
try {
|
||||||
//verify token tx
|
|
||||||
let req = this.getRequest(reqID)
|
|
||||||
if (req.rtype !== "closeLoan")
|
if (req.rtype !== "closeLoan")
|
||||||
throw Error('Invalid request: Request-type is not closeLoan')
|
throw Error('Invalid request: Request-type is not closeLoan');
|
||||||
|
if (req.status !== "pending")
|
||||||
|
throw Error("Request already processed");
|
||||||
|
//verify signature
|
||||||
|
if (!floCrypto.verifySign(this.istr + `close-loan#${req.index}:${req.txid}`, req.sign, req.pubKey))
|
||||||
|
throw Error("R>Invalid signature: Re-signing required");
|
||||||
|
//verify token tx
|
||||||
let tx = await this.tokenAPI.getTx(req.txid);
|
let tx = await this.tokenAPI.getTx(req.txid);
|
||||||
|
await this.refreshData()
|
||||||
let acc = this.getAccount(req.floID, req.index);
|
let acc = this.getAccount(req.floID, req.index);
|
||||||
if (tx.amount !== acc.netAmt)
|
if (acc.status !== "active")
|
||||||
throw Error("R>Invalid request: Token amount insufficient")
|
throw Error("Account already closed");
|
||||||
|
if (tx.amount < acc.netAmt)
|
||||||
|
throw Error("R>Invalid request: Token amount insufficient");
|
||||||
|
if (!this.tokenAPI.validateToken(tx, req.floID, floGlobals.adminID))
|
||||||
|
throw Error("R>Invalid request: Invalid token transaction")
|
||||||
|
let timestamp = this.util.lastUpdate;
|
||||||
//close loan via blockchain
|
//close loan via blockchain
|
||||||
let data = ["close-loan", timestamp, req.floID, req.index, req.pubKey, req, sign, req.txid].join('|')
|
let data = ["close-loan", timestamp, req.floID, req.index, req.pubKey, req, sign, req.txid].join('|')
|
||||||
txid = await floBlockchainAPI.writeData(myFloID, data, myPrivKey, floGlobals.adminID)
|
txid = await floBlockchainAPI.writeData(myFloID, this.util.istr + data, myPrivKey, floGlobals.adminID)
|
||||||
let response = {
|
let response = {
|
||||||
requestID: reqID,
|
requestID: reqID,
|
||||||
rtype: "closeLoan",
|
rtype: "closeLoan",
|
||||||
status: "success",
|
status: "success",
|
||||||
txid: txid
|
txid: txid
|
||||||
}
|
}
|
||||||
|
req.status = "completed";
|
||||||
|
compactIDB.writeData("requests", req, reqID);
|
||||||
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
||||||
receiverID: clientID
|
receiverID: clientID
|
||||||
}).then(result => resolve(result))
|
}).then(result => resolve(result))
|
||||||
@ -10097,6 +10212,8 @@ Bitcoin.Util = {
|
|||||||
status: "failed",
|
status: "failed",
|
||||||
reason: error.message.substring(2)
|
reason: error.message.substring(2)
|
||||||
}
|
}
|
||||||
|
req.status = `failed (${response.reason})`;
|
||||||
|
compactIDB.writeData("requests", req, reqID);
|
||||||
//send rejected response to client
|
//send rejected response to client
|
||||||
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
floCloudAPI.sendApplicationData(response, "RESPONSE", {
|
||||||
receiverID: clientID
|
receiverID: clientID
|
||||||
@ -10105,6 +10222,22 @@ Bitcoin.Util = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
viewAllRequests: function(floID = null) {
|
||||||
|
let requests = this.requests
|
||||||
|
if (floID) {
|
||||||
|
let result = {}
|
||||||
|
for (let r in requests)
|
||||||
|
if (requests[r].floID === floID)
|
||||||
|
result[r] = requests[r];
|
||||||
|
return result;
|
||||||
|
} else
|
||||||
|
return requests;
|
||||||
|
},
|
||||||
|
|
||||||
|
viewAllResponses: function() {
|
||||||
|
return this.responses;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user