Skip to content

Commit

Permalink
cheqd Accumulator (#493)
Browse files Browse the repository at this point in the history
* WIP

* WIP: tests

* Implement `Accumulator` module for `cheqd`

* Tweaks

* Fix docs

* Lints

* Tweak

* Tweaks

* Renamings

* Fix import

* Remove outdated test

* Test corrections
  • Loading branch information
olegnn authored Dec 13, 2024
1 parent d5668ff commit cbfbf0d
Show file tree
Hide file tree
Showing 55 changed files with 2,372 additions and 822 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.4

### Patch Changes

- Updated dependencies
- @docknetwork/dock-blockchain-modules@0.10.0
- @docknetwork/credential-sdk@0.18.0
- @docknetwork/dock-blockchain-api@0.8.4

## 0.6.3

### 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.3",
"version": "0.6.4",
"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.17.0",
"@docknetwork/dock-blockchain-api": "0.8.3",
"@docknetwork/dock-blockchain-modules": "0.9.3"
"@docknetwork/credential-sdk": "0.18.0",
"@docknetwork/dock-blockchain-api": "0.8.4",
"@docknetwork/dock-blockchain-modules": "0.10.0"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
Expand Down
7 changes: 7 additions & 0 deletions packages/cheqd-blockchain-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @docknetwork/cheqd-blockchain-api

## 0.14.3

### Patch Changes

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

## 0.14.2

### 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.2",
"version": "0.14.3",
"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.17.0"
"@docknetwork/credential-sdk": "0.18.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand Down
11 changes: 11 additions & 0 deletions packages/cheqd-blockchain-modules/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @docknetwork/cheqd-blockchain-modules

## 0.13.0

### Minor Changes

- `Accumulator` module for `cheqd`

### Patch Changes

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

## 0.12.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.12.0",
"version": "0.13.0",
"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.17.0"
"@docknetwork/credential-sdk": "0.18.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.2",
"@docknetwork/cheqd-blockchain-api": "0.14.3",
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
224 changes: 224 additions & 0 deletions packages/cheqd-blockchain-modules/src/accumulator/internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import {
CheqdStoredAccumulator,
AccumulatorParams,
CheqdAccumulatorId,
CheqdAccumulatorParamsRef,
CheqdAccumulatorWithUpdateInfo,
CheqdCreateResource,
CheqdAccumulatorPublicKey,
} from '@docknetwork/credential-sdk/types';
import { TypedUUID, option } from '@docknetwork/credential-sdk/types/generic';
import { stringToU8a, u8aToString } from '@docknetwork/credential-sdk/utils';
import { VBWitnessUpdateInfo } from '@docknetwork/credential-sdk/crypto';
import {
injectParams,
injectPublicKeys,
createInternalCheqdModule,
SortedResourceVersions,
validateResource,
} from '../common';

const Type = 'accumulator';

const methods = {
addAccumulator: (accumulatorId, accumulator) => {
const [did, id] = CheqdAccumulatorId.from(accumulatorId).value;
const storedAcc = new CheqdStoredAccumulator(
accumulator,
).toJSONStringBytes();

return new CheqdCreateResource(
did.value.value,
TypedUUID.random(),
'1.0',
[],
String(id),
Type,
storedAcc,
);
},
updateAccumulator: (
accumulatorId,
accumulator,
{ additions, removals, witnessUpdateInfo },
) => {
const [did, id] = CheqdAccumulatorId.from(accumulatorId).value;
const storedAcc = new CheqdStoredAccumulator(
accumulator,
additions,
removals,
witnessUpdateInfo,
).toJSONStringBytes();

return new CheqdCreateResource(
did.value.value,
TypedUUID.random(),
'1.0',
[],
String(id),
Type,
storedAcc,
);
},
removeAccumulator: (accumulatorId) => {
const [did, id] = CheqdAccumulatorId.from(accumulatorId).value;

return new CheqdCreateResource(
did.value.value,
TypedUUID.random(),
'1.0',
[],
String(id),
Type,
stringToU8a('null'),
);
},
};

export default class CheqdInternalAccumulatorModule extends injectParams(
injectPublicKeys(createInternalCheqdModule(methods)),
) {
static get MsgNames() {
return {
...super.MsgNames,
addAccumulator: 'MsgCreateResource',
updateAccumulator: 'MsgCreateResource',
removeAccumulator: 'MsgCreateResource',
};
}

static PublicKey = CheqdAccumulatorPublicKey;

static PublicKeyName = 'AccumulatorPublicKey';

static PublicKeyType = 'accumulator-public-key';

static Params = AccumulatorParams;

static ParamsName = 'AccumulatorParams';

static ParamsType = 'accumulator-params';

static ParamsRef = CheqdAccumulatorParamsRef;

createAccumulatorMetadataFilter(name) {
const strName = String(name);

return (meta) => meta.resourceType === 'accumulator' && meta.name === strName;
}

async accumulator(accumulatorId) {
const [did, name] = CheqdAccumulatorId.from(accumulatorId).value;
const ids = new SortedResourceVersions(
await this.resourcesMetadataBy(
did,
this.createAccumulatorMetadataFilter(name),
),
).ids();

if (!ids.length) {
return null;
}

const acc = option(CheqdStoredAccumulator).from(
JSON.parse(
u8aToString(
validateResource(
await this.resource(did, ids[ids.length - 1]),
String(name),
Type,
),
),
),
);

if (acc == null) {
return null;
}

return new CheqdAccumulatorWithUpdateInfo(
ids[0],
ids[ids.length - 1],
acc.accumulator,
);
}

async lastParamsId(did) {
const res = await this.latestResourceMetadataBy(
did,
this.filterParamsMetadata,
);

return res?.id;
}

async lastPublicKeyId(did) {
const res = await this.latestResourceMetadataBy(
did,
this.filterPublicKeyMetadata,
);

return res?.id;
}

/**
* Update given witness by downloading necessary blocks and applying the updates if found. Both start and end are inclusive
* @param accumulatorId
* @param member
* @param witness - this will be updated to the latest witness
* @param start - accumulator version id to start from
* @param end - accumulator version id to end at
* @returns {Promise<void>}
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
async updateVbAccumulatorWitnessFromUpdatesInBlocks(
accumulatorId,
member,
witness,
start,
end,
) {
const [did, name] = CheqdAccumulatorId.from(accumulatorId).value;
const startUUID = String(TypedUUID.from(start));
const endUUID = String(TypedUUID.from(end));

const sortedIDs = new SortedResourceVersions(
await this.resourcesMetadataBy(
did,
this.createAccumulatorMetadataFilter(name),
),
).ids();

const startIdx = sortedIDs.findIndex((id) => id === startUUID);
let endIdx;
if (end != null) {
endIdx = sortedIDs.findIndex((id) => id === endUUID);

if (endIdx === -1) {
throw new Error(
`Accumulator \`${accumulatorId}\` with version \`${end}\` doesn't exist`,
);
}
} else {
endIdx = sortedIDs.length - 1;
}

const accumulators = await this.resources(
did,
sortedIDs.slice(startIdx, endIdx + 1),
);

for (const accumulator of new SortedResourceVersions(accumulators)) {
const { additions, removals, witnessUpdateInfo } = CheqdStoredAccumulator.from(
validateResource(accumulator, String(name), Type),
);

witness.updateUsingPublicInfoPostBatchUpdate(
member,
additions ? [...additions].map((addition) => addition.bytes) : [],
removals ? [...removals].map((removal) => removal.bytes) : [],
new VBWitnessUpdateInfo(witnessUpdateInfo?.bytes || new Uint8Array()),
);
}
}
}
Loading

0 comments on commit cbfbf0d

Please sign in to comment.