Update flo-webwallet.js

- removed syncTransactions and readTransactions
- replace the above with listTransactions function set
- listTransactions(address) gets the latest  tx (1000 max)
- listTransactions.syncOld(address, initItem): use the initItem received by listTransaction or prevous .syncOld call. give the next old 1000 tx
- listTransaction.syncNew(address, lastItem): use lastItem received by listTransaction or previous .syncNew call). gives the new txs (1000 max) from last call
This commit is contained in:
sairajzero 2023-05-13 18:24:11 +05:30
parent 5c2ce4f03e
commit f83884b591

View File

@ -30,10 +30,10 @@
}) })
} }
//get balance of addr using API : resolves (balance) //get balance of address using API : resolves (balance)
floWebWallet.getBalance = function (addr) { floWebWallet.getBalance = function (address) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
floBlockchainAPI.getBalance(addr) floBlockchainAPI.getBalance(address)
.then(txid => resolve(txid)) .then(txid => resolve(txid))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
@ -48,45 +48,52 @@
}) })
} }
//sync new transactions from blockchain using API and stores in IDB : resolves Array(newItems) function listTransactions_raw(address, options = {}) {
floWebWallet.syncTransactions = function (addr) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
compactIDB.readData('lastSync', addr).then(lastSync => { options.latest = true;
const old_support = Number.isInteger(lastSync); //backward support floBlockchainAPI.readTxs(address, options).then(response => {
let fetch_options = {}; const result = {}
if (typeof lastSync == 'string' && /^[a-f0-9]{64}$/i.test(lastSync)) //txid as lastSync result.items = response.items.map(({ time, txid, floData, isCoinBase, vin, vout }) => ({
fetch_options.after = lastSync; time, txid, floData, isCoinBase,
floBlockchainAPI.readAllTxs(addr, fetch_options).then(response => { sender: isCoinBase ? `(mined)${vin[0].coinbase}` : vin[0].addr,
let newItems = response.items.map(({ time, txid, floData, isCoinBase, vin, vout }) => ({ receiver: isCoinBase ? address : vout[0].scriptPubKey.addresses[0]
time, txid, floData, isCoinBase, }));
sender: isCoinBase ? `(mined)${vin[0].coinbase}` : vin[0].addr, result.lastItem = response.lastItem;
receiver: isCoinBase ? addr : vout[0].scriptPubKey.addresses[0] result.initItem = response.initItem;
})).reverse(); resolve(result);
compactIDB.readData('transactions', addr).then(IDBresult => {
if ((IDBresult === undefined || old_support))//backward support
IDBresult = [];
compactIDB.writeData('transactions', IDBresult.concat(newItems), addr).then(result => {
compactIDB.writeData('lastSync', response.lastItem, addr)
.then(result => resolve(newItems))
.catch(error => reject(error))
}).catch(error => reject(error))
})
}).catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))
}) })
} }
//read transactions stored in IDB : resolves Array(storedItems)
floWebWallet.readTransactions = function (addr) { floWebWallet.listTransactions = function (address) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
compactIDB.readData('transactions', addr) listTransactions_raw(address)
.then(IDBresult => resolve(IDBresult)) .then(result => resolve(result))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
//get address-label pairs from IDB : resolves Object(addr:label)
floWebWallet.listTransactions.syncNew = function (address, lastItem) {
return new Promise((resolve, reject) => {
listTransactions_raw(address, { after: lastItem }).then(result => {
delete result.initItem;
resolve(result);
}).catch(error => reject(error))
})
}
floWebWallet.listTransactions.syncOld = function (address, initItem) {
return new Promise((resolve, reject) => {
listTransactions_raw(address, { before: initItem }).then(result => {
delete result.lastItem;
resolve(result);
}).catch(error => reject(error))
})
}
//get address-label pairs from IDB : resolves Object(floID:label)
floWebWallet.getLabels = function () { floWebWallet.getLabels = function () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
compactIDB.readAllData('labels') compactIDB.readAllData('labels')