Adding settings and encrypted generalData feature
- floDapps now reads 'settings' from blockchain - improved manageSubAdmins: doesnot send tx if addList and rmList are empty (i.e, no changes) - setApplicationSettings: used to set settings for the application. Note: existing settings are overwritten (ie, not appended) if same key is used. - sendGeneralData now supports a new option (encrypt): Encrypts the message before sending. Note: option value can be boolean true or pubKey of a floID. Passing true will encrypt the data using the default encryptionKey (default 'encryptionKey' must be set in settings). - getNextGeneralData now supports a new option (decrypt): Decrypts the message if possible. Note: option value can be true, privateKey or array of privateKeys. If boolean true is passed, decrypts using myPrivKey (loggedIn privateKey). If privateKey (string) is passed, decrypts using the given privateKey. If array of privateKeys are passed, decrypts the message using the suitable key from the given array. (for example, say messages A, B, C are encrypted for keys X, Y, Z respectively, passing [X, Z] will decrypt messages A and C.
This commit is contained in:
parent
7cbb0908d8
commit
62c190567e
@ -8660,6 +8660,10 @@ Bitcoin.Util = {
|
||||
//send General Data
|
||||
sendGeneralData: function (message, type, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(options.encrypt){
|
||||
let encryptionKey = (options.encrypt === true) ? floGlobals.settings.encryptionKey : options.encrypt
|
||||
message = floCrypto.encryptData(JSON.stringify(message), encryptionKey)
|
||||
}
|
||||
this.sendApplicationData(message, type, options)
|
||||
.then(result => resolve(result))
|
||||
.catch(error => reject(error))
|
||||
@ -8772,6 +8776,7 @@ Bitcoin.Util = {
|
||||
credentials: {},
|
||||
//for Dapps
|
||||
subAdmins: {},
|
||||
settings: {},
|
||||
appObjects: {},
|
||||
vectorClock: {},
|
||||
generalData: {},
|
||||
@ -8844,6 +8849,10 @@ Bitcoin.Util = {
|
||||
.length; k++)
|
||||
compactIDB.writeData("subAdmins", true,
|
||||
content.addSubAdmin[k]);
|
||||
if (content.settings)
|
||||
for (let l in content.settings)
|
||||
compactIDB.writeData("settings", content
|
||||
.settings[l], l)
|
||||
}
|
||||
compactIDB.writeData("lastTx", result.totalTxs,
|
||||
floGlobals.adminID);
|
||||
@ -8964,19 +8973,20 @@ Bitcoin.Util = {
|
||||
})
|
||||
}
|
||||
|
||||
const checkIfPinRequired = function(key){
|
||||
const checkIfPinRequired = function (key) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(key.length == 52)
|
||||
if (key.length == 52)
|
||||
resolve(key)
|
||||
else {
|
||||
inputFn("PIN/Password").then(pwd => {
|
||||
try{
|
||||
try {
|
||||
let privKey = Crypto.AES.decrypt(key, pwd);
|
||||
resolve(privKey)
|
||||
}catch(error){
|
||||
} catch (error) {
|
||||
reject("Access Denied: Incorrect PIN/Password")
|
||||
}
|
||||
}).catch(error => reject("Access Denied: PIN/Password required"))
|
||||
}).catch(error => reject(
|
||||
"Access Denied: PIN/Password required"))
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -8984,12 +8994,12 @@ Bitcoin.Util = {
|
||||
return new Promise((resolve, reject) => {
|
||||
getPrivateKeyCredentials().then(key => {
|
||||
checkIfPinRequired(key).then(privKey => {
|
||||
try{
|
||||
try {
|
||||
myPrivKey = privKey
|
||||
myPubKey = floCrypto.getPubKeyHex(myPrivKey)
|
||||
myFloID = floCrypto.getFloIDfromPubkeyHex(myPubKey)
|
||||
resolve('Login Credentials loaded successful')
|
||||
}catch(error){
|
||||
} catch (error) {
|
||||
reject("Corrupted Private Key")
|
||||
}
|
||||
}).catch(error => reject(error))
|
||||
@ -9060,8 +9070,10 @@ Bitcoin.Util = {
|
||||
|
||||
manageSubAdmins(adminPrivKey, addList, rmList) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!Array.isArray(addList)) addList = undefined;
|
||||
if (!Array.isArray(rmList)) rmList = undefined;
|
||||
if (!Array.isArray(addList) || !addList.length) addList = undefined;
|
||||
if (!Array.isArray(rmList) || !rmList.length) rmList = undefined;
|
||||
if (!addList && !rmList)
|
||||
return reject("subAdmin manage list is empty")
|
||||
var floData = {
|
||||
[floGlobals.application]: {
|
||||
addSubAdmin: addList,
|
||||
@ -9078,6 +9090,25 @@ Bitcoin.Util = {
|
||||
})
|
||||
},
|
||||
|
||||
setApplicationSettings(adminPrivKey, settings = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!settings || typeof settings !== "object")
|
||||
return reject("Settings must be object")
|
||||
var floData = {
|
||||
[floGlobals.application]: {
|
||||
settings: settings
|
||||
}
|
||||
}
|
||||
var floID = floCrypto.getFloIDfromPubkeyHex(floCrypto.getPubKeyHex(adminPrivKey))
|
||||
if (floID != floGlobals.adminID)
|
||||
reject('Access Denied for Admin privilege')
|
||||
else
|
||||
floBlockchainAPI.writeData(floID, JSON.stringify(floData), adminPrivKey)
|
||||
.then(result => resolve(['Updated app settings', result]))
|
||||
.catch(error => reject(error))
|
||||
})
|
||||
},
|
||||
|
||||
clearCredentials: function () {
|
||||
return new Promise((resolve, reject) => {
|
||||
compactIDB.clearData('credentials').then(result => {
|
||||
@ -9088,18 +9119,19 @@ Bitcoin.Util = {
|
||||
})
|
||||
},
|
||||
|
||||
securePrivKey: function(pwd){
|
||||
securePrivKey: function (pwd) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let indexArr = localStorage.getItem(`${floGlobals.application}#privKey`)
|
||||
if(!indexArr)
|
||||
if (!indexArr)
|
||||
return reject("PrivKey not found");
|
||||
indexArr = JSON.parse(indexArr)
|
||||
let encryptedKey = Crypto.AES.encrypt(myPrivKey, pwd);
|
||||
let threshold = indexArr.length;
|
||||
let shares = floCrypto.createShamirsSecretShares(encryptedKey, threshold, threshold)
|
||||
let promises = [];
|
||||
for(var i=0; i<threshold;i++)
|
||||
promises.push(compactIDB.writeData("credentials", shares[i], indexArr[i], floGlobals.application));
|
||||
for (var i = 0; i < threshold; i++)
|
||||
promises.push(compactIDB.writeData("credentials", shares[i], indexArr[i], floGlobals
|
||||
.application));
|
||||
Promise.all(promises)
|
||||
.then(results => resolve("Private Key Secured"))
|
||||
.catch(error => reject(error))
|
||||
@ -9122,7 +9154,25 @@ Bitcoin.Util = {
|
||||
var filteredResult = []
|
||||
for (var i = 0; i < floGlobals.generalData[filter].length; i++)
|
||||
if (floGlobals.generalData[filter][i].vectorClock > vectorClock)
|
||||
filteredResult.push(floGlobals.generalData[filter][i])
|
||||
filteredResult.push(JSON.parse(JSON.stringify(floGlobals.generalData[filter][i])))
|
||||
if (options.decrypt) {
|
||||
let decryptionKey = (options.decrypt === true) ? myPrivKey : options.decrypt;
|
||||
if (!Array.isArray(decryptionKey))
|
||||
decryptionKey = [decryptionKey];
|
||||
filteredResult.forEach(data => {
|
||||
try {
|
||||
if ("secret" in data.message && "senderPublicKeyString" in data.message) {
|
||||
for (let key of decryptionKey) {
|
||||
try {
|
||||
let tmp = floCrypto.decryptData(data.message, key)
|
||||
data.message = JSON.parse(tmp)
|
||||
break;
|
||||
} catch (error) {}
|
||||
}
|
||||
}
|
||||
} catch (error) {}
|
||||
})
|
||||
}
|
||||
return filteredResult
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user