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:
parent
836f359cf2
commit
05f110661e
@ -6955,7 +6955,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 = {
|
||||||
|
|
||||||
@ -6964,6 +6964,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"))
|
||||||
},
|
},
|
||||||
@ -6989,9 +6991,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
|
||||||
@ -7008,20 +7009,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) {
|
||||||
@ -7029,14 +7025,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) {
|
||||||
@ -7241,10 +7231,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 = {
|
||||||
|
|
||||||
@ -7316,6 +7364,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))
|
||||||
@ -7362,6 +7412,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;
|
||||||
@ -7425,7 +7477,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
|
||||||
@ -7643,8 +7696,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;
|
||||||
@ -7655,22 +7708,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);
|
||||||
}
|
}
|
||||||
@ -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) {
|
searchData: function (obsName, options = {}, dbName = this.defaultDB) {
|
||||||
options.lowerKey = options.atKey || options.lowerKey || 0
|
options.lowerKey = options.atKey || options.lowerKey || 0
|
||||||
options.upperKey = options.atKey || options.upperKey || false
|
options.upperKey = options.atKey || options.upperKey || false
|
||||||
options.patternEval = options.patternEval || ((k, v) => {
|
options.patternEval = options.patternEval || ((k, v) => {
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
options.limit = options.limit || false;
|
||||||
options.lastOnly = options.lastOnly || false
|
options.lastOnly = options.lastOnly || false
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.openDB(dbName).then(db => {
|
this.openDB(dbName).then(db => {
|
||||||
var obs = db.transaction(obsName, "readonly").objectStore(obsName);
|
var obs = db.transaction(obsName, "readonly").objectStore(obsName);
|
||||||
var filteredResult = {}
|
var filteredResult = {}
|
||||||
let curReq = obs.openCursor(
|
let curReq = obs.openCursor(
|
||||||
options.upperKey ? IDBKeyRange.bound(options.lowerKey, options
|
options.upperKey ? IDBKeyRange.bound(options.lowerKey, options.upperKey) : IDBKeyRange.lowerBound(options.lowerKey),
|
||||||
.upperKey) : IDBKeyRange.lowerBound(options.lowerKey),
|
|
||||||
options.lastOnly ? "prev" : "next");
|
options.lastOnly ? "prev" : "next");
|
||||||
curReq.onsuccess = (evt) => {
|
curReq.onsuccess = (evt) => {
|
||||||
var cursor = evt.target.result;
|
var cursor = evt.target.result;
|
||||||
if (cursor) {
|
if (cursor) {
|
||||||
if (options.patternEval(cursor.primaryKey, cursor.value)) {
|
if (options.patternEval(cursor.primaryKey, cursor.value)) {
|
||||||
filteredResult[cursor.primaryKey] = cursor.value;
|
filteredResult[cursor.primaryKey] = cursor.value;
|
||||||
options.lastOnly ? resolve(filteredResult) : cursor
|
options.lastOnly ? resolve(filteredResult) : cursor.continue();
|
||||||
.continue();
|
|
||||||
} else
|
} else
|
||||||
cursor.continue();
|
cursor.continue();
|
||||||
} else
|
} else
|
||||||
resolve(filteredResult);
|
resolve(filteredResult);
|
||||||
}
|
}
|
||||||
curReq.onerror = (evt) => reject(
|
curReq.onerror = (evt) => reject(`Search unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`);
|
||||||
`Search unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`
|
|
||||||
);
|
|
||||||
db.close();
|
db.close();
|
||||||
}).catch(error => reject(error));
|
}).catch(error => reject(error));
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user