floCrypto_v2.0.1, floBlockchainAPI_v2.0.1a

floCrypto_v2.0.1 (ASCII fns)
- validateASCII
- convertToASCII
- revertUnicode

floBlockchainAPI_v2.0.1a
bugfix
This commit is contained in:
sairajzero 2021-04-18 02:36:51 +05:30
parent 836f359cf2
commit 05f110661e

View File

@ -6955,7 +6955,7 @@ Bitcoin.Util = {
})();
</script>
<script id="floCrypto" version="2.0.0">
<script id="floCrypto" version="2.0.1">
/* FLO Crypto Operators*/
const floCrypto = {
@ -6964,6 +6964,8 @@ Bitcoin.Util = {
ecparams: EllipticCurve.getSECCurveByName("secp256k1"),
asciiAlternatives: ` '\n '\n“ "\n” "\n --\n— ---\n≥ >=\n≤ <=\n≠ !=\n× *\n÷ /\n← <-\n ->\n↔ <->\n⇒ =>\n⇐ <=\n⇔ <=>`,
exponent1: function() {
return this.p.add(BigInteger.ONE).divide(BigInteger("4"))
},
@ -6989,9 +6991,8 @@ Bitcoin.Util = {
// verify y value
let resultBigInt = y.mod(BigInteger("2"));
let check = resultBigInt.toString() % 2;
if (prefix_modulus !== check) {
if (prefix_modulus !== check)
yDecimalValue = y.negate().mod(p).toString();
}
return {
x: xDecimalValue,
y: yDecimalValue
@ -7008,20 +7009,15 @@ Bitcoin.Util = {
},
deriveSharedKeySender: function(receiverCompressedPublicKey, senderPrivateKey) {
try {
let receiverPublicKeyString = this.getUncompressedPublicKey(receiverCompressedPublicKey);
var senderDerivedKey = ellipticCurveEncryption.senderSharedKeyDerivation(
receiverPublicKeyString.x, receiverPublicKeyString.y, senderPrivateKey);
return senderDerivedKey;
} catch (error) {
return new Error(error);
}
},
deriveReceiverSharedKey: function(senderPublicKeyString, receiverPrivateKey) {
return ellipticCurveEncryption.receiverSharedKeyDerivation(
senderPublicKeyString.XValuePublicString, senderPublicKeyString.YValuePublicString,
receiverPrivateKey);
senderPublicKeyString.XValuePublicString, senderPublicKeyString.YValuePublicString, receiverPrivateKey);
},
getReceiverPublicKeyString: function(privateKey) {
@ -7029,14 +7025,8 @@ Bitcoin.Util = {
},
deriveSharedKeyReceiver: function(senderPublicKeyString, receiverPrivateKey) {
try {
return ellipticCurveEncryption.receiverSharedKeyDerivation(senderPublicKeyString
.XValuePublicString,
senderPublicKeyString.YValuePublicString, receiverPrivateKey);
} catch (error) {
return new Error(error);
}
return ellipticCurveEncryption.receiverSharedKeyDerivation(
senderPublicKeyString.XValuePublicString, senderPublicKeyString.YValuePublicString, receiverPrivateKey);
},
wifToDecimal: function(pk_wif, isPubKeyCompressed = false) {
@ -7241,10 +7231,68 @@ Bitcoin.Util = {
} catch {
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 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*/
const floBlockchainAPI = {
@ -7316,6 +7364,8 @@ Bitcoin.Util = {
//Send Tx to blockchain
sendTx: function(senderAddr, receiverAddr, sendAmt, privKey, floData = '') {
return new Promise((resolve, reject) => {
if (!floCrypto.validateASCII(floData))
return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
if (!floCrypto.validateAddr(senderAddr))
reject(`Invalid address : ${senderAddr}`);
else if (!floCrypto.validateAddr(receiverAddr))
@ -7362,6 +7412,8 @@ Bitcoin.Util = {
return reject(`Invalid floID`);
if (!floCrypto.verifyPrivKey(privKey, floID))
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 utxoAmt = 0.0;
@ -7425,7 +7477,8 @@ Bitcoin.Util = {
*/
sendTxMultiple: function(senderPrivKeys, receivers, floData = '') {
return new Promise((resolve, reject) => {
if (!floCrypto.validateASCII(floData))
return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
let senders = {},
preserveRatio;
//check for argument validations
@ -7643,8 +7696,8 @@ Bitcoin.Util = {
filter : custom filter funtion for floData (eg . filter: d => {return d[0] == '$'})
*/
readData: function(addr, options = {}) {
options.limit = options.limit | 0
options.ignoreOld = options.ignoreOld | 0
options.limit = options.limit || 0
options.ignoreOld = options.ignoreOld || 0
return new Promise((resolve, reject) => {
this.promisedAPI(`api/addrs/${addr}/txs?from=0&to=1`).then(response => {
var newItems = response.totalItems - options.ignoreOld;
@ -7655,22 +7708,18 @@ Bitcoin.Util = {
var filteredData = [];
for (i = 0; i < (response.totalItems - options.ignoreOld) &&
filteredData.length < options.limit; i++) {
if (options.sentOnly && response.items[i].vin[0].addr !==
addr)
if (options.sentOnly && response.items[i].vin[0].addr !== addr)
continue;
if (options.pattern) {
try {
let jsonContent = JSON.parse(response.items[i]
.floData)
if (!Object.keys(jsonContent).includes(options
.pattern))
let jsonContent = JSON.parse(response.items[i].floData)
if (!Object.keys(jsonContent).includes(options.pattern))
continue;
} catch (error) {
continue;
}
}
if (options.filter && !options.filter(response.items[i]
.floData))
if (options.filter && !options.filter(response.items[i].floData))
continue;
filteredData.push(response.items[i].floData);
}
@ -7889,36 +7938,52 @@ Bitcoin.Util = {
});
},
/*searchData: function (obsName, options = {}, dbName = this.defaultDB) {
return new Promise((resolve, reject) => {
this.openDB(dbName).then(db => {
var obs = db.transaction(obsName, "readonly").objectStore(obsName);
var filteredResult = {}
let keyRange;
if(options.lowerKey!==null && options.upperKey!==null)
keyRange = IDBKeyRange.bound(options.lowerKey, options.upperKey);
else if(options.lowerKey!==null)
keyRange = IDBKeyRange.lowerBound(options.lowerKey);
else if (options.upperKey!==null)
keyRange = IDBKeyRange.upperBound(options.upperBound);
else if (options.atKey)
let curReq = obs.openCursor(keyRange, )
}).catch(error => reject(error))
})
},*/
searchData: function (obsName, options = {}, dbName = this.defaultDB) {
options.lowerKey = options.atKey || options.lowerKey || 0
options.upperKey = options.atKey || options.upperKey || false
options.patternEval = options.patternEval || ((k, v) => {
return true
})
options.limit = options.limit || false;
options.lastOnly = options.lastOnly || false
return new Promise((resolve, reject) => {
this.openDB(dbName).then(db => {
var obs = db.transaction(obsName, "readonly").objectStore(obsName);
var filteredResult = {}
let curReq = obs.openCursor(
options.upperKey ? IDBKeyRange.bound(options.lowerKey, options
.upperKey) : IDBKeyRange.lowerBound(options.lowerKey),
options.upperKey ? IDBKeyRange.bound(options.lowerKey, options.upperKey) : IDBKeyRange.lowerBound(options.lowerKey),
options.lastOnly ? "prev" : "next");
curReq.onsuccess = (evt) => {
var cursor = evt.target.result;
if (cursor) {
if (options.patternEval(cursor.primaryKey, cursor.value)) {
filteredResult[cursor.primaryKey] = cursor.value;
options.lastOnly ? resolve(filteredResult) : cursor
.continue();
options.lastOnly ? resolve(filteredResult) : cursor.continue();
} else
cursor.continue();
} else
resolve(filteredResult);
}
curReq.onerror = (evt) => reject(
`Search unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`
);
curReq.onerror = (evt) => reject(`Search unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`);
db.close();
}).catch(error => reject(error));
});