Skip to content

Commit

Permalink
Finalize migration scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Jan 9, 2025
1 parent ce79a60 commit 9a17ef5
Show file tree
Hide file tree
Showing 16 changed files with 624 additions and 330 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
],
"import/no-extraneous-dependencies": "off",
"class-methods-use-this": "off",
"generator-star-spacing": ["error", { "before": true, "after": false }],
"no-return-await": "off",
"no-plusplus": "off",
"no-void": "off",
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ turbo-debug.log*
npm-debug.log*
package-lock.json
tsconfig*buildinfo
yarn-debug.log*
yarn-error.log*
out/
*.log
6 changes: 5 additions & 1 deletion packages/cheqd-blockchain-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import {
CheqdTestnetOffchainSignatureKeyRef,
CheqdMainnetOffchainSignatureParamsRef,
CheqdMainnetOffchainSignatureKeyRef,
CheqdTestnetAccumulator,
CheqdMainnetAccumulator,
} from '@docknetwork/credential-sdk/types';
import { TypedEnum } from '@docknetwork/credential-sdk/types/generic';

Expand Down Expand Up @@ -96,6 +98,7 @@ export class CheqdAPI extends AbstractApiProvider {
[CheqdNetwork.Testnet]: extendNull({
Did: CheqdTestnetDid,
DidDocument: CheqdTestnetDIDDocument,
Accumulator: CheqdTestnetAccumulator,
AccumulatorId: CheqdTestnetAccumulatorId,
AccumulatorPublicKey: CheqdTestnetAccumulatorPublicKey,
StoredAccumulator: CheqdTestnetStoredAccumulator,
Expand All @@ -109,6 +112,7 @@ export class CheqdAPI extends AbstractApiProvider {
DidDocument: CheqdMainnetDIDDocument,
AccumulatorId: CheqdMainnetAccumulatorId,
AccumulatorPublicKey: CheqdMainnetAccumulatorPublicKey,
Accumulator: CheqdMainnetAccumulator,
StoredAccumulator: CheqdMainnetStoredAccumulator,
OffchainSignatureParamsRef: CheqdMainnetOffchainSignatureParamsRef,
OffchainSignatureKeyRef: CheqdMainnetOffchainSignatureKeyRef,
Expand All @@ -123,7 +127,7 @@ export class CheqdAPI extends AbstractApiProvider {
* @param {string} [configuration.url]
* @param {*} [configuration.wallet]
* @param {string} [configuration.network]
* @returns {this}
* @returns {Promise<this>}
*/
async init({ url, wallet, network } = {}) {
if (network !== CheqdNetwork.Mainnet && network !== CheqdNetwork.Testnet) {
Expand Down
39 changes: 20 additions & 19 deletions packages/cheqd-blockchain-modules/src/accumulator/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,32 +155,33 @@ export default class CheqdInternalAccumulatorModule extends injectParams(
this.createAccumulatorMetadataFilter(name),
),
).ids();
if (!ids.length) {
return null;
}

const resources = [
...new SortedResourceVersions(await this.resources(did, ids)),
];
const mapUpdate = (
{
accumulator,
additions,
removals,
witnessUpdateInfo = new Uint8Array(),
},
idx,
) => new AccumulatorUpdate(
resources[idx + 1].metadata.id,
accumulator.accumulated,
additions,
removals,
witnessUpdateInfo,
);

const accumulators = resources.map((acc) => CheqdStoredAccumulator.from(
JSON.parse(u8aToString(validateResource(acc, String(name), Type))),
));
const updates = accumulators
.slice(1)
.map(
(
{
accumulator,
additions,
removals,
witnessUpdateInfo = new Uint8Array(),
},
idx,
) => new AccumulatorUpdate(
resources[idx + 1].metadata.id,
accumulator.accumulated,
additions,
removals,
witnessUpdateInfo,
),
);
const updates = accumulators.slice(1).map(mapUpdate);

return new CheqdAccumulatorHistory(
new CheqdAccumulatorWithUpdateInfo(
Expand Down
2 changes: 1 addition & 1 deletion packages/cheqd-blockchain-modules/src/did/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TypedUUID } from '@docknetwork/credential-sdk/types/generic';
import { createInternalCheqdModule } from '../common';

function parseDocument(document) {
return DIDDocument.from(document).toCheqd(void 0, this.types.DidDocument);
return DIDDocument.from(document).toCheqd(this.types.DidDocument);
}

const methods = {
Expand Down
8 changes: 2 additions & 6 deletions packages/credential-sdk/src/keypairs/did-keypair.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ export default class DidKeypair {
* @param {DockKeypair} keyPair - Key pair provided.
*/
constructor(keyRef, keyPair) {
const ref = VerificationMethodRefOrIdentRef.from(keyRef);

ensureInstanceOf(keyPair, DockKeypair);

this.verificationMethodId = ref;
this.keyPair = keyPair;
this.verificationMethodId = VerificationMethodRefOrIdentRef.from(keyRef);
this.keyPair = ensureInstanceOf(keyPair, DockKeypair);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/credential-sdk/src/types/did/document/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class DIDDocument extends withFrom(
}

// eslint-disable-next-line no-use-before-define
toCheqd(versionId = TypedUUID.random(), Class = CheqdDIDDocument) {
toCheqd(Class = CheqdDIDDocument, versionId = TypedUUID.random()) {
const {
'@context': context,
id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export function patchWithFromDock(klass, Type, mapping) {
const hexId = value.toEncodedString();
const dockDid = mapping.testnet[hexId] ?? mapping.mainnet[hexId];
if (dockDid == null) {
throw new Error(`Dock DID not found for ${hexId}`);
throw new Error(
`Dock DID not found for ${hexId} - can't create ${this.name}`,
);
}

id = [DockDid.from(dockDid), value];
Expand Down
3 changes: 3 additions & 0 deletions packages/credential-sdk/src/utils/dock-did-by-id.json
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@
"0xd66601f260993c69119d2d6206f6f9109ee64376644e15392af3e11db35c1b47": "did:dock:5GbfQzGCmHL8oHVGWhqyo5zaoBQkY7dMox4ppFqSmpbnNjZn",
"0x1193a1a2b8c8d486dd43bdf980fe50553124e363dec2b459cb63b5bef943d3f4": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0x3f3d6224d2a6a7af90045c78b269ba55a24a73670cf55d002516ed565c64feb9": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0x7792c3daef089ed6fafec9a3d6ddf02fe76cfc8f06726c208d190aa6fa128739": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0xfc1775cbed57fa0eb6f2c1c869924da072e196643327d826ed3b771338b3c288": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0xf1008ea054479197ce954b81a41bd8560dcbe4f28ac86ffac639949feeb33ca9": "did:dock:5CYjsSrTtyPvqdHTnsExQn8RnWFAsrkUjWX3ru9AStukRJyE",
"0xea26b1a42f4ba56f568da53c7c1ea042edc981f86651ac277676b878ebd9c00a": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
Expand Down Expand Up @@ -1261,6 +1262,7 @@
"0x8eec44ff296754b58a2f8a80959cea4d9e15ae0c542dde5c68e74da4e55c0bff": "did:dock:5CYjsSrTtyPvqdHTnsExQn8RnWFAsrkUjWX3ru9AStukRJyE",
"0xf03dfd650fefb4020e043b9af07f0f7330e89dd3571e324fc8e10f387a8e5c55": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0x47b0c7f379ef61a4c5d6716fe2ec0eb52f0842c88018b708985d07a9d140b6ba": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0xd777711b06f0e31cc6bd151b58f1600d63142f18a0082d263265224750c9aec4": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0x7876964a93332dfdd384ec9fc9e1e0047bc0a112b4deace7deccec58e091315e": "did:dock:5FiyjWBxwS16YCeLWuFGA5va8PoTvXQGxTWQSXYYEPWjbYDS",
"0x029895fc45c9af4972ef2d8ff0034d8f9e768735d3072b91a1d2bc0cbb9de034": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0xad8f3c5f6ecac60e948158f48a7fbdb96816cb9b15c59f65231fa4836ea78ded": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
Expand Down Expand Up @@ -1654,6 +1656,7 @@
"0xdf56533bb7fdd76d49403a3662bc941ab5f9d825b70334665fddde88763dd5b0": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0xe601f2b83751f4ad5bb4a674fce3547cd69f3f633481976add96e962704bec31": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0x72c2b56cca10b2acf4038fc3c16596664f283e8984554aa80b4901e9ed242548": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0xa460d3e371376fe7d31feb34e05a88a78d5ae74c3cf63b660053bef2a11fe0d9": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0x73104b241f7ee5e566317238430cd8f0105aaf3b5cd48c48c1215defe07f53b4": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
"0xd9381a271f2102d02e334f5ee95af324918a5e18f49b7d2293ff6796c1b795a7": "did:dock:5GbfQzGCmHL8oHVGWhqyo5zaoBQkY7dMox4ppFqSmpbnNjZn",
"0x6fcb3eebe310ce16e64c327714b3d3fc586d881b21d82ab3291610a49853f0a8": "did:dock:5FgoGVWJVyqFa9tcWQbfbSCfmPcKet8fqtU1dtE15jx7UeVP",
Expand Down
6 changes: 3 additions & 3 deletions packages/credential-sdk/tests/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ describe("DID document workflow", () => {
[],
[],
[]
).toCheqd("0557d7da-8dea-45cb-8f30-7370b59eab71");
).toCheqd(void 0, "0557d7da-8dea-45cb-8f30-7370b59eab71");

const doc2 = DIDDocument.create(
cheqdDid,
[new DidKey(RANDOM_PKS[0]), new DidKey(CHEQD_OFFCHAIN_PKS[0])],
[cheqdDid],
[]
).toCheqd("28edc043-872b-4ab7-9cb8-b01d1bd677c5");
).toCheqd(void 0, "28edc043-872b-4ab7-9cb8-b01d1bd677c5");

const doc2DIDDocument = DIDDocument.create(
cheqdDid,
Expand Down Expand Up @@ -213,7 +213,7 @@ describe("DID document workflow", () => {
"did:cheqd:testnet:df3ba823-c1e2-46e0-bb54-056f9c506e90",
],
[]
).toCheqd("47b4d97b-1ebe-4d81-a3ed-12eb63baaeb9");
).toCheqd(void 0, "47b4d97b-1ebe-4d81-a3ed-12eb63baaeb9");

checkDocs(doc1, doc2, doc3);
});
Expand Down
45 changes: 37 additions & 8 deletions packages/dock-blockchain-modules/src/accumulator/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ export default class DockInternalAccumulatorModule extends injectParams(

async accumulatorHistory(accumulatorId) {
let acc = await this.getAccumulator(accumulatorId);
if (acc == null) {
return null;
}
let updates = [];

const mapUpdate = ({
Expand Down Expand Up @@ -308,21 +311,47 @@ export default class DockInternalAccumulatorModule extends injectParams(
* @returns {Promise<object|undefined>} - Resolves to an `update` object with keys `newAccumulated`, `additions`, `removals` and `witnessUpdateInfo`.
* The last keys have value null if they were not provided in the extrinsic.
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
getUpdatesFromExtrinsic(ext, accumulatorId) {
const accId = DockAccumulatorId.from(accumulatorId);

// Helper function to process individual calls
const processCall = (call) => {
if (
call.section === 'accumulator'
&& call.method === 'updateAccumulator'
) {
const update = UpdateAccumulator.from(
this.apiProvider.api.createType('UpdateAccumulator', call.args[0]),
);

if (update.id.eq(accId.asDock)) {
return update;
}
}
return null;
};

// Check if the extrinsic is a batch or batchAll
if (
ext.method
&& ext.method.section === 'accumulator'
&& ext.method.method === 'updateAccumulator'
&& ext.method.section === 'utility'
&& (ext.method.method === 'batch' || ext.method.method === 'batchAll')
) {
const update = UpdateAccumulator.from(
this.apiProvider.api.createType('UpdateAccumulator', ext.method.args[0]),
);

if (update.id.eq(accId.asDock)) {
return update;
const calls = ext.method.args[0]; // Array of calls
if (calls && Array.isArray(calls)) {
for (const call of calls) {
const result = processCall(call);
if (result) {
return result; // Return the first matching update
}
}
} else {
console.error('Failed to parse batch calls:', calls);
}
} else {
// Process as a single call
return processCall(ext.method);
}

return null;
Expand Down
9 changes: 3 additions & 6 deletions packages/dock-blockchain-modules/src/common/keypair.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ export const allSigners = (didKeypair) => [].concat(didKeypair).map((keyPair) =>

export const firstSigner = (didKeypair) => {
const signers = allSigners(didKeypair);
if (signers.length !== 1) {
throw new Error(`Must provide 1 valid DID keypair, received ${signers.length}`);
if (!signers.length) {
throw new Error('Must provide at least 1 DID keypair');
}

return signers[0];
};

export const targetKeypair = (targetDid, didKeypair) => {
const signerKp = allSigners(didKeypair)
.find(
(keyPair) => keyPair.did.eq(targetDid),
);
const signerKp = allSigners(didKeypair).find((keyPair) => keyPair.did.eq(targetDid));

if (signerKp == null) {
throw new Error(`No keypair provided for ${targetDid}`);
Expand Down
41 changes: 19 additions & 22 deletions scripts/migration/src/did-by-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {
DockBlobId,
DockAccumulatorId,
DockStatusListCredentialId,
} from "@docknetwork/credential-sdk/types";
import { DockAPI } from "@docknetwork/dock-blockchain-api";
} from '@docknetwork/credential-sdk/types';
import { DockAPI } from '@docknetwork/dock-blockchain-api';
import {
DockBlobModule,
DockAccumulatorModule,
DockStatusListCredentialModule,
} from "@docknetwork/dock-blockchain-modules";
import pLimit from "p-limit";
} from '@docknetwork/dock-blockchain-modules';
import pLimit from 'p-limit';

const mergeAwait = async (objectPromises) => {
const acc = {};
Expand All @@ -32,11 +32,10 @@ export default class Mappings {
const module = new DockAccumulatorModule(dock);

const ids = [...(await dock.api.query.accumulator.accumulators.keys())].map(
(rawId) =>
this.spawn(async () => {
const id = DockAccumulatorId.from(rawId.toHuman()[0]);
return [id, await module.getAccumulator(id)];
})
(rawId) => this.spawn(async () => {
const id = DockAccumulatorId.from(rawId.toHuman()[0]);
return [id, await module.getAccumulator(id)];
}),
);

for await (const [id, acc] of ids) {
Expand All @@ -55,7 +54,7 @@ export default class Mappings {
async (rawId) => {
const id = DockBlobId.from(rawId.toHuman()[0]);
return [id, await module.get(id)];
}
},
);

for await (const [id, [owner]] of ids) {
Expand All @@ -71,13 +70,11 @@ export default class Mappings {

const ids = [
...(await dock.api.query.statusListCredential.statusListCredentials.keys()),
].map((rawId) =>
this.spawn(async () => {
const id = DockStatusListCredentialId.from(rawId.toHuman()[0]);
].map((rawId) => this.spawn(async () => {
const id = DockStatusListCredentialId.from(rawId.toHuman()[0]);

return [id, await module.dockOnly.statusListCredential(id)];
})
);
return [id, await module.dockOnly.statusListCredential(id)];
}));

for await (const [id, { policy }] of ids) {
lists[id.toEncodedString()] = String([...policy.value][0]);
Expand All @@ -88,17 +85,17 @@ export default class Mappings {

async run() {
await Promise.all([
this.mainnet.init({ address: "wss://mainnet-node.dock.io" }),
this.testnet.init({ address: "wss://knox-1.dock.io" }),
this.mainnet.init({ address: 'wss://mainnet-node.dock.io' }),
this.testnet.init({ address: 'wss://knox-1.dock.io' }),
]);
const data = await mergeAwait(
["accumulators", "blobs", "statusLists"].map(async (entity) => ({
['accumulators', 'blobs', 'statusLists'].map(async (entity) => ({
[entity]: await mergeAwait(
["mainnet", "testnet"].map(async (key) => ({
['mainnet', 'testnet'].map(async (key) => ({
[key]: await this[entity](this[key]),
}))
})),
),
}))
})),
);

await Promise.all([this.mainnet.disconnect(), this.testnet.disconnect()]);
Expand Down
Loading

0 comments on commit 9a17ef5

Please sign in to comment.