Skip to content

Commit

Permalink
Blob module for Cheqd, improvements for DIDDocument and misc tw…
Browse files Browse the repository at this point in the history
…eaks (#459)

* `CheqdBlobModule` + tweaks

* Lints

* Bump up versions

* Tweaks

* Corrections

* Reenable other tests

* Fix arguments

* `withDidNonce`

* Tweak

* Make `withDidNonce` non-async function

* `await`
  • Loading branch information
olegnn authored Oct 21, 2024
1 parent a5be46b commit e8e8131
Show file tree
Hide file tree
Showing 82 changed files with 1,366 additions and 657 deletions.
16 changes: 16 additions & 0 deletions examples/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# @docknetwork/sdk-examples

## 0.2.0

### Minor Changes

- - Updated `DIDDocument`: modified the rules for storing keys in the verificationMethod and added the required validations.
- Developed generic tests for modules to ensure they cover the same use cases, regardless of the specific implementation.
- Implemented the `CheqdBlobModule`.
- Misc tweaks.

### Patch Changes

- Updated dependencies
- @docknetwork/dock-blockchain-modules@0.3.0
- @docknetwork/dock-blockchain-api@0.2.0
- @docknetwork/credential-sdk@0.3.0

## 0.1.2

### 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.1.2",
"version": "0.2.0",
"scripts": {
"bbs-dock-example": "babel-node --loader=./loader.mjs ./bbs-dock.js",
"claim-deduction-example": "babel-node --loader=./loader.mjs ./claim-deduction.js",
Expand All @@ -19,9 +19,9 @@
"lint": "eslint \"*.js\""
},
"dependencies": {
"@docknetwork/credential-sdk": "0.2.0",
"@docknetwork/dock-blockchain-api": "0.1.2",
"@docknetwork/dock-blockchain-modules": "0.2.0"
"@docknetwork/credential-sdk": "0.3.0",
"@docknetwork/dock-blockchain-api": "0.2.0",
"@docknetwork/dock-blockchain-modules": "0.3.0"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
Expand Down
3 changes: 2 additions & 1 deletion examples/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DIDDocument,
DidKey,
VerificationRelationship,
DockBlobId,
} from '@docknetwork/credential-sdk/types';
import { Schema } from '@docknetwork/credential-sdk/modules';
import { DockCoreModules } from '@docknetwork/dock-blockchain-modules';
Expand Down Expand Up @@ -70,7 +71,7 @@ async function main() {
await createDID(subjectDID, subjectPair);

console.log('Creating a new schema...');
const schema = new Schema();
const schema = new Schema(DockBlobId.random());
await schema.setJSONSchema({
$schema: 'http://json-schema.org/draft-07/schema#',
description: 'Dock Schema Example',
Expand Down
9 changes: 9 additions & 0 deletions packages/cheqd-blockchain-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @docknetwork/cheqd-blockchain-api

## 0.2.0

### Minor Changes

- - Updated `DIDDocument`: modified the rules for storing keys in the verificationMethod and added the required validations.
- Developed generic tests for modules to ensure they cover the same use cases, regardless of the specific implementation.
- Implemented the `CheqdBlobModule`.
- Misc tweaks.

## 0.1.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion 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.1.2",
"version": "0.2.0",
"license": "MIT",
"main": "./dist/esm/index.js",
"type": "module",
Expand Down
4 changes: 3 additions & 1 deletion packages/cheqd-blockchain-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ export class CheqdAPI extends ApiProvider {
if (res.code) {
console.error(res);

throw new Error(`Error: ${JSON.stringify(res)}`);
throw new Error(
JSON.stringify(res, (_, value) => (typeof value === 'bigint' ? `${value.toString()}n` : value)),
);
}

return res;
Expand Down
14 changes: 14 additions & 0 deletions packages/cheqd-blockchain-modules/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# @docknetwork/cheqd-blockchain-modules

## 0.3.0

### Minor Changes

- - Updated `DIDDocument`: modified the rules for storing keys in the verificationMethod and added the required validations.
- Developed generic tests for modules to ensure they cover the same use cases, regardless of the specific implementation.
- Implemented the `CheqdBlobModule`.
- Misc tweaks.

### Patch Changes

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

## 0.2.0

### Minor 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.2.0",
"version": "0.3.0",
"type": "module",
"license": "MIT",
"main": "./dist/esm/index.js",
Expand Down Expand Up @@ -28,7 +28,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@docknetwork/credential-sdk": "0.2.0"
"@docknetwork/credential-sdk": "0.3.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand All @@ -37,7 +37,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.1.2",
"@docknetwork/cheqd-blockchain-api": "0.2.0",
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
30 changes: 8 additions & 22 deletions packages/cheqd-blockchain-modules/src/attest/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CheqdCreateResource, createInternalCheqdModule } from '../common';

const methods = {
setClaim: (iri, targetDid) => new CheqdCreateResource(
targetDid.value,
CheqdDid.from(targetDid).value,
TypedUUID.random(),
'1.0',
[],
Expand All @@ -24,29 +24,15 @@ export default class CheqdInternalAttestModule extends createInternalCheqdModule
};

async attest(did, attestId) {
const stringDid = CheqdDid.from(did).toEncodedString();
const stringAttestId = String(TypedUUID.from(attestId));
const item = await this.query.resource(stringDid, stringAttestId);

return option(Iri).from(item?.resource?.data);
return option(Iri).from(
(await this.resource(did, attestId))?.resource?.data,
);
}

async attestId(did) {
let resources = [];
let paginationKey;

do {
// eslint-disable-next-line no-await-in-loop
const res = await this.query.collectionResources(
CheqdDid.from(did).toEncodedString(),
paginationKey,
);
resources = resources.concat(
res.resources.filter((resource) => resource.resourceType === 'attest'),
);
({ paginationKey } = res);
} while (!resources.length && paginationKey != null);

return resources.find((resource) => !resource.nextVersionId)?.id;
return await this.latestResourceIdBy(
did,
(resource) => resource.resourceType === 'attest',
);
}
}
41 changes: 41 additions & 0 deletions packages/cheqd-blockchain-modules/src/blob/internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
Blob,
CheqdBlobId,
CheqdBlobWithId,
CheqdDid,
} from '@docknetwork/credential-sdk/types';
import { option } from '@docknetwork/credential-sdk/types/generic';
import { CheqdCreateResource, createInternalCheqdModule } from '../common';

const methods = {
new: (blobWithId) => {
const { blob, id } = CheqdBlobWithId.from(blobWithId);
const [did, uuid] = id;

return new CheqdCreateResource(
CheqdDid.from(did).value,
uuid,
'1.0',
[],
'Blob',
'blob',
blob,
);
},
};

export default class CheqdInternalBlobModule extends createInternalCheqdModule(
methods,
) {
static Prop = 'resource';

static MsgNames = {
new: 'MsgCreateResource',
};

async blob(blobId) {
return option(Blob).from(
(await this.resource(...CheqdBlobId.from(blobId)))?.resource?.data,
);
}
}
25 changes: 25 additions & 0 deletions packages/cheqd-blockchain-modules/src/blob/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import AbstractBlobModule from '@docknetwork/credential-sdk/modules/blob/module';
import { NoBlobError } from '@docknetwork/credential-sdk/modules/blob/errors';
import { CheqdBlobId } from '@docknetwork/credential-sdk/types';
import { injectCheqd } from '../common';
import CheqdInternalBlobModule from './internal';
import { OwnerWithBlob } from './types';

export default class CheqdBlobModule extends injectCheqd(AbstractBlobModule) {
static CheqdOnly = CheqdInternalBlobModule;

async newTx(blobWithId, targetDid, didKeypair) {
return await this.cheqdOnly.tx.new(blobWithId, targetDid, didKeypair);
}

async get(blobId) {
const id = CheqdBlobId.from(blobId);
const blob = await this.cheqdOnly.blob(id);

if (blob == null) {
throw new NoBlobError(id);
}

return new OwnerWithBlob(id[0], blob);
}
}
6 changes: 6 additions & 0 deletions packages/cheqd-blockchain-modules/src/blob/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { CheqdDid, Blob } from '@docknetwork/credential-sdk/types';
import { TypedTuple } from '@docknetwork/credential-sdk/types/generic';

export class OwnerWithBlob extends TypedTuple {
static Classes = [CheqdDid, Blob];
}
57 changes: 56 additions & 1 deletion packages/cheqd-blockchain-modules/src/common/builders.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { VerificationMethodSignature } from '@docknetwork/credential-sdk/types';
import {
VerificationMethodSignature,
CheqdDid,
} from '@docknetwork/credential-sdk/types';
import { TypedUUID } from '@docknetwork/credential-sdk/types/generic';
import { CheqdPayloadWithTypeUrl } from './payload';

/**
Expand Down Expand Up @@ -75,6 +79,7 @@ class Root {
}
}

/* eslint-disable sonarjs/cognitive-complexity */
export function createInternalCheqdModule(
methods = Object.create(null),
baseClass = class CheqdModuleBaseClass {},
Expand All @@ -100,6 +105,56 @@ export function createInternalCheqdModule(
this.apiProvider = apiProvider;
}

async resource(did, id) {
const strDid = CheqdDid.from(did).toEncodedString();
const strID = String(TypedUUID.from(id));

try {
return await this.apiProvider.sdk.querier.resource.resource(
strDid,
strID,
);
} catch (err) {
if (!String(err).includes('DID Doc not found')) {
throw err;
}
}

return null;
}

async latestResourceIdBy(did, cond) {
let resources;
let paginationKey;
const encodedDid = CheqdDid.from(did).toEncodedString();

do {
try {
// eslint-disable-next-line operator-linebreak
({ resources, paginationKey } =
// eslint-disable-next-line no-await-in-loop
await this.apiProvider.sdk.querier.resource.collectionResources(
encodedDid,
paginationKey,
));
} catch (err) {
if (!String(err).includes('DID Doc not found')) {
throw err;
} else {
break;
}
}

for (const resource of resources) {
if (cond(resource) && !resource.nextVersionId) {
return resource.id;
}
}
} while (paginationKey != null);

return null;
}

get query() {
return this.apiProvider.sdk.querier[this.constructor.Prop];
}
Expand Down
17 changes: 10 additions & 7 deletions packages/cheqd-blockchain-modules/src/did/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ export default class CheqdDIDModule extends injectCheqd(AbstractDIDModule) {
async updateDocumentTx(document, didSigners) {
const didDocument = DIDDocument.from(document);
if (didDocument.attests != null) {
throw new Error(
`Non-null \`attest\` was provided for DID document \`${didDocument.id}\`. Use \`attest\` module to set attestations.`,
);
const currentDocument = await this.getDocument(didDocument.id);

if (!didDocument.attests.eq(currentDocument.attests)) {
throw new Error(
'`attests` modifications are not supported in the `updateDocument` transaction.',
);
}
}

return await this.cheqdOnly.tx.updateDidDocument(didDocument, didSigners);
Expand Down Expand Up @@ -71,9 +75,8 @@ export default class CheqdDIDModule extends injectCheqd(AbstractDIDModule) {
throw new NoDIDError(cheqdDid);
}

const document = CheqdDIDDocument.from(doc).toDIDDocument();
const attests = await this.attest.getAttests(did);

return document.setAttests(attests);
return CheqdDIDDocument.from(doc)
.toDIDDocument()
.setAttests(await this.attest.getAttests(cheqdDid));
}
}
7 changes: 4 additions & 3 deletions packages/cheqd-blockchain-modules/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AbstractCoreModules } from '@docknetwork/credential-sdk/modules';
import CheqdAttestModule from './attest/module';
import CheqdBlobModule from './blob/module';
import CheqdDIDModule from './did/module';
// import CheqdAccumulatorModule from './accumulator/module';
// import CheqdAnchorModule from './anchor/module';
// import CheqdBlobModule from './blob/module';
// import CheqdOffchainSignaturesModule from './offchain-signatures/module';
// import CheqdBBSModule from './offchain-signatures/bbs';
// import CheqdBBSPlusModule from './offchain-signatures/bbs-plus';
Expand All @@ -18,7 +18,7 @@ export class CheqdCoreModules extends AbstractCoreModules {

static AttestModule = CheqdAttestModule;

// static BlobModule = CheqdBlobModule;
static BlobModule = CheqdBlobModule;

static DIDModule = CheqdDIDModule;

Expand All @@ -38,9 +38,10 @@ export class CheqdCoreModules extends AbstractCoreModules {
export {
CheqdAttestModule,
CheqdDIDModule,
CheqdBlobModule,
// CheqdAccumulatorModule,
// CheqdAnchorModule,
// CheqdBlobModule,
// ,
// CheqdOffchainSignaturesModule,
// CheqdBBSModule,
// CheqdBBSPlusModule,
Expand Down
Loading

0 comments on commit e8e8131

Please sign in to comment.