Improved sendTxMultiple and writeDataMultiple
sendTxMultiple:
There are 2 modes (given send amount, preserve ratio)
- Given Send Amount: Uses the given send amount from each sender IDs. Pass the senderPrivKeys as object with amount to invoke this mode
eg. { privateKey1: sendAmt1, privateKey2: sendAmt2...}
- preserveRatio: The ratio of the balance of the senders are maintained. Pass the senderPrivKeys as array of private keys.
eg. [privatekey1, privatekey2....]
*Note: receivers must always be object of floIDs with receive amount.
eg. {receiverID1: receiveAmt1, receiver2: receiveAmt2...}
writeDataMultiple:
There are 2 modes (preserveRatio, equalContribution)
- preserveRatio: preserves the ratio of the balance of senders
- equalContribution: all senders contribute equal amount to the transaction
Note:
*senderPrivKey and receivers are arrays
eq. [privateKey1, privateKey2, ..] and [receiverID1, receiverID2...]
*sends default amount (floGlobals.sendAmt) to each receiver)
*last (4th) parameter is optional:
(default) true: invokes preserveRatio mode
false: invokes equalContibution mode
This commit is contained in:
parent
a569adcb74
commit
1954fc0782
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
<head>
|
<head>
|
||||||
<title>FLO Standard Operators</title>
|
<title>FLO Standard Operators</title>
|
||||||
|
|
||||||
<script id="floGlobals">
|
<script id="floGlobals">
|
||||||
/* Constants for FLO blockchain operations !!Make sure to add this at begining!! */
|
/* Constants for FLO blockchain operations !!Make sure to add this at begining!! */
|
||||||
const floGlobals = {
|
const floGlobals = {
|
||||||
@ -7218,11 +7217,11 @@ Bitcoin.Util = {
|
|||||||
|
|
||||||
//Returns public-key from private-key
|
//Returns public-key from private-key
|
||||||
getPubKeyHex: function (privateKeyHex) {
|
getPubKeyHex: function (privateKeyHex) {
|
||||||
|
if(!privateKeyHex)
|
||||||
|
return null;
|
||||||
var key = new Bitcoin.ECKey(privateKeyHex);
|
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||||
if (key.priv == null) {
|
if (key.priv == null)
|
||||||
console.error("Invalid Private key");
|
return null;
|
||||||
return;
|
|
||||||
}
|
|
||||||
key.setCompressed(true);
|
key.setCompressed(true);
|
||||||
var pubkeyHex = key.getPubKeyHex();
|
var pubkeyHex = key.getPubKeyHex();
|
||||||
return pubkeyHex;
|
return pubkeyHex;
|
||||||
@ -7296,7 +7295,7 @@ Bitcoin.Util = {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} catch {
|
} catch {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -7442,109 +7441,163 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/**Write data into blockchain from (and/or) to multiple floID
|
/**Write data into blockchain from (and/or) to multiple floID
|
||||||
* @param {Object or Array} senders List of sender floIDs with respective private-key (or) Array of sender private-keys
|
* @param {Array} senderPrivKeys List of sender private-keys
|
||||||
* @param {string} data FLO data of the txn
|
* @param {string} data FLO data of the txn
|
||||||
* @param {Array} receivers Array of receivers
|
* @param {Array} receivers List of receivers
|
||||||
* @param {float} sendAmt (optional) amount to be sent to receivers (default value: floGlobals.sendAmt)
|
* @param {float} sendAmt (optional) amount to be sent to receivers (default value: floGlobals.sendAmt)
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
writeDataMulti: function (senders, data, receivers = [floGlobals.adminID], sendAmt = floGlobals.sendAmt) {
|
writeDataMultiple: function (senderPrivKeys, data, receivers = [floGlobals.adminID], preserveRatio = true){
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (Array.isArray(senders)) {
|
if (!Array.isArray(senderPrivKeys))
|
||||||
let tmp = {}
|
return reject("Invalid senderPrivKeys: SenderPrivKeys must be Array")
|
||||||
senders.forEach(key => {
|
if(!preserveRatio){
|
||||||
if (key) {
|
let tmp = {};
|
||||||
let floID = floCrypto.getFloIDfromPubkeyHex(floCrypto.getPubKeyHex(
|
let amount = (floGlobals.sendAmt * receivers.length) / senderPrivKeys.length;
|
||||||
key))
|
senderPrivKeys.forEach(key => tmp[key] = amount);
|
||||||
tmp[floID] = key;
|
senderPrivKeys = tmp
|
||||||
}
|
|
||||||
});
|
|
||||||
senders = tmp;
|
|
||||||
}
|
}
|
||||||
if (!Array.isArray(receivers))
|
if (!Array.isArray(receivers))
|
||||||
return reject("Invalid receivers: Receivers must be Array")
|
return reject("Invalid receivers: Receivers must be Array")
|
||||||
else {
|
else {
|
||||||
let tmp = {}
|
let tmp = {};
|
||||||
receivers.forEach(floID => tmp[floID] = sendAmt)
|
let amount = floGlobals.sendAmt;
|
||||||
receivers = tmp;
|
receivers.forEach(floID => tmp[floID] = amount);
|
||||||
|
receivers = tmp
|
||||||
}
|
}
|
||||||
if (typeof data != "string")
|
if (typeof data != "string")
|
||||||
data = JSON.stringify(data);
|
data = JSON.stringify(data);
|
||||||
this.sendTxMulti(senders, receivers, data)
|
this.sendTxMultiple(senderPrivKeys, receivers, data)
|
||||||
.then(txid => resolve(txid))
|
.then(txid => resolve(txid))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
/**Send Tx from (and/or) to multiple floID
|
/**Send Tx from (and/or) to multiple floID
|
||||||
* @param {Object} senders List of sender floIDs with respective private-key
|
* @param {Array or Object} senderPrivKeys List of sender private-key (optional: with coins to be sent)
|
||||||
* @param {Object} receivers List of receivers with respective amount to be sent
|
* @param {Object} receivers List of receivers with respective amount to be sent
|
||||||
* @param {string} floData FLO data of the txn
|
* @param {string} floData FLO data of the txn
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
sendTxMulti: function (senders, receivers, floData = '') {
|
sendTxMultiple: function (senderPrivKeys, receivers, floData = '') {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let invalids = {
|
|
||||||
InvalidSenderIDs: [],
|
let senders = {}, preserveRatio;
|
||||||
InvalidPrivKeysFor: [],
|
//check for argument validations
|
||||||
InvalidReceiverIDs: [],
|
try{
|
||||||
InvalidAmountFor: [],
|
let invalids = {
|
||||||
InvalidChangeAddress: []
|
InvalidSenderPrivKeys: [],
|
||||||
|
InvalidSenderAmountFor: [],
|
||||||
|
InvalidReceiverIDs: [],
|
||||||
|
InvalidReceiveAmountFor: []
|
||||||
|
}
|
||||||
|
let inputVal = 0, outputVal = 0;
|
||||||
|
//Validate sender privatekeys (and send amount if passed)
|
||||||
|
//conversion when only privateKeys are passed (preserveRatio mode)
|
||||||
|
if(Array.isArray(senderPrivKeys)){
|
||||||
|
senderPrivKeys.forEach(key => {
|
||||||
|
try{
|
||||||
|
if(!key)
|
||||||
|
invalids.InvalidSenderPrivKeys.push(key);
|
||||||
|
else{
|
||||||
|
let floID = floCrypto.getFloIDfromPubkeyHex(floCrypto.getPubKeyHex(key));
|
||||||
|
senders[floID] = {
|
||||||
|
wif: key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch(error){
|
||||||
|
invalids.InvalidSenderPrivKeys.push(key)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
preserveRatio = true;
|
||||||
|
}
|
||||||
|
//conversion when privatekeys are passed with send amount
|
||||||
|
else{
|
||||||
|
for(let key in senderPrivKeys){
|
||||||
|
try{
|
||||||
|
if(!key)
|
||||||
|
invalids.InvalidSenderPrivKeys.push(key);
|
||||||
|
else{
|
||||||
|
if(typeof senderPrivKeys[key] !== 'number' || senderPrivKeys[key] <= 0)
|
||||||
|
invalids.InvalidSenderAmountFor.push(key)
|
||||||
|
else
|
||||||
|
inputVal += senderPrivKeys[key];
|
||||||
|
let floID = floCrypto.getFloIDfromPubkeyHex(floCrypto.getPubKeyHex(key));
|
||||||
|
senders[floID] = {
|
||||||
|
wif: key,
|
||||||
|
coins: senderPrivKeys[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch(error){
|
||||||
|
invalids.InvalidSenderPrivKeys.push(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
preserveRatio = false;
|
||||||
|
}
|
||||||
|
//Validate the receiver IDs and receive amount
|
||||||
|
for (let floID in receivers) {
|
||||||
|
if (!floCrypto.validateAddr(floID))
|
||||||
|
invalids.InvalidReceiverIDs.push(floID)
|
||||||
|
if (typeof receivers[floID] !== 'number' || receivers[floID] <= 0)
|
||||||
|
invalids.InvalidReceiveAmountFor.push(floID)
|
||||||
|
else
|
||||||
|
outputVal += receivers[floID];
|
||||||
|
}
|
||||||
|
//Reject if any invalids are found
|
||||||
|
for (let i in invalids)
|
||||||
|
if (!invalids[i].length)
|
||||||
|
delete invalids[i];
|
||||||
|
if (Object.keys(invalids).length)
|
||||||
|
return reject(invalids);
|
||||||
|
//Reject if given inputVal and outputVal are not equal
|
||||||
|
if(!preserveRatio && inputVal != outputVal)
|
||||||
|
return reject(`Input Amount (${inputVal}) not equal to Output Amount (${outputVal})`)
|
||||||
|
}catch(error){
|
||||||
|
return reject(error)
|
||||||
}
|
}
|
||||||
for (floID in senders) {
|
|
||||||
if (!floCrypto.validateAddr(floID))
|
|
||||||
invalids.InvalidSenderIDs.push(floID)
|
|
||||||
else if (!floCrypto.verifyPrivKey(senders[floID], floID))
|
|
||||||
invalids.InvalidPrivKeysFor.push(floID)
|
|
||||||
}
|
|
||||||
for (floID in receivers) {
|
|
||||||
if (!floCrypto.validateAddr(floID))
|
|
||||||
invalids.InvalidReceiverIDs.push(floID)
|
|
||||||
if (typeof receivers[floID] !== 'number' || receivers[floID] <= 0)
|
|
||||||
invalids.InvalidAmountFor.push(floID)
|
|
||||||
}
|
|
||||||
for (i in invalids)
|
|
||||||
if (!invalids[i].length)
|
|
||||||
delete invalids[i];
|
|
||||||
if (Object.keys(invalids).length)
|
|
||||||
return reject(invalids);
|
|
||||||
//Get balance of senders
|
//Get balance of senders
|
||||||
let promises = []
|
let promises = []
|
||||||
for (floID in senders)
|
for (let floID in senders)
|
||||||
promises.push(this.getBalance(floID))
|
promises.push(this.getBalance(floID))
|
||||||
Promise.all(promises).then(results => {
|
Promise.all(promises).then(results => {
|
||||||
console.log(results)
|
|
||||||
let totalBalance = 0,
|
let totalBalance = 0,
|
||||||
fee = floGlobals.fee,
|
totalFee = floGlobals.fee,
|
||||||
balance = {};
|
balance = {};
|
||||||
invalids.InsufficientBalance = [];
|
//Divide fee among sender if not for preserveRatio
|
||||||
for (floID in senders) {
|
if(!preserveRatio)
|
||||||
|
var dividedFee = totalFee / Object.keys(senders).length;
|
||||||
|
//Check if balance of each sender is sufficient enough
|
||||||
|
let insufficient = [];
|
||||||
|
for (let floID in senders) {
|
||||||
balance[floID] = parseFloat(results.shift());
|
balance[floID] = parseFloat(results.shift());
|
||||||
if (isNaN(balance[floID]) || balance[floID] <= fee)
|
if (isNaN(balance[floID]) || (preserveRatio && balance[floID] <= totalFee) || (!preserveRatio && balance[floID] < senders[floID].coins + dividedFee))
|
||||||
invalids.InsufficientBalance.push(floID)
|
insufficient.push(floID)
|
||||||
totalBalance += balance[floID];
|
totalBalance += balance[floID];
|
||||||
}
|
}
|
||||||
if (invalids.InsufficientBalance.length)
|
if (insufficient.length)
|
||||||
return reject(invalids)
|
return reject({InsufficientBalance: insufficient})
|
||||||
delete invalids.InsufficientBalance;
|
//Calculate totalSentAmount and check if totalBalance is sufficient
|
||||||
console.log(balance);
|
let totalSendAmt = totalFee;
|
||||||
let totalSendAmt = fee;
|
|
||||||
for (floID in receivers)
|
for (floID in receivers)
|
||||||
totalSendAmt += receivers[floID];
|
totalSendAmt += receivers[floID];
|
||||||
if (totalBalance < totalSendAmt)
|
if (totalBalance < totalSendAmt)
|
||||||
return reject("Insufficient total Balance")
|
return reject("Insufficient total Balance")
|
||||||
|
//Get the UTXOs of the senders
|
||||||
let promises = []
|
let promises = []
|
||||||
for (floID in senders)
|
for (floID in senders)
|
||||||
promises.push(this.promisedAPI(`api/addr/${floID}/utxo`))
|
promises.push(this.promisedAPI(`api/addr/${floID}/utxo`))
|
||||||
Promise.all(promises).then(results => {
|
Promise.all(promises).then(results => {
|
||||||
let tmp = {};
|
|
||||||
let wifSeq = [];
|
let wifSeq = [];
|
||||||
var trx = bitjs.transaction();
|
var trx = bitjs.transaction();
|
||||||
for (floID in senders) {
|
for (floID in senders) {
|
||||||
let utxos = results.shift();
|
let utxos = results.shift();
|
||||||
let ratio = (balance[floID] / totalBalance).toFixed(2);
|
let sendAmt;
|
||||||
let sendAmt = totalSendAmt * ratio;
|
if(preserveRatio){
|
||||||
let wif = senders[floID]
|
let ratio = (balance[floID] / totalBalance);
|
||||||
|
sendAmt = totalSendAmt * ratio;
|
||||||
|
} else
|
||||||
|
sendAmt = senders[floID].coins + dividedFee;
|
||||||
|
let wif = senders[floID].wif;
|
||||||
let utxoAmt = 0.0;
|
let utxoAmt = 0.0;
|
||||||
for (let i = utxos.length - 1;
|
for (let i = utxos.length - 1;
|
||||||
(i >= 0) && (utxoAmt < sendAmt); i--) {
|
(i >= 0) && (utxoAmt < sendAmt); i--) {
|
||||||
@ -7557,16 +7610,9 @@ Bitcoin.Util = {
|
|||||||
}
|
}
|
||||||
if (utxoAmt < sendAmt)
|
if (utxoAmt < sendAmt)
|
||||||
return reject("Insufficient balance:" + floID);
|
return reject("Insufficient balance:" + floID);
|
||||||
let change = (utxoAmt - sendAmt).toFixed(5);
|
let change = (utxoAmt - sendAmt);
|
||||||
if (change > 0)
|
if (change > 0)
|
||||||
trx.addoutput(floID, change);
|
trx.addoutput(floID, change);
|
||||||
|
|
||||||
tmp[floID] = {
|
|
||||||
ratio: ratio,
|
|
||||||
sendAmt: sendAmt,
|
|
||||||
utxoAmt: utxoAmt,
|
|
||||||
change: change
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for (floID in receivers)
|
for (floID in receivers)
|
||||||
trx.addoutput(floID, receivers[floID]);
|
trx.addoutput(floID, receivers[floID]);
|
||||||
@ -7574,9 +7620,6 @@ Bitcoin.Util = {
|
|||||||
for (let i = 0; i < wifSeq.length; i++)
|
for (let i = 0; i < wifSeq.length; i++)
|
||||||
trx.signinput(i, wifSeq[i], 1);
|
trx.signinput(i, wifSeq[i], 1);
|
||||||
var signedTxHash = trx.serialize();
|
var signedTxHash = trx.serialize();
|
||||||
console.log(signedTxHash),
|
|
||||||
console.log(trx)
|
|
||||||
console.log(tmp)
|
|
||||||
this.broadcastTx(signedTxHash)
|
this.broadcastTx(signedTxHash)
|
||||||
.then(txid => resolve(txid))
|
.then(txid => resolve(txid))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user