Skip to content

Commit

Permalink
Merge pull request #389 from docknetwork/trust-registry-new-update-me…
Browse files Browse the repository at this point in the history
…chanism

`Trust registry`: combine adding/updating schema operations
  • Loading branch information
cykoder authored Feb 7, 2024
2 parents 2d56ba1 + 41fc099 commit 47fdd23
Show file tree
Hide file tree
Showing 12 changed files with 1,112 additions and 107 deletions.
2 changes: 1 addition & 1 deletion scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export const formatDock = (value, config = {}) =>
/**
* Enhances supplied function by providing access to the initialized global dock API.
*
* Global dock API object will be initialized with the given params before passing to the function,
* Dock API object will be initialized with the given params before passing to the function,
* and will be disconnected when the promise will be either resolved or rejected.
*
* @template T
Expand Down
7 changes: 3 additions & 4 deletions scripts/validator_stash_payouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
notNilAnd,
finiteNumber,
parseBool,
timestampLogger,
binarySearchFirstSatisfyingBlock,
} from "./helpers";
import { o, defaultTo, unless, either, curry, __, sum } from "ramda";
Expand Down Expand Up @@ -153,7 +152,7 @@ const main = withDockAPI(
.sort(([i1], [i2]) => i1 - i2)
.reduce(
(acc, [index, { total, staking, commission, blocks, prefs }]) => {
timestampLogger.log(
console.log(
`Era ${index}: paid = \`${formatDock(
total
)}\` (staking = ${formatDock(staking)}, commission = ${formatDock(
Expand All @@ -178,7 +177,7 @@ const main = withDockAPI(
}
);

timestampLogger.log(
console.log(
`Summarised stash payout for ${Stash} in ${StartEra}-${
StartEra + ErasCount - 1
} eras - total = \`${formatDock(total, {
Expand Down Expand Up @@ -390,4 +389,4 @@ const validatorStashPayout = (
};
};

main().catch(timestampLogger.error);
main().catch(console.error);
195 changes: 146 additions & 49 deletions src/modules/trust-registry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BTreeSet } from '@polkadot/types';
import { typedHexDID } from '../utils/did';
import { getDidNonce } from '../utils/misc';
import { BTreeSet, BTreeMap } from '@polkadot/types';
import { DidMethodKey, DockDid, typedHexDID } from '../utils/did';
import { getDidNonce, ensureMatchesPattern } from '../utils/misc';

/**
* `Trust Registry` module.
Expand Down Expand Up @@ -42,7 +42,10 @@ export default class TrustRegistryModule {
waitForFinalization = true,
params = {},
) {
const [convenerHexDid, lastNonce] = await this.getActorDidAndNonce(convenerDid, { nonce, didModule });
const [convenerHexDid, lastNonce] = await this.getActorDidAndNonce(
convenerDid,
{ nonce, didModule },
);

return this.signAndSend(
convenerHexDid.changeState(
Expand All @@ -62,43 +65,6 @@ export default class TrustRegistryModule {
);
}

/**
* Appends new schemas to the registry.
* @param convenerDid
* @param registryId
* @param schemas
* @param signingKeyRef
* @param nonce
* @param didModule
* @param params
* @param waitForFinalization
* @param params
* @returns {Promise<null>}
*/
async addSchemaMetadata(
convenerDid,
registryId,
schemas,
signingKeyRef,
{ nonce = undefined, didModule = undefined } = {},
waitForFinalization = true,
params = {},
) {
const [convenerHexDid, lastNonce] = await this.getActorDidAndNonce(convenerDid, { nonce, didModule });

return this.signAndSend(
convenerHexDid.changeState(
this.api,
this.module.addSchemaMetadata,
'AddSchemaMetadata',
{ registryId, schemas, nonce: lastNonce },
signingKeyRef,
),
waitForFinalization,
params,
);
}

