@sphereon/did-provider-oyd 0.28.1-feature.oyd.cmsm.improv.20 → 0.28.1-next.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import { importProvidedOrGeneratedKey } from '@sphereon/ssi-sdk-ext.key-utils'
1
2
  import { IAgentContext, IIdentifier, IKey, IKeyManager, IService, TKeyType } from '@veramo/core'
2
3
  import { AbstractIdentifierProvider } from '@veramo/did-manager'
3
4
  import { KeyManager } from '@veramo/key-manager'
@@ -5,6 +6,7 @@ import fetch from 'cross-fetch'
5
6
  import Multibase from 'multibase'
6
7
  import Multicodec from 'multicodec'
7
8
 
9
+ // @ts-ignore
8
10
  import * as u8a from 'uint8arrays'
9
11
 
10
12
  import Debug from 'debug'
@@ -12,9 +14,9 @@ import type {
12
14
  CMSMCallbackOpts,
13
15
  OydConstructorOptions,
14
16
  OydCreateIdentifierOptions,
15
- OydDidHoldKeysArgs,
17
+ // OydDidHoldKeysArgs,
16
18
  OydDidSupportedKeyTypes,
17
- } from './types/oyd-provider-types.js'
19
+ } from './types/oyd-provider-types'
18
20
 
19
21
  const debug = Debug('veramo:oyd-did:identifier-provider')
20
22
  const OYDID_REGISTRAR_URL = 'https://oydid-registrar.data-container.net/1.0/createIdentifier'
@@ -47,7 +49,7 @@ export class OydDIDProvider extends AbstractIdentifierProvider {
47
49
  }
48
50
 
