-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtxnprocessing.js
46 lines (42 loc) · 1.47 KB
/
txnprocessing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// txnprocessing.js - demo APIs for storing and retrieving the transactions
const KJUR = require('jsrsasign');
const logger = require('./logging.js');
const fidoutils = require('./fidoutils.js');
// in memory cache for demo
var _txnCache = {};
/**
* In-memory storage of 5 most recent transactions
*/
function storeTransactionForCredential(credId, txt, timestamp) {
// temporary storage of recent transactions
let lookupKey = "txns_" + KJUR.BAtohex(fidoutils.sha256(KJUR.b64toBA(KJUR.utf8tob64(credId))));
let txnObj = {
timestamp: timestamp,
txt: txt
};
logger.logWithTS("storeTransactionForCredential: credId: " + credId + " txnObj: " + JSON.stringify(txnObj) + " timestamp: " + timestamp);
let existingTxnsStr = _txnCache[lookupKey];
let existingTxns = [];
if (existingTxnsStr != null) {
existingTxns = JSON.parse(''+existingTxnsStr);
}
existingTxns.unshift(txnObj);
if (existingTxns.length > 5) {
existingTxns = existingTxns.slice(0,5);
}
// store for an hour
_txnCache[lookupKey] = JSON.stringify(existingTxns);
}
function getTransactionsForCredentialID(credId) {
let result = [];
let lookupKey = "txns_" + KJUR.BAtohex(fidoutils.sha256(KJUR.b64toBA(KJUR.utf8tob64(credId))));
let txnsStr = _txnCache[lookupKey];
if (txnsStr != null) {
result = JSON.parse(txnsStr);
}
return result;
}
module.exports = {
storeTransactionForCredential: storeTransactionForCredential,
getTransactionsForCredentialID: getTransactionsForCredentialID
};