Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions app/lib/handleZcapRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ZCAP_EXPIRES } from '../../app.config';
import { ZCAP_EXPIRES, WAS } from '../../app.config';
// @ts-ignore
import { ZcapClient } from '@digitalcredentials/ezcap';
import { Ed25519Signature2020 } from '@digitalcredentials/ed25519-signature-2020';
import AsyncStorage from '@react-native-async-storage/async-storage';

import { displayGlobalModal } from './globalModal';
import { getRootSigner } from './getRootSigner';
Expand Down Expand Up @@ -48,28 +49,37 @@ export default async function handleZcapRequest({
throw new Error('User denied Zcap delegation');
}

let rootSigner
try {
rootSigner = await getRootSigner();
} catch (error) {
throw new Error(`Error getting root signer: ${error}`);
}


const invocationTargetType = typeof invocationTarget === 'string' ? invocationTarget : invocationTarget.type;

const rootSigner = await getRootSigner();

const parentCapability = `urn:zcap:root:${encodeURIComponent(invocationTargetType)}`;
// Get stored space UUID
const storedSpaceUUID = await AsyncStorage.getItem(WAS.KEYS.SPACE_ID);
if (!storedSpaceUUID) {
throw new Error('No stored space ID found for WAS delegation');
}

const zcapClient = new ZcapClient({
SuiteClass: Ed25519Signature2020,
delegationSigner: rootSigner
delegationSigner: rootSigner,
});

const invocationTargetUrl = new URL(`/space/${storedSpaceUUID}`, WAS.BASE_URL).toString();

const allowedActions = ['GET', 'POST', 'PUT', 'DELETE'];

const parentCapability = `urn:zcap:root:${encodeURIComponent(invocationTargetUrl)}`;

const delegatedZcap = await zcapClient.delegate({
capability: parentCapability,
controller,
invocationTarget: invocationTargetType,
allowedActions: allowedAction,
expires: ZCAP_EXPIRES.toISOString()
invocationTarget: invocationTargetUrl,
allowedActions,
expires: ZCAP_EXPIRES.toISOString(),
});

return {
zcap: delegatedZcap
};
return { zcap: delegatedZcap };
}
18 changes: 16 additions & 2 deletions app/screens/WAS/WasConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,21 @@ export default function WasConnectScreen() {
} catch (error) {
console.error('Connection failed:', error);
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
setStatusMessage(`Failed to connect wallet: ${errorMessage}`);
if (
typeof errorMessage === 'string' &&
(
errorMessage.includes('WAS is not enabled') ||
errorMessage.includes('Root signer not found in wallet') ||
errorMessage.includes('WAS connection required') ||
errorMessage.includes('Error getting root signer')
)
) {
setStatusMessage(
'This request first requires you to set up a WAS connection. Go to the dev menu and enable "Connect to WAS".'
);
} else {
setStatusMessage(`Failed to connect wallet: ${errorMessage}`);
}
setStatusType('error');

if (navigationRef.isReady()) {
Expand Down Expand Up @@ -102,7 +116,7 @@ const styles = StyleSheet.create({
padding: 20,
},
statusText: {
marginTop: 20,
marginBottom: 20,
fontSize: 16,
textAlign: 'center',
},
Expand Down