floBlockchainAPI
floBlockchainAPI
object method can be used to send/recieve data to/from blockchain. These functions are asynchronous and return a promise.
NOTE: Most of FLO Blockchain API operations have been promisified. i.e, output needs to be handled using .then and .catch
Example:
floBlockchainAPI.someFunction(parameters)
.then(result => process_resolve(result))
.catch(error => process_reject(error))
Note: for nodejs use require and assign floBlockchainAPI to global
global.floBlockchainAPI = require('/path/to/floBlockchainAPI');
This module uses some default values. They can be overwritten by assigning values floGlobals.
DEFAULT = {
blockchain: 'FLO',
apiURL: {
FLO: ['https://flosight.ranchimall.net/'],
FLO_TEST: ['https://flosight-testnet.ranchimall.net/']
},
sendAmt: 0.0003,
fee: 0.0002,
minChangeAmt: 0.0002,
receiverID: floGlobals.adminID
}
Blockchain to use (FLO
or FLO_TEST
)
Configured using floGlobals.blockchain
Default value: 'FLO'
Default (flosight) API URLs.
Can be overwritten using floGlobals.apiURL
Default send amount (FLO) when amount not specified.
Default value: 0.0003
Can be overwitten using floGlobals.sendAmt
floBlockchainAPI.sendAmt
setter and getter can be used to update or get the default sendAmt value. Example:
floBlockchainAPI.sendAmt = 10;
var amount = floBlockchainAPI.sendAmt;
Default fee amount (FLO).
Default value: 0.0002
Can be overwitten using floGlobals.fee
floBlockchainAPI.fee
setter and getter can be used to update or get the default sendAmt value. Example:
floBlockchainAPI.fee = 0.1;
var fee = floBlockchainAPI.fee;
Minimum change amount required so that change is returned in a transaction.
This is to prevent dust transaction error when broadcasting transactions
Default value: 0.0002
Default receiverID for the transactions
Configured using floGlobals.adminID
Base fetch function that other functions use to request data from floSight API.
floBlockchainAPI.promisedAPI(apicall, query_params)
floBlockchainAPI.fetch(apicall, query_params)
- apicall - API uri
- query_params (optional) - search/query params (GET params)
- Resolves: responseData from the API
floBlockchainAPI.getBalance(addr)
Requests balance for specified FLO address.
- addr - FLO address for which balance has to be retrieved
- Resolves: balance (Number)
floBlockchainAPI.createTx(senderAddr, receiverAddr, sendAmt, floData, strict_utxo)
- senderAddr - FLO address from which the data and amount has to be sent.
- receiverAddr - Receiver FLO address.
- sendAmt - Amount of FLO coins to be sent to receiver.
- floData (optional) - FLO data (string, max 1040 characters) (DEFAULT: '')
- strict_utxo (optional) - ignore or use unconfirmed UTXOs
-
true
: (DEFAULT) ignore unconfirmed UTXOs -
false
: allow unconfirmed UTXOs to be used in tx inputs
-
- Resolves: Transaction Hex (String)
floBlockchainAPI.sendTx(senderAddr, receiverAddr, sendAmt, privKey, floData = '')
Sends a transaction to blockchain, resolves transaction id if the broadcast was succsessful.
- senderAddr - FLO address from which the data and amount has to be sent.
- receiverAddr - Receiver FLO address.
- sendAmt - Amount of FLO coins to be sent to receiver.
- privKey - private-key of sender to verify sender.
- floData (optional) - FLO data (string, max 1040 characters) (DEFAULT: '')
- strict_utxo (optional) - ignore or use unconfirmed UTXOs
-
true
: (DEFAULT) ignore unconfirmed UTXOs -
false
: allow unconfirmed UTXOs to be used in tx inputs
-
- Resolves: TransactionID (String)
floBlockchainAPI.writeData(senderAddr, data, privKey, receiverAddr)
Writes data into blockchain, resolves transaction id if the broadcast was successful.
- senderAddr - FLO address from which the data and amount has to be sent.
- data - FLO data (string, max 1040 characters)
- privKey - private-key of sender
- receiverAddr (optional) - Receiver FLO address. (uses DEFAULT.receiverID if not specified)
- options (optional) - Accepts an object with following options
-
strict_utxo
: same as in floBlockchainAPI.sendTx -
sendAmt
: uses DEFAULT.sendAmt if not specified
-
- Resolves: TransactionID (String)
floBlockchainAPI.mergeUTXOs(floID, privKey, floData = '')
Sends a transaction to blockchain that merges all UTXOs of the given floID.
- floID - FLO address of which the UTXOs should be merged.
- privKey - private-key of the floID.
- floData (optional) - FLO data (string, max 1040 characters) (DEFAULT: '')
- Resolves: TransactionID (String)
floBlockchainAPI.splitUTXOs(floID, privKey, count, floData)
Sends a transaction to blockchain that splits UTXOs into required number of UTXOs. Useful for parallel sending txs without need to wait for previous tx to get confirmation.
- floID - FLO address for spliting UTXOs
- privKey - private-key of the floID.
- count - number of split UTXOs required
- floData (optional) - FLO data (string, max 1040 characters) (DEFAULT: '')
- Resolves: TransactionID (String)
NOTE: Each UTXO split have DEFAULT.sendAmt + DEFAULT.fee
FLO amount in them. example, if count = 3, then 3 utxos will be formed each with value 0.0005 (ie, 0.0003 + 0.0002).
NOTE: Some functions in this category are not promised and directly return values
floBlockchainAPI.signTx(tx, privateKey, sighashtype)
Signs the transaction using given private-key
- tx - (unsigned) transaction hex
- privateKey - private-key for signing
- sighashtype (optional) - sign hash type for signing. (DEFAULT: 1)
- Returns: signed_tx_hex (String)
floBlockchainAPI.checkSigned(tx, bool)
Checks if the given transaction hex is signed or not
- tx - transaction hex to check for signatures
- bool (optional) - return signed detail
-
true
: (DEFAULT) return boolean value for result. (true for signed, false for unsigned or partial signed) -
false
: give detailed sign info for each input
-
- Returns: boolean or array
NOTE: for multisig, unsigned inputs have object {s, r, t} when bool
parameter is false
.
- s: number of signatures present
- r: minimum number of signatures required
- t: total number of signatures possible
floBlockchainAPI.checkIfSameTx(tx1, tx2)
Checks if the given 2 transactions are same (ie, input,output, flodata). Signatures are not considered.
- tx1 - transaction hex to compare
- tx2 - transaction hex to compare
- Returns: boolean
floBlockchainAPI.transactionID(tx)
Calculates the transaction ID from transaction hex
- tx - transaction hex for which txid should be calculated
- Returns: txid (String)
floBlockchainAPI.parseTransaction(tx)
Parse the given transaction hex
- tx - transaction hex to be parsed
- Resolves: (Object) transaction details
floBlockchainAPI.broadcastTx(signedTxHash)
Broadcasts the signed tx hex and resolves txid if successful
- signedTxHash - fully signed transaction hex to be broadcasted
- Resolves: (String) transaction ID
floBlockchainAPI.waitForConfirmation = function (txid, max_retry = -1, retry_timeout = 20)
Wait until the transaction gets confirmation
- txid - transaction ID
- max_retry (optional) - maximum retries until exit wait. negative values = Infinite retries (DEFAULT: -1, ie, Infinite retries).
- retry_timeout (optional) - interval between wait retries (in seconds) (DEFAULT: 20 seconds)
floBlockchainAPI.sendTxMultiple(senderPrivKeys, receivers, floData = '')
Sends a transaction to blockchain from multiple floIDs to multiple floIDs, resolves transaction id if the broadcast was successful.
- senderPrivKeys - Array of Private keys of the senders (or) Object of private keys with sendAmount for each floID (eg: { privateKey1: sendAmt1, privateKey2: sendAmt2...}) . Note: If passed as Array, then ratio of the balance of the senders are preserved. If passed as Object uses the amount given.
- receivers - Object of receiver floIDs with receive amount (eg: {receiverID1: receiveAmt1, receiver2: receiveAmt2...})
- floData (optional) - FLO data (string, max 1040 characters) (DEFAULT: '')
- Resolves: TransactionID (String)
floBlockchainAPI.writeDataMultiple(senderPrivKeys, data, receivers, options)
Writes data into blockchain from multiple floIDs to multiple floIDs, resolves transaction id if the broadcast was successful.
- senderPrivKeys - Array of Private keys of the senders.
- Data - FLO data (string, max 1040 characters)
- receivers (optional) - Array of receiver FLO Addresses. (uses DEFAULT.receiverID if not specified)
- options (optional) - Accepts an object with following options
-
sendAmt
: uses DEFAULT.sendAmt if not specified -
preserveRatio
: boolean- true: (DEFAULT) preserves the ratio of the balance of senders
- false: all senders contribute equal amount to the transaction (optional, default is true)
-
- Resolves: TransactionID (String)
floBlockchainAPI.createMultisigTx(redeemScript, receivers, amounts, floData, strict_utxo)
Creates a unsigned multisig transaction.
- redeemScript - redeem-script of the multisig address
- receivers - array of receiver address
- amounts - array of receiver amounts
- floData (optional) - FLO data (string, max 1040 characters) (DEFAULT: '')
- strict_utxo (optional) - ignore or use unconfirmed UTXOs
-
true
: (DEFAULT) ignore unconfirmed UTXOs -
false
: allow unconfirmed UTXOs to be used in tx inputs
-
- Resolves: Transaction Hex (String)
NOTE: use floBlockchainAPI.signTx
to sign later for broadcasting
floBlockchainAPI.sendMultisigTx(redeemScript, privateKeys, receivers, amounts, floData, strict_utxo)
Creates a signed multisig transaction and broadcast it.
- redeemScript - redeem-script of the multisig address
- privateKeys - array of private keys for the multisig
- receivers - array of receiver address
- amounts - array of receiver amounts
- floData (optional) - FLO data (string, max 1040 characters) (DEFAULT: '')
- strict_utxo (optional) - ignore or use unconfirmed UTXOs
-
true
: (DEFAULT) ignore unconfirmed UTXOs -
false
: allow unconfirmed UTXOs to be used in tx inputs
-
- Resolves: Transaction ID (String)
NOTE: receivers
and amounts
arrays must be of equal length
floBlockchainAPI.writeMultisigData = function (redeemScript, data, privatekeys, receiverAddr = DEFAULT.receiverID, options = {})
Writes data using multisig address and resolves transaction id if the broadcast was successful.
- redeemScript - redeem-script of the multisig address
- data - FLO data (string, max 1040 characters)
- privatekeys - array of private keys for the multisig
- receiverID (optional) - Receiver FLO address. (uses DEFAULT.receiverID if not specified)
- options (optional) - Accepts an object with following options
-
strict_utxo
: same as in floBlockchainAPI.sendTx -
sendAmt
: uses DEFAULT.sendAmt if not specified
-
floBlockchainAPI.getTx(txid)
Requests the details of a given txid
- txid - transaction ID
- Resolves: transaction_details (Object)
floBlockchainAPI.readTxs(addr, options)
readTxs
reads transactions of specified address between from and to.
- addr - FLO address for which the transactions has to be read.
- options - Accepts object with following options
-
after
txid: fetch after given txid -
before
txid: fetch before given txid -
latest
: iftrue
, fetch latest first -
mempool
: query mempool or not. allowed values are-
'true'
,true
: query tx from mempool -
'false'
,false
: ignore tx from mempool -
'only'
: query tx only from mempool
-
-
- Resolves: TransactionList
NOTE: resolves atmost 1000 transactions at a time
floBlockchainAPI.readTxs(addr, options)
Reads all transactions of specified address(newest first).
- addr - FLO address for which the transactions has to be read.
- options - Accepts object with following options
-
after
txid: fetch after given txid -
before
txid: fetch before given txid -
latest
: iftrue
, fetch latest first -
mempool
: query mempool or not. allowed values are-
'true'
,true
: query tx from mempool -
'false'
,false
: ignore tx from mempool -
'only'
: query tx only from mempool
-
-
- Resolves: TransactionData (Object)
floBlockchainAPI.readData(addr, options = {})
readData
reads FLO data from transactions of specified address
- addr - FLO address for which the transactions data has to be read.
- options - Contains options for filter data from transactions.
-
after
: query after the given txid -
before
: query before the given txid -
mempool
: query mempool tx or not (options same as readAllTx) (DEFAULT=false
: ignore unconfirmed tx) -
sentOnly
: filters only sent data -
receivedOnly
: filters only received data -
pattern
: filters data that contains pattern as an object key in the JSON string -
filter
: custom filter funtion for floData (eg . filter: d => {return d[0] == '$'}) -
sender
: filter flo-id(s) of sender -
receiver
: filter flo-id(s) of receiver -
tx
: (boolean) resolve tx data or not-
true
: resolves an Array of Object with tx details (in.items
) -
false
: (DEFAULT) resolves only flo-data (in.data
)
-
-
- Resolves: Object {lastItem, items or data (Array)}
floBlockchainAPI.getLatestData = function (addr, caseFn, options = {})
Get the latest flo-data from the given address satisfying the filter conditions
- addr - FLO address for which the transactions data has to be read.
- caseFn - satisfy case function
- options - Contains options for filter data from transactions.
-
after
: query after the given txid -
before
: query before the given txid -
mempool
: query mempool tx or not (options same as readAllTx) (DEFAULT=false
: ignore unconfirmed tx) -
sentOnly
: filters only sent data -
receivedOnly
: filters only received data -
sender
: filter flo-id(s) of sender -
receiver
: filter flo-id(s) of receiver -
tx
: (boolean) resolve tx data or not-
true
: resolves an Array of Object with tx details (in.items
) -
false
: (DEFAULT) resolves only flo-data (in.data
)
-
-
- Resolves: Object {lastItem, items or data (Array)}