Skip to content

Commit

Permalink
Merge pull request #396 from docknetwork/last-trust-registry
Browse files Browse the repository at this point in the history
Align with the latest Trust Registry implementation
  • Loading branch information
cykoder authored Feb 19, 2024
2 parents bdf7acc + 1c9a144 commit 80f7e97
Show file tree
Hide file tree
Showing 7 changed files with 634 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/sdk",
"version": "7.4.0",
"version": "7.5.0",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
123 changes: 96 additions & 27 deletions src/modules/trust-registry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { BTreeSet, BTreeMap } from '@polkadot/types';
import { DidMethodKey, DockDid, typedHexDID } from '../utils/did';
import { u8aToHex } from '@polkadot/util';
import {
DidMethodKey,
DockDid,
typedHexDID,
typedHexDIDFromSubstrate,
} from '../utils/did';
import { isHexWithGivenByteSize } from '../utils/codec';
import { getDidNonce, ensureMatchesPattern } from '../utils/misc';

/**
Expand All @@ -18,6 +25,23 @@ export default class TrustRegistryModule {
this.signAndSend = signAndSend;
}

/**
* Returns Trust Registries information according to the supplied `by` argument.
* @param by
*/
async registriesInfo(by) {
ensureMatchesPattern(this.constructor.TrustRegistriesInfoByPattern, by);

const registriesInfo = await this.api.rpc.trustRegistry.registriesInfoBy(by);

return Object.fromEntries(
[...registriesInfo.entries()].map(([id, info]) => [
id.toString(),
this.parseRegistryInfo(info),
]),
);
}

/**
* Initializes Trust Registry with the supplied parameters.
* @param convenerDid
Expand Down Expand Up @@ -48,11 +72,7 @@ export default class TrustRegistryModule {
signingKeyRef,
nonceOrDidModule,
);
return this.signAndSend(
tx,
waitForFinalization,
params,
);
return this.signAndSend(tx, waitForFinalization, params);
}

/**
Expand Down Expand Up @@ -121,11 +141,7 @@ export default class TrustRegistryModule {
signingKeyRef,
nonceOrDidModule,
);
return this.signAndSend(
tx,
waitForFinalization,
params,
);
return this.signAndSend(tx, waitForFinalization, params);
}

/**
Expand All @@ -149,10 +165,7 @@ export default class TrustRegistryModule {
nonce,
didModule,
});
ensureMatchesPattern(
this.constructor.SchemasUpdatePattern,
schemas,
);
ensureMatchesPattern(this.constructor.SchemasUpdatePattern, schemas);

return convenerOrIssuerOrVerifierHexDid.changeState(
this.api,
Expand Down Expand Up @@ -190,11 +203,7 @@ export default class TrustRegistryModule {
signingKeyRef,
nonceOrDidModule,
);
return this.signAndSend(
tx,
waitForFinalization,
params,
);
return this.signAndSend(tx, waitForFinalization, params);
}

/**
Expand Down Expand Up @@ -259,11 +268,7 @@ export default class TrustRegistryModule {
signingKeyRef,
nonceOrDidModule,
);
return this.signAndSend(
tx,
waitForFinalization,
params,
);
return this.signAndSend(tx, waitForFinalization, params);
}

/**
Expand Down Expand Up @@ -355,6 +360,21 @@ export default class TrustRegistryModule {
const lastNonce = nonce ?? (await getDidNonce(hexDID, nonce, didModule));
return [hexDID, lastNonce];
}

/**
* Parses Trust Registry information received from the substrate side.
* @param registryInfo
*/
parseRegistryInfo({ name, convener, govFramework }) {
return {
name: name.toString(),
convener: typedHexDIDFromSubstrate(
this.api,
convener,
).toQualifiedEncodedString(),
govFramework: u8aToHex(govFramework),
};
}
}

