Skip to content

Commit

Permalink
add file-system storage option
Browse files Browse the repository at this point in the history
  • Loading branch information
jchartrand committed Sep 26, 2023
1 parent d5f1432 commit 6668c6c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"version": "0.0.1",
"type": "module",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "NODE_OPTIONS=--experimental-vm-modules npx mocha --timeout 10000 src/app.test.js "
"start": "node -r dotenv/config server.js",
"dev": "nodemon -r dotenv/config server.js",
"test": "NODE_OPTIONS=--experimental-vm-modules npx mocha --timeout 10000 -r dotenv/config dotenv_config_path=src/test-fixtures/.env.testing src/app.test.js "
},
"dependencies": {
"@digitalbazaar/did-io": "^2.0.0",
Expand All @@ -25,6 +25,7 @@
"express": "~4.16.1",
"jsonld-document-loader": "^2.0.0",
"keyv": "^4.5.2",
"keyv-file": "^0.2.0",
"morgan": "~1.9.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/test-fixtures/.env.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PERSIST_TO_FILE=/Users/jameschartrand/Documents/github/dcc/transaction-service/src/data/transactionsFile.json
28 changes: 24 additions & 4 deletions src/transactionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@
*/
import crypto from 'crypto';
import Keyv from 'keyv';
import {KeyvFile} from 'keyv-file'
import { verifyDIDAuth } from './didAuth.js';

const persistToFile = process.env.PERSIST_TO_FILE
const defaultTimeToLive = process.env.DEFAULT_TTL = 1000 * 60 * 10; // keyv entry expires after ten minutes

let keyv;
const expiresAfter = 1000 * 60 * 10; // keyv entry expires after ten minutes


export const initializeTransactionManager = () => {
if (!keyv) keyv = new Keyv();
if (!keyv) {
if (persistToFile) {
keyv = new Keyv({
store: new KeyvFile({
filename: persistToFile, // the file path to store the data
expiredCheckDelay: 4 * 3600 * 1000, // ms (so every 4 hours) how often to check for and remove expired records
writeDelay: 100, // ms, batch write to disk in a specific duration, enhance write performance.
encode: JSON.stringify, // serialize function
decode: JSON.parse // deserialize function
})
})
} else {
keyv = new Keyv();
}
}

}

/**
Expand Down Expand Up @@ -70,8 +89,9 @@ const getProcessRecordFnForExchangeHostAndTenant = (exchangeHost, tenantName) =>
record.exchangeHost = exchangeHost
record.transactionId = crypto.randomUUID()
record.exchangeId = crypto.randomUUID()

await keyv.set(record.exchangeId, record, expiresAfter);

const timeToLive = record.timeToLive || defaultTimeToLive
await keyv.set(record.exchangeId, record, timeToLive);

// directDeepLink bypasses the VPR step and assumes the wallet knows to send a DIDAuth.
const directDeepLink = `https://lcw.app/request.html?issuer=issuer.example.com&auth_type=bearer&challenge=${record.transactionId}&vc_request_url=${exchangeHost}/exchange/${record.exchangeId}/${record.transactionId}`
Expand Down

0 comments on commit 6668c6c

Please sign in to comment.