-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from docknetwork/DCKM-591-new-wallet-sdk-edv-…
…storage-package-for-edv-integration edv sync implementation
- Loading branch information
Showing
56 changed files
with
1,742 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import { | ||
DataStore, | ||
DataStoreEvents, | ||
} from '@docknetwork/wallet-sdk-data-store/src/types'; | ||
import {logger} from '@docknetwork/wallet-sdk-data-store/src/logger'; | ||
import {edvService} from '@docknetwork/wallet-sdk-wasm/src/services/edv'; | ||
|
||
export const SYNC_MARKER_TYPE = 'SyncMarkerDocument'; | ||
|
||
export async function initializeCloudWallet({ | ||
dataStore, | ||
edvUrl, | ||
agreementKey, | ||
verificationKey, | ||
hmacKey, | ||
authKey, | ||
}: { | ||
dataStore?: DataStore; | ||
edvUrl: string; | ||
agreementKey: any; | ||
verificationKey: any; | ||
hmacKey: any; | ||
authKey: string; | ||
}) { | ||
|
||
await edvService.initialize({ | ||
hmacKey, | ||
agreementKey, | ||
verificationKey, | ||
edvUrl, | ||
authKey | ||
}); | ||
|
||
let pendingOperations = 0; | ||
let pendingOperationsResolvers = []; | ||
|
||
function waitForEdvIdle() { | ||
if (pendingOperations === 0) { | ||
return Promise.resolve(); | ||
} | ||
return new Promise(resolve => { | ||
pendingOperationsResolvers.push(resolve); | ||
}); | ||
} | ||
|
||
async function findDocumentByContentId(id) { | ||
const result = await edvService.find({ | ||
equals: { | ||
'content.id': id, | ||
}, | ||
}); | ||
|
||
return result.documents[0]; | ||
} | ||
|
||
async function updateDocumentByContentId(documentContent) { | ||
const edvDocument = await findDocumentByContentId(documentContent.id); | ||
|
||
if (!edvDocument) { | ||
throw new Error('Document not found in EDV'); | ||
} | ||
|
||
logger.debug(`Updating document ${documentContent.id} in EDV`); | ||
|
||
await edvService.update({ | ||
document: { | ||
id: edvDocument.id, | ||
content: documentContent, | ||
}, | ||
}); | ||
|
||
logger.debug(`Document ${documentContent.id} updated in EDV`); | ||
} | ||
|
||
async function addDocumentHandler(content) { | ||
pendingOperations++; | ||
try { | ||
logger.debug(`Adding document to EDV: ${content.id}`); | ||
await edvService.insert({ | ||
document: { | ||
content: content, | ||
}, | ||
}); | ||
logger.debug(`Document added to EDV: ${content.id}`); | ||
} finally { | ||
pendingOperations--; | ||
if (pendingOperations === 0) { | ||
pendingOperationsResolvers.forEach(resolve => resolve()); | ||
pendingOperationsResolvers = []; | ||
} | ||
} | ||
} | ||
|
||
async function removeDocumentHandler(documentId) { | ||
pendingOperations++; | ||
try { | ||
logger.debug(`Removing document from EDV: ${documentId}`); | ||
const edvDocument = await findDocumentByContentId(documentId); | ||
await edvService.delete({document: edvDocument}); | ||
logger.debug(`Document removed from EDV: ${documentId}`); | ||
} finally { | ||
pendingOperations--; | ||
if (pendingOperations === 0) { | ||
pendingOperationsResolvers.forEach(resolve => resolve()); | ||
pendingOperationsResolvers = []; | ||
} | ||
} | ||
} | ||
|
||
async function updateDocumentHandler(documentContent) { | ||
pendingOperations++; | ||
try { | ||
await updateDocumentByContentId(documentContent); | ||
} finally { | ||
pendingOperations--; | ||
if (pendingOperations === 0) { | ||
pendingOperationsResolvers.forEach(resolve => resolve()); | ||
pendingOperationsResolvers = []; | ||
} | ||
} | ||
} | ||
|
||
dataStore.events.on(DataStoreEvents.DocumentCreated, addDocumentHandler); | ||
dataStore.events.on(DataStoreEvents.DocumentDeleted, removeDocumentHandler); | ||
dataStore.events.on(DataStoreEvents.DocumentUpdated, updateDocumentHandler); | ||
|
||
async function getSyncMarkerDiff() { | ||
const edvSyncMaker = await findDocumentByContentId(SYNC_MARKER_TYPE); | ||
const localSyncMarker = await dataStore.documents.getDocumentById( | ||
SYNC_MARKER_TYPE, | ||
); | ||
|
||
return edvSyncMaker?.content?.updatedAt - localSyncMarker?.updatedAt; | ||
} | ||
|
||
async function pushSyncMarker() { | ||
const edvSyncMarker = await findDocumentByContentId(SYNC_MARKER_TYPE); | ||
const syncMarker = { | ||
id: SYNC_MARKER_TYPE, | ||
type: SYNC_MARKER_TYPE, | ||
updatedAt: Date.now(), | ||
}; | ||
|
||
if (edvSyncMarker) { | ||
await dataStore.documents.updateDocument(syncMarker); | ||
} else { | ||
await dataStore.documents.addDocument(syncMarker); | ||
} | ||
} | ||
|
||
async function pullDocuments() { | ||
const allDocs = await edvService.find({}); | ||
|
||
for (const doc of allDocs.documents) { | ||
const edvDoc = doc.content; | ||
const walletDoc = await dataStore.documents.getDocumentById(edvDoc.id); | ||
|
||
if (!walletDoc) { | ||
const result = await dataStore.documents.addDocument(edvDoc, { | ||
stopPropagation: true, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
async function clearEdvDocuments() { | ||
const allDocs = await edvService.find({}); | ||
|
||
for (const doc of allDocs.documents) { | ||
await edvService.delete({document: doc}); | ||
} | ||
} | ||
|
||
return { | ||
clearEdvDocuments, | ||
pushSyncMarker, | ||
getSyncMarkerDiff, | ||
findDocumentByContentId, | ||
updateDocumentByContentId, | ||
waitForEdvIdle, | ||
pullDocuments, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 1 addition & 6 deletions
7
packages/data-store-typeorm/src/migration/migration1/v1-data-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import {getDataSource} from './helpers'; | ||
import {createDataSource} from './helpers'; | ||
|
||
export default getDataSource({ | ||
export default createDataSource({ | ||
dbType: 'sqlite', | ||
databasePath: 'data-store.sqlite', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.sqlite | ||
lib/ | ||
tsconfig.build.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
sqlite/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
testTimeout: 30000, | ||
testMatch: [ | ||
"<rootDir>/src/**/!(*.e2e).test.ts" | ||
], | ||
coverageThreshold: { | ||
global: { | ||
branches: 10, | ||
functions: 10, | ||
lines: 10, | ||
statements: 10, | ||
}, | ||
}, | ||
transform: { | ||
'^.+\\.ts$': 'ts-jest', | ||
}, | ||
resetMocks: false, | ||
setupFiles: ['jest-localstorage-mock'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "@docknetwork/wallet-sdk-data-store-web", | ||
"version": "0.4.19", | ||
"license": "https://github.com/docknetwork/react-native-sdk/LICENSE", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/docknetwork/react-native-sdk", | ||
"directory": "packages/data-store" | ||
}, | ||
"scripts": { | ||
"test": "jest", | ||
"build": "tsc -p tsconfig.build.json" | ||
}, | ||
"peerDependencies": { | ||
"typeorm": "^0.3.15" | ||
}, | ||
"dependencies": { | ||
"@docknetwork/wallet-sdk-wasm": "^0.4.19", | ||
"uuid": "^8.3.2" | ||
}, | ||
"devDependencies": { | ||
"jest": "29.1.0", | ||
"ts-jest": "29.1.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.4", | ||
"typeorm": "^0.3.15", | ||
"sqlite3": "^5.0.2", | ||
"reflect-metadata": "^0.1.13" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
yarn build | ||
npm publish --access public | ||
|
Oops, something went wrong.