Skip to content

Commit

Permalink
feat: Add SIOPv2OID4VP RP auth and REST module
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Apr 11, 2023
1 parent a085c81 commit 91b1da3
Show file tree
Hide file tree
Showing 100 changed files with 2,426 additions and 2,568 deletions.
5 changes: 2 additions & 3 deletions packages/did-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
},
"dependencies": {
"@sphereon/did-uni-client": "^0.6.0",
"@sphereon/jsencrypt": "3.3.2-unstable.0",
"@sphereon/ssi-sdk-core": "^0.9.0",
"@sphereon/ssi-types": "^0.9.0",
"@trust/keyto": "^2.0.0-alpha1",
"@veramo/core": "4.2.0",
"@veramo/utils": "4.2.0",
"did-resolver": "^4.0.1",
"did-resolver": "^4.1.0",
"did-jwt": "^6.11.6",
"elliptic": "^6.5.4",
"uint8arrays": "^3.1.1"
},
Expand Down
80 changes: 76 additions & 4 deletions packages/did-utils/src/didFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { UniResolver } from '@sphereon/did-uni-client'
import { DIDDocument, DIDDocumentSection, DIDResolutionResult, IAgentContext, IDIDManager, IIdentifier, IResolver } from '@veramo/core'
import {
DIDDocument,
DIDDocumentSection,
DIDResolutionResult,
IAgentContext,
IDIDManager,
IIdentifier,
IKey,
IResolver,
} from '@veramo/core'
import {
_ExtendedIKey,
_ExtendedVerificationMethod,
Expand All @@ -14,6 +23,7 @@ import { DIDResolutionOptions, Resolvable, VerificationMethod } from 'did-resolv
import elliptic from 'elliptic'
import * as u8a from 'uint8arrays'
import { hexKeyFromPEMBasedJwk } from './x509-utils'
import { IDIDOptions, IIdentifierOpts } from './types'

export const getFirstKeyWithRelation = async (
identifier: IIdentifier,
Expand Down Expand Up @@ -144,10 +154,10 @@ export async function mapIdentifierKeysToDocWithJwkSupport(
const localKeys = identifier.keys.filter(isDefined)
// finally map the didDocument keys to the identifier keys by comparing `publicKeyHex`
const extendedKeys: _ExtendedIKey[] = documentKeys
.map((verificationMethod) => {
if (verificationMethod.type !== 'JsonWebKey2020') {
. map((verificationMethod) => {
/*if (verificationMethod.type !== 'JsonWebKey2020') {
return null
}
}*/
const localKey = localKeys.find((localKey) => localKey.publicKeyHex === verificationMethod.publicKeyHex)
if (localKey) {
const { meta, ...localProps } = localKey
Expand All @@ -165,6 +175,68 @@ export async function getAgentDIDMethods(context: IAgentContext<IDIDManager>) {
return (await context.agent.didManagerGetProviders()).map((provider) => provider.toLowerCase().replace('did:', ''))
}

export async function getIdentifier(identifierOpts: IIdentifierOpts, context: IAgentContext<IDIDManager>): Promise<IIdentifier> {
if (typeof identifierOpts.identifier === 'string') {
return context.agent.didManagerGet({ did: identifierOpts.identifier })
} else if (typeof identifierOpts.identifier === 'object') {
return identifierOpts.identifier
}
throw Error(`Cannot get agent identifier value from options`)
}

export function getDID(identifierOpts: IIdentifierOpts): string {
if (typeof identifierOpts.identifier === 'string') {
return identifierOpts.identifier
} else if (typeof identifierOpts.identifier === 'object') {
return identifierOpts.identifier.did
}
throw Error(`Cannot get DID from identifier value`)
}

export function toDID(identifier: string | IIdentifier | Partial<IIdentifier>): string {
if (typeof identifier === 'string') {
return identifier
}
if (identifier.did) {
return identifier.did
}
throw Error(`No DID value present in identifier`)
}

export function toDIDs(identifiers?: (string | IIdentifier | Partial<IIdentifier>)[]): string[] {
if (!identifiers) {
return []
}
return identifiers.map(toDID)
}

export async function getKey(
identifier: IIdentifier,
verificationMethodSection: DIDDocumentSection = 'authentication',
context: IAgentContext<IResolver>,
keyId?: string
): Promise<IKey> {
const keys = await mapIdentifierKeysToDocWithJwkSupport(identifier, verificationMethodSection, context)
if (!keys || keys.length === 0) {
throw new Error(`No keys found for verificationMethodSection: ${verificationMethodSection} and did ${identifier.did}`)
}

const identifierKey = keyId ? keys.find((key: _ExtendedIKey) => key.kid === keyId || key.meta.verificationMethod.id === keyId) : keys[0]
if (!identifierKey) {
throw new Error(`No matching verificationMethodSection key found for keyId: ${keyId}`)
}

return identifierKey
}

export function determineKid(key: IKey, idOpts: IIdentifierOpts): string {
return key.meta?.verificationMethod.id ?? idOpts.kid ?? key.kid
}

export async function getSupportedDIDMethods(didOpts: IDIDOptions, context: IAgentContext<IDIDManager>) {
return didOpts.supportedDIDMethods ?? (await getAgentDIDMethods(context))
}

export class AgentDIDResolver implements Resolvable {
private readonly context: IAgentContext<IResolver>
private readonly uniresolverFallback: boolean
Expand Down
24 changes: 24 additions & 0 deletions packages/did-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { JWTVerifyOptions } from 'did-jwt'
import { Resolvable } from 'did-resolver'
import { DIDDocumentSection, IIdentifier } from '@veramo/core'

export interface JWK extends JsonWebKey {
x5c?: string
x5u?: string
Expand All @@ -13,4 +17,24 @@ export interface X509Opts {
certificateChainPEM?: string // Base64 (not url!) encoded DER certificate chain. Please provide even if certificateChainURL is used!
}

export interface ResolveOpts {
jwtVerifyOpts?: JWTVerifyOptions
resolver?: Resolvable
resolveUrl?: string
noUniversalResolverFallback?: boolean
subjectSyntaxTypesSupported?: string[]
}

export interface IDIDOptions {
resolveOpts?: ResolveOpts
identifierOpts: IIdentifierOpts
supportedDIDMethods?: string[]
}

export interface IIdentifierOpts {
identifier: IIdentifier | string
verificationMethodSection?: DIDDocumentSection
kid?: string
}

export const DID_PREFIX = 'did:'
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Provides a {@link @veramo/did-manager#DIDManager | plugin} for the
* {@link @veramo/core#Agent} that implements {@link @veramo/core-types#IDIDManager} interface.
* Provides a {@link @veramo/kv-store#KeyValueStore} for the
* {@link @veramo/core#Agent} plugin that implements {@link @veramo/kv-store#IKeyValueStore} interface
*
* @packageDocumentation
*/
export { KeyValueStore } from './key-value-store.js';
export * from './store-adapters/tiered/index.js';
export * from './store-adapters/typeorm/index.js';
export * from './key-value-types.js';
export { KeyValueStore } from './key-value-store';
export * from './store-adapters/tiered/index';
export * from './store-adapters/typeorm/index';
export * from './key-value-types';
//# sourceMappingURL=index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IKeyValueStore, IKeyValueStoreOnArgs, IKeyValueStoreOptions, IValueData } from './key-value-types.js';
import { IKeyValueStore, IKeyValueStoreOnArgs, IKeyValueStoreOptions, IValueData } from './key-value-types';
/**
* Agent plugin that implements {@link @veramo/core-types#IKeyValueStore} interface
* Agent plugin that implements {@link @veramo/kv-store#IKeyValueStore} interface
* @public
*/
export declare class KeyValueStore<ValueType> implements IKeyValueStore<ValueType> {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 91b1da3

Please sign in to comment.