Skip to content

Commit

Permalink
Convert payload on signAndSend call (#492)
Browse files Browse the repository at this point in the history
* Convert payload on `signAndSend` call

* Bump up version

* Renamings
  • Loading branch information
olegnn authored Dec 6, 2024
1 parent 3cc2188 commit d5668ff
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "root",
"version": "0.17.0",
"version": "0.19.0",
"private": true,
"workspaces": [
"packages/*",
Expand Down
41 changes: 29 additions & 12 deletions packages/cheqd-blockchain-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
maybeToJSON,
maybeToJSONString,
fmtIter,
extendNull,
} from '@docknetwork/credential-sdk/utils';
import {
DIDModule,
Expand All @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cheqd-blockchain-modules/src/attest/internal.js
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
3 changes: 2 additions & 1 deletion packages/cheqd-blockchain-modules/src/blob/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 0 additions & 1 deletion packages/cheqd-blockchain-modules/src/common/index.js
Original file line number Diff line number Diff line change
@@ -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';
11 changes: 5 additions & 6 deletions packages/cheqd-blockchain-modules/src/common/inject-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 ?? {};

Expand Down Expand Up @@ -66,7 +65,7 @@ export default function injectParams(klass) {
* @returns {Promise<Map<CheqdParamsId, Params>>}
*/
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]) => [
Expand All @@ -78,5 +77,5 @@ export default function injectParams(klass) {
},
};

return obj[name];
return withExtendedStaticProperties(['Params'], withExtendedPrototypeProperties(['filterParamsMetadata'], obj[name]));
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function withParams(klass) {
(
await this.cheqdOnly.latestResourceMetadataBy(
targetDid,
this.cheqdOnly.filterMetadata,
this.cheqdOnly.filterParamsMetadata,
)
)?.id,
);
Expand Down
7 changes: 3 additions & 4 deletions packages/cheqd-blockchain-modules/src/did/internal.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
1 change: 1 addition & 0 deletions packages/credential-sdk/src/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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})`;
Expand Down Expand Up @@ -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,
Expand All @@ -75,6 +75,6 @@ export const [
] = createTypes(CheqdDIDDocument);

export const [
CheqdDeactivateDidDocumentPayloadWithSignatures,
CheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures,
] = createTypes(DeactivateDidDocument);
CheqdCheqdDeactivateDidDocumentPayloadWithSignatures,
CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures,
] = createTypes(CheqdDeactivateDidDocument);
1 change: 1 addition & 0 deletions packages/credential-sdk/src/types/payload/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cheqd';
8 changes: 8 additions & 0 deletions packages/credential-sdk/src/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/dock-blockchain-api/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit d5668ff

Please sign in to comment.