Compare commits
6 Commits
v3.0.1b-bl
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae69e8e1d7 | ||
|
|
2ac32bf360 | ||
| 0b547f73e9 | |||
|
|
098a62047c | ||
|
|
5be35c28ce | ||
|
|
471f291848 |
149
btcOperator.js
149
btcOperator.js
@ -1,4 +1,4 @@
|
|||||||
(function (EXPORTS) { //btcOperator v1.1.3b
|
(function (EXPORTS) { //btcOperator v1.1.3c
|
||||||
/* BTC Crypto and API Operator */
|
/* BTC Crypto and API Operator */
|
||||||
const btcOperator = EXPORTS;
|
const btcOperator = EXPORTS;
|
||||||
|
|
||||||
@ -92,6 +92,9 @@
|
|||||||
},
|
},
|
||||||
bech32Address: {
|
bech32Address: {
|
||||||
value: key => coinjs.bech32Address(btcOperator.pubkey(key)).address
|
value: key => coinjs.bech32Address(btcOperator.pubkey(key)).address
|
||||||
|
},
|
||||||
|
bech32mAddress: {
|
||||||
|
value: key => segwit_addr.encode("bc", 1, key)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -107,6 +110,8 @@
|
|||||||
return btcOperator.segwitAddress(key) === addr;
|
return btcOperator.segwitAddress(key) === addr;
|
||||||
case "bech32":
|
case "bech32":
|
||||||
return btcOperator.bech32Address(key) === addr;
|
return btcOperator.bech32Address(key) === addr;
|
||||||
|
case "bech32m":
|
||||||
|
return btcOperator.bech32mAddress(key) === addr; // Key is a byte array of 32 bytes
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -116,7 +121,7 @@
|
|||||||
if (!addr)
|
if (!addr)
|
||||||
return undefined;
|
return undefined;
|
||||||
let type = coinjs.addressDecode(addr).type;
|
let type = coinjs.addressDecode(addr).type;
|
||||||
if (["standard", "multisig", "bech32", "multisigBech32"].includes(type))
|
if (["standard", "multisig", "bech32", "multisigBech32", "bech32m"].includes(type))
|
||||||
return type;
|
return type;
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
@ -281,6 +286,7 @@
|
|||||||
BECH32_OUTPUT_SIZE = 23,
|
BECH32_OUTPUT_SIZE = 23,
|
||||||
BECH32_MULTISIG_OUTPUT_SIZE = 34,
|
BECH32_MULTISIG_OUTPUT_SIZE = 34,
|
||||||
SEGWIT_OUTPUT_SIZE = 23;
|
SEGWIT_OUTPUT_SIZE = 23;
|
||||||
|
BECH32M_OUTPUT_SIZE = 35; // Check this later
|
||||||
|
|
||||||
function _redeemScript(addr, key) {
|
function _redeemScript(addr, key) {
|
||||||
let decode = coinjs.addressDecode(addr);
|
let decode = coinjs.addressDecode(addr);
|
||||||
@ -291,10 +297,15 @@
|
|||||||
return key ? coinjs.segwitAddress(btcOperator.pubkey(key)).redeemscript : null;
|
return key ? coinjs.segwitAddress(btcOperator.pubkey(key)).redeemscript : null;
|
||||||
case "bech32":
|
case "bech32":
|
||||||
return decode.redeemscript;
|
return decode.redeemscript;
|
||||||
|
case "'multisigBech32":
|
||||||
|
return decode.redeemscript; //Multisig-edit-fee-change1
|
||||||
|
case "bech32m":
|
||||||
|
return decode.outstring; //Maybe the redeemscript will come when input processing happens for bech32m
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
btcOperator._redeemScript = _redeemScript;
|
||||||
|
|
||||||
function _sizePerInput(addr, rs) {
|
function _sizePerInput(addr, rs) {
|
||||||
switch (coinjs.addressDecode(addr).type) {
|
switch (coinjs.addressDecode(addr).type) {
|
||||||
@ -328,6 +339,8 @@
|
|||||||
return BASE_OUTPUT_SIZE + BECH32_MULTISIG_OUTPUT_SIZE;
|
return BASE_OUTPUT_SIZE + BECH32_MULTISIG_OUTPUT_SIZE;
|
||||||
case "multisig":
|
case "multisig":
|
||||||
return BASE_OUTPUT_SIZE + SEGWIT_OUTPUT_SIZE;
|
return BASE_OUTPUT_SIZE + SEGWIT_OUTPUT_SIZE;
|
||||||
|
case "bech32m":
|
||||||
|
return BASE_OUTPUT_SIZE + BECH32M_OUTPUT_SIZE;
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -379,6 +392,7 @@
|
|||||||
//return
|
//return
|
||||||
return parameters;
|
return parameters;
|
||||||
}
|
}
|
||||||
|
btcOperator.validateTxParameters = validateTxParameters;
|
||||||
|
|
||||||
function createTransaction(senders, redeemScripts, receivers, amounts, fee, change_address, fee_from_receiver) {
|
function createTransaction(senders, redeemScripts, receivers, amounts, fee, change_address, fee_from_receiver) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -417,6 +431,7 @@
|
|||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
btcOperator.createTransaction = createTransaction;
|
||||||
|
|
||||||
function addInputs(tx, senders, redeemScripts, total_amount, fee, output_size, fee_from_receiver) {
|
function addInputs(tx, senders, redeemScripts, total_amount, fee, output_size, fee_from_receiver) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -441,6 +456,7 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
btcOperator.addInputs = addInputs;
|
||||||
|
|
||||||
function addUTXOs(tx, senders, redeemScripts, required_amount, fee_rate, rec_args = {}) {
|
function addUTXOs(tx, senders, redeemScripts, required_amount, fee_rate, rec_args = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -495,6 +511,7 @@
|
|||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
btcOperator.addUTXOs = addUTXOs;
|
||||||
|
|
||||||
function addOutputs(tx, receivers, amounts, change_address) {
|
function addOutputs(tx, receivers, amounts, change_address) {
|
||||||
let size = 0;
|
let size = 0;
|
||||||
@ -506,6 +523,7 @@
|
|||||||
size += _sizePerOutput(change_address);
|
size += _sizePerOutput(change_address);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
btcOperator.addOutputs = addOutputs;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
function autoFeeCalc(tx) {
|
function autoFeeCalc(tx) {
|
||||||
@ -553,7 +571,19 @@
|
|||||||
} else resolve(deserializeTx(tx));
|
} else resolve(deserializeTx(tx));
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
btcOperator.tx_fetch_for_editing = tx_fetch_for_editing;
|
||||||
|
|
||||||
|
const extractLastHexStrings = btcOperator.extractLastHexStrings = function (arr) {
|
||||||
|
const result = [];
|
||||||
|
for (let i = 0; i < arr.length; i++) {
|
||||||
|
const innerArray = arr[i];
|
||||||
|
if (innerArray.length > 0) {
|
||||||
|
const lastHexString = innerArray[innerArray.length - 1];
|
||||||
|
result.push(lastHexString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
btcOperator.editFee = function (tx_hex, new_fee, private_keys, change_only = true) {
|
btcOperator.editFee = function (tx_hex, new_fee, private_keys, change_only = true) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -644,10 +674,119 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
btcOperator.editFee_corewallet = function(tx_hex, new_fee, private_keys, change_only = true) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!Array.isArray(private_keys))
|
||||||
|
private_keys = [private_keys];
|
||||||
|
tx_fetch_for_editing(tx_hex).then(tx => {
|
||||||
|
parseTransaction(tx).then(tx_parsed => {
|
||||||
|
if (tx_parsed.fee >= new_fee)
|
||||||
|
return reject("Fees can only be increased");
|
||||||
|
|
||||||
|
//editable addresses in output values (for fee increase)
|
||||||
|
var edit_output_address = new Set();
|
||||||
|
if (change_only === true) //allow only change values (ie, sender address) to be edited to inc fee
|
||||||
|
tx_parsed.inputs.forEach(inp => edit_output_address.add(inp.address));
|
||||||
|
else if (change_only === false) //allow all output values to be edited
|
||||||
|
tx_parsed.outputs.forEach(out => edit_output_address.add(out.address));
|
||||||
|
else if (typeof change_only == 'string') // allow only given receiver id output to be edited
|
||||||
|
edit_output_address.add(change_only);
|
||||||
|
else if (Array.isArray(change_only)) //allow only given set of receiver id outputs to be edited
|
||||||
|
change_only.forEach(id => edit_output_address.add(id));
|
||||||
|
|
||||||
|
//edit output values to increase fee
|
||||||
|
let inc_fee = util.BTC_to_Sat(new_fee - tx_parsed.fee);
|
||||||
|
if (inc_fee < MIN_FEE_UPDATE)
|
||||||
|
return reject(`Insufficient additional fee. Minimum increment: ${MIN_FEE_UPDATE}`);
|
||||||
|
for (let i = tx.outs.length - 1; i >= 0 && inc_fee > 0; i--) //reduce in reverse order
|
||||||
|
if (edit_output_address.has(tx_parsed.outputs[i].address)) {
|
||||||
|
let current_value = tx.outs[i].value;
|
||||||
|
if (current_value instanceof BigInteger) //convert BigInteger class to inv value
|
||||||
|
current_value = current_value.intValue();
|
||||||
|
//edit the value as required
|
||||||
|
if (current_value > inc_fee) {
|
||||||
|
tx.outs[i].value = current_value - inc_fee;
|
||||||
|
inc_fee = 0;
|
||||||
|
} else {
|
||||||
|
inc_fee -= current_value;
|
||||||
|
tx.outs[i].value = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (inc_fee > 0) {
|
||||||
|
let max_possible_fee = util.BTC_to_Sat(new_fee) - inc_fee; //in satoshi
|
||||||
|
return reject(`Insufficient output values to increase fee. Maximum fee possible: ${util.Sat_to_BTC(max_possible_fee)}`);
|
||||||
|
}
|
||||||
|
tx.outs = tx.outs.filter(o => o.value >= DUST_AMT); //remove all output with value less than DUST amount
|
||||||
|
|
||||||
|
//remove existing signatures and reset the scripts
|
||||||
|
let wif_keys = [];
|
||||||
|
let witness_position = 0;
|
||||||
|
for (let i in tx.ins) {
|
||||||
|
var addr = tx_parsed.inputs[i].address,
|
||||||
|
value = util.BTC_to_Sat(tx_parsed.inputs[i].value);
|
||||||
|
let addr_decode = coinjs.addressDecode(addr);
|
||||||
|
|
||||||
|
//find the correct key for addr
|
||||||
|
var privKey = private_keys.find(pk => verifyKey(addr, pk));
|
||||||
|
if (!privKey)
|
||||||
|
return reject(`Private key missing for ${addr}`);
|
||||||
|
//find redeemScript (if any)
|
||||||
|
const rs = _redeemScript(addr, privKey);
|
||||||
|
rs === false ? wif_keys.unshift(privKey) : wif_keys.push(privKey); //sorting private-keys (wif)
|
||||||
|
//reset the script for re-signing
|
||||||
|
var script;
|
||||||
|
if (!rs || !rs.length) {
|
||||||
|
//legacy script (derive from address)
|
||||||
|
let s = coinjs.script();
|
||||||
|
s.writeOp(118); //OP_DUP
|
||||||
|
s.writeOp(169); //OP_HASH160
|
||||||
|
s.writeBytes(addr_decode.bytes);
|
||||||
|
s.writeOp(136); //OP_EQUALVERIFY
|
||||||
|
s.writeOp(172); //OP_CHECKSIG
|
||||||
|
script = Crypto.util.bytesToHex(s.buffer);
|
||||||
|
} else if (((rs.match(/^00/) && rs.length == 44)) || (rs.length == 40 && rs.match(/^[a-f0-9]+$/gi))) {
|
||||||
|
//redeemScript for segwit/bech32
|
||||||
|
let s = coinjs.script();
|
||||||
|
s.writeBytes(Crypto.util.hexToBytes(rs));
|
||||||
|
s.writeOp(0);
|
||||||
|
s.writeBytes(coinjs.numToBytes(value.toFixed(0), 8));
|
||||||
|
script = Crypto.util.bytesToHex(s.buffer);
|
||||||
|
if (addr_decode == "bech32") {witness_position = witness_position + 1;} //bech32 has witness
|
||||||
|
} else if (addr_decode.type === 'multisigBech32') {
|
||||||
|
var rs_array = [];
|
||||||
|
rs_array = btcOperator.extractLastHexStrings(tx.witness);
|
||||||
|
let redeemScript = rs_array[witness_position];
|
||||||
|
witness_position = witness_position + 1;
|
||||||
|
|
||||||
|
//redeemScript multisig (bech32)
|
||||||
|
let s = coinjs.script();
|
||||||
|
s.writeBytes(Crypto.util.hexToBytes(redeemScript));
|
||||||
|
s.writeOp(0);
|
||||||
|
s.writeBytes(coinjs.numToBytes(value.toFixed(0), 8));
|
||||||
|
script = Crypto.util.bytesToHex(s.buffer);
|
||||||
|
} else //redeemScript for multisig (segwit)
|
||||||
|
script = rs;
|
||||||
|
tx.ins[i].script = coinjs.script(script);
|
||||||
|
}
|
||||||
|
tx.witness = false; //remove all witness signatures
|
||||||
|
console.debug("Unsigned:", tx.serialize());
|
||||||
|
//re-sign the transaction
|
||||||
|
new Set(wif_keys).forEach(key => tx.sign(key, 1 /*sighashtype*/ )); //Sign the tx using private key WIF
|
||||||
|
if (btcOperator.checkSigned(tx)) {
|
||||||
|
resolve(tx.serialize());
|
||||||
|
} else {
|
||||||
|
reject("All private keys not present");
|
||||||
|
}
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
btcOperator.sendTx = function (senders, privkeys, receivers, amounts, fee = null, options = {}) {
|
btcOperator.sendTx = function (senders, privkeys, receivers, amounts, fee = null, options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
createSignedTx(senders, privkeys, receivers, amounts, fee, options).then(result => {
|
createSignedTx(senders, privkeys, receivers, amounts, fee, options).then(result => {
|
||||||
debugger;
|
// debugger;
|
||||||
broadcastTx(result.transaction.serialize())
|
broadcastTx(result.transaction.serialize())
|
||||||
.then(txid => resolve(txid))
|
.then(txid => resolve(txid))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error));
|
||||||
@ -760,7 +899,7 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function deserializeTx(tx) {
|
const deserializeTx = btcOperator.deserializeTx = function (tx) {
|
||||||
if (typeof tx === 'string' || Array.isArray(tx)) {
|
if (typeof tx === 'string' || Array.isArray(tx)) {
|
||||||
try {
|
try {
|
||||||
tx = coinjs.transaction().deserialize(tx);
|
tx = coinjs.transaction().deserialize(tx);
|
||||||
@ -905,7 +1044,7 @@
|
|||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
});
|
});
|
||||||
|
|
||||||
getTx.hex = txid => new Promise((resolve, reject) => {
|
getTx.hex = btcOperator.getTx.hex = txid => new Promise((resolve, reject) => {
|
||||||
fetch_api(`rawtx/${txid}?format=hex`, false)
|
fetch_api(`rawtx/${txid}?format=hex`, false)
|
||||||
.then(result => resolve(result))
|
.then(result => resolve(result))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
blockchain: floGlobals.blockchain,
|
blockchain: floGlobals.blockchain,
|
||||||
apiURL: {
|
apiURL: {
|
||||||
FLO: ['https://blockbook.ranchimall.net/'],
|
FLO: ['https://blockbook.ranchimall.net/'],
|
||||||
FLO_TEST: []
|
FLO_TEST: ['https://blockbook-testnet.ranchimall.net/']
|
||||||
},
|
},
|
||||||
sendAmt: 0.0003,
|
sendAmt: 0.0003,
|
||||||
fee: 0.0002,
|
fee: 0.0002,
|
||||||
@ -1041,4 +1041,4 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
})('object' === typeof module ? module.exports : window.floBlockchainAPI = {});
|
})('object' === typeof module ? module.exports : window.floBlockchainAPI = {});
|
||||||
|
|||||||
125
floCloudAPI.js
125
floCloudAPI.js
@ -1,4 +1,4 @@
|
|||||||
(function (EXPORTS) { //floCloudAPI v2.4.3a
|
(function (EXPORTS) { //floCloudAPI v2.4.5
|
||||||
/* FLO Cloud operations to send/request application data*/
|
/* FLO Cloud operations to send/request application data*/
|
||||||
'use strict';
|
'use strict';
|
||||||
const floCloudAPI = EXPORTS;
|
const floCloudAPI = EXPORTS;
|
||||||
@ -609,49 +609,39 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
/*(NEEDS UPDATE)
|
//edit comment of data in supernode cloud (sender only)
|
||||||
//edit comment of data in supernode cloud (mutable comments only)
|
floCloudAPI.editApplicationData = function (vectorClock, comment_edit, options = {}) {
|
||||||
floCloudAPI.editApplicationData = function(vectorClock, newComment, oldData, options = {}) {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let p0
|
//request the data from cloud for resigning
|
||||||
if (!oldData) {
|
let req_options = Object.assign({}, options);
|
||||||
options.atVectorClock = vectorClock;
|
req_options.atVectorClock = vectorClock;
|
||||||
options.callback = false;
|
requestApplicationData(undefined, req_options).then(result => {
|
||||||
p0 = requestApplicationData(false, options)
|
if (!result.length)
|
||||||
} else
|
return reject("Data not found");
|
||||||
p0 = Promise.resolve({
|
let data = result[0];
|
||||||
vectorClock: {
|
if (data.senderID !== user.id)
|
||||||
...oldData
|
return reject("Only sender can edit comment");
|
||||||
}
|
data.comment = comment_edit;
|
||||||
})
|
let hashcontent = ["receiverID", "time", "application", "type", "message", "comment"]
|
||||||
p0.then(d => {
|
.map(d => data[d]).join("|");
|
||||||
if (d.senderID != user.id)
|
let re_sign = user.sign(hashcontent);
|
||||||
return reject("Invalid requestorID")
|
var request = {
|
||||||
else if (!d.comment.startsWith("EDIT:"))
|
receiverID: options.receiverID || DEFAULT.adminID,
|
||||||
return reject("Data immutable")
|
|
||||||
let data = {
|
|
||||||
requestorID: user.id,
|
requestorID: user.id,
|
||||||
receiverID: d.receiverID,
|
pubKey: user.public,
|
||||||
time: Date.now(),
|
time: Date.now(),
|
||||||
application: d.application,
|
vectorClock: vectorClock,
|
||||||
edit: {
|
edit: comment_edit,
|
||||||
vectorClock: vectorClock,
|
re_sign: re_sign
|
||||||
comment: newComment
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
d.comment = data.edit.comment;
|
let request_hash = ["time", "vectorClock", "edit", "re_sign"].map(d => request[d]).join("|");
|
||||||
let hashcontent = ["receiverID", "time", "application", "type", "message",
|
request.sign = user.sign(request_hash);
|
||||||
"comment"
|
singleRequest(request.receiverID, request)
|
||||||
]
|
.then(result => resolve(result))
|
||||||
.map(x => d[x]).join("|")
|
|
||||||
data.edit.sign = user.sign(hashcontent)
|
|
||||||
singleRequest(data.receiverID, data)
|
|
||||||
.then(result => resolve("Data comment updated"))
|
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
})
|
}).catch(error => reject(error))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
//tag data in supernode cloud (subAdmin access only)
|
//tag data in supernode cloud (subAdmin access only)
|
||||||
floCloudAPI.tagApplicationData = function (vectorClock, tag, options = {}) {
|
floCloudAPI.tagApplicationData = function (vectorClock, tag, options = {}) {
|
||||||
@ -811,6 +801,67 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//upload file
|
||||||
|
floCloudAPI.uploadFile = function (fileBlob, type, options = {}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!(fileBlob instanceof File) && !(fileBlob instanceof Blob))
|
||||||
|
return reject("file must be instance of File/Blob");
|
||||||
|
fileBlob.arrayBuffer().then(arraybuf => {
|
||||||
|
let file_data = { type: fileBlob.type, name: fileBlob.name };
|
||||||
|
file_data.content = Crypto.util.bytesToBase64(new Uint8Array(arraybuf));
|
||||||
|
if (options.encrypt) {
|
||||||
|
let encryptionKey = options.encrypt === true ?
|
||||||
|
floGlobals.settings.encryptionKey : options.encrypt
|
||||||
|
file_data = floCrypto.encryptData(JSON.stringify(file_data), encryptionKey)
|
||||||
|
}
|
||||||
|
sendApplicationData(file_data, type, options)
|
||||||
|
.then(({ vectorClock, receiverID, type, application }) => resolve({ vectorClock, receiverID, type, application }))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//download file
|
||||||
|
floCloudAPI.downloadFile = function (vectorClock, options = {}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
options.atVectorClock = vectorClock;
|
||||||
|
requestApplicationData(options.type, options).then(result => {
|
||||||
|
if (!result.length)
|
||||||
|
return reject("File not found");
|
||||||
|
result = result[0];
|
||||||
|
try {
|
||||||
|
let file_data = decodeMessage(result.message);
|
||||||
|
//file is encrypted: decryption required
|
||||||
|
if (file_data instanceof Object && "secret" in file_data) {
|
||||||
|
if (!options.decrypt)
|
||||||
|
return reject("Data is encrypted");
|
||||||
|
let decryptionKey = (options.decrypt === true) ? Crypto.AES.decrypt(user_private, aes_key) : options.decrypt;
|
||||||
|
if (!Array.isArray(decryptionKey))
|
||||||
|
decryptionKey = [decryptionKey];
|
||||||
|
let flag = false;
|
||||||
|
for (let key of decryptionKey) {
|
||||||
|
try {
|
||||||
|
let tmp = floCrypto.decryptData(file_data, key);
|
||||||
|
file_data = JSON.parse(tmp);
|
||||||
|
flag = true;
|
||||||
|
break;
|
||||||
|
} catch (error) { }
|
||||||
|
}
|
||||||
|
if (!flag)
|
||||||
|
return reject("Unable to decrypt file: Invalid private key");
|
||||||
|
}
|
||||||
|
//reconstruct the file
|
||||||
|
let arraybuf = new Uint8Array(Crypto.util.base64ToBytes(file_data.content))
|
||||||
|
result.file = new File([arraybuf], file_data.name, { type: file_data.type });
|
||||||
|
resolve(result)
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
reject("Data is not a file");
|
||||||
|
}
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Functions:
|
Functions:
|
||||||
findDiff(original, updatedObj) returns an object with the added, deleted and updated differences
|
findDiff(original, updatedObj) returns an object with the added, deleted and updated differences
|
||||||
|
|||||||
52
floDapps.js
52
floDapps.js
@ -1,4 +1,4 @@
|
|||||||
(function (EXPORTS) { //floDapps v2.4.0
|
(function (EXPORTS) { //floDapps v2.4.1
|
||||||
/* General functions for FLO Dapps*/
|
/* General functions for FLO Dapps*/
|
||||||
'use strict';
|
'use strict';
|
||||||
const floDapps = EXPORTS;
|
const floDapps = EXPORTS;
|
||||||
@ -172,12 +172,7 @@
|
|||||||
//general
|
//general
|
||||||
lastTx: {},
|
lastTx: {},
|
||||||
//supernode (cloud list)
|
//supernode (cloud list)
|
||||||
supernodes: {
|
supernodes: {}
|
||||||
indexes: {
|
|
||||||
uri: null,
|
|
||||||
pubKey: null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var obs_a = {
|
var obs_a = {
|
||||||
//login credentials
|
//login credentials
|
||||||
@ -257,7 +252,8 @@
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!startUpOptions.cloud)
|
if (!startUpOptions.cloud)
|
||||||
return resolve("No cloud for this app");
|
return resolve("No cloud for this app");
|
||||||
compactIDB.readData("lastTx", floCloudAPI.SNStorageID, DEFAULT.root).then(lastTx => {
|
const CLOUD_KEY = "floCloudAPI#" + floCloudAPI.SNStorageID;
|
||||||
|
compactIDB.readData("lastTx", CLOUD_KEY, DEFAULT.root).then(lastTx => {
|
||||||
var query_options = { sentOnly: true, pattern: floCloudAPI.SNStorageName };
|
var query_options = { sentOnly: true, pattern: floCloudAPI.SNStorageName };
|
||||||
if (typeof lastTx == 'number') //lastTx is tx count (*backward support)
|
if (typeof lastTx == 'number') //lastTx is tx count (*backward support)
|
||||||
query_options.ignoreOld = lastTx;
|
query_options.ignoreOld = lastTx;
|
||||||
@ -265,25 +261,27 @@
|
|||||||
query_options.after = lastTx;
|
query_options.after = lastTx;
|
||||||
//fetch data from flosight
|
//fetch data from flosight
|
||||||
floBlockchainAPI.readData(floCloudAPI.SNStorageID, query_options).then(result => {
|
floBlockchainAPI.readData(floCloudAPI.SNStorageID, query_options).then(result => {
|
||||||
for (var i = result.data.length - 1; i >= 0; i--) {
|
compactIDB.readData("supernodes", CLOUD_KEY, DEFAULT.root).then(nodes => {
|
||||||
var content = JSON.parse(result.data[i])[floCloudAPI.SNStorageName];
|
nodes = nodes || {};
|
||||||
for (let sn in content.removeNodes)
|
for (var i = result.data.length - 1; i >= 0; i--) {
|
||||||
compactIDB.removeData("supernodes", sn, DEFAULT.root);
|
var content = JSON.parse(result.data[i])[floCloudAPI.SNStorageName];
|
||||||
for (let sn in content.newNodes)
|
for (let sn in content.removeNodes)
|
||||||
compactIDB.writeData("supernodes", content.newNodes[sn], sn, DEFAULT.root);
|
delete nodes[sn];
|
||||||
for (let sn in content.updateNodes)
|
for (let sn in content.newNodes)
|
||||||
compactIDB.readData("supernodes", sn, DEFAULT.root).then(r => {
|
nodes[sn] = content.newNodes[sn];
|
||||||
r = r || {}
|
for (let sn in content.updateNodes)
|
||||||
r.uri = content.updateNodes[sn];
|
if (sn in nodes) //check if node is listed
|
||||||
compactIDB.writeData("supernodes", r, sn, DEFAULT.root);
|
nodes[sn].uri = content.updateNodes[sn];
|
||||||
});
|
}
|
||||||
}
|
Promise.all([
|
||||||
compactIDB.writeData("lastTx", result.lastItem, floCloudAPI.SNStorageID, DEFAULT.root);
|
compactIDB.writeData("lastTx", result.lastItem, CLOUD_KEY, DEFAULT.root),
|
||||||
compactIDB.readAllData("supernodes", DEFAULT.root).then(nodes => {
|
compactIDB.writeData("supernodes", nodes, CLOUD_KEY, DEFAULT.root)
|
||||||
floCloudAPI.init(nodes)
|
]).then(_ => {
|
||||||
.then(result => resolve("Loaded Supernode list\n" + result))
|
floCloudAPI.init(nodes)
|
||||||
.catch(error => reject(error))
|
.then(result => resolve("Loaded Supernode list\n" + result))
|
||||||
})
|
.catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
})
|
})
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
})
|
})
|
||||||
|
|||||||
@ -6,9 +6,9 @@
|
|||||||
<script id="floGlobals">
|
<script id="floGlobals">
|
||||||
/* Constants for FLO blockchain operations !!Make sure to add this at beginning!! */
|
/* Constants for FLO blockchain operations !!Make sure to add this at beginning!! */
|
||||||
const floGlobals = {
|
const floGlobals = {
|
||||||
blockchain: "FLO",
|
blockchain: "FLO_TEST",
|
||||||
adminID: "FKAEdnPfjXLHSYwrXQu377ugN4tXU7VGdf",
|
adminID: "oKKHdK5uYAJ52U91sYsWhnEaEAAhZP779B",
|
||||||
application: "TEST_MODE",
|
application: "TEST_MODE_testnet",
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
@ -54,4 +54,4 @@
|
|||||||
(use console)
|
(use console)
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user