49
51
  async createIdentifier(
50
- { kms, options }: { kms?: string; options: OydCreateIdentifierOptions },
52
+ { kms, alias, options }: { kms?: string; alias?: string; options: OydCreateIdentifierOptions },
51
53
  context: IContext
52
54
  ): Promise<Omit<IIdentifier, 'provider'>> {
53
55
  const resolvedKms = await this.assertedKms(kms, this.defaultKms)
@@ -62,7 +64,7 @@ export class OydDIDProvider extends AbstractIdentifierProvider {
62
64
  const body = {
63
65
  options: {
64
66
  cmsm: false,
65
- key_type: options.keyType ?? 'Secp256r1',
67
+ key_type: options.type ?? 'Secp256r1',
66
68
  },
67
69
  }
68
70
  let didDoc: any | undefined
@@ -84,15 +86,18 @@ export class OydDIDProvider extends AbstractIdentifierProvider {
84
86
  return Promise.reject(Error('There has been a problem with the fetch operation: ' + error.toString()))
85
87
  }
86
88
 
87
- const keyType: OydDidSupportedKeyTypes = options?.keyType ?? 'Secp256r1'
88
- const key = await this.importOrCreateKey(
89
+ const keyType: OydDidSupportedKeyTypes = options?.type ?? 'Secp256r1'
90
+ const key = await importProvidedOrGeneratedKey(
89
91
  {
90
92
  kms: resolvedKms,
93
+ alias: alias ?? options.alias ?? options.kid ?? `${didDoc.did}#key-doc`,
91
94
  options: {
92
- keyType,
93
- kid: didDoc.did + '#key-doc',
94
- publicKeyHex: didDoc.keys[0].publicKeyHex,
95
- privateKeyHex: didDoc.keys[0].privateKeyHex,
95
+ key: {
96
+ kid: `${didDoc.did}#key-doc`,
97
+ type: keyType,
98
+ publicKeyHex: didDoc.keys[0].publicKeyHex,
99
+ privateKeyHex: didDoc.keys[0].privateKeyHex,
100
+ },
96
101
  },
97
102
  },
98
103
  context
@@ -119,11 +124,10 @@ export class OydDIDProvider extends AbstractIdentifierProvider {
119
124
 
120
125
  const assertedKms = await this.assertedKms(kms, this.defaultKms)
121
126
  const pubKey =
122
- options.key ??
123
- (await cmsmCallbackOpts.publicKeyCallback(options.kid ?? 'default', assertedKms, options.cmsm?.create !== false, options.keyType)) // "default" is probably not right, TODO!!
127
+ options.key ?? (await cmsmCallbackOpts.publicKeyCallback(options.kid ?? 'default', assertedKms, options.cmsm?.create !== false, options.type)) // "default" is probably not right, TODO!!
124
128
  const kid = pubKey.kid
125
129
  const keyType = pubKey.type
126
- const key = base58btc({publicKeyHex: pubKey.publicKeyHex, keyType})
130
+ const key = base58btc({ publicKeyHex: pubKey.publicKeyHex, keyType })
127
131
 
128
132
  console.log(`Bae58 pubkey key: ${key}`)
129
133
  let signValue: any | undefined // do the request
@@ -161,8 +165,6 @@ export class OydDIDProvider extends AbstractIdentifierProvider {
161
165
 
162
166
  console.log(`Signature: ${signature}`)
163
167
 
164
-
165
-
166
168
  const body_signed = {
167
169
  key,
168
170
  options: {
@@ -195,24 +197,6 @@ export class OydDIDProvider extends AbstractIdentifierProvider {
195
197
  return Promise.reject(Error('There has been a problem with the fetch operation: ' + error.toString()))
196
198
  }
197
199
 
198
- /* let oydKeyType: OydDidSupportedKeyTypes = "Secp256r1";
199
-
200
- const key = await this.holdKeys(
201
- {
202
- kms: assertedKms,
203
- options: {
204
- keyType: oydKeyType,
205
- kid: kid,
206
- publicKeyHex: pubKey.publicKeyHex,
207
- },
208
- },
209
- context
210
- );*/
211
-
212
-
213
-
214
-
215
-
216
200
  const identifier: Omit<IIdentifier, 'provider'> = {
217
201
  did: didDoc.did,
218
202
  controllerKeyId: pubKey.kid,
@@ -252,31 +236,6 @@ export class OydDIDProvider extends AbstractIdentifierProvider {
252
236
  async removeService(args: { identifier: IIdentifier; id: string; options?: any }, context: IContext): Promise<any> {
253
237
  return { success: true }
254
238
  }
255
-
256
- private async importOrCreateKey(args: OydDidHoldKeysArgs, context: IContext): Promise<IKey> {
257
- const kms = await this.assertedKms(args.kms, this.defaultKms)
258
- if (args.options.privateKeyHex) {
259
- return context.agent.keyManagerImport({
260
- kms,
261
- type: args.options.keyType,
262
- kid: args.options.kid,
263
- privateKeyHex: args.options.privateKeyHex,
264
- /*meta: {
265
- algorithms: ['Secp256r1'],
266
- },*/
267
- })
268
- }
269
- return context.agent.keyManagerCreate({
270
- type: args.options.keyType,
271
- kms,
272
- meta: {
273
- algorithms: ['Secp256r1'],
274
- },
275
- })
276
- }
277
-
278
-
279
-
280
239
  }
281
240
 
282
241
  const keyCodecs = {
@@ -289,17 +248,15 @@ const keyCodecs = {
289
248
  Bls12381G2: 'bls12_381-g2-pub',
290
249
  } as const
291
250
 
292
- const base58btc = ({publicKeyHex, keyType = 'Secp256r1'}:{publicKeyHex: string, keyType?: TKeyType}): string => {
251
+ const base58btc = ({ publicKeyHex, keyType = 'Secp256r1' }: { publicKeyHex: string; keyType?: TKeyType }): string => {
293
252
  const codecName = keyCodecs[keyType]
294
253
 
295
-
296
254
  // methodSpecificId = bytesToMultibase({bytes: u8a.fromString(key.publicKeyHex, 'hex'), codecName})
297
255
  return u8a
298
- .toString(
299
- Multibase.encode('base58btc', Multicodec.addPrefix(codecName as Multicodec.CodecName, u8a.fromString(publicKeyHex, 'hex')))
300
- )
256
+ .toString(Multibase.encode('base58btc', Multicodec.addPrefix(codecName as Multicodec.CodecName, u8a.fromString(publicKeyHex, 'hex'))))
301
257
  .toString()
302
258
  }
259
+
303
260
  export function defaultOydCmsmPublicKeyCallback(
304
261
  keyManager: KeyManager
305
262
  ): (kid: string, kms?: string, create?: boolean, createKeyType?: TKeyType) => Promise<IKey> {
@@ -314,7 +271,27 @@ export function defaultOydCmsmPublicKeyCallback(
314
271
  if (!kms) {
315
272
  return Promise.reject(Error('No KMS provided, whilst creating a new key!'))
316
273
  }
317
- return await keyManager.keyManagerCreate({ kms, type: createKeyType ?? 'Secp256r1' })
274
+ const alias = kid ?? `oyd-${new Date().toISOString()}`
275
+
276
+ const agent = keyManager
277
+ const key = await importProvidedOrGeneratedKey(
278
+ {
279
+ kms,
280
+ alias,
281
+ options: {
282
+ key: {
283
+ type: createKeyType ?? 'Secp256r1',
284
+ },
285
+ },
286
+ },
287
+ {
288
+ //@ts-ignore
289
+ agent,
290
+ }
291
+ )
292
+ return key
293
+
294
+ // return await keyManager.keyManagerCreate({ kms, type: createKeyType ?? 'Secp256r1' })
318
295
  }
319
296
  return Promise.reject(Error('No existing key found, and create is false!'))
320
297
  }
@@ -327,11 +304,17 @@ export function defaultOydCmsmSignCallback(keyManager: KeyManager): (kid: string
327
304
  }
328
305
 
329
306
  export class DefaultOydCmsmCallbacks implements CMSMCallbackOpts {
330
- constructor(private keyManager: KeyManager) {}
307
+ private readonly keyManager: KeyManager
331
308
 
332
- publicKeyCallback: (kid: string, kms?: string, create?: boolean, createKeyType?: TKeyType) => Promise<IKey> = defaultOydCmsmPublicKeyCallback(
333
- this.keyManager
334
- )
309
+ constructor(keyManager: KeyManager) {
310
+ this.keyManager = keyManager
311
+ }
335
312
 
336
- signCallback: (kid: string, value: string) => Promise<string> = defaultOydCmsmSignCallback(this.keyManager)
313
+ publicKeyCallback(kid: string, kms?: string, create?: boolean, createKeyType?: TKeyType): Promise<IKey> {
314
+ return defaultOydCmsmPublicKeyCallback(this.keyManager)(kid, kms, create, createKeyType)
315
+ }
316
+
317
+ signCallback(kid: string, value: string): Promise<string> {
318
+ return defaultOydCmsmSignCallback(this.keyManager)(kid, value)
319
+ }
337
320
  }
@@ -1,49 +1,49 @@
1
1
  import { IKey, TKeyType } from '@veramo/core'
2
2
 
3
3
  export type OydConstructorOptions = {
4
- defaultKms?: string;
5
- clientManagedSecretMode?: CMSMCallbackOpts;
4
+ defaultKms?: string
5
+ clientManagedSecretMode?: CMSMCallbackOpts
6
6
  }
7
7
 
8
8
  export type OydCreateIdentifierOptions = {
9
- keyType?: OydDidSupportedKeyTypes;
10
- privateKeyHex?: string;
11
- kid?: string;
12
- keyUse?: KeyUse;
13
- cmsm?: CmsmOptions;
9
+ type?: OydDidSupportedKeyTypes
10
+ privateKeyHex?: string
11
+ kid?: string
12
+ alias?: string
13
+ keyUse?: KeyUse
14
+ cmsm?: CmsmOptions
14
15
  key?: IKey // Use the supplied key instead of looking it up in the KMS or creating a new one
15
16
  }
16
17
 
17
-
18
18
  export type CmsmOptions = {
19
19
  enabled: boolean
20
20
  create?: boolean
21
21
  }
22
22
 
23
23
  export type OydDidHoldKeysArgs = {
24
- kms?: string;
25
- options: HoldKeysOpts;
24
+ kms?: string
25
+ options: HoldKeysOpts
26
26
  }
27
27
 
28
28
  type HoldKeysOpts = {
29
- keyType: OydDidSupportedKeyTypes;
30
- kid: string;
31
- publicKeyHex?: string;
32
- privateKeyHex?: string;
29
+ keyType: OydDidSupportedKeyTypes
30
+ kid: string
31
+ publicKeyHex?: string
32
+ privateKeyHex?: string
33
33
  }
34
34
 
35
35
  export type CMSMCallbackOpts = {
36
- publicKeyCallback: (kid: string, kms?: string, create?: boolean, createKeyType?: TKeyType) => Promise<IKey>;
37
- signCallback: (kid: string, value: string) => Promise<string>;
36
+ publicKeyCallback: (kid: string, kms?: string, create?: boolean, createKeyType?: TKeyType) => Promise<IKey>
37
+ signCallback: (kid: string, value: string) => Promise<string>
38
38
  }
39
39
 
40
- export enum SupportedKeyTypes {
40
+ enum SupportedKeyTypes {
41
41
  Secp256r1 = 'Secp256r1',
42
42
  Secp256k1 = 'Secp256k1',
43
43
  Ed25519 = 'Ed25519',
44
44
  X25519 = 'X25519',
45
45
  }
46
46
 
47
- export type OydDidSupportedKeyTypes = keyof typeof SupportedKeyTypes;
47
+ export type OydDidSupportedKeyTypes = keyof typeof SupportedKeyTypes
48
48
 
49
- export type KeyUse = 'sig' | 'enc';
49
+ export type KeyUse = 'sig' | 'enc'
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAC9C,cAAc,4BAA4B,CAAA"}
@@ -1,61 +0,0 @@
1
- import { IAgentContext, IIdentifier, IKey, IKeyManager, IService, TKeyType } from '@veramo/core';
2
- import { AbstractIdentifierProvider } from '@veramo/did-manager';
3
- import { KeyManager } from '@veramo/key-manager';
4
- import type { CMSMCallbackOpts, OydConstructorOptions, OydCreateIdentifierOptions } from './types/oyd-provider-types.js';
5
- type IContext = IAgentContext<IKeyManager>;
6
- /**
7
- * {@link @veramo/did-manager#DIDManager} identifier provider for `did:oyd` identifiers
8
- * @public
9
- */
10
- export declare class OydDIDProvider extends AbstractIdentifierProvider {
11
- private readonly defaultKms?;
12
- private readonly cmsmCallbackOpts?;
13
- constructor(options?: OydConstructorOptions);
14
- private assertedKms;
15
- createIdentifier({ kms, options }: {
16
- kms?: string;
17
- options: OydCreateIdentifierOptions;
18
- }, context: IContext): Promise<Omit<IIdentifier, 'provider'>>;
19
- createIdentifierWithCMSM({ kms, options }: {
20
- kms?: string;
21
- options: OydCreateIdentifierOptions;
22
- }, context: IContext): Promise<Omit<IIdentifier, 'provider'>>;
23
- updateIdentifier(args: {
24
- did: string;
25
- kms?: string | undefined;
26
- alias?: string | undefined;
27
- options?: any;
28
- }, context: IAgentContext<IKeyManager>): Promise<IIdentifier>;
29
- deleteIdentifier(identifier: IIdentifier, context: IContext): Promise<boolean>;
30
- addKey({ identifier, key, options }: {
31
- identifier: IIdentifier;
32
- key: IKey;
33
- options?: any;
34
- }, context: IContext): Promise<any>;
35
- addService({ identifier, service, options }: {
36
- identifier: IIdentifier;
37
- service: IService;
38
- options?: any;
39
- }, context: IContext): Promise<any>;
40
- removeKey(args: {
41
- identifier: IIdentifier;
42
- kid: string;
43
- options?: any;
44
- }, context: IContext): Promise<any>;
45
- removeService(args: {
46
- identifier: IIdentifier;
47
- id: string;
48
- options?: any;
49
- }, context: IContext): Promise<any>;
50
- private importOrCreateKey;
51
- }
52
- export declare function defaultOydCmsmPublicKeyCallback(keyManager: KeyManager): (kid: string, kms?: string, create?: boolean, createKeyType?: TKeyType) => Promise<IKey>;
53
- export declare function defaultOydCmsmSignCallback(keyManager: KeyManager): (kid: string, data: string) => Promise<string>;
54
- export declare class DefaultOydCmsmCallbacks implements CMSMCallbackOpts {
55
- private keyManager;
56
- constructor(keyManager: KeyManager);
57
- publicKeyCallback: (kid: string, kms?: string, create?: boolean, createKeyType?: TKeyType) => Promise<IKey>;
58
- signCallback: (kid: string, value: string) => Promise<string>;
59
- }
60
- export {};
61
- //# sourceMappingURL=oyd-did-provider.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"oyd-did-provider.d.ts","sourceRoot":"","sources":["../src/oyd-did-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,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