/**
* Updates schemas metadatas in the registry.
* @param convenerOrIssuerOrVerifierDid
Expand All @@ -112,7 +78,7 @@ export default class TrustRegistryModule {
* @param params
* @returns {Promise<null>}
*/
async updateSchemaMetadata(
async setSchemasMetadata(
convenerOrIssuerOrVerifierDid,
registryId,
schemas,
Expand All @@ -121,13 +87,20 @@ export default class TrustRegistryModule {
waitForFinalization = true,
params = {},
) {
const [convenerOrIssuerOrVerifierHexDid, lastNonce] = await this.getActorDidAndNonce(convenerOrIssuerOrVerifierDid, { nonce, didModule });
const [convenerOrIssuerOrVerifierHexDid, lastNonce] = await this.getActorDidAndNonce(convenerOrIssuerOrVerifierDid, {
nonce,
didModule,
});
ensureMatchesPattern(
this.constructor.SchemasUpdatePattern,
schemas,
);

return this.signAndSend(
convenerOrIssuerOrVerifierHexDid.changeState(
this.api,
this.module.updateSchemaMetadata,
'UpdateSchemaMetadata',
this.module.setSchemasMetadata,
'SetSchemasMetadata',
{ registryId, schemas, nonce: lastNonce },
signingKeyRef,
),
Expand Down Expand Up @@ -158,7 +131,10 @@ export default class TrustRegistryModule {
waitForFinalization = true,
params = {},
) {
const [convenerHexDid, lastNonce] = await this.getActorDidAndNonce(convenerDid, { nonce, didModule });
const [convenerHexDid, lastNonce] = await this.getActorDidAndNonce(
convenerDid,
{ nonce, didModule },
);

const hexIssuers = new BTreeSet();
for (const issuer of issuers) {
Expand Down Expand Up @@ -200,7 +176,10 @@ export default class TrustRegistryModule {
waitForFinalization = true,
params = {},
) {
const [convenerHexDid, lastNonce] = await this.getActorDidAndNonce(convenerDid, { nonce, didModule });
const [convenerHexDid, lastNonce] = await this.getActorDidAndNonce(
convenerDid,
{ nonce, didModule },
);

const hexIssuers = new BTreeSet();
for (const issuer of issuers) {
Expand Down Expand Up @@ -241,7 +220,10 @@ export default class TrustRegistryModule {
waitForFinalization = true,
params = {},
) {
const [issuerHexDid, lastNonce] = await this.getActorDidAndNonce(issuerDid, { nonce, didModule });
const [issuerHexDid, lastNonce] = await this.getActorDidAndNonce(
issuerDid,
{ nonce, didModule },
);

return this.signAndSend(
issuerHexDid.changeState(
Expand All @@ -263,9 +245,124 @@ export default class TrustRegistryModule {
* @param didModule
* @returns {Promise}
*/
async getActorDidAndNonce(actorDid, { nonce = undefined, didModule = undefined } = {}) {
async getActorDidAndNonce(
actorDid,
{ nonce = undefined, didModule = undefined } = {},
) {
const hexDID = typedHexDID(this.api, actorDid);
const lastNonce = nonce ?? (await getDidNonce(hexDID, nonce, didModule));
return [hexDID, lastNonce];
}
}

const DockDidOrDidMethodKeyPattern = {
$anyOf: [{ $instanceOf: DockDid }, { $instanceOf: DidMethodKey }],
};

const VerificationPricePattern = {
$anyOf: [{ $matchType: 'number' }, { $matchType: 'object' }],
};

const VerifiersPattern = {
$instanceOf: BTreeSet,
$iterableOf: DockDidOrDidMethodKeyPattern,
};

const VerifiersUpdatePattern = {
$instanceOf: BTreeMap,
$mapOf: [
DockDidOrDidMethodKeyPattern,
{
$anyOf: [
{ $matchValue: 'Remove' },
{
$matchValue: 'Add',
},
],
},
],
};

const IssuerPricesPattern = {
$instanceOf: BTreeMap,
$mapOf: [{ $matchType: 'string' }, VerificationPricePattern],
};

const IssuerPricesUpdatePattern = {
$instanceOf: BTreeMap,
$mapOf: [
{ $matchType: 'string' },
{
$anyOf: [
{ $matchValue: 'Remove' },
{
$objOf: {
Add: VerificationPricePattern,
Set: VerificationPricePattern,
},
},
],
},
],
};

const IssuersPattern = {
$instanceOf: BTreeMap,
$mapOf: [DockDidOrDidMethodKeyPattern, IssuerPricesPattern],
};

const IssuersUpdatePattern = {
$instanceOf: BTreeMap,
$mapOf: [
DockDidOrDidMethodKeyPattern,
{
$objOf: {
Modify: IssuerPricesUpdatePattern,
Set: IssuerPricesPattern,
},
},
],
};

TrustRegistryModule.SchemasUpdatePattern = {
$instanceOf: BTreeMap,
$mapOf: [
{ $matchType: 'string' },
{
$anyOf: [
{
$objOf: {
Add: {
$matchObject: {
issuers: IssuersPattern,
verifiers: VerifiersPattern,
},
},
Set: {
$matchObject: {
issuers: IssuersPattern,
verifiers: VerifiersPattern,
},
},
Modify: {
$matchObject: {
issuers: {
$objOf: { Modify: IssuersUpdatePattern, Set: IssuersPattern },
},
verifiers: {
$objOf: {
Modify: VerifiersUpdatePattern,
Set: VerifiersPattern,
},
},
},
},
},
},
{
$matchValue: 'Remove',
},
],
},
],
};
11 changes: 2 additions & 9 deletions src/resolver/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fmtIter } from '../utils/misc';

/**
* Before resolving an entity, ensures that `DockAPI` is initialized, throws an error otherwise.
* @template T
Expand All @@ -18,15 +20,6 @@ export const withInitializedDockAPI = (
}
};

/**
* Returns string containing comma separated items of the provided iterable.
*
* @template V
* @param {Iterable<V>} iter
* @returns {string}
*/
export const fmtIter = (iter) => `\`[${[...iter].map((item) => item.toString()).join(', ')}]\``;

/**
* Caches last function result and returns it if function called with the same args again.
* @param {Function}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/did/did-keypair.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { randomAsHex } from '@polkadot/util-crypto';
import { DockKeyPair } from '../misc';
import { DockKeypair } from '../misc';

/**
* Signing keypair along with the optional key identifier.
*/
export class DidKeypair extends DockKeyPair {
export class DidKeypair extends DockKeypair {
/**
* Wraps supplied keypair into a `DidKeypair`.
*
Expand Down
2 changes: 1 addition & 1 deletion src/utils/did/typed-did/did-method-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class DidMethodKey extends DockDidOrDidMethodKey {
/**
* Creates a new `DidMethodKey` from the supplied keypair.
*
* @param {DockKeyPair} keypair
* @param {DockKeypair} keypair
* @returns {this}
*/
static fromKeypair(keypair) {
Expand Down
Loading

0 comments on commit 47fdd23

Please sign in to comment.