-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Siopv2Holder module implementing xstate Siopv2Machine
- Loading branch information
1 parent
2a300f4
commit 7dd0651
Showing
13 changed files
with
603 additions
and
414 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
packages/siopv2-holder/__tests__/shared/eventLoggerAgentLogic.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
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,77 @@ | ||
import { KeyUse } from '@sphereon/ssi-sdk-ext.did-resolver-jwk' | ||
import { _ExtendedIKey } from '@veramo/utils' | ||
import { | ||
CreateIdentifierArgs, | ||
DID_PREFIX, | ||
GetAuthenticationKeyArgs, | ||
GetIdentifierArgs, | ||
GetOrCreatePrimaryIdentifierArgs, | ||
IdentifierAliasEnum, | ||
IdentifierOpts, | ||
KeyManagementSystemEnum, | ||
SupportedDidMethodEnum, | ||
} from '../types/identifier' | ||
import { IDIDManager, IIdentifier, IKey, IResolver, TAgent } from '@veramo/core' | ||
import { getFirstKeyWithRelation } from '@sphereon/ssi-sdk-ext.did-utils' | ||
import { Siopv2HolderEvent } from '../types/ISiopv2Holder' | ||
|
||
export const getIdentifier = async (args: GetIdentifierArgs): Promise<IdentifierOpts> => { | ||
const { keyOpts, context } = args | ||
|
||
const identifier = | ||
keyOpts.identifier ?? | ||
(await getOrCreatePrimaryIdentifier({ | ||
context, | ||
opts: { | ||
method: keyOpts.didMethod, | ||
createOpts: { options: { type: keyOpts.keyType, use: KeyUse.Signature, codecName: keyOpts.codecName } }, | ||
}, | ||
})) | ||
const key: _ExtendedIKey = await getAuthenticationKey({ identifier, context }) | ||
const kid: string = key.meta.verificationMethod.id | ||
|
||
return { identifier, key, kid } | ||
} | ||
|
||
export const getAuthenticationKey = async (args: GetAuthenticationKeyArgs): Promise<_ExtendedIKey> => { | ||
const { identifier, context } = args | ||
const agentContext = { ...context, agent: context.agent as TAgent<IResolver & IDIDManager> } | ||
|
||
return ( | ||
(await getFirstKeyWithRelation(identifier, agentContext, 'authentication', false)) || | ||
((await getFirstKeyWithRelation(identifier, agentContext, 'verificationMethod', true)) as _ExtendedIKey) | ||
) | ||
} | ||
|
||
export const getOrCreatePrimaryIdentifier = async (args: GetOrCreatePrimaryIdentifierArgs): Promise<IIdentifier> => { | ||
const { context, opts } = args | ||
|
||
const identifiers = (await context.agent.didManagerFind(opts?.method ? { provider: `${DID_PREFIX}:${opts?.method}` } : {})).filter( | ||
(identifier: IIdentifier) => | ||
opts?.createOpts?.options?.type === undefined || identifier.keys.some((key: IKey) => key.type === opts?.createOpts?.options?.type), | ||
) | ||
|
||
if (opts?.method === SupportedDidMethodEnum.DID_KEY) { | ||
const createOpts = opts?.createOpts ?? {} | ||
createOpts.options = { codecName: 'EBSI', type: 'Secp256r1', ...createOpts } | ||
opts.createOpts = createOpts | ||
} | ||
const identifier: IIdentifier = !identifiers || identifiers.length == 0 ? await createIdentifier({ context, opts }) : identifiers[0] | ||
|
||
return await context.agent.didManagerGet({ did: identifier.did }) | ||
} | ||
|
||
export const createIdentifier = async (args: CreateIdentifierArgs): Promise<IIdentifier> => { | ||
const { context, opts } = args | ||
|
||
const identifier = await context.agent.didManagerCreate({ | ||
kms: opts?.createOpts?.kms ?? KeyManagementSystemEnum.LOCAL, | ||
...(opts?.method && { provider: `${DID_PREFIX}:${opts?.method}` }), | ||
alias: opts?.createOpts?.alias ?? `${IdentifierAliasEnum.PRIMARY}-${opts?.method}-${opts?.createOpts?.options?.type}-${new Date().toUTCString()}`, | ||
options: opts?.createOpts?.options, | ||
}) | ||
|
||
await context.agent.emit(Siopv2HolderEvent.IDENTIFIER_CREATED, { identifier }) | ||
|
||
return identifier | ||
} |
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
Oops, something went wrong.