floCrypto v2.3.2: validate any address

floCrypto.validateAddr: validate any blockchain address
- Parameters:
1. address - address to validate
2. std - checks for legacy version (optional, default=true) (true: allow any, array: list of versions, value: one version only, false: allow none)
3. bech - checks for bech version (optional, default=false) (true: allow any, array: list of versions, value: one version only, false: allow none)

- For validating only floID, use floCrypto.validateFloID(floID)
This commit is contained in:
sairajzero 2022-07-19 21:21:06 +05:30
parent fcc5f3ce4e
commit 0a74e393ed
4 changed files with 60 additions and 16 deletions

View File

@ -216,9 +216,17 @@ In addition, we have these system variables outside FLO Globals but used globall
3. isfloID - boolean value (true: compare as flo ID, false: compare as public key) (optional, default is true) 3. isfloID - boolean value (true: compare as flo ID, false: compare as public key) (optional, default is true)
* Returns : boolen (true or false) * Returns : boolen (true or false)
#### Validate Address
floCrypto.validateAddr(address, *std, *bech)
`validateAddr` check if the given Address (any blockchain) is valid or not
1. address - address to validate
2. std - checks for legacy version (optional, default=true) (true: allow any, array: list of versions, value: one version only, false: allow none)
3. bech - checks for bech version (optional, default=false) (true: allow any, array: list of versions, value: one version only, false: allow none) [requires additional library: [btc_api](https://ranchimall.github.io/btc-webwallet/lib_btc.js)]
* Returns : boolen (true or false)
#### Validate FLO ID #### Validate FLO ID
floCrypto.validateAddr(floID) floCrypto.validateFloID(floID)
`validateAddr` check if the given Address is valid or not `validateFloID` check if the given floID is valid or not
1. floID - flo ID to validate 1. floID - flo ID to validate
* Returns : boolen (true or false) * Returns : boolen (true or false)

View File

@ -1,4 +1,4 @@
(function(EXPORTS) { //floBlockchainAPI v2.3.3 (function(EXPORTS) { //floBlockchainAPI v2.3.3a
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/ /* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
'use strict'; 'use strict';
const floBlockchainAPI = EXPORTS; const floBlockchainAPI = EXPORTS;
@ -125,9 +125,9 @@
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!floCrypto.validateASCII(floData)) if (!floCrypto.validateASCII(floData))
return reject("Invalid FLO_Data: only printable ASCII characters are allowed"); return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
else if (!floCrypto.validateAddr(senderAddr)) else if (!floCrypto.validateFloID(senderAddr))
return reject(`Invalid address : ${senderAddr}`); return reject(`Invalid address : ${senderAddr}`);
else if (!floCrypto.validateAddr(receiverAddr)) else if (!floCrypto.validateFloID(receiverAddr))
return reject(`Invalid address : ${receiverAddr}`); return reject(`Invalid address : ${receiverAddr}`);
else if (privKey.length < 1 || !floCrypto.verifyPrivKey(privKey, senderAddr)) else if (privKey.length < 1 || !floCrypto.verifyPrivKey(privKey, senderAddr))
return reject("Invalid Private key!"); return reject("Invalid Private key!");
@ -202,7 +202,7 @@
//merge all UTXOs of a given floID into a single UTXO //merge all UTXOs of a given floID into a single UTXO
floBlockchainAPI.mergeUTXOs = function(floID, privKey, floData = '') { floBlockchainAPI.mergeUTXOs = function(floID, privKey, floData = '') {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!floCrypto.validateAddr(floID)) if (!floCrypto.validateFloID(floID))
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");
@ -326,7 +326,7 @@
} }
//Validate the receiver IDs and receive amount //Validate the receiver IDs and receive amount
for (let floID in receivers) { for (let floID in receivers) {
if (!floCrypto.validateAddr(floID)) if (!floCrypto.validateFloID(floID))
invalids.InvalidReceiverIDs.push(floID); invalids.InvalidReceiverIDs.push(floID);
if (typeof receivers[floID] !== 'number' || receivers[floID] <= 0) if (typeof receivers[floID] !== 'number' || receivers[floID] <= 0)
invalids.InvalidReceiveAmountFor.push(floID); invalids.InvalidReceiveAmountFor.push(floID);

View File

@ -1,4 +1,4 @@
(function(EXPORTS) { //floCrypto v2.3.1 (function(EXPORTS) { //floCrypto v2.3.2
/* FLO Crypto Operators */ /* FLO Crypto Operators */
'use strict'; 'use strict';
const floCrypto = EXPORTS; const floCrypto = EXPORTS;
@ -196,18 +196,54 @@
} }
} }
//Check if the given Address is valid or not //Check if the given flo-id is valid or not
floCrypto.validateFloID = floCrypto.validateAddr = function(inpAddr) { floCrypto.validateFloID = function(floID) {
if (!inpAddr) if (!floID)
return false; return false;
try { try {
let addr = new Bitcoin.Address(inpAddr); let addr = new Bitcoin.Address(floID);
return true; return true;
} catch { } catch {
return false; return false;
} }
} }
//Check if the given address (any blockchain) is valid or not
floCrypto.validateAddr = function(address, std = true, bech = false) {
if (address.length == 34) { //legacy or segwit encoding
if (std === false)
return false;
let decode = bitjs.Base58.decode(address);
var raw = decode.slice(0, decode.length - 4),
checksum = decode.slice(decode.length - 4);
var hash = Crypto.SHA256(Crypto.SHA256(raw, {
asBytes: true
}), {
asBytes: true
});
if (hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3])
return false;
else if (std === true || (!Array.isArray(std) && std === raw[0]) || (Array.isArray(std) && std.includes(raw[0])))
return true;
else
return false;
} else if (address.length == 42 || address.length == 62) { //bech encoding
if (bech === false)
return false;
else if (typeof btc_api !== "object")
throw "btc_api library missing (lib_btc.js)";
let decode = coinjs.bech32_decode(address);
if (!decode)
return false;
var raw = decode.data;
if (bech === true || (!Array.isArray(bech) && bech === raw[0]) || (Array.isArray(bech) && bech.includes(raw[0])))
return true;
else
return false;
} else //unknown length
return false;
}
//Split the str using shamir's Secret and Returns the shares //Split the str using shamir's Secret and Returns the shares
floCrypto.createShamirsSecretShares = function(str, total_shares, threshold_limit) { floCrypto.createShamirsSecretShares = function(str, total_shares, threshold_limit) {
try { try {

View File

@ -1,4 +1,4 @@
(function(EXPORTS) { //floDapps v2.3.1 (function(EXPORTS) { //floDapps v2.3.1a
/* General functions for FLO Dapps*/ /* General functions for FLO Dapps*/
'use strict'; 'use strict';
const floDapps = EXPORTS; const floDapps = EXPORTS;
@ -284,7 +284,7 @@
if (!result) if (!result)
return reject("Empty Private Key") return reject("Empty Private Key")
var floID = floCrypto.getFloID(result) var floID = floCrypto.getFloID(result)
if (!floID || !floCrypto.validateAddr(floID)) if (!floID || !floCrypto.validateFloID(floID))
return reject("Invalid Private Key") return reject("Invalid Private Key")
privKey = result; privKey = result;
}).catch(error => { }).catch(error => {
@ -426,7 +426,7 @@
floDapps.storeContact = function(floID, name) { floDapps.storeContact = function(floID, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!floCrypto.validateAddr(floID)) if (!floCrypto.validateFloID(floID))
return reject("Invalid floID!") return reject("Invalid floID!")
compactIDB.writeData("contacts", name, floID, user.db_name).then(result => { compactIDB.writeData("contacts", name, floID, user.db_name).then(result => {
user.contacts[floID] = name; user.contacts[floID] = name;
@ -439,7 +439,7 @@
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (floID in user.pubKeys) if (floID in user.pubKeys)
return resolve("pubKey already stored") return resolve("pubKey already stored")
if (!floCrypto.validateAddr(floID)) if (!floCrypto.validateFloID(floID))
return reject("Invalid floID!") return reject("Invalid floID!")
if (floCrypto.getFloID(pubKey) != floID) if (floCrypto.getFloID(pubKey) != floID)
return reject("Incorrect pubKey") return reject("Incorrect pubKey")