@docknetwork/wallet-sdk-data-store 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +39 -0
- package/jest.config.ts +21 -0
- package/lib/configs.d.ts +3 -0
- package/lib/configs.d.ts.map +1 -0
- package/lib/configs.js +30 -0
- package/lib/configs.js.map +1 -0
- package/lib/document-network-resolver.d.ts +3 -0
- package/lib/document-network-resolver.d.ts.map +1 -0
- package/lib/document-network-resolver.js +52 -0
- package/lib/document-network-resolver.js.map +1 -0
- package/lib/entities/document/create-document.d.ts +10 -0
- package/lib/entities/document/create-document.d.ts.map +1 -0
- package/lib/entities/document/create-document.js +111 -0
- package/lib/entities/document/create-document.js.map +1 -0
- package/lib/entities/document/document.entity.d.ts +11 -0
- package/lib/entities/document/document.entity.d.ts.map +1 -0
- package/lib/entities/document/document.entity.js +55 -0
- package/lib/entities/document/document.entity.js.map +1 -0
- package/lib/entities/document/get-all-documents.d.ts +11 -0
- package/lib/entities/document/get-all-documents.d.ts.map +1 -0
- package/lib/entities/document/get-all-documents.js +78 -0
- package/lib/entities/document/get-all-documents.js.map +1 -0
- package/lib/entities/document/get-documens-by-type.d.ts +10 -0
- package/lib/entities/document/get-documens-by-type.d.ts.map +1 -0
- package/lib/entities/document/get-documens-by-type.js +71 -0
- package/lib/entities/document/get-documens-by-type.js.map +1 -0
- package/lib/entities/document/get-document-by-id.d.ts +13 -0
- package/lib/entities/document/get-document-by-id.d.ts.map +1 -0
- package/lib/entities/document/get-document-by-id.js +92 -0
- package/lib/entities/document/get-document-by-id.js.map +1 -0
- package/lib/entities/document/get-document-correlations.d.ts +10 -0
- package/lib/entities/document/get-document-correlations.d.ts.map +1 -0
- package/lib/entities/document/get-document-correlations.js +78 -0
- package/lib/entities/document/get-document-correlations.js.map +1 -0
- package/lib/entities/document/helpers.d.ts +30 -0
- package/lib/entities/document/helpers.d.ts.map +1 -0
- package/lib/entities/document/helpers.js +196 -0
- package/lib/entities/document/helpers.js.map +1 -0
- package/lib/entities/document/index.d.ts +10 -0
- package/lib/entities/document/index.d.ts.map +1 -0
- package/lib/entities/document/index.js +26 -0
- package/lib/entities/document/index.js.map +1 -0
- package/lib/entities/document/remove-document.d.ts +15 -0
- package/lib/entities/document/remove-document.d.ts.map +1 -0
- package/lib/entities/document/remove-document.js +94 -0
- package/lib/entities/document/remove-document.js.map +1 -0
- package/lib/entities/document/update-document.d.ts +10 -0
- package/lib/entities/document/update-document.d.ts.map +1 -0
- package/lib/entities/document/update-document.js +72 -0
- package/lib/entities/document/update-document.js.map +1 -0
- package/lib/entities/document-type.entity.d.ts +6 -0
- package/lib/entities/document-type.entity.d.ts.map +1 -0
- package/lib/entities/document-type.entity.js +32 -0
- package/lib/entities/document-type.entity.js.map +1 -0
- package/lib/entities/log.entity.d.ts +15 -0
- package/lib/entities/log.entity.d.ts.map +1 -0
- package/lib/entities/log.entity.js +108 -0
- package/lib/entities/log.entity.js.map +1 -0
- package/lib/entities/network.entity.d.ts +6 -0
- package/lib/entities/network.entity.d.ts.map +1 -0
- package/lib/entities/network.entity.js +35 -0
- package/lib/entities/network.entity.js.map +1 -0
- package/lib/entities/transaction.entity.d.ts +24 -0
- package/lib/entities/transaction.entity.d.ts.map +1 -0
- package/lib/entities/transaction.entity.js +139 -0
- package/lib/entities/transaction.entity.js.map +1 -0
- package/lib/entities/wallet.entity.d.ts +10 -0
- package/lib/entities/wallet.entity.d.ts.map +1 -0
- package/lib/entities/wallet.entity.js +109 -0
- package/lib/entities/wallet.entity.js.map +1 -0
- package/lib/helpers.d.ts +6 -0
- package/lib/helpers.d.ts.map +1 -0
- package/lib/helpers.js +98 -0
- package/lib/helpers.js.map +1 -0
- package/lib/index.d.ts +14 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +145 -0
- package/lib/index.js.map +1 -0
- package/lib/logger.d.ts +14 -0
- package/lib/logger.d.ts.map +1 -0
- package/lib/logger.js +40 -0
- package/lib/logger.js.map +1 -0
- package/lib/migration/bootstrap-tables.d.ts +3 -0
- package/lib/migration/bootstrap-tables.d.ts.map +1 -0
- package/lib/migration/bootstrap-tables.js +8 -0
- package/lib/migration/bootstrap-tables.js.map +1 -0
- package/lib/migration/index.d.ts +8 -0
- package/lib/migration/index.d.ts.map +1 -0
- package/lib/migration/index.js +132 -0
- package/lib/migration/index.js.map +1 -0
- package/lib/migration/migration1/index.d.ts +16 -0
- package/lib/migration/migration1/index.d.ts.map +1 -0
- package/lib/migration/migration1/index.js +110 -0
- package/lib/migration/migration1/index.js.map +1 -0
- package/lib/migration/migration1/migrate-v1-data.d.ts +8 -0
- package/lib/migration/migration1/migrate-v1-data.d.ts.map +1 -0
- package/lib/migration/migration1/migrate-v1-data.js +171 -0
- package/lib/migration/migration1/migrate-v1-data.js.map +1 -0
- package/lib/migration/migration1/v1-data-store.d.ts +15 -0
- package/lib/migration/migration1/v1-data-store.d.ts.map +1 -0
- package/lib/migration/migration1/v1-data-store.js +80 -0
- package/lib/migration/migration1/v1-data-store.js.map +1 -0
- package/lib/migrations/1691498362273-bootstrap.d.ts +7 -0
- package/lib/migrations/1691498362273-bootstrap.d.ts.map +1 -0
- package/lib/migrations/1691498362273-bootstrap.js +178 -0
- package/lib/migrations/1691498362273-bootstrap.js.map +1 -0
- package/lib/migrations/index.d.ts +4 -0
- package/lib/migrations/index.d.ts.map +1 -0
- package/lib/migrations/index.js +5 -0
- package/lib/migrations/index.js.map +1 -0
- package/lib/migrations-data-source.d.ts +3 -0
- package/lib/migrations-data-source.d.ts.map +1 -0
- package/lib/migrations-data-source.js +8 -0
- package/lib/migrations-data-source.js.map +1 -0
- package/lib/typeorm.d.ts +2 -0
- package/lib/typeorm.d.ts.map +1 -0
- package/lib/typeorm.js +18 -0
- package/lib/typeorm.js.map +1 -0
- package/lib/types.d.ts +49 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/package.json +31 -0
- package/scripts/publish.sh +3 -0
- package/setup-tests.ts +0 -0
- package/src/__tests__/index.test.ts +56 -0
- package/src/__tests__/v1-data-store.test.ts +24 -0
- package/src/configs.ts +28 -0
- package/src/document-network-resolver.ts +19 -0
- package/src/entities/document/create-document.ts +72 -0
- package/src/entities/document/decument.entity.test.ts +135 -0
- package/src/entities/document/document.entity.ts +40 -0
- package/src/entities/document/get-all-documents.ts +39 -0
- package/src/entities/document/get-documens-by-type.ts +30 -0
- package/src/entities/document/get-document-by-id.ts +45 -0
- package/src/entities/document/get-document-correlations.ts +34 -0
- package/src/entities/document/helpers.ts +115 -0
- package/src/entities/document/index.ts +9 -0
- package/src/entities/document/remove-document.ts +34 -0
- package/src/entities/document/update-document.ts +25 -0
- package/src/entities/document-type.entity.ts +11 -0
- package/src/entities/log.entity.ts +46 -0
- package/src/entities/network.entity.ts +14 -0
- package/src/entities/transaction.entity.ts +67 -0
- package/src/entities/wallet.entity.ts +37 -0
- package/src/helpers.ts +52 -0
- package/src/index.ts +83 -0
- package/src/logger.ts +37 -0
- package/src/migration/bootstrap-tables.ts +5 -0
- package/src/migration/index.ts +53 -0
- package/src/migration/migration1/index.ts +64 -0
- package/src/migration/migration1/migrate-v1-data.ts +57 -0
- package/src/migration/migration1/v1-data-store.ts +39 -0
- package/src/migrations/1691498362273-bootstrap.ts +130 -0
- package/src/migrations/index.ts +3 -0
- package/src/migrations-data-source.ts +6 -0
- package/src/typeorm.ts +1 -0
- package/src/types.ts +67 -0
- package/test/mock-local-storage.ts +17 -0
- package/test/test-utils.ts +23 -0
- package/test/wallet.json +209 -0
- package/tsconfig.build.json +25 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.json +32 -0
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@docknetwork/wallet-sdk-data-store",
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"license": "LICENSE",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/docknetwork/react-native-sdk",
|
|
10
|
+
"directory": "packages/data-store"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "jest",
|
|
14
|
+
"build": "tsc -p tsconfig.build.json"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"typeorm": "^0.3.15"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"uuid": "^8.3.2"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"jest": "29.1.0",
|
|
24
|
+
"ts-jest": "29.1.0",
|
|
25
|
+
"ts-node": "^10.9.1",
|
|
26
|
+
"typescript": "^5.0.4",
|
|
27
|
+
"typeorm": "^0.3.15",
|
|
28
|
+
"sqlite3": "^5.0.2",
|
|
29
|
+
"reflect-metadata": "^0.1.13"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/setup-tests.ts
ADDED
|
File without changes
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createTestDataStore,
|
|
3
|
+
createV1EmptyDataStore,
|
|
4
|
+
setupV1MockDataStore,
|
|
5
|
+
} from '../../test/test-utils';
|
|
6
|
+
import {getV1LocalStorage} from '../migration/migration1/v1-data-store';
|
|
7
|
+
import {CURRENT_DATA_STORE_VERSION} from '../migration';
|
|
8
|
+
import {DataStore} from '../types';
|
|
9
|
+
import {WalletEntity, getWallet} from '../entities/wallet.entity';
|
|
10
|
+
import {closeDataStore} from '../index';
|
|
11
|
+
|
|
12
|
+
describe('Data store', () => {
|
|
13
|
+
describe('v2-data-store migration', () => {
|
|
14
|
+
let dataStore: DataStore;
|
|
15
|
+
|
|
16
|
+
beforeAll(async () => {
|
|
17
|
+
await setupV1MockDataStore();
|
|
18
|
+
dataStore = await createTestDataStore();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should remove wallet json from local storage', async () => {
|
|
22
|
+
const jsonData = await getV1LocalStorage().getItem('wallet');
|
|
23
|
+
expect(jsonData).toBeUndefined();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should create DataStoreConfigs', async () => {
|
|
27
|
+
const configs = await getWallet({dataStore});
|
|
28
|
+
expect(configs).toBeDefined();
|
|
29
|
+
expect(configs.networkId).toBe('mainnet');
|
|
30
|
+
expect(configs.version).toBe(CURRENT_DATA_STORE_VERSION);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterAll(() => {
|
|
34
|
+
closeDataStore(dataStore);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('empty wallet migration', () => {
|
|
39
|
+
let dataStore: DataStore;
|
|
40
|
+
|
|
41
|
+
beforeAll(async () => {
|
|
42
|
+
await createV1EmptyDataStore();
|
|
43
|
+
dataStore = await createTestDataStore();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should create SDKConfigs', async () => {
|
|
47
|
+
const configs = await getWallet({dataStore});
|
|
48
|
+
expect(configs.networkId).toBe('mainnet');
|
|
49
|
+
expect(configs.version).toBe(CURRENT_DATA_STORE_VERSION);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterAll(() => {
|
|
53
|
+
closeDataStore(dataStore);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {setupV1MockDataStore} from '../../test/test-utils';
|
|
2
|
+
import {
|
|
3
|
+
getV1LocalStorage,
|
|
4
|
+
getWalletDocuments,
|
|
5
|
+
} from '../migration/migration1/v1-data-store';
|
|
6
|
+
import _walletJSON from '../../test/wallet.json';
|
|
7
|
+
|
|
8
|
+
describe('v2-data-store', () => {
|
|
9
|
+
beforeAll(async () => {
|
|
10
|
+
await setupV1MockDataStore();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should have access to v2 localStorage', async () => {
|
|
14
|
+
const jsonData = await getV1LocalStorage().getItem('wallet');
|
|
15
|
+
expect(jsonData).toBeDefined();
|
|
16
|
+
const wallet = JSON.parse(jsonData as string);
|
|
17
|
+
expect(wallet).toStrictEqual(_walletJSON);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should be able to fetch v2 documents', async () => {
|
|
21
|
+
const documents = await getWalletDocuments();
|
|
22
|
+
expect(documents.length).toBeGreaterThan(1);
|
|
23
|
+
});
|
|
24
|
+
});
|
package/src/configs.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {DataStoreConfigs} from './types';
|
|
2
|
+
import {genericDocumentNetworkResolver} from './document-network-resolver';
|
|
3
|
+
|
|
4
|
+
export const DEFAULT_CONFIGS: DataStoreConfigs = {
|
|
5
|
+
dbType: 'sqlite',
|
|
6
|
+
databasePath: 'dock-wallet-sdk.db',
|
|
7
|
+
documentNetworkResolver: genericDocumentNetworkResolver,
|
|
8
|
+
networks: [
|
|
9
|
+
{
|
|
10
|
+
name: 'Mainnet',
|
|
11
|
+
id: 'mainnet',
|
|
12
|
+
credentialHostnames: ['creds.dock.io'],
|
|
13
|
+
configs: {
|
|
14
|
+
substrateUrl: 'wss://mainnet-node.dock.io',
|
|
15
|
+
addressPrefix: 22,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
name: 'Testnet',
|
|
20
|
+
id: 'testnet',
|
|
21
|
+
credentialHostnames: ['creds-testnet.dock.io', 'creds-staging.dock.io'],
|
|
22
|
+
configs: {
|
|
23
|
+
substrateUrl: 'wss://knox-1.dock.io',
|
|
24
|
+
addressPrefix: 21,
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataStore,
|
|
3
|
+
DocumentNetworkResolver,
|
|
4
|
+
DocumentResolverProps,
|
|
5
|
+
DocumentResolverResult,
|
|
6
|
+
WalletDocument,
|
|
7
|
+
} from './types';
|
|
8
|
+
|
|
9
|
+
type ResolverResult = string | null;
|
|
10
|
+
|
|
11
|
+
export const genericDocumentNetworkResolver: DocumentNetworkResolver = async ({
|
|
12
|
+
document,
|
|
13
|
+
dataStore,
|
|
14
|
+
}: DocumentResolverProps): Promise<DocumentResolverResult> => {
|
|
15
|
+
return {
|
|
16
|
+
networkId: dataStore.networkId,
|
|
17
|
+
isFallback: true,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {ContextProps, WalletDocument} from '../../types';
|
|
2
|
+
import {v4 as uuid} from 'uuid';
|
|
3
|
+
import {DocumentEntity} from './document.entity';
|
|
4
|
+
import {getOrCreateDocumentTypes, saveOptions} from './helpers';
|
|
5
|
+
import {getDocumentById} from './get-document-by-id';
|
|
6
|
+
import {logger} from '../../logger';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Create document
|
|
10
|
+
* @param dataStore
|
|
11
|
+
* @param json
|
|
12
|
+
*/
|
|
13
|
+
export async function createDocument({
|
|
14
|
+
dataStore,
|
|
15
|
+
json,
|
|
16
|
+
}: ContextProps & {
|
|
17
|
+
json: any;
|
|
18
|
+
}): Promise<WalletDocument> {
|
|
19
|
+
logger.debug(`Creating document with id ${json.id}...`);
|
|
20
|
+
|
|
21
|
+
if (json.id) {
|
|
22
|
+
const existingDocument = await getDocumentById({
|
|
23
|
+
dataStore,
|
|
24
|
+
id: json.id,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
if (existingDocument) {
|
|
28
|
+
logger.debug(`Document with id ${json.id} already exists`);
|
|
29
|
+
throw new Error(`Document with id ${json.id} already exists`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const _typeRel = await getOrCreateDocumentTypes({
|
|
34
|
+
dataStore,
|
|
35
|
+
types: json.type,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (!json.id) {
|
|
39
|
+
json.id = uuid();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let networkId;
|
|
43
|
+
|
|
44
|
+
if (json._networkId) {
|
|
45
|
+
networkId = json._networkId;
|
|
46
|
+
delete json._networkId;
|
|
47
|
+
} else {
|
|
48
|
+
const resolution = await dataStore.resolveDocumentNetwork({
|
|
49
|
+
document: json,
|
|
50
|
+
dataStore,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
networkId = resolution.networkId;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const entity: DocumentEntity = {
|
|
57
|
+
networkId,
|
|
58
|
+
id: json.id,
|
|
59
|
+
type: json.type,
|
|
60
|
+
_typeRel,
|
|
61
|
+
correlation: json.correlation || [],
|
|
62
|
+
data: JSON.stringify(json),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const repository = dataStore.db.getRepository(DocumentEntity);
|
|
66
|
+
|
|
67
|
+
const result = await repository.save(entity, saveOptions);
|
|
68
|
+
|
|
69
|
+
logger.debug(`Document added to the wallet`);
|
|
70
|
+
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {DocumentEntity} from './document.entity';
|
|
2
|
+
import {createTestDataStore} from '../../../test/test-utils';
|
|
3
|
+
import {DataStore} from '../../types';
|
|
4
|
+
import {createDocument} from './create-document';
|
|
5
|
+
import {getDocumentsByType} from './get-documens-by-type';
|
|
6
|
+
import {getDocumentById} from './get-document-by-id';
|
|
7
|
+
import {getDocumentCorrelations} from './get-document-correlations';
|
|
8
|
+
|
|
9
|
+
const mockDocuments = [
|
|
10
|
+
{
|
|
11
|
+
'@context': [
|
|
12
|
+
'https://www.w3.org/2018/credentials/v1',
|
|
13
|
+
'https://www.w3.org/2018/credentials/examples/v1',
|
|
14
|
+
],
|
|
15
|
+
id: 'http://example.gov/credentials/test',
|
|
16
|
+
type: ['VerifiableCredential', 'UniversityDegreeCredential'],
|
|
17
|
+
issuer: {
|
|
18
|
+
id: 'did:example:123456789abcdefghi',
|
|
19
|
+
},
|
|
20
|
+
issuanceDate: '2020-03-10T04:24:12.164Z',
|
|
21
|
+
credentialSubject: {
|
|
22
|
+
id: 'did:example:ebfeb1f712ebc6f1c276e12ec21',
|
|
23
|
+
degree: {
|
|
24
|
+
type: 'BachelorDegree',
|
|
25
|
+
name: 'Bachelor of Science and Arts',
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
'@context': [
|
|
31
|
+
'https://www.w3.org/2018/credentials/v1',
|
|
32
|
+
'https://www.w3.org/2018/credentials/examples/v1',
|
|
33
|
+
],
|
|
34
|
+
id: 'http://example.gov/credentials/test2',
|
|
35
|
+
type: ['VerifiableCredential', 'BasicCredential'],
|
|
36
|
+
issuer: {
|
|
37
|
+
id: 'did:example:123456789abcdefghi',
|
|
38
|
+
},
|
|
39
|
+
issuanceDate: '2020-03-10T04:24:12.164Z',
|
|
40
|
+
credentialSubject: {
|
|
41
|
+
id: 'did:example:ebfeb1f712ebc6f1c276e12ec21',
|
|
42
|
+
name: 'Testing',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
'@context': ['https://w3id.org/wallet/v1'],
|
|
47
|
+
id: 'urn:uuid:c410e44a-9525-11ea-bb37-0242ac130002',
|
|
48
|
+
name: 'My Ropsten Mnemonic 1',
|
|
49
|
+
image: 'https://via.placeholder.com/150',
|
|
50
|
+
description: 'For testing only, totally compromised.',
|
|
51
|
+
tags: ['professional', 'organization', 'compromised'],
|
|
52
|
+
correlation: [],
|
|
53
|
+
type: 'Mnemonic',
|
|
54
|
+
value:
|
|
55
|
+
'humble piece toy mimic miss hurdle smile awkward patch drama hurry mixture',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
'@context': ['https://w3id.org/wallet/v1'],
|
|
59
|
+
id: 'c47f3ed0-b4b5-4983-8962-759196fd3ece',
|
|
60
|
+
type: 'Currency',
|
|
61
|
+
value: 0,
|
|
62
|
+
symbol: 'DOCK',
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
'@context': ['https://w3id.org/wallet/v1'],
|
|
66
|
+
id: '37NKEP14n9omsAgxJ3sn14XtHo2vs5a34UcpCyybXQJoUQ92',
|
|
67
|
+
type: 'Address',
|
|
68
|
+
value: '37NKEP14n9omsAgxJ3sn14XtHo2vs5a34UcpCyybXQJoUQ92',
|
|
69
|
+
address: '37NKEP14n9omsAgxJ3sn14XtHo2vs5a34UcpCyybXQJoUQ92',
|
|
70
|
+
name: 'Test',
|
|
71
|
+
correlation: [
|
|
72
|
+
'urn:uuid:c410e44a-9525-11ea-bb37-0242ac130002',
|
|
73
|
+
'c47f3ed0-b4b5-4983-8962-759196fd3ece',
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
describe('DocumentEntity', () => {
|
|
79
|
+
let dataStore: DataStore;
|
|
80
|
+
|
|
81
|
+
beforeAll(async () => {
|
|
82
|
+
dataStore = await createTestDataStore();
|
|
83
|
+
|
|
84
|
+
for (const document of mockDocuments) {
|
|
85
|
+
await createDocument({
|
|
86
|
+
dataStore,
|
|
87
|
+
json: document,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should be able to find a document by id', async () => {
|
|
93
|
+
const mockData = mockDocuments[0];
|
|
94
|
+
const document = await getDocumentById({
|
|
95
|
+
dataStore,
|
|
96
|
+
id: mockData.id,
|
|
97
|
+
});
|
|
98
|
+
expect(document).toBeDefined();
|
|
99
|
+
expect(document.type).toEqual(mockData.type);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('getDocumentsByType', () => {
|
|
103
|
+
it('should be able to query all VerifiableCredential', async () => {
|
|
104
|
+
const documents = await getDocumentsByType({
|
|
105
|
+
dataStore,
|
|
106
|
+
type: 'VerifiableCredential',
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
expect(documents).toBeDefined();
|
|
110
|
+
expect(documents.length).toEqual(2);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('should be able query only BasicCredential', async () => {
|
|
114
|
+
const documents = await getDocumentsByType({
|
|
115
|
+
dataStore,
|
|
116
|
+
type: 'BasicCredential',
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(documents).toBeDefined();
|
|
120
|
+
expect(documents.length).toEqual(1);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('getDocumentCorrelations', () => {
|
|
125
|
+
it('expect to query all correlations', async () => {
|
|
126
|
+
const documents = await getDocumentCorrelations({
|
|
127
|
+
dataStore,
|
|
128
|
+
documentId: '37NKEP14n9omsAgxJ3sn14XtHo2vs5a34UcpCyybXQJoUQ92',
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
expect(documents).toBeDefined();
|
|
132
|
+
expect(documents.length).toEqual(2);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Column,
|
|
3
|
+
Entity,
|
|
4
|
+
JoinTable,
|
|
5
|
+
ManyToMany,
|
|
6
|
+
PrimaryColumn,
|
|
7
|
+
Relation,
|
|
8
|
+
} from '../../typeorm';
|
|
9
|
+
import {DocumentTypeEntity} from '../document-type.entity';
|
|
10
|
+
|
|
11
|
+
@Entity()
|
|
12
|
+
export class DocumentEntity {
|
|
13
|
+
@PrimaryColumn('text')
|
|
14
|
+
id: string;
|
|
15
|
+
|
|
16
|
+
@Column('text', {
|
|
17
|
+
nullable: true,
|
|
18
|
+
})
|
|
19
|
+
networkId: string;
|
|
20
|
+
|
|
21
|
+
@Column('simple-array')
|
|
22
|
+
type: string[];
|
|
23
|
+
|
|
24
|
+
@ManyToMany(() => DocumentTypeEntity, type => type?.documents, {
|
|
25
|
+
cascade: ['insert'],
|
|
26
|
+
eager: true,
|
|
27
|
+
nullable: true,
|
|
28
|
+
})
|
|
29
|
+
@JoinTable()
|
|
30
|
+
_typeRel: Relation<DocumentTypeEntity[]>;
|
|
31
|
+
|
|
32
|
+
// Add the ManyToMany self-referencing relationship to track document correlations
|
|
33
|
+
// @ManyToMany(() => DocumentEntity, {cascade: true, nullable: true})
|
|
34
|
+
// @JoinTable()
|
|
35
|
+
@Column('simple-array')
|
|
36
|
+
correlation: string[];
|
|
37
|
+
|
|
38
|
+
@Column('blob')
|
|
39
|
+
data: string;
|
|
40
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {WalletDocument, DataStore} from '../../types';
|
|
2
|
+
import {toWalletDocument} from './helpers';
|
|
3
|
+
import {DocumentEntity} from './document.entity';
|
|
4
|
+
|
|
5
|
+
function toWalletDocumentExpanded(entity: DocumentEntity): WalletDocument {
|
|
6
|
+
const result = toWalletDocument(entity);
|
|
7
|
+
|
|
8
|
+
if (result) {
|
|
9
|
+
result['_networkId'] = entity.networkId;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get all documents
|
|
17
|
+
*
|
|
18
|
+
* @param dataStore
|
|
19
|
+
*/
|
|
20
|
+
export async function getAllDocuments({
|
|
21
|
+
dataStore,
|
|
22
|
+
allNetworks,
|
|
23
|
+
}: {
|
|
24
|
+
dataStore: DataStore;
|
|
25
|
+
allNetworks?: boolean;
|
|
26
|
+
}): Promise<WalletDocument[]> {
|
|
27
|
+
const repository = dataStore.db.getRepository(DocumentEntity);
|
|
28
|
+
const entities = await repository.find({
|
|
29
|
+
where: allNetworks
|
|
30
|
+
? {}
|
|
31
|
+
: {
|
|
32
|
+
networkId: dataStore.networkId,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const mapDocument = allNetworks ? toWalletDocumentExpanded : toWalletDocument;
|
|
37
|
+
|
|
38
|
+
return Promise.all(entities.map(mapDocument));
|
|
39
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {ContextProps, WalletDocument} from '../../types';
|
|
2
|
+
import {DocumentEntity} from './document.entity';
|
|
3
|
+
import {createQueryBuilder} from '../../typeorm';
|
|
4
|
+
import {toWalletDocument} from './helpers';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get documents by type
|
|
8
|
+
* @param dataStore
|
|
9
|
+
* @param type
|
|
10
|
+
*/
|
|
11
|
+
export async function getDocumentsByType({
|
|
12
|
+
dataStore,
|
|
13
|
+
type,
|
|
14
|
+
}: ContextProps & {
|
|
15
|
+
type: string;
|
|
16
|
+
}): Promise<WalletDocument[]> {
|
|
17
|
+
const repository = dataStore.db.getRepository(DocumentEntity);
|
|
18
|
+
|
|
19
|
+
const documents = repository
|
|
20
|
+
.createQueryBuilder('document')
|
|
21
|
+
.leftJoinAndSelect('document._typeRel', 'documentType')
|
|
22
|
+
.where('documentType.id = :type', {type})
|
|
23
|
+
.andWhere('document.networkId = :networkId', {
|
|
24
|
+
networkId: dataStore.networkId,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const result = await documents.getMany();
|
|
28
|
+
|
|
29
|
+
return result.map(toWalletDocument);
|
|
30
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {ContextProps, WalletDocument} from '../../types';
|
|
2
|
+
import {toWalletDocument} from './helpers';
|
|
3
|
+
import {DocumentEntity} from './document.entity';
|
|
4
|
+
import {In} from 'typeorm';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get document by id
|
|
8
|
+
* @param dataStore
|
|
9
|
+
* @param id
|
|
10
|
+
*/
|
|
11
|
+
export async function getDocumentById({
|
|
12
|
+
dataStore,
|
|
13
|
+
id,
|
|
14
|
+
}: ContextProps & {
|
|
15
|
+
id: string;
|
|
16
|
+
}): Promise<WalletDocument> {
|
|
17
|
+
const repository = dataStore.db.getRepository(DocumentEntity);
|
|
18
|
+
|
|
19
|
+
const entity = await repository.findOne({
|
|
20
|
+
where: {
|
|
21
|
+
id: id,
|
|
22
|
+
networkId: dataStore.networkId,
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return toWalletDocument(entity);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function getDocumentsById({
|
|
30
|
+
dataStore,
|
|
31
|
+
idList,
|
|
32
|
+
}: ContextProps & {
|
|
33
|
+
idList: string[];
|
|
34
|
+
}): Promise<WalletDocument> {
|
|
35
|
+
const repository = dataStore.db.getRepository(DocumentEntity);
|
|
36
|
+
|
|
37
|
+
const entities = await repository.find({
|
|
38
|
+
where: {
|
|
39
|
+
id: In(idList),
|
|
40
|
+
networkId: dataStore.networkId,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return entities.map(toWalletDocument);
|
|
45
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {ContextProps, WalletDocument} from '../../types';
|
|
2
|
+
import {DocumentEntity} from './document.entity';
|
|
3
|
+
import {In} from '../../typeorm';
|
|
4
|
+
import {toWalletDocument} from './helpers';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get related documents
|
|
8
|
+
* @param dataStore
|
|
9
|
+
* @param type
|
|
10
|
+
*/
|
|
11
|
+
export async function getDocumentCorrelations({
|
|
12
|
+
dataStore,
|
|
13
|
+
documentId,
|
|
14
|
+
}: ContextProps & {
|
|
15
|
+
documentId: string;
|
|
16
|
+
}): Promise<WalletDocument[]> {
|
|
17
|
+
const repository = dataStore.db.getRepository(DocumentEntity);
|
|
18
|
+
|
|
19
|
+
const entity = await repository.findOne({
|
|
20
|
+
where: {
|
|
21
|
+
id: documentId,
|
|
22
|
+
networkId: dataStore.networkId,
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const data = await repository.find({
|
|
27
|
+
where: {
|
|
28
|
+
id: In(entity.correlation),
|
|
29
|
+
networkId: dataStore.networkId,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return data.map(toWalletDocument);
|
|
34
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {ContextProps, WalletDocument} from '../../types';
|
|
2
|
+
import {DocumentTypeEntity} from '../document-type.entity';
|
|
3
|
+
import assert from 'assert';
|
|
4
|
+
import {logger} from '../../logger';
|
|
5
|
+
import {DocumentEntity} from './document.entity';
|
|
6
|
+
import {In} from '../../typeorm';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get document by id
|
|
10
|
+
* @param dataStore
|
|
11
|
+
* @param types
|
|
12
|
+
*/
|
|
13
|
+
export async function getOrCreateDocumentTypes({
|
|
14
|
+
dataStore,
|
|
15
|
+
types,
|
|
16
|
+
}: ContextProps & {
|
|
17
|
+
types: string | string[];
|
|
18
|
+
}): Promise<DocumentTypeEntity[]> {
|
|
19
|
+
assert(!!types, 'Document type must be provided');
|
|
20
|
+
|
|
21
|
+
logger.debug(`getOrCreateDocumentTypes: ${JSON.stringify(types)}`);
|
|
22
|
+
|
|
23
|
+
if (!Array.isArray(types)) {
|
|
24
|
+
types = [types];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const typeRepository = dataStore.db.getRepository(DocumentTypeEntity);
|
|
28
|
+
const typeEntityList = [];
|
|
29
|
+
|
|
30
|
+
for (const type of types) {
|
|
31
|
+
let typeEntity = await typeRepository.findOne({
|
|
32
|
+
where: {
|
|
33
|
+
id: type,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (!typeEntity) {
|
|
38
|
+
typeEntity = typeRepository.create({
|
|
39
|
+
id: type,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
await typeRepository.save(typeEntity);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
typeEntityList.push(typeEntity);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return typeEntityList;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function findDocumentEntitiesById({
|
|
52
|
+
dataStore,
|
|
53
|
+
entityIds,
|
|
54
|
+
}: ContextProps & {
|
|
55
|
+
entityIds: string[];
|
|
56
|
+
}): Promise<DocumentEntity[]> {
|
|
57
|
+
assert(!!entityIds, 'Document ids must be provided');
|
|
58
|
+
const repository = dataStore.db.getRepository(DocumentEntity);
|
|
59
|
+
|
|
60
|
+
const entities = await repository.findBy({
|
|
61
|
+
id: In(entityIds),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return entities;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Convert document entity to wallet document
|
|
69
|
+
* @param entity
|
|
70
|
+
*/
|
|
71
|
+
export function toWalletDocument(entity: DocumentEntity): WalletDocument {
|
|
72
|
+
if (!entity) {
|
|
73
|
+
return entity;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const result = JSON.parse(entity.data);
|
|
77
|
+
|
|
78
|
+
if (!result.id) {
|
|
79
|
+
result.id = entity.id;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Convert wallet document to document entity
|
|
87
|
+
* @param walletDocument
|
|
88
|
+
*/
|
|
89
|
+
export async function toDocumentEntity({
|
|
90
|
+
dataStore,
|
|
91
|
+
document,
|
|
92
|
+
}: ContextProps & {
|
|
93
|
+
document: any;
|
|
94
|
+
}): Promise<DocumentEntity> {
|
|
95
|
+
const type = document.type || [];
|
|
96
|
+
const _typeRel = await getOrCreateDocumentTypes({
|
|
97
|
+
dataStore,
|
|
98
|
+
types: document.type,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
id: document.id,
|
|
103
|
+
type: type,
|
|
104
|
+
data: JSON.stringify(document),
|
|
105
|
+
_typeRel,
|
|
106
|
+
correlation: document.correlation || [],
|
|
107
|
+
networkId: dataStore.networkId,
|
|
108
|
+
} as DocumentEntity;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const saveOptions = {
|
|
112
|
+
// Android is having issues when running multiple document saves in a short period of time
|
|
113
|
+
// We will disable transactions for now until we find a better solution
|
|
114
|
+
transaction: false,
|
|
115
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './document.entity';
|
|
2
|
+
export * from './create-document';
|
|
3
|
+
export * from './helpers';
|
|
4
|
+
export * from './get-documens-by-type';
|
|
5
|
+
export * from './get-document-by-id';
|
|
6
|
+
export * from './get-all-documents';
|
|
7
|
+
export * from './update-document';
|
|
8
|
+
export * from './remove-document';
|
|
9
|
+
export * from './get-document-correlations';
|