19 KiB
Standard_Operations
Standard operations required for FLO Crypto, Blockchain API, Supernode WS, IndexedDB
This template contains standard operations that can be used for the following:
- FLO Globals
- FLO Crypto Operations
- FLO Blockchain API Operations
- FLO SuperNode Websocket Operations
- Compact IndexedDB Operations
- FLO Cloud API Operations
- FLO Decentralized app (Dapp) module
FLO Globals
floGlobals object contains the global variables and constants required for the operations. Make sure to add this object before any other scripts.
floGlobals contains the following properties :
blockchain: Indicates the blockchain ("FLO"or"FLO_TEST").apiURL: Indicates the URL for blockchain API calls.adminID: Indicates the master admin FLO ID for the project/application.application: Name of the project/application.sendAmt: Indicates the default flo amount to be sent while sending transactions into the blockchain.fee: Indicates the default fee amount to be deduced while sending transactions into the blockchain.SNStorageID* : SuperNode Storage adminID.supernodes⁺ : List of supernodes.subAdmins⁺ : subAdmins of the application.appData⁺ : Application data for the app (data sent by subAdmins).generalData⁺: General Data for the app (data sent by any user).vectorClock⁺ andgeneralVC⁺ : vectorclocks for application data and general data respectively.
* Values that shoult NOT be edited. ⁺ Values that updates in runtime. 1-8 : Required for all applications. 9-12: Required for applications based on floClouldAPI and floDapps module.
FLO Crypto Operations
floCrypto operations can be used to perform blockchain-cryptography methods. floCrypto operations are synchronized and return a value. Contains the following Operations.
Generate New FLO ID pair
floCrypto.generateNewID()
generateNewID generates a new flo ID and returns private-key, public-key and floID
- Returns : Object { privKey , pubKey, floID }
Calculate Public Key Hex
floCrypto.getPubKeyHex(privateKey)
getPubKeyHex returns public-key from given private-key
- privateKey - private key in WIF format (Hex)
- Returns : pubKey (string)
Calculate FLO ID
floCrypto.getFloIDfromPubkeyHex(publicKey)
getFloIDfromPubkeyHex returns flo-ID from public-key
- publicKey - public key hex value
- Returns : floID (string)
Verify Private Key
floCrypto.verifyPrivKey(privateKey, pubKey_floID, *isfloID)
verifyPrivKey verify the private-key for the given public-key or flo-ID
- privateKey - private key in WIF format (Hex)
- pubKey_floID - public Key or flo ID
- isfloID - boolean value (true: compare as flo ID, false: compare as public key) (optional, default is true)
- Returns : boolen (true or false)
Validate FLO ID
floCrypto.validateAddr(floID)
validateAddr check if the given Address is valid or not
- floID - flo ID to validate
- Returns : boolen (true or false)
Data Encryption
floCrypto.encryptData(data, publicKey)
encryptData encrypts the given data using public-key
- data - data to encrypt (String)
- publicKey - public key of the recipient
- Returns : Encrypted data (Object)
Data Decryption
floCrypto.decryptData(data, privateKey)
decryptData decrypts the given data using private-key
- data - encrypted data to decrypt (Object that was returned from encryptData)
- privateKey - private key of the recipient
- Returns : Decrypted Data (String)
Sign Data
floCrypto.signData(data, privateKey)
signData signs the data using the private key
- data - data to sign
- privateKey - private key of the signer
- Returns : signature (String)
Verify Signature
floCrypto.verifySign(data, signature, publicKey)
verifySign verifies signatue of the data using public-key
- data - data of the given signature
- signature - signature of the data
- publicKey - public key of the signer
- Returns : boolen (true or false)
Generate Random Interger
floCrypto.randInt(min, max)
randInt returns a randomly generated interger in a given range.
- min - minimum value of the range
- max - maximum value of the range
- Returns : randomNum (Interger)
Generate Random String
floCrypto.randString(length, alphaNumeric)
randString returns a randomly generated string of given length.
- length - length of the string to be generated
- alphaNumeric - boolean (true: generated string will only contain alphabets and digits, false: generated string will also have symbols) (optional, default value is false)
- Returns : randomString (String)
Create Shamir's Secret shares
floCrypto.createShamirsSecretShares(str, total_shares, threshold_limit)
createShamirsSecretShares splits the str into shares using shamir's secret.
- str - string to be split
- total_shares - total number of shares to be split into
- threshold_limit - minimum number of shares required to reconstruct the secret str
- Returns : Shares (Array of string)
Retrieve Shamir's Secret
floCrypto.retrieveShamirSecret(sharesArray)
retrieveShamirSecret reconstructs the secret from the shares.
- sharesArray - Array of shares
- Returns : retrivedData (String)
Verify Shamir's Secret
floCrypto.verifyShamirsSecret(sharesArray, str)
verifyShamirsSecret verfies the validity of the created shares.
- sharesArray - Array of shares
- str - originalData (string).
- Returns : boolen (true or false)
FLO Blockchain API Operations
floBlockchainAPI object method can be used to send/recieve data to/from blockchain. These functions are asynchronous and return a promise. Contains the following functions.
promisedAJAX
floBlockchainAPI.promisedAJAX(method, uri)
promisedAJAX requests data using API
- method - GET/POST
- GET - Requests data from a specified resource.
- POST - Submits data to be processed to a specified resource.
- uri(Uniform Resource Identifier) - identifier for AJAX resource. It is used to create URL(Uniform Resource Locator) for further operations.
- Resolves: responseData from the API
getBalance
floBlockchainAPI.getBalance(addr)
getBalance requests balance for specified FLO address.
- addr - FLO address for which balance has to be retrieved
- Resolves: balance (Number)
writeData
floBlockchainAPI.writeData(senderAddr, Data, PrivKey, receiverAddr = floGlobals.adminID)
writeData writes data into blockchain, resolves transaction id if the transacation was succsessful.
- senderAddr - FLO address from which the data and amount has to be sent.
- Data - FLO data (string, max 1040 characters)
- receiverAddr - FLO address to which has to be sent. (Default is specified in floGlobals.adminID)
- PrivKey - Private key of sender
- Resolves: TransactionID (String)
writeDataMultiple
floBlockchainAPI.writeDataMultiple(senderPrivKeys, data, receivers = [floGlobals.adminID], preserveRatio = true)
writeDataMultiple writes data into blockchain from multiple floIDs to multiple floIDs, resolves transaction id if the transacation was succsessful.
- senderPrivKeys - Array of Private keys of the senders.
- Data - FLO data (string, max 1040 characters)
- receivers - Array of receiver FLO Addresses. (Default is specified in floGlobals.adminID)
- preserveRatio - boolean (true: preserves the ratio of the balance of senders, false: all senders contribute equal amount to the transaction) (optional, default is true)
- Resolves: TransactionID (String)
sendTx
floBlockchainAPI.sendTx(senderAddr, receiverAddr, sendAmt, PrivKey, floData = '')
sendTx sends a transaction to blockchain, resolves transaction id if the transacation was succsessful.
- senderAddr - FLO address from which the data and amount has to be sent.
- receiverAddr - FLO address to which has to be sent.
- sendAmt - Amount of FLO coins to be sent to receiver.
- PrivKey - Private key of sender to verify sender.
- floData - FLO data (string, max 1040 characters) (optional, default value is empty string)
- Resolves: TransactionID (String)
sendTxMultiple
floBlockchainAPI.sendTxMultiple(senderPrivKeys, receivers, floData = '')
sendTxMultiple sends a transaction to blockchain from multiple floIDs to multiple floIDs, resolves transaction id if the transacation was succsessful.
- 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 - FLO data (string, max 1040 characters) (optional, default value is empty string)
- Resolves: TransactionID (String)
mergeUTXOs
floBlockchainAPI.mergeUTXOs(floID, privKey, floData = '')
mergeUTXOs 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 - FLO data (string, max 1040 characters) (optional, default value is empty string)
- Resolves: TransactionID (String)
readTxs
floBlockchainAPI.readTxs(addr, from, to)
readTxs reads transactions of specified address between from and to.
- addr - FLO address for which the transactions has to be read.
- from - Reading transactions starts from 'from'.
- to - Reading transactions ends on 'to'.
- Resolves: TransactionData (Object)
readAllTxs
floBlockchainAPI.readTxs(addr)
readAllTxs reads all transactions of specified address(newest first).
- addr - FLO address for which the transactions has to be read.
- Resolves: TransactionData (Object)
readData
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.
- limit : maximum number of filtered data (default = 1000, negative = no limit)
- ignoreOld : ignore old transactions (default = 0)
- sentOnly : filters only sent data
- pattern : filters data that starts with a pattern
- contains : filters data that contains a string
- filter : custom filter funtion for floData (eg . filter: d => {return d[0] == '$'})
- Resolves: Object {totalTxs, floData (Array)}
Compact IndexedDB operations
compactIDB operations can be used to perform basic IndexedDB operations such as add, read/write, modify and remove.Contains following operations.
setDefaultDB
compactIDB.setDefaultDB(dbName)
setDefaultDB sets the database on which further operations will be performed.
- dbName - This is the name of default database to be used.
initDB
compactIDB.initDB(dbName, objectStores = {})
initDB initializes new IndexedDB.
- dbName - Specifies database to be initialized.
- objectStores - This is an object containing various objectStores to be initiazed when creating an IDB.
openDB
compactIDB.openDB(dbName = this.defaultDB)
openDB returns a promise that resolves to a default database object.
- dbName - Name of the database (optional, uses defaultDB if not specified)
deleteDB
compactIDB.deleteDB(dbName = this.defaultDB)
deleteDB deletes the specified database.
- dbName - Name of the database (optional, uses defaultDB if not specified)
writeData
compactIDB.writeData(obsName, data, key = false, dbName = this.defaultDB)
writeData writes specified data into the database if data doesn't exists or replaces it when data is already present.
- obsName - object store name in which the data is to be written.
- data - data that has to be written in specified object store.
- key - Primary key of the data (optional, false indicates key is autoincremented or passed in data)
- dbName - Name of the database (optional, uses defaultDB if not specified)
addData
compactIDB.addData(obsName, data, key = false, dbName = this.defaultDB)
addData writes new data into object store. If data already exists, it will return an error.
- obsName - Object store name in which has to be stored.
- data - The data which has to be added to obeject store.
- key - Primary key of the data (optional, false indicates key is autoincremented or passed in data)
- dbName - Name of the database (optional, uses defaultDB if not specified)
removeData
compactDB.removeData(obsName, key, dbName = this.defaultDB)
removeData deletes data from specified object store using primary key.
- obsName - Name of object store from which the data has to be removed.
- key - Primary key of the data.
- dbName - Name of the database (optional, uses defaultDB if not specified)
clearData
compactDB.clearData(obsName, dbName = this.defaultDB)
clearData clears all data in the objectStore.
- obsName - Name of object store from which the data has to be removed.
- dbName - Name of the database (optional, uses defaultDB if not specified)
readData
compactDB.readData(obsName, key, dbName = this.defaultDB)
readData reads the data from object store associated with specified key.
- obsName - Name of object store from which the data has to be retrieved.
- key - Primary key of the data to read.
- dbName - Name of the database (optional, uses defaultDB if not specified)
readAllData
compactDB.readAllData(obsName, dbName = this.defaultDB)
readAllData reads all the data from specified object store using IndexedDB openCursor method.
- obsName - Name of object store from which the data has to be retrieved.
- dbName - Name of the database (optional, uses defaultDB if not specified)
FLO Supernode module
This module contains functions that interact with the supernode to send and retrive data in the backend. Use floClouldAPI operations to send and receive data for application.
FLO Cloud API operations
floCloudAPI operations can interact with floSupernode cloud to send and retrive data for applications. floCloudAPI uses floSupernode module for backend interactions.
sendApplicationData
floCloudAPI.sendApplicationData(message, type, options = {})
sendApplicationData sends application data to the cloud.
- message - data to be sent
- type - type of the data
- options - (optional, options detailed at end of module)
requestApplicationData
floCloudAPI.requestApplicationData(options = {})
requestApplicationData requests application data from the cloud.
- options - (optional, options detailed at end of module)
sendGeneralData
floCloudAPI.sendGeneralData(message, type, options = {})
sendGeneralData sends general data to the cloud.
- message - data to be sent
- type - type of the data
- options - (optional, options detailed at end of module)
requestGeneralData
floCloudAPI.requestGeneralData(type, options = {})
requestGeneralData requests application data from the cloud.
- type - type of the data
- options - (optional, options detailed at end of module)
resetObjectData
floCloudAPI.resetObjectData(objectName, options = {})
resetObjectData resets the objectData to cloud.
- objectName - Name of the objectData to be reset
- options - (optional, options detailed at end of module) Note: value of objectData is taken from floGlobals
updateObjectData
floCloudAPI.updateObjectData(objectName, options = {})
updateObjectData updates the objectData to cloud.
- objectName - Name of the objectData to be updated
- options - (optional, options detailed at end of module) Note: value of objectData is taken from floGlobals
requestObjectData
floCloudAPI.requestObjectData(objectName, options = {})
requestObjectData requests application data from the cloud.
- objectName - Name of the objectData to be requested
- options - (optional, options detailed at end of module)
options:
-
send options:
- receiverID - received of the data
- application - application of the data
- comment - comment of the data
-
request options
- receiverID - received of the data
- senderIDs - array of senderIDs
- application - application of the data
- type - type of the data
- comment - comment of the data
- lowerVectorClock - VC from which the data is to be requested
- upperVectorClock - VC till which the data is to be requested
- atVectorClock - VC at which the data is to requested
- mostRecent - boolean (true: request only the recent data matching the pattern)
FLO Decentralised Applications (Dapps)
floDapps module contains methods for basic Dapp. floDapps uses all of the above modules.
addStartUpFunction
floDapps.addStartUpFunction(fname, fn)
addStartUpFunction adds a startup funtion to the Dapp
- fname - Name of the startup function
- fn - body of the function Note: startup funtions are called in parallel. Therefore only add custom startup funtion only if it can run in parallel with other startup functions. (default startup funtions are read supernode list and subAdmin list from blockchain API, load data from indexedDB, get login credentials)
setCustomPrivKeyInput
floDapps.setCustomPrivKeyInput(customFn)
setCustomPrivKeyInput adds a startup funtion to the Dapp
- customFn - custom function to get login credentials (privateKey)
setAppObjectStores
floDapps.setAppObjectStores(appObs)
setAppObjectStores adds additionals objectstores for the app
- appObs - additionals objects for the app
manageSubAdmins
floDapps.manageSubAdmins(adminPrivKey, addList, rmList)
manageSubAdmins adds and/or removes subAdmins by sending transaction to blockchain.
- adminPrivKey - private key of the adminID
- addList - Array of subAdmins to add
- rmList - Array of subAdmins to remove
clearCredentials
floDapps.clearCredentials()
clearCredentials removes the login credentials stored in IDB.
securePrivKey
floDapps.securePrivKey(pwd)
securePrivKey replaces the stored private key with an encrypted variant
- pwd - password for the encryption Note: if securePrivKey is used, then password must be requested during customPrivKeyInput (in setCustomPrivKeyInput).
objectDataMapper
floDapps.objectDataMapper(object, path, data)
objectDataMapper maps the object and data via path
- object - object to be mapped
- path - end path for the data holder
- data - data to be pushed in map
getNextGeneralData
floDapps.getNextGeneralData(type, vectorClock, options = {})
getNextGeneralData return the next generaldata
- type - type of the general data
- vectorClock - current known VC, from which next data to be retrived
- options - (optional, {comment, application} of the data)
launchStartUp
floDapps.launchStartUp()
launchStartUp launchs the Dapp startup functions
reactorEvents
- startUpSuccessLog - called when a startup funtion is successfully completed
- startUpErrorLog - called when a startup funtion encounters an error
onLoadStartUp
Sample startup is defined in onLoadStartUp function