Skip to content

Commit ba6aca9

Browse files
authored
Merge branch 'master' into fix/account-backup-warning
2 parents 59437f5 + 467004b commit ba6aca9

File tree

7 files changed

+123
-227
lines changed

7 files changed

+123
-227
lines changed

packages/core/src/did-provider.ts

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,52 @@ export async function createDIDock({wallet, address, name}) {
8383
await wallet.add(dockDIDResolution);
8484
}
8585

86-
const createDIDKeyDocument = async (
86+
async function editDID({wallet, id, name}){
87+
if (typeof id === 'string' && id.length > 0) {
88+
const docs = await wallet.query({
89+
id,
90+
});
91+
if (docs.length === 1) {
92+
const doc = docs[0];
93+
await wallet.update({
94+
...doc,
95+
name,
96+
});
97+
}
98+
} else {
99+
throw new Error('Document ID is not set');
100+
}
101+
};
102+
103+
async function deleteDID({wallet, id}){
104+
if (typeof id === 'string' && id.length > 0) {
105+
return await wallet.remove(id);
106+
} else {
107+
throw Error('Document ID is not set');
108+
}
109+
}
110+
111+
async function exportDID({wallet, id, password }){
112+
const existingDoc = await wallet.getDocumentById(id);
113+
if (existingDoc) {
114+
const allCorrelationDocuments = (
115+
await wallet.resolveCorrelations(id)
116+
).filter(doc => doc && doc?.id !== existingDoc.id);
117+
const documentsToExport = [existingDoc, ...allCorrelationDocuments];
118+
119+
if (allCorrelationDocuments.length >= 1) {
120+
return wallet.exportDocuments({
121+
documents: documentsToExport,
122+
password,
123+
});
124+
}
125+
throw new Error('DID KeyPair not found');
126+
}
127+
128+
throw new Error('DID Document not found');
129+
}
130+
131+
export const createDIDKeyDocument = async (
87132
keypairDoc: any,
88133
didDocParams: any = {},
89134
) => {
@@ -110,11 +155,11 @@ const createDIDKeyDocument = async (
110155
};
111156
};
112157

113-
export async function createDIKey({wallet, name}) {
158+
export async function createDIDKey({wallet, name, derivePath=undefined, type=undefined}) {
114159
assert(!!wallet, 'wallet is required');
115160
assert(!!name, 'name is required');
116161

117-
const keyDoc = await didServiceRPC.generateKeyDoc({});
162+
const keyDoc = await didServiceRPC.generateKeyDoc({derivePath, type});
118163

119164
const {didDocumentResolution} = await createDIDKeyDocument(keyDoc, {
120165
name,
@@ -156,7 +201,7 @@ export async function ensureDID({wallet}) {
156201
assert(!!wallet, 'wallet is required');
157202
const dids = await getAll({wallet});
158203
if (dids.length === 0) {
159-
return createDIKey({wallet, name: 'Default DID'});
204+
return createDIDKey({wallet, name: 'Default DID'});
160205
}
161206
}
162207

@@ -166,7 +211,10 @@ export interface IDIDProvider {
166211
password: string;
167212
}): Promise<void>;
168213
createDIDock(params: {address: string; name: string}): Promise<void>;
169-
createDIDKey(params: {name: string}): Promise<any>;
214+
createDIDKey(params: {name: string, derivePath?:string, type?: string}): Promise<any>;
215+
editDID(params: {id: string; name: string}): Promise<void>;
216+
deleteDID(params: {id: string;}): Promise<void>;
217+
exportDID(params: {id: string; password: string}): Promise<void>;
170218
getAll(): Promise<any>;
171219
getDIDKeyPairs(): Promise<any>;
172220
ensureDID(): Promise<any>;
@@ -180,8 +228,17 @@ export function createDIDProvider({wallet}): IDIDProvider {
180228
async createDIDock({address, name}) {
181229
return createDIDock({wallet, address, name});
182230
},
183-
async createDIDKey({name}) {
184-
return createDIKey({wallet, name});
231+
async createDIDKey({name, derivePath, type}) {
232+
return createDIDKey({wallet, name, derivePath, type});
233+
},
234+
async editDID(params) {
235+
return editDID({wallet, ...params});
236+
},
237+
async deleteDID(params) {
238+
return deleteDID({wallet, ...params});
239+
},
240+
async exportDID(params) {
241+
return exportDID({wallet, ...params});
185242
},
186243
async getAll() {
187244
return getAll({wallet});

packages/data-store/src/configs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const DEFAULT_CONFIGS: DataStoreConfigs = {
1111
id: 'mainnet',
1212
credentialHostnames: ['creds.dock.io'],
1313
configs: {
14-
substrateUrl: 'wss://mainnet-node.dock.io',
14+
substrateUrl: 'https://mainnet-node.dock.io/h',
1515
addressPrefix: 22,
1616
},
1717
},
@@ -20,7 +20,7 @@ export const DEFAULT_CONFIGS: DataStoreConfigs = {
2020
id: 'testnet',
2121
credentialHostnames: ['creds-testnet.dock.io', 'creds-staging.dock.io'],
2222
configs: {
23-
substrateUrl: 'wss://knox-1.dock.io',
23+
substrateUrl: 'https://knox-1.dock.io/h',
2424
addressPrefix: 21,
2525
},
2626
},

packages/react-native/lib/accountsHooks.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {getAccount, useWallet} from './index';
22
import {useMemo} from 'react';
33

44
export function useAccounts() {
5-
const {documents, wallet} = useWallet({syncDocs: true});
5+
const { documents, wallet } = useWallet();
66
const accounts = useMemo(() => {
77
if (Array.isArray(documents)) {
88
return documents
@@ -21,9 +21,7 @@ export function useAccounts() {
2121
return [];
2222
}, [documents, wallet?.accounts]);
2323

24-
return useMemo(() => {
25-
return {
26-
accounts,
27-
};
28-
}, [accounts]);
24+
return {
25+
accounts,
26+
};
2927
}

packages/react-native/lib/credentialPresentHooks.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/react-native/lib/credentials/credentialHooks.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async function getCredentialValidityStatus(credential) {
6767
}
6868

6969
export function useCredentialUtils() {
70-
const {documents, wallet} = useWallet({syncDocs: true});
70+
const { documents, wallet } = useWallet();
7171

7272
const credentials = useMemo(() => {
7373
if (Array.isArray(documents)) {
@@ -83,39 +83,31 @@ export function useCredentialUtils() {
8383
return [];
8484
}, [documents]);
8585

86-
const doesCredentialExist = useCallback((allCredentials, credentialToAdd) => {
86+
const doesCredentialExist = (allCredentials, credentialToAdd) => {
8787
return !!allCredentials.find(item => item.id === credentialToAdd.id);
88-
}, []);
88+
};
8989

90-
const saveCredential = useCallback(
91-
async jsonData => {
92-
validateCredential(jsonData);
93-
if (doesCredentialExist(credentials, jsonData)) {
94-
throw new Error('This credential already exists in the wallet');
95-
}
96-
await wallet.addDocument(jsonData);
97-
},
98-
[credentials, doesCredentialExist, wallet],
99-
);
100-
const deleteCredential = useCallback(
101-
async credentialId => {
102-
assert(
103-
typeof credentialId === 'string' && credentialId.length > 0,
104-
'Credential ID is not set',
105-
);
106-
return await wallet.remove(credentialId);
107-
},
108-
[wallet],
109-
);
90+
const saveCredential = async jsonData => {
91+
validateCredential(jsonData);
92+
if (doesCredentialExist(credentials, jsonData)) {
93+
throw new Error('This credential already exists in the wallet');
94+
}
95+
await wallet.addDocument(jsonData);
96+
};
97+
const deleteCredential = async credentialId => {
98+
assert(
99+
typeof credentialId === 'string' && credentialId.length > 0,
100+
'Credential ID is not set',
101+
);
102+
return await wallet.remove(credentialId);
103+
};
110104

111-
return useMemo(() => {
112-
return {
113-
credentials,
114-
doesCredentialExist,
115-
saveCredential,
116-
deleteCredential,
117-
};
118-
}, [credentials, doesCredentialExist, saveCredential, deleteCredential]);
105+
return {
106+
credentials,
107+
doesCredentialExist,
108+
saveCredential,
109+
deleteCredential,
110+
};
119111
}
120112
export function isInThePast(date) {
121113
const today = new Date();
@@ -160,6 +152,7 @@ function buildStatusResponse(status, error = null) {
160152
};
161153
}
162154

155+
//TODO: Implement a caching mechanism that is attached to credentials context instead of global
163156
export let cachedCredentialStatus = {};
164157

165158
export function useGetCredentialStatus({credential}) {

0 commit comments

Comments
 (0)