Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MWALL-712] New holder keys/identities are not linked to holder contact #360

Open
wants to merge 3 commits into
base: funke
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {DID_PREFIX} from '../@config/constants';
import {DEFAULT_DB_CONNECTION} from '../services/databaseService';
import {IRequiredContext, SupportedDidMethodEnum, TAgentTypes} from '../types';
import {createAgentPlugins} from './plugins';
import {EventEmitter} from 'events';
import DefaultCallbacks = com.sphereon.crypto.DefaultCallbacks;
import {initializeIdentityCreatedEventListeners} from '../services/identityService';

export const didResolver = new Resolver({
...getDidEbsiResolver(),
Expand Down Expand Up @@ -44,5 +46,7 @@ const agent = createAgent<TAgentTypes>({

export default agent;
export const agentContext: IRequiredContext = {...agent.context, agent};

DefaultCallbacks.setCoseCryptoDefault(new CoseCryptoService(agentContext));

export const agentEventBus: EventEmitter = (agent as any).eventBus as EventEmitter // agent has an eventBus but it is nog exposing it, it only exposes its events to the agent plugins, but we want them here as well
initializeIdentityCreatedEventListeners()
30 changes: 25 additions & 5 deletions src/services/identityService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {IIdentifier, IKey} from '@veramo/core';
import Debug, {Debugger} from 'debug';

import {APP_ID, DID_PREFIX} from '../@config/constants';

Expand All @@ -12,12 +11,15 @@ import {
IdentifierAliasEnum,
IDispatchIdentifierArgs,
IRequiredContext,
KeyManagementSystemEnum,
SupportedDidMethodEnum,
} from '../types';
import {sphereonKeyManager} from '../agent/plugins';
import {OID4VCIHolderEvent} from '@sphereon/ssi-sdk.oid4vci-holder';
import {agentEventBus} from '../agent';
import {Siopv2HolderEvent} from '@sphereon/ssi-sdk.siopv2-oid4vp-op-auth';
import {Loggers} from '@sphereon/ssi-types';

const debug: Debugger = Debug(`${APP_ID}:identity`);
const logger = Loggers.DEFAULT.get(`${APP_ID}:identity`);

export const getIdentifiers = async (context: IRequiredContext): Promise<IIdentifier[]> => {
// TODO fully implement
Expand All @@ -37,6 +39,24 @@ export const createIdentifier = async (args: ICreateIdentifierArgs, context: IRe
return identifier;
};


export const initializeIdentityCreatedEventListeners = () => {
agentEventBus.addListener(OID4VCIHolderEvent.IDENTIFIER_CREATED, args => {
logger.debug('Received OID4VCIHolderEvent.IDENTIFIER_CREATED event, dispatching the new identifier', args.identifier)
dispatchIdentifier({identifier: args.identifier}).then(value => {
logger.debug('identifier linked to the active user')
})
})
agentEventBus.addListener(Siopv2HolderEvent.IDENTIFIER_CREATED, args => {
logger.debug('Received Siopv2HolderEvent.IDENTIFIER_CREATED event, dispatching the new identifier', args.result)
dispatchIdentifier({identifier: args.result}).then(value => {
logger.debug('identifier linked to the active user')
})
})
}



export const dispatchIdentifier = async (args: IDispatchIdentifierArgs): Promise<void> => {
const {identifier} = args;
if (store.getState().user.users.size > 0) {
Expand All @@ -60,7 +80,7 @@ export const getOrCreatePrimaryIdentifier = async (args: ICreateOrGetIdentifierA
args?.createOpts?.options?.type === undefined || identifier.keys.some((key: IKey) => key.type === args?.createOpts?.options?.type),
);

debug(`Currently available identifiers for ${args?.method} / ${args?.createOpts?.options?.type}: ${identifiers.length}`);
logger.debug(`Currently available identifiers for ${args?.method} / ${args?.createOpts?.options?.type}: ${identifiers.length}`);

// Currently we only support one identifier

Expand All @@ -71,6 +91,6 @@ export const getOrCreatePrimaryIdentifier = async (args: ICreateOrGetIdentifierA
}
const identifier: IIdentifier = !identifiers || identifiers.length == 0 ? await createIdentifier(args, context) : identifiers[0];

debug(`identifier: ${JSON.stringify(identifier, null, 2)}`);
logger.debug(`identifier: ${JSON.stringify(identifier, null, 2)}`);
return await context.agent.didManagerGet({did: identifier.did});
};
23 changes: 16 additions & 7 deletions src/store/actions/user.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,30 @@ export const getUsers = (): ThunkAction<Promise<void>, RootState, unknown, Actio
export const addIdentifier = (args: IAddIdentifierArgs): ThunkAction<Promise<void>, RootState, unknown, Action> => {
return async (dispatch: ThunkDispatch<RootState, unknown, Action>, getState: CombinedState<any>) => {
dispatch({type: USERS_LOADING});
const userSate: IUserState = getState().user;
const userState: IUserState = getState().user;
const user: IUser = userState.users.values().next().value;

// Check if identifier already exists
const isDuplicate = user.identifiers.some(identifier => identifier.did === args.did);
if (isDuplicate) {
return;
}

const userIdentifier = {
did: args.did,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};

// We are currently only supporting a single user right now
const user: IUser = {
...userSate.users.values().next().value,
identifiers: [...userSate.users.values().next().value.identifiers, userIdentifier],
const updatedUser: IUser = {
...user,
identifiers: [...user.identifiers, userIdentifier],
};

userServiceUpdateUser(user)
.then((user: IUser) => dispatch({type: UPDATE_USER_SUCCESS, payload: user}))
.catch(() => dispatch({type: UPDATE_USER_FAILED}));
userServiceUpdateUser(updatedUser)
.then((user: IUser) => dispatch({type: UPDATE_USER_SUCCESS, payload: user}))
.catch(() => dispatch({type: UPDATE_USER_FAILED}));
};
};

Expand Down