aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2017-08-24 02:12:46 +0800
committerDan Finlay <dan@danfinlay.com>2017-08-24 02:12:46 +0800
commitb3b62d97a367ebcd1b69555a97311fca19b3a27f (patch)
treebab3bb390c4367ceca11c6158f519fb087ce68ca /app/scripts/lib
parenta1618ca1b4ef6568cd0c9660c56adeb8659af2bb (diff)
parent9203b4edf9df8b616877c57970fb01a7fb87924b (diff)
downloadtangerine-wallet-browser-b3b62d97a367ebcd1b69555a97311fca19b3a27f.tar.gz
tangerine-wallet-browser-b3b62d97a367ebcd1b69555a97311fca19b3a27f.tar.zst
tangerine-wallet-browser-b3b62d97a367ebcd1b69555a97311fca19b3a27f.zip
Merge branch 'master' into useLocalNonce
Diffstat (limited to 'app/scripts/lib')
-rw-r--r--app/scripts/lib/tx-state-history-helper.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/app/scripts/lib/tx-state-history-helper.js b/app/scripts/lib/tx-state-history-helper.js
new file mode 100644
index 000000000..304069d57
--- /dev/null
+++ b/app/scripts/lib/tx-state-history-helper.js
@@ -0,0 +1,37 @@
+const jsonDiffer = require('fast-json-patch')
+const clone = require('clone')
+
+module.exports = {
+ generateHistoryEntry,
+ replayHistory,
+ snapshotFromTxMeta,
+ migrateFromSnapshotsToDiffs,
+}
+
+
+function migrateFromSnapshotsToDiffs(longHistory) {
+ return (
+ longHistory
+ // convert non-initial history entries into diffs
+ .map((entry, index) => {
+ if (index === 0) return entry
+ return generateHistoryEntry(longHistory[index - 1], entry)
+ })
+ )
+}
+
+function generateHistoryEntry(previousState, newState) {
+ return jsonDiffer.compare(previousState, newState)
+}
+
+function replayHistory(shortHistory) {
+ return shortHistory.reduce((val, entry) => jsonDiffer.applyPatch(val, entry).newDocument)
+}
+
+function snapshotFromTxMeta(txMeta) {
+ // create txMeta snapshot for history
+ const snapshot = clone(txMeta)
+ // dont include previous history in this snapshot
+ delete snapshot.history
+ return snapshot
+} \ No newline at end of file