Skip to content

Commit

Permalink
Merge pull request #496 from docknetwork/cheqd-serde
Browse files Browse the repository at this point in the history
Serialization/deserialization tweaks for `cheqd`
  • Loading branch information
cykoder authored Dec 18, 2024
2 parents 23812ce + 0d44073 commit 0d07c3f
Show file tree
Hide file tree
Showing 40 changed files with 328 additions and 116 deletions.
9 changes: 9 additions & 0 deletions examples/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @docknetwork/sdk-examples

## 0.6.7

### Patch Changes

- Updated dependencies
- @docknetwork/credential-sdk@0.21.0
- @docknetwork/dock-blockchain-api@0.8.7
- @docknetwork/dock-blockchain-modules@0.11.2

## 0.6.6

### Patch Changes
Expand Down
8 changes: 4 additions & 4 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@docknetwork/sdk-examples",
"private": true,
"type": "module",
"version": "0.6.6",
"version": "0.6.7",
"scripts": {
"bbs-dock-example": "babel-node ./bbs-dock.js",
"claim-deduction-example": "babel-node ./claim-deduction.js",
Expand All @@ -19,9 +19,9 @@
"lint": "eslint \"*.js\""
},
"dependencies": {
"@docknetwork/credential-sdk": "0.20.0",
"@docknetwork/dock-blockchain-api": "0.8.6",
"@docknetwork/dock-blockchain-modules": "0.11.1"
"@docknetwork/credential-sdk": "0.21.0",
"@docknetwork/dock-blockchain-api": "0.8.7",
"@docknetwork/dock-blockchain-modules": "0.11.2"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
Expand Down
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.21.0",
"version": "0.22.0",
"private": true,
"workspaces": [
"packages/*",
Expand Down
11 changes: 11 additions & 0 deletions packages/cheqd-blockchain-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @docknetwork/cheqd-blockchain-api

## 0.15.0

### Minor Changes

- Serialization/deserialization tweaks for `cheqd`

### Patch Changes

- Updated dependencies
- @docknetwork/credential-sdk@0.21.0

## 0.14.5

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/cheqd-blockchain-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/cheqd-blockchain-api",
"version": "0.14.5",
"version": "0.15.0",
"license": "MIT",
"main": "./dist/esm/index.js",
"type": "module",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@cheqd/sdk": "cjs",
"@docknetwork/credential-sdk": "0.20.0"
"@docknetwork/credential-sdk": "0.21.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand Down
38 changes: 27 additions & 11 deletions packages/cheqd-blockchain-api/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AbstractApiProvider } from '@docknetwork/credential-sdk/modules/abstract/common';
import {
maybeToJSON,
maybeToJSONString,
fmtIter,
extendNull,
Expand All @@ -22,9 +21,17 @@ import {
protobufPackage as resourceProtobufPackage,
} from '@cheqd/ts-proto/cheqd/resource/v2/index.js';
import {
DidRef, NamespaceDid, CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures, CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures, CheqdCreateResourcePayloadWithTypeUrlAndSignatures,
DidRef,
NamespaceDid,
CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures,
CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures,
CheqdCreateResourcePayloadWithTypeUrlAndSignatures,
CheqdCreateResource,
CheqdDIDDocument,
CheqdDeactivateDidDocument,
} from '@docknetwork/credential-sdk/types';
import { TypedEnum } from '@docknetwork/credential-sdk/types/generic';
import { maybeToCheqdPayloadOrJSON } from '../../credential-sdk/src/utils';

export class CheqdAPI extends AbstractApiProvider {
/**
Expand All @@ -50,16 +57,20 @@ export class CheqdAPI extends AbstractApiProvider {
});

static Payloads = extendNull({
MsgCreateDidDoc: MsgCreateDidDocPayload,
MsgUpdateDidDoc: MsgUpdateDidDocPayload,
MsgDeactivateDidDoc: MsgDeactivateDidDocPayload,
MsgCreateResource: MsgCreateResourcePayload,
MsgCreateDidDoc: [CheqdDIDDocument, MsgCreateDidDocPayload],
MsgUpdateDidDoc: [CheqdDIDDocument, MsgUpdateDidDocPayload],
MsgDeactivateDidDoc: [
CheqdDeactivateDidDocument,
MsgDeactivateDidDocPayload,
],
MsgCreateResource: [CheqdCreateResource, MsgCreateResourcePayload],
});

static PayloadWrappers = extendNull({
MsgCreateDidDoc: CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures,
MsgUpdateDidDoc: CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures,
MsgDeactivateDidDoc: CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures,
MsgDeactivateDidDoc:
CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures,
MsgCreateResource: CheqdCreateResourcePayloadWithTypeUrlAndSignatures,
});

Expand Down Expand Up @@ -123,13 +134,16 @@ export class CheqdAPI extends AbstractApiProvider {
* @returns {Promise<Uint8Array>}
*/
async stateChangeBytes(method, payload) {
const { [method]: Payload } = this.constructor.Payloads;
if (Payload == null) {
const { [method]: Payloads } = this.constructor.Payloads;
if (Payloads == null) {
throw new Error(
`Can't find payload constructor for the provided method \`${method}\``,
);
}
const jsonPayload = maybeToJSON(payload);
const [TypedPayload, Payload] = Payloads;

const typedPayload = TypedPayload.from(payload);
const jsonPayload = maybeToCheqdPayloadOrJSON(typedPayload);
const sdkPayload = Payload.fromPartial(jsonPayload);

try {
Expand Down Expand Up @@ -169,7 +183,9 @@ export class CheqdAPI extends AbstractApiProvider {
payer: sender,
};

const txJSON = PayloadWrapper.from(tx).toJSON();
const txJSON = maybeToCheqdPayloadOrJSON(
PayloadWrapper.from(JSON.parse(JSON.stringify(tx.toJSON()))),
);
txJSON.typeUrl = `/${prefix}.${typeUrl}`;

const res = await this.sdk.signer.signAndBroadcast(
Expand Down
7 changes: 7 additions & 0 deletions packages/cheqd-blockchain-modules/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @docknetwork/cheqd-blockchain-modules

## 0.13.3

### Patch Changes

- Updated dependencies
- @docknetwork/credential-sdk@0.21.0

## 0.13.2

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/cheqd-blockchain-modules/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/cheqd-blockchain-modules",
"version": "0.13.2",
"version": "0.13.3",
"type": "module",
"license": "MIT",
"main": "./dist/esm/index.js",
Expand Down Expand Up @@ -33,7 +33,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@docknetwork/credential-sdk": "0.20.0"
"@docknetwork/credential-sdk": "0.21.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand All @@ -42,7 +42,7 @@
"@babel/plugin-syntax-import-attributes": "^7.25.6",
"@babel/plugin-transform-modules-commonjs": "^7.24.1",
"@babel/preset-env": "^7.24.3",
"@docknetwork/cheqd-blockchain-api": "0.14.5",
"@docknetwork/cheqd-blockchain-api": "0.15.0",
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/credential-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @docknetwork/credential-sdk

## 0.21.0

### Minor Changes

- Serialization/deserialization tweaks for `cheqd`

## 0.20.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/credential-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/credential-sdk",
"version": "0.20.0",
"version": "0.21.0",
"license": "MIT",
"type": "module",
"files": [
Expand Down
11 changes: 6 additions & 5 deletions packages/credential-sdk/src/types/accumulator/accumulator-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {

export class AccumulatorId extends withFrom(
withQualifier(TypedEnum, true),
(value, from) => {
(valueWithUncheckedPrefix, from) => {
const value = typeof valueWithUncheckedPrefix === 'string'
&& valueWithUncheckedPrefix.startsWith('dock:accumulator:')
? `accumulator:dock:${valueWithUncheckedPrefix.slice(17)}`
: valueWithUncheckedPrefix;

try {
// eslint-disable-next-line no-use-before-define
return from(DockAccumulatorIdValue.from(value));
Expand All @@ -20,10 +25,6 @@ export class AccumulatorId extends withFrom(
},
) {
static Qualifier = 'accumulator:';

toJSON() {
return String(this);
}
}

export class CheqdAccumulatorIdValue extends withQualifier(DidRef) {
Expand Down
4 changes: 4 additions & 0 deletions packages/credential-sdk/src/types/did/document/ident-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export default class IdentRef extends withQualifier(TypedTuple) {
return `${did}#${value}`;
}

toCheqdPayload() {
return this.toString();
}

toJSON() {
return this.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import VerificationMethodRef from './verification-method-ref';
import { CheqdVerificationMethod } from './verification-method';
import { CheqdVerificationMethodAssertion } from './verification-method';
import { withFrom } from '../../generic';

export default class VerificationMethodRefOrCheqdVerificationMethod extends withFrom(
VerificationMethodRef,
(value, from) => {
try {
return class CheqdVerificationMethodToJSONString extends CheqdVerificationMethod {
toJSON() {
return JSON.stringify(JSON.stringify(super.toJSON()));
}

static from(obj) {
return typeof obj === 'string' ? super.fromJSON(JSON.parse(JSON.parse(obj))) : super.from(obj);
}
}.from(value);
return CheqdVerificationMethodAssertion.from(value);
} catch {
return from(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default class VerificationMethodRef extends withQualifier(TypedTuple) {
return `${did}#keys-${index}`;
}

toCheqdPayload() {
return this.toEncodedString();
}

toJSON() {
return this.toEncodedString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {
withFrom, TypedStruct, withBase58, TypedBytes, option, TypedString,
withFrom,
TypedStruct,
withBase58,
TypedBytes,
option,
TypedString,
} from '../../generic';
import {
BBDT16PublicKey,
Expand All @@ -26,11 +31,18 @@ import {
Bls12381PSDockVerKeyName,
Bls12381BBDT16DockVerKeyName,
} from '../../../vc/crypto';
import { Ed25519Verification2018Method, Ed25519Verification2020Method, VerificationMethodType } from './verification-method-type';
import {
Ed25519Verification2018Method,
Ed25519Verification2020Method,
VerificationMethodType,
} from './verification-method-type';
import VerificationMethodRef from './verification-method-ref';
import { NamespaceDid } from '../onchain/typed-did';
import {
fmtIter, valueBytes, filterObj,
fmtIter,
valueBytes,
filterObj,
maybeToCheqdPayloadOrJSON,
} from '../../../utils';

export class PublicKeyBase58 extends withBase58(TypedBytes) {}
Expand Down Expand Up @@ -152,3 +164,21 @@ export class CheqdVerificationMethod extends withFrom(
);
}
}

export class CheqdVerificationMethodAssertion extends CheqdVerificationMethod {
toCheqdPayload() {
return JSON.stringify(
JSON.stringify(this.apply(maybeToCheqdPayloadOrJSON)),
);
}

toJSON() {
return this.toCheqdPayload();
}

static from(obj) {
return typeof obj === 'string'
? super.fromJSON(JSON.parse(JSON.parse(obj)))
: super.from(obj);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { maybeToNumber } from '../../../utils';
import { maybeToJSONString, maybeToNumber } from '../../../utils';
import {
TypedStruct,
TypedString,
Expand All @@ -17,7 +17,7 @@ const LinkedDomainsPlaceholder = createPlaceholder((value) => {
) {
return 0b0001;
} else {
throw new Error(`Unknown value \`${value}\``);
throw new Error(`Unknown value \`${maybeToJSONString(value)}\``);
}
});

Expand All @@ -42,6 +42,10 @@ export class LinkedDomains extends ServiceEndpointType {
toJSON() {
return this.constructor.Type;
}

apply(fn) {
return fn(this.constructor.Type);
}
}

ServiceEndpointType.bindVariants(LinkedDomains);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export class CheqdDid extends withQualifier(TypedEnum, true) {
toJSON() {
return String(this);
}

toCheqdPayload() {
return String(this);
}
}

export class CheqdTestnetDid extends CheqdDid {
Expand Down
Loading

0 comments on commit 0d07c3f

Please sign in to comment.