diff --git a/package.json b/package.json index 7faf6e1ed..97979028e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "root", - "version": "0.17.0", + "version": "0.19.0", "private": true, "workspaces": [ "packages/*", diff --git a/packages/cheqd-blockchain-api/src/index.js b/packages/cheqd-blockchain-api/src/index.js index c69d0c7d7..82c21ab2f 100644 --- a/packages/cheqd-blockchain-api/src/index.js +++ b/packages/cheqd-blockchain-api/src/index.js @@ -3,6 +3,7 @@ import { maybeToJSON, maybeToJSONString, fmtIter, + extendNull, } from '@docknetwork/credential-sdk/utils'; import { DIDModule, @@ -20,7 +21,9 @@ import { MsgCreateResourcePayload, protobufPackage as resourceProtobufPackage, } from '@cheqd/ts-proto/cheqd/resource/v2/index.js'; -import { DidRef, NamespaceDid } from '@docknetwork/credential-sdk/types'; +import { + DidRef, NamespaceDid, CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures, CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures, CheqdCreateResourcePayloadWithTypeUrlAndSignatures, +} from '@docknetwork/credential-sdk/types'; import { TypedEnum } from '@docknetwork/credential-sdk/types/generic'; export class CheqdAPI extends AbstractApiProvider { @@ -32,26 +35,33 @@ export class CheqdAPI extends AbstractApiProvider { super(); } - static Fees = { + static Fees = extendNull({ MsgCreateDidDoc: DIDModule.fees.DefaultCreateDidDocFee, MsgUpdateDidDoc: DIDModule.fees.DefaultUpdateDidDocFee, MsgDeactivateDidDoc: DIDModule.fees.DefaultDeactivateDidDocFee, MsgCreateResource: ResourceModule.fees.DefaultCreateResourceDefaultFee, - }; + }); - static Prefixes = { + static Prefixes = extendNull({ MsgCreateDidDoc: didProtobufPackage, MsgUpdateDidDoc: didProtobufPackage, MsgDeactivateDidDoc: didProtobufPackage, MsgCreateResource: resourceProtobufPackage, - }; + }); - static Payloads = { + static Payloads = extendNull({ MsgCreateDidDoc: MsgCreateDidDocPayload, MsgUpdateDidDoc: MsgUpdateDidDocPayload, MsgDeactivateDidDoc: MsgDeactivateDidDocPayload, MsgCreateResource: MsgCreateResourcePayload, - }; + }); + + static PayloadWrappers = extendNull({ + MsgCreateDidDoc: CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures, + MsgUpdateDidDoc: CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures, + MsgDeactivateDidDoc: CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures, + MsgCreateResource: CheqdCreateResourcePayloadWithTypeUrlAndSignatures, + }); /** * Initializes `CheqdAPI` with the supplied url, wallet and network type. @@ -141,18 +151,25 @@ export class CheqdAPI extends AbstractApiProvider { * @returns {Promise<*>} */ async signAndSend(tx, { from, fee, memo } = {}) { - const sender = from ?? (await this.sdk.options.wallet.getAccounts())[0].address; + const { PayloadWrappers, Prefixes, Fees } = this.constructor; const { typeUrl } = tx; - const prefix = this.constructor.Prefixes[typeUrl]; - const amount = fee ?? this.constructor.Fees[typeUrl]; + const PayloadWrapper = PayloadWrappers[typeUrl]; + const prefix = Prefixes[typeUrl]; + const amount = fee ?? Fees[typeUrl]; + + if (PayloadWrapper == null) { + throw new Error(`No payload wrapper found for \`${typeUrl}\``); + } + + const sender = from ?? (await this.sdk.options.wallet.getAccounts())[0].address; const payment = { amount: [amount], gas: '3600000', // TODO: dynamically calculate needed amount payer: sender, }; - const txJSON = tx.toJSON(); + const txJSON = PayloadWrapper.from(tx).toJSON(); txJSON.typeUrl = `/${prefix}.${typeUrl}`; const res = await this.sdk.signer.signAndBroadcast( @@ -193,7 +210,7 @@ export class CheqdAPI extends AbstractApiProvider { return this.supportsIdentifier(id[0]); } else if (id instanceof TypedEnum) { return this.supportsIdentifier(id.value); - } else if (id?.constructor?.Qualifier?.includes(`cheqd:${network}:`)) { + } else if (String(id).includes(`:cheqd:${network}:`)) { return true; } diff --git a/packages/cheqd-blockchain-modules/src/attest/internal.js b/packages/cheqd-blockchain-modules/src/attest/internal.js index 653e86d9e..f0c8a30c1 100644 --- a/packages/cheqd-blockchain-modules/src/attest/internal.js +++ b/packages/cheqd-blockchain-modules/src/attest/internal.js @@ -1,6 +1,6 @@ -import { CheqdDid, Iri } from '@docknetwork/credential-sdk/types'; +import { CheqdDid, Iri, CheqdCreateResource } from '@docknetwork/credential-sdk/types'; import { TypedUUID, option } from '@docknetwork/credential-sdk/types/generic'; -import { CheqdCreateResource, createInternalCheqdModule } from '../common'; +import { createInternalCheqdModule } from '../common'; const methods = { setClaim: (iri, targetDid) => new CheqdCreateResource( diff --git a/packages/cheqd-blockchain-modules/src/blob/internal.js b/packages/cheqd-blockchain-modules/src/blob/internal.js index ba6aeb307..15e133bcd 100644 --- a/packages/cheqd-blockchain-modules/src/blob/internal.js +++ b/packages/cheqd-blockchain-modules/src/blob/internal.js @@ -2,9 +2,10 @@ import { Blob, CheqdBlobId, CheqdBlobWithId, + CheqdCreateResource, } from '@docknetwork/credential-sdk/types'; import { option } from '@docknetwork/credential-sdk/types/generic'; -import { CheqdCreateResource, createInternalCheqdModule } from '../common'; +import { createInternalCheqdModule } from '../common'; const methods = { new: (blobWithId) => { diff --git a/packages/cheqd-blockchain-modules/src/common/index.js b/packages/cheqd-blockchain-modules/src/common/index.js index b1f0ac4f6..19c627100 100644 --- a/packages/cheqd-blockchain-modules/src/common/index.js +++ b/packages/cheqd-blockchain-modules/src/common/index.js @@ -1,5 +1,4 @@ export { default as createInternalCheqdModule } from './create-internal-cheqd-module'; -export * from './payload'; export { default as injectCheqd } from './inject-cheqd'; export { default as CheqdApiProvider } from './cheqd-api-provider'; export { default as withParams } from './with-params'; diff --git a/packages/cheqd-blockchain-modules/src/common/inject-params.js b/packages/cheqd-blockchain-modules/src/common/inject-params.js index b9aa33953..da840065a 100644 --- a/packages/cheqd-blockchain-modules/src/common/inject-params.js +++ b/packages/cheqd-blockchain-modules/src/common/inject-params.js @@ -3,10 +3,11 @@ import { stringToU8a, maybeToJSONString, u8aToString, + withExtendedStaticProperties, + withExtendedPrototypeProperties, } from '@docknetwork/credential-sdk/utils'; -import { CheqdParamsId } from '@docknetwork/credential-sdk/types'; +import { CheqdParamsId, CheqdCreateResource } from '@docknetwork/credential-sdk/types'; import createInternalCheqdModule from './create-internal-cheqd-module'; -import { CheqdCreateResource } from './payload'; const methods = { addParams: (id, params, did) => new CheqdCreateResource( @@ -25,8 +26,6 @@ export default function injectParams(klass) { const obj = { [name]: class extends createInternalCheqdModule(methods, klass) { - static Prop = 'resource'; - static get MsgNames() { const names = super.MsgNames ?? {}; @@ -66,7 +65,7 @@ export default function injectParams(klass) { * @returns {Promise>} */ async getAllParamsByDid(did) { - const resources = await this.resourcesBy(did, this.filterMetadata); + const resources = await this.resourcesBy(did, this.filterParamsMetadata); return new this.constructor.ParamsMap( [...resources].map(([key, item]) => [ @@ -78,5 +77,5 @@ export default function injectParams(klass) { }, }; - return obj[name]; + return withExtendedStaticProperties(['Params'], withExtendedPrototypeProperties(['filterParamsMetadata'], obj[name])); } diff --git a/packages/cheqd-blockchain-modules/src/common/with-params.js b/packages/cheqd-blockchain-modules/src/common/with-params.js index 52555a657..d06579440 100644 --- a/packages/cheqd-blockchain-modules/src/common/with-params.js +++ b/packages/cheqd-blockchain-modules/src/common/with-params.js @@ -50,7 +50,7 @@ export default function withParams(klass) { ( await this.cheqdOnly.latestResourceMetadataBy( targetDid, - this.cheqdOnly.filterMetadata, + this.cheqdOnly.filterParamsMetadata, ) )?.id, ); diff --git a/packages/cheqd-blockchain-modules/src/did/internal.js b/packages/cheqd-blockchain-modules/src/did/internal.js index e416428c7..9a846ba44 100644 --- a/packages/cheqd-blockchain-modules/src/did/internal.js +++ b/packages/cheqd-blockchain-modules/src/did/internal.js @@ -1,16 +1,15 @@ -import { CheqdDid } from '@docknetwork/credential-sdk/types/did/onchain/typed-did'; +import { CheqdDid, DIDDocument, CheqdDeactivateDidDocument } from '@docknetwork/credential-sdk/types'; import { TypedUUID, } from '@docknetwork/credential-sdk/types/generic'; -import { DIDDocument } from '@docknetwork/credential-sdk/types/did'; -import { createInternalCheqdModule, DeactivateDidDocument } from '../common'; +import { createInternalCheqdModule } from '../common'; const parseDocument = (document) => DIDDocument.from(document).toCheqd(); const methods = { createDidDocument: parseDocument, updateDidDocument: parseDocument, - deactivateDidDocument: (id) => new DeactivateDidDocument(id, TypedUUID.random()), + deactivateDidDocument: (id) => new CheqdDeactivateDidDocument(id, TypedUUID.random()), }; export class CheqdDIDModuleInternal extends createInternalCheqdModule(methods) { diff --git a/packages/cheqd-blockchain-modules/src/offchain-signatures/internal.js b/packages/cheqd-blockchain-modules/src/offchain-signatures/internal.js index a83b60f51..c26e4b6b0 100644 --- a/packages/cheqd-blockchain-modules/src/offchain-signatures/internal.js +++ b/packages/cheqd-blockchain-modules/src/offchain-signatures/internal.js @@ -3,7 +3,7 @@ import injectParams from '../common/inject-params'; export default class CheqdOffchainSignaturesInternalModule extends injectParams( class {}, ) { - filterMetadata(meta) { + filterParamsMetadata(meta) { return meta.resourceType === 'offchain-signature-params'; } } diff --git a/packages/cheqd-blockchain-modules/src/status-list-credential/internal.js b/packages/cheqd-blockchain-modules/src/status-list-credential/internal.js index 9d5a47160..fa075cd51 100644 --- a/packages/cheqd-blockchain-modules/src/status-list-credential/internal.js +++ b/packages/cheqd-blockchain-modules/src/status-list-credential/internal.js @@ -1,10 +1,11 @@ import { CheqdStatusListCredentialId, + CheqdCreateResource, } from '@docknetwork/credential-sdk/types'; import { StatusList2021Credential } from '@docknetwork/credential-sdk/vc'; import { option, TypedUUID } from '@docknetwork/credential-sdk/types/generic'; import { stringToU8a, maybeToJSONString, u8aToString } from '@docknetwork/credential-sdk/utils'; -import { CheqdCreateResource, createInternalCheqdModule } from '../common'; +import { createInternalCheqdModule } from '../common'; const Type = 'status-list-credential'; diff --git a/packages/credential-sdk/src/types/index.js b/packages/credential-sdk/src/types/index.js index 0e53a2e9e..8790c1fca 100644 --- a/packages/credential-sdk/src/types/index.js +++ b/packages/credential-sdk/src/types/index.js @@ -2,6 +2,7 @@ export * from './blob'; export * from './attest'; export * from './did'; export * from './offchain-signatures'; +export * from './payload'; export * from './policy'; export * from './public-keys'; export * from './signatures'; diff --git a/packages/cheqd-blockchain-modules/src/common/payload.js b/packages/credential-sdk/src/types/payload/cheqd.js similarity index 86% rename from packages/cheqd-blockchain-modules/src/common/payload.js rename to packages/credential-sdk/src/types/payload/cheqd.js index 5c7cefee4..bd29849aa 100644 --- a/packages/cheqd-blockchain-modules/src/common/payload.js +++ b/packages/credential-sdk/src/types/payload/cheqd.js @@ -2,14 +2,14 @@ import { VerificationMethodSignature, CheqdDid, CheqdDIDDocument, -} from '@docknetwork/credential-sdk/types'; +} from '../did'; import { TypedArray, TypedString, TypedStruct, TypedBytesArray, TypedUUID, -} from '@docknetwork/credential-sdk/types/generic'; +} from '../generic'; const createTypes = (Payload) => { const payloadWithSigsName = `CheqdPayloadWithSignatures(${Payload.name})`; @@ -57,7 +57,7 @@ export class CheqdCreateResource extends TypedStruct { }; } -export class DeactivateDidDocument extends TypedStruct { +export class CheqdDeactivateDidDocument extends TypedStruct { static Classes = { id: CheqdDid, versionId: TypedUUID, @@ -75,6 +75,6 @@ export const [ ] = createTypes(CheqdDIDDocument); export const [ - CheqdDeactivateDidDocumentPayloadWithSignatures, - CheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures, -] = createTypes(DeactivateDidDocument); + CheqdCheqdDeactivateDidDocumentPayloadWithSignatures, + CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures, +] = createTypes(CheqdDeactivateDidDocument); diff --git a/packages/credential-sdk/src/types/payload/index.js b/packages/credential-sdk/src/types/payload/index.js new file mode 100644 index 000000000..ad7cf333a --- /dev/null +++ b/packages/credential-sdk/src/types/payload/index.js @@ -0,0 +1 @@ +export * from './cheqd'; diff --git a/packages/credential-sdk/src/utils/misc.js b/packages/credential-sdk/src/utils/misc.js index 4097ca52b..275ba7671 100644 --- a/packages/credential-sdk/src/utils/misc.js +++ b/packages/credential-sdk/src/utils/misc.js @@ -150,6 +150,14 @@ export const filterObj = (obj, filter) => { return res; }; +/** + * Sets prototype of the supplied object to `null`, returns the object. + * @template T + * @param {T} + * @returns {T} + */ +export const extendNull = (obj) => Object.setPrototypeOf(obj, null); + /** * Ensures that provided value matches supplied pattern(s), throws an error otherwise. * diff --git a/packages/dock-blockchain-api/src/api/index.js b/packages/dock-blockchain-api/src/api/index.js index 3e3dca1c7..9a12705c2 100644 --- a/packages/dock-blockchain-api/src/api/index.js +++ b/packages/dock-blockchain-api/src/api/index.js @@ -352,7 +352,7 @@ export default class DockAPI extends AbstractApiProvider { return this.supportsIdentifier(id[0]); } else if (id instanceof TypedEnum) { return this.supportsIdentifier(id.value); - } else if (id?.constructor?.Qualifier?.includes(':dock:')) { + } else if (String(id).includes(':dock:')) { return true; }