const DockDidOrDidMethodKeyPattern = {
Expand All @@ -365,6 +385,14 @@ const VerificationPricePattern = {
$anyOf: [{ $matchType: 'number' }, { $matchType: 'object' }],
};

const Hex32Pattern = {
$ensure: (value) => {
if (!isHexWithGivenByteSize(value, 32)) {
throw new Error(`Expected 32-byte hex sequence, received: ${value}`);
}
},
};

const VerifiersPattern = {
$instanceOf: BTreeSet,
$iterableOf: DockDidOrDidMethodKeyPattern,
Expand Down Expand Up @@ -426,10 +454,23 @@ const IssuersUpdatePattern = {
],
};

TrustRegistryModule.SchemasUpdatePattern = {
const SetAllSchemasPattern = {
$instanceOf: BTreeMap,
$mapOf: [
{ $matchType: 'string' },
Hex32Pattern,
{
$matchObject: {
issuers: IssuersPattern,
verifiers: VerifiersPattern,
},
},
],
};

const ModifySchemasPattern = {
$instanceOf: BTreeMap,
$mapOf: [
Hex32Pattern,
{
$anyOf: [
{
Expand Down Expand Up @@ -468,3 +509,31 @@ TrustRegistryModule.SchemasUpdatePattern = {
},
],
};

TrustRegistryModule.SchemasUpdatePattern = {
$matchObject: {
Set: SetAllSchemasPattern,
Modify: ModifySchemasPattern,
},
};
TrustRegistryModule.TrustRegistriesInfoByPattern = {
$matchObject: {
Issuer: DockDidOrDidMethodKeyPattern,
Verifier: DockDidOrDidMethodKeyPattern,
SchemaId: Hex32Pattern,
IssuerOrVerifier: DockDidOrDidMethodKeyPattern,
IssuerAndVerifier: DockDidOrDidMethodKeyPattern,
IssuerAndSchemaId: {
$matchIterable: [DockDidOrDidMethodKeyPattern, Hex32Pattern],
},
VerifierAndSchemaId: {
$matchIterable: [DockDidOrDidMethodKeyPattern, Hex32Pattern],
},
IssuerOrVerifierAndSchemaId: {
$matchIterable: [DockDidOrDidMethodKeyPattern, Hex32Pattern],
},
IssuerAndVerifierAndSchemaId: {
$matchIterable: [DockDidOrDidMethodKeyPattern, Hex32Pattern],
},
},
};
104 changes: 104 additions & 0 deletions src/rpc-defs/core-mods-rpc-defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,108 @@ export default {
type: 'Vec<Option<AggregatedDidDetailsResponse>>',
},
},
trustRegistry: {
schemaMetadata: {
params: [
{
name: 'id',
type: 'TrustRegistrySchemaId',
},
],
type: 'BTreeMap<TrustRegistryId, AggregatedTrustRegistrySchemaMetadata>',
},
schemaIssuers: {
params: [
{
name: 'id',
type: 'TrustRegistrySchemaId',
},
],
type: 'BTreeMap<TrustRegistryId, AggregatedTrustRegistrySchemaIssuers>',
},
schemaVerifiers: {
params: [
{
name: 'id',
type: 'TrustRegistrySchemaId',
},
],
type: 'BTreeMap<TrustRegistryId, TrustRegistrySchemaVerifiers>',
},
schemaMetadataInRegistry: {
params: [
{
name: 'id',
type: 'TrustRegistrySchemaId',
},
{
name: 'registry_id',
type: 'TrustRegistryId',
},
],
type: 'Option<AggregatedTrustRegistrySchemaMetadata>',
},
schemaIssuersInRegistry: {
params: [
{
name: 'id',
type: 'TrustRegistrySchemaId',
},
{
name: 'registry_id',
type: 'TrustRegistryId',
},
],
type: 'Option<AggregatedTrustRegistrySchemaIssuers>',
},
schemaVerifiersInRegistry: {
params: [
{
name: 'id',
type: 'TrustRegistrySchemaId',
},
{
name: 'registry_id',
type: 'TrustRegistryId',
},
],
type: 'Option<TrustRegistrySchemaVerifiers>',
},
allRegistrySchemaMetadata: {
params: [
{
name: 'registry_id',
type: 'TrustRegistryId',
},
],
type: 'BTreeMap<TrustRegistrySchemaId, AggregatedTrustRegistrySchemaMetadata>',
},
allRegistrySchemaIssuers: {
params: [
{
name: 'registry_id',
type: 'TrustRegistryId',
},
],
type: 'BTreeMap<TrustRegistrySchemaId, AggregatedTrustRegistrySchemaIssuers>',
},
allRegistrySchemaVerifiers: {
params: [
{
name: 'registry_id',
type: 'TrustRegistryId',
},
],
type: 'BTreeMap<TrustRegistrySchemaId, TrustRegistrySchemaVerifiers>',
},
registriesInfoBy: {
params: [
{
name: 'by',
type: 'TrustRegistriesInfoBy',
},
],
type: 'BTreeMap<TrustRegistryId, TrustRegistryInfo>',
},
},
};
4 changes: 2 additions & 2 deletions src/status-list-credential/status-list2021-credential.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export default class StatusList2021Credential extends VerifiableCredential {

// Caches decoded status list.
Object.defineProperty(this, 'decodedStatusList', {
value: async function decodedStatusList() {
value: function decodedStatusList() {
if (
encodedStatusList === this.credentialSubject.encodedList
&& cachedDecodedStatusList !== void 0
) {
return cachedDecodedStatusList;
} else {
cachedDecodedStatusList = await decodeList(this.credentialSubject);
cachedDecodedStatusList = decodeList(this.credentialSubject);
encodedStatusList = this.credentialSubject.encodedList;

return cachedDecodedStatusList;
Expand Down
11 changes: 11 additions & 0 deletions src/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,17 @@ export class PatternMatcher {

this.check(pattern.$objOf[key], value[key], path.concat(key));
}

/**
* Ensures that supplied value satisfies provided function.
*
* @param pattern
* @param value
* @param path
*/
$ensure(pattern, value) {
pattern.$ensure(value);
}
}

/**
Expand Down
Loading

0 comments on commit 80f7e97

Please sign in to comment.