@cheqd/did-provider-cheqd 3.1.1-develop.1 → 3.1.1-develop.2

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.
@@ -13,7 +13,8 @@ import {
13
13
  VerificationMethod,
14
14
  DidStdFee,
15
15
  ISignInputs,
16
- IContext as ISDKContext
16
+ IContext as ISDKContext,
17
+ CheqdNetwork
17
18
  } from '@cheqd/sdk'
18
19
  import { MsgCreateResourcePayload } from '@cheqd/ts-proto/cheqd/resource/v2'
19
20
  import {
@@ -55,11 +56,6 @@ export enum DefaultRPCUrl {
55
56
  Testnet = 'https://rpc.cheqd.network'
56
57
  }
57
58
 
58
- export enum NetworkType {
59
- Mainnet = "mainnet",
60
- Testnet = "testnet"
61
- }
62
-
63
59
  export type LinkedResource = Omit<MsgCreateResourcePayload, 'data'> & { data?: string }
64
60
 
65
61
  export type ResourcePayload = Partial<MsgCreateResourcePayload>
@@ -67,7 +63,7 @@ export type ResourcePayload = Partial<MsgCreateResourcePayload>
67
63
  export type TImportableEd25519Key = Required<Pick<IKey, 'publicKeyHex' | 'privateKeyHex'>> & { kid: TImportableEd25519Key['publicKeyHex'], type: 'Ed25519' }
68
64
 
69
65
  declare const TImportableEd25519Key: {
70
- isTImportableEd25519Key(object: Object[]): object is TImportableEd25519Key[];
66
+ isTImportableEd25519Key(object: object[]): object is TImportableEd25519Key[];
71
67
  }
72
68
 
73
69
  export type TSupportedKeyType = 'Ed25519' | 'Secp256k1'
@@ -76,52 +72,23 @@ export class EnglishMnemonic extends _ {
76
72
  static readonly _mnemonicMatcher = /^[a-z]+( [a-z]+)*$/;
77
73
  }
78
74
 
79
- async function createMsgCreateDidDocPayloadToSign(didPayload: DIDDocument, versionId: string) {
80
- const { protobufVerificationMethod, protobufService } = await DIDModule.validateSpecCompliantPayload(didPayload)
81
- return MsgCreateDidDocPayload.encode(
82
- MsgCreateDidDocPayload.fromPartial({
83
- context: <string[]>didPayload?.['@context'],
84
- id: didPayload.id,
85
- controller: <string[]>didPayload.controller,
86
- verificationMethod: protobufVerificationMethod,
87
- authentication: <string[]>didPayload.authentication,
88
- assertionMethod: <string[]>didPayload.assertionMethod,
89
- capabilityInvocation: <string[]>didPayload.capabilityInvocation,
90
- capabilityDelegation: <string[]>didPayload.capabilityDelegation,
91
- keyAgreement: <string[]>didPayload.keyAgreement,
92
- service: protobufService,
93
- alsoKnownAs: <string[]>didPayload.alsoKnownAs,
94
- versionId,
95
- })
96
- ).finish()
97
- }
98
-
99
- function createMsgDeactivateDidDocPayloadToSign(didPayload: DIDDocument, versionId?: string) {
100
- return MsgDeactivateDidDocPayload.encode(
101
- MsgDeactivateDidDocPayload.fromPartial({
102
- id: didPayload.id,
103
- versionId,
104
- })
105
- ).finish()
106
- }
107
-
108
75
  /**
109
76
  * {@link @veramo/did-manager#DIDManager} identifier provider for `did:cheqd` identifiers.
110
77
  * @public
111
78
  */
112
79
  export class CheqdDIDProvider extends AbstractIdentifierProvider {
113
80
  private defaultKms: string
114
- public readonly network: NetworkType
81
+ public readonly network: CheqdNetwork
115
82
  private rpcUrl: string
116
83
  private readonly cosmosPayerWallet: Promise<DirectSecp256k1HdWallet | DirectSecp256k1Wallet>
117
84
  private sdk?: CheqdSDK
118
85
  private fee?: DidStdFee
119
86
 
120
- constructor(options: { defaultKms: string, cosmosPayerSeed: string, networkType?: NetworkType, rpcUrl?: string }) {
87
+ constructor(options: { defaultKms: string, cosmosPayerSeed: string, networkType?: CheqdNetwork, rpcUrl?: string }) {
121
88
  super()
122
89
  this.defaultKms = options.defaultKms
123
- this.network = options.networkType ? options.networkType : NetworkType.Testnet
124
- this.rpcUrl = options.rpcUrl ? options.rpcUrl : (this.network === NetworkType.Testnet ? DefaultRPCUrl.Testnet : DefaultRPCUrl.Mainnet)
90
+ this.network = options.networkType ? options.networkType : CheqdNetwork.Testnet
91
+ this.rpcUrl = options.rpcUrl ? options.rpcUrl : (this.network === CheqdNetwork.Testnet ? DefaultRPCUrl.Testnet : DefaultRPCUrl.Mainnet)
125
92
 
126
93
  if (!options?.cosmosPayerSeed || options.cosmosPayerSeed === '') {
127
94
  this.cosmosPayerWallet = DirectSecp256k1HdWallet.generate()
@@ -166,13 +133,14 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
166
133
  ): Promise<Omit<IIdentifier, 'provider'>> {
167
134
  const sdk = await this.getCheqdSDK(options?.fee)
168
135
  const versionId = options.versionId || v4()
169
- let signInputs : ISignInputs[] | SignInfo[]
170
- if(options.keys) {
171
- signInputs = options.keys.map(key => createSignInputsFromImportableEd25519Key(key, options.document.verificationMethod ?? []))
172
- } else {
173
- const data = await createMsgCreateDidDocPayloadToSign(options.document, versionId)
174
- signInputs = await this.signPayload(context, data, options.document.verificationMethod)
175
- }
136
+ const signInputs: ISignInputs[] | SignInfo[] = options.keys
137
+ ? function () {
138
+ return options.keys.map(key => createSignInputsFromImportableEd25519Key(key, options.document.verificationMethod || []))
139
+ }()
140
+ : await (async function (that: CheqdDIDProvider) {
141
+ const data = await createMsgCreateDidDocPayloadToSign(options.document, versionId)
142
+ return await that.signPayload(context, data, options.document.verificationMethod)
143
+ }(this))
176
144
 
177
145
  const tx = await sdk.createDidDocTx(
178
146
  signInputs,
@@ -181,7 +149,7 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
181
149
  this?.fee,
182
150
  undefined,
183
151
  versionId,
184
- { sdk: sdk } as ISDKContext,
152
+ { sdk: sdk } satisfies ISDKContext,
185
153
  )
186
154
 
187
155
  assert(tx.code === 0, `cosmos_transaction: Failed to create DID. Reason: ${tx.rawLog}`)
@@ -189,25 +157,26 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
189
157
  //* Currently, only one controller key is supported.
190
158
  //* We assume that the first key in the list is the controller key.
191
159
  //* This is subject to change in the near future.
192
- let keys: ManagedKeyInfo[] = []
193
- if(options.keys) {
194
- for (const key of options.keys) {
195
- let managedKey: ManagedKeyInfo | undefined
196
- try {
197
- managedKey = await context.agent.keyManagerImport({
198
- ...key,
199
- kms: kms || this.defaultKms,
200
- } as MinimalImportableKey)
201
- } catch (e) {
202
- debug(`Failed to import key ${key.kid}. Reason: ${e}`)
203
- }
204
- if (managedKey) {
205
- keys.push(managedKey)
206
- }
207
- }
208
- } else {
209
- keys = await this.getKeysFromVerificationMethod(context, options.document.verificationMethod)
210
- }
160
+ const keys: ManagedKeyInfo[] = options.keys
161
+ ? await (async function (that: CheqdDIDProvider) {
162
+ const scopedKeys: ManagedKeyInfo[] = []
163
+ for (const key of options.keys!) {
164
+ let managedKey: ManagedKeyInfo | undefined
165
+ try {
166
+ managedKey = await context.agent.keyManagerImport({
167
+ ...key,
168
+ kms: kms || that.defaultKms,
169
+ } satisfies MinimalImportableKey)
170
+ } catch (e) {
171
+ debug(`Failed to import key ${key.kid}. Reason: ${e}`)
172
+ }
173
+ if (managedKey) {
174
+ scopedKeys.push(managedKey)
175
+ }
176
+ }
177
+ return scopedKeys
178
+ }(this))
179
+ : await this.getKeysFromVerificationMethod(context, options.document.verificationMethod)
211
180
 
212
181
  const controllerKey: IKey = keys[0]
213
182
  const identifier: IIdentifier = {
@@ -229,22 +198,23 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
229
198
  ): Promise<IIdentifier> {
230
199
  const sdk = await this.getCheqdSDK(options?.fee)
231
200
  const versionId = options.versionId || v4()
232
- let signInputs : ISignInputs[] | SignInfo[]
233
- if(options.keys) {
234
- signInputs = options.keys.map(key => createSignInputsFromImportableEd25519Key(key, document.verificationMethod ?? []))
235
- } else {
236
- const data = await createMsgCreateDidDocPayloadToSign(document, versionId)
237
- signInputs = await this.signPayload(context, data, document.verificationMethod)
238
- }
201
+ const signInputs: ISignInputs[] | SignInfo[] = options.keys
202
+ ? function (){
203
+ return options.keys.map(key => createSignInputsFromImportableEd25519Key(key, document.verificationMethod || []))
204
+ }()
205
+ : await (async function (that: CheqdDIDProvider){
206
+ const data = await createMsgCreateDidDocPayloadToSign(document, versionId)
207
+ return await that.signPayload(context, data, document.verificationMethod)
208
+ }(this))
239
209
 
240
210
  const tx = await sdk.updateDidDocTx(
241
211
  signInputs,
242
- document as DIDDocument,
212
+ document satisfies DIDDocument,
243
213
  '',
244
214
  this?.fee,
245
215
  undefined,
246
216
  versionId,
247
- { sdk: sdk } as ISDKContext,
217
+ { sdk: sdk } satisfies ISDKContext,
248
218
  )
249
219
 
250
220
  assert(tx.code === 0, `cosmos_transaction: Failed to update DID. Reason: ${tx.rawLog}`)
@@ -252,26 +222,26 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
252
222
  //* Currently, only one controller key is supported.
253
223
  //* We assume that the first key in the list is the controller key.
254
224
  //* This is subject to change in the near future.
255
- let keys: ManagedKeyInfo[] = []
256
- if(options.keys) {
257
- const keys: ManagedKeyInfo[] = []
258
- for (const key of options.keys) {
259
- let managedKey: ManagedKeyInfo | undefined
260
- try {
261
- managedKey = await context.agent.keyManagerImport({
262
- ...key,
263
- kms: options.kms || this.defaultKms,
264
- } as MinimalImportableKey)
265
- } catch (e) {
266
- debug(`Failed to import key ${key.kid}. Reason: ${e}`)
267
- }
268
- if (managedKey) {
269
- keys.push(managedKey)
270
- }
271
- }
272
- } else {
273
- keys = await this.getKeysFromVerificationMethod(context, document.verificationMethod)
274
- }
225
+ const keys: ManagedKeyInfo[] = options.keys
226
+ ? await (async function (that: CheqdDIDProvider) {
227
+ const scopedKeys: ManagedKeyInfo[] = []
228
+ for (const key of options.keys!) {
229
+ let managedKey: ManagedKeyInfo | undefined
230
+ try {
231
+ managedKey = await context.agent.keyManagerImport({
232
+ ...key,
233
+ kms: options.kms || that.defaultKms,
234
+ } satisfies MinimalImportableKey)
235
+ } catch (e) {
236
+ debug(`Failed to import key ${key.kid}. Reason: ${e}`)
237
+ }
238
+ if (managedKey) {
239
+ scopedKeys.push(managedKey)
240
+ }
241
+ }
242
+ return scopedKeys
243
+ }(this))
244
+ : await this.getKeysFromVerificationMethod(context, document.verificationMethod)
275
245
 
276
246
  const controllerKey = keys[0]
277
247
 
@@ -294,22 +264,23 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
294
264
  ): Promise<boolean> {
295
265
  const sdk = await this.getCheqdSDK(options?.fee)
296
266
  const versionId = options.versionId || v4()
297
- let signInputs : ISignInputs[] | SignInfo[]
298
- if(options.keys) {
299
- signInputs = options.keys.map(key => createSignInputsFromImportableEd25519Key(key, document.verificationMethod ?? []))
300
- } else {
301
- const data = createMsgDeactivateDidDocPayloadToSign(document, versionId)
302
- signInputs = await this.signPayload(context, data, document.verificationMethod)
303
- }
267
+ const signInputs: ISignInputs[] | SignInfo[] = options.keys
268
+ ? function (){
269
+ return options.keys.map(key => createSignInputsFromImportableEd25519Key(key, document.verificationMethod || []))
270
+ }()
271
+ : await (async function (that: CheqdDIDProvider){
272
+ const data = await createMsgDeactivateDidDocPayloadToSign(document, versionId)
273
+ return await that.signPayload(context, data, document.verificationMethod)
274
+ }(this))
304
275
 
305
276
  const tx = await sdk.deactivateDidDocTx(
306
277
  signInputs,
307
- document as DIDDocument,
278
+ document satisfies DIDDocument,
308
279
  '',
309
280
  this?.fee,
310
281
  undefined,
311
282
  versionId,
312
- { sdk: sdk } as ISDKContext,
283
+ { sdk: sdk } satisfies ISDKContext,
313
284
  )
314
285
 
315
286
  assert(tx.code === 0, `cosmos_transaction: Failed to update DID. Reason: ${tx.rawLog}`)
@@ -325,22 +296,21 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
325
296
  ): Promise<boolean> {
326
297
  const sdk = await this.getCheqdSDK(options?.fee)
327
298
 
328
- let signInputs: ISignInputs[] | SignInfo[]
329
- if(!options.signInputs) {
330
- const did = `did:cheqd:${this.network}:${options.payload.collectionId}`
331
- const { didDocument } = await sdk.queryDidDoc(
332
- did,
333
- { sdk: sdk }
334
- )
335
-
336
- signInputs = await this.signPayload(
337
- context,
338
- MsgCreateResourcePayload.encode(MsgCreateResourcePayload.fromPartial(options.payload)).finish(),
339
- didDocument?.verificationMethod
340
- )
341
- } else {
342
- signInputs = options.signInputs
343
- }
299
+ const signInputs: ISignInputs[] | SignInfo[] = options.signInputs
300
+ ? options.signInputs
301
+ : await (async function (that: CheqdDIDProvider){
302
+ const did = `did:cheqd:${that.network}:${options.payload.collectionId}`
303
+ const { didDocument } = await sdk.queryDidDoc(
304
+ did,
305
+ { sdk: sdk }
306
+ )
307
+
308
+ return await that.signPayload(
309
+ context,
310
+ MsgCreateResourcePayload.encode(MsgCreateResourcePayload.fromPartial(options.payload)).finish(),
311
+ didDocument?.verificationMethod
312
+ )
313
+ }(this))
344
314
 
345
315
  const tx = await sdk.createLinkedResourceTx(
346
316
  signInputs,
@@ -376,7 +346,7 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
376
346
  privateKeyHex: input.privateKeyHex,
377
347
  type: mapKeyType(input.keyType) as TSupportedKeyType,
378
348
  kms: options.kms || this.defaultKms,
379
- } as MinimalImportableKey)
349
+ } satisfies MinimalImportableKey)
380
350
  } catch (e) {
381
351
  debug(`Failed to import key ${input.verificationMethodId}. Reason: ${e}`)
382
352
  }
@@ -447,28 +417,57 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
447
417
 
448
418
  private async signPayload(context: IAgentContext<IKeyManager>, data: Uint8Array, verificationMethod: VerificationMethod[] = []): Promise<SignInfo[]> {
449
419
  return Promise.all(
450
- verificationMethod.map(async (method)=>{
451
- const keyRef = extractPublicKeyHex(method)
452
- return {
453
- verificationMethodId: method.id,
454
- signature: base64ToBytes(await context.agent.keyManagerSign({
455
- keyRef,
456
- data: toString(data, 'hex'),
457
- encoding: 'hex'
458
- }))
459
- } satisfies SignInfo
460
- })
420
+ verificationMethod.map(async (method) => {
421
+ const keyRef = extractPublicKeyHex(method)
422
+ return {
423
+ verificationMethodId: method.id,
424
+ signature: base64ToBytes(await context.agent.keyManagerSign({
425
+ keyRef,
426
+ data: toString(data, 'hex'),
427
+ encoding: 'hex'
428
+ }))
429
+ } satisfies SignInfo
430
+ })
461
431
  )
462
432
  }
463
433
 
464
434
  private async getKeysFromVerificationMethod(context: IAgentContext<IKeyManager>, verificationMethod: VerificationMethod[] = []): Promise<ManagedKeyInfo[]> {
465
435
  return Promise.all(
466
- verificationMethod.map(async (method)=>{
467
- const kid = extractPublicKeyHex(method)
468
- return await context.agent.keyManagerGet({kid})
469
- })
436
+ verificationMethod.map(async (method)=>{
437
+ const kid = extractPublicKeyHex(method)
438
+ return await context.agent.keyManagerGet({kid})
439
+ })
470
440
  ).catch((error)=>{
471
441
  throw new Error(`Failed to sign payload: ${error}`)
472
442
  })
473
443
  }
474
444
  }
445
+
446
+ export async function createMsgCreateDidDocPayloadToSign(didPayload: DIDDocument, versionId: string): Promise<Uint8Array> {
447
+ const { protobufVerificationMethod, protobufService } = await DIDModule.validateSpecCompliantPayload(didPayload)
448
+ return MsgCreateDidDocPayload.encode(
449
+ MsgCreateDidDocPayload.fromPartial({
450
+ context: <string[]>didPayload?.['@context'],
451
+ id: didPayload.id,
452
+ controller: <string[]>didPayload.controller,
453
+ verificationMethod: protobufVerificationMethod,
454
+ authentication: <string[]>didPayload.authentication,
455
+ assertionMethod: <string[]>didPayload.assertionMethod,
456
+ capabilityInvocation: <string[]>didPayload.capabilityInvocation,
457
+ capabilityDelegation: <string[]>didPayload.capabilityDelegation,
458
+ keyAgreement: <string[]>didPayload.keyAgreement,
459
+ service: protobufService,
460
+ alsoKnownAs: <string[]>didPayload.alsoKnownAs,
461
+ versionId,
462
+ })
463
+ ).finish()
464
+ }
465
+
466
+ export async function createMsgDeactivateDidDocPayloadToSign(didPayload: DIDDocument, versionId?: string): Promise<Uint8Array> {
467
+ return MsgDeactivateDidDocPayload.encode(
468
+ MsgDeactivateDidDocPayload.fromPartial({
469
+ id: didPayload.id,
470
+ versionId,
471
+ })
472
+ ).finish()
473
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,18 @@
1
1
  /**
2
2
  * @public
3
3
  */
4
- export { CheqdDIDProvider } from './did-manager/cheqd-did-provider.js'
5
- export { CheqdDidResolver, getResolver } from './did-manager/cheqd-did-resolver.js'
4
+ export {
5
+ CheqdDIDProvider,
6
+ DefaultRPCUrl,
7
+ LinkedResource,
8
+ ResourcePayload,
9
+ TImportableEd25519Key,
10
+ TSupportedKeyType,
11
+ EnglishMnemonic,
12
+ } from './did-manager/cheqd-did-provider.js'
13
+ export {
14
+ CheqdDidResolver,
15
+ getResolver
16
+ } from './did-manager/cheqd-did-resolver.js'
6
17
  export { CheqdUniversalResolver } from './did-manager/resolver.js'
7
18
  export { Cheqd } from './agent/ICheqd.js'