Skip to content

Commit

Permalink
Merge pull request #431 from docknetwork/trust-registry-change-partic…
Browse files Browse the repository at this point in the history
…ipants

`TrustRegistry` change participants + remove outdated code
  • Loading branch information
olegnn authored Jun 14, 2024
2 parents ffcf8bd + 78b999d commit df35df3
Show file tree
Hide file tree
Showing 10 changed files with 530 additions and 297 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,38 @@ jobs:
master:
runs-on: ubuntu-latest
env:
CONFIG_DOCK_NODE_IMAGE_TAG: 'master'
CONFIG_DOCK_NODE_IMAGE_TAG: "master"
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '18.x'
node-version: "18.x"
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test-with-node
mainnet:
runs-on: ubuntu-latest
env:
CONFIG_DOCK_NODE_IMAGE_TAG: 'mainnet'
CONFIG_DOCK_NODE_IMAGE_TAG: "mainnet"
DisableTrustRegistryParticipantsTests: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '18.x'
node-version: "18.x"
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test-with-node
testnet:
runs-on: ubuntu-latest
env:
CONFIG_DOCK_NODE_IMAGE_TAG: 'testnet'
CONFIG_DOCK_NODE_IMAGE_TAG: "testnet"
DisableTrustRegistryParticipantsTests: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '18.x'
node-version: "18.x"
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test-with-node
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": "8.2.0",
"version": "8.3.0",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
93 changes: 93 additions & 0 deletions src/modules/trust-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,84 @@ export default class TrustRegistryModule {
);
}

/**
* Creates a signature for participants change produced by given `convenerOrIssuerOrVerifierDid` using supplied `signingKeyRef`.
*
* @param convenerOrIssuerOrVerifierDid
* @param registryId
* @param participants
* @param signingKeyRef
*/
async signChangeParticipants(
convenerOrIssuerOrVerifierDid,
registryId,
participants,
signingKeyRef,
{ nonce = undefined, didModule = undefined } = {},
) {
const [convenerOrIssuerOrVerifierHexDid, lastNonce] = await this.getActorDidAndNonce(convenerOrIssuerOrVerifierDid, {
nonce,
didModule,
});

return {
sig: convenerOrIssuerOrVerifierHexDid.signStateChange(
this.api,
'ChangeParticipants',
{ data: { registryId, participants }, nonce: lastNonce },
signingKeyRef,
),
nonce: lastNonce,
};
}

/**
* Changes participants in the provided registry.
*
* @param registryId
* @param participants
* @param sigs
* @param waitForFinalization
* @param params
* @returns {Promise<null>}
*/
async changeParticipants(
registryId,
participants,
sigs,
waitForFinalization = true,
params = {},
) {
return this.signAndSend(
await this.changeParticipantsTx(registryId, participants, sigs),
waitForFinalization,
params,
);
}

/**
* Creates a transaction to change participants in the registry.
*
* @param registryId
* @param participants
* @param sigs
* @returns {Promise<null>}
*/
async changeParticipantsTx(registryId, participants, sigs) {
ensureMatchesPattern(
this.constructor.ChangeParticipantsPattern,
participants,
);

return this.module.changeParticipants(
{
registryId,
participants,
},
sigs,
);
}

/**
* Sets schema metadatas in the registry.
*
Expand Down Expand Up @@ -718,6 +796,21 @@ TrustRegistryModule.SchemasUpdatePattern = {
Modify: ModifySchemasPattern,
},
};
TrustRegistryModule.ChangeParticipantsPattern = {
$mapOf: [
DockDidOrDidMethodKeyPattern,
{
$anyOf: [
{
$matchValue: 'Add',
},
{
$matchValue: 'Remove',
},
],
},
],
};
TrustRegistryModule.RegistryQueryByPattern = {
$matchObject: {
issuers: AnyOfOrAllDockDidOrDidMethodKeyPattern,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/did/typed-did/did-method-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class DidMethodKey extends DockDidOrDidMethodKey {
* Creates a new `DidMethodKey` from the supplied keypair.
*
* @param {DockKeypair} keypair
* @returns {this}
* @returns {DidMethodKey}
*/
static fromKeypair(keypair) {
return new this(keypair.publicKey());
Expand All @@ -57,7 +57,7 @@ export default class DidMethodKey extends DockDidOrDidMethodKey {
/**
* Instantiates `DidMethodKey` from a fully qualified did string.
* @param {string} did - fully qualified `did:key:*` string
* @returns {this}
* @returns {DidMethodKey}
*/
static fromQualifiedString(did) {
const { id } = parseDIDUrl(did);
Expand All @@ -79,7 +79,7 @@ export default class DidMethodKey extends DockDidOrDidMethodKey {
/**
* Instantiates `DockDid` from a did method key object received from the substrate side.
* @param {object} key - substrate did method key
* @returns {this}
* @returns {DidMethodKey}
*/
static fromSubstrateValue(did) {
const key = did.asDidMethodKey;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/did/typed-did/dock-did.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class DockDid extends DockDidOrDidMethodKey {
/**
* Generates a random DID.
*
* @returns {this}
* @returns {DockDid}
*/
static random() {
return new this(randomAsHex(DockDIDByteSize));
Expand All @@ -35,7 +35,7 @@ export default class DockDid extends DockDidOrDidMethodKey {
/**
* Instantiates `DockDid` from a fully qualified did string.
* @param {string} did - fully qualified `did:dock:*` string
* @returns {this}
* @returns {DockDid}
*/
static fromQualifiedString(did) {
return new this(getHexIdentifier(did, DockDIDQualifier, DockDIDByteSize));
Expand All @@ -44,7 +44,7 @@ export default class DockDid extends DockDidOrDidMethodKey {
/**
* Instantiates `DockDid` from a did object received from the substrate side.
* @param {object} did - substrate did
* @returns {this}
* @returns {DockDid}
*/
static fromSubstrateValue(did) {
return new this(getHexIdentifier(did.asDid, [], DockDIDByteSize));
Expand Down
52 changes: 8 additions & 44 deletions src/utils/did/typed-did/helpers.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,24 @@
import { getHexIdentifier, isHexWithGivenByteSize } from '../../codec';

import DockDidOrDidMethodKey from './dock-did-or-did-method-key';
import DockDid from './dock-did';
import DidMethodKey from './did-method-key'; // eslint-disable-line

import { DockDIDQualifier, DockDIDByteSize } from '../constants';
import DidMethodKey from "./did-method-key"; // eslint-disable-line

/**
* Takes a DID string, gets the hexadecimal value of that and returns either the `DockDid` or `DidMethodKey` object.
* @param {{ specVersion: number }} api
* @deprecated Use `DockDidOrDidMethodKey.from`/`DockDid.from`/`DidMethodKey.from` instead.
* @todo remove `api` parameter
*
* @param {*} api - unused
* @param {string|DockDid|DidMethodKey|object} did - The DID can be passed as fully qualified DID like `did:dock:<SS58 string>` or
* `did:key:<BS58 public key>` or a 32 byte hex string
* @return {string|DockDid|DidMethodKey} Returns a `string` or `DockDid` or `DidMethodKey` object.
*/
export function typedHexDID(api, did) {
if (api.specVersion < 50) {
return getHexIdentifier(did, DockDIDQualifier, DockDIDByteSize);
} else {
return DockDidOrDidMethodKey.from(did);
}
export function typedHexDID(_api, did) {
return DockDidOrDidMethodKey.from(did);
}

/**
* Create and return a fully qualified Dock DID, i.e. "did:dock:<SS58 string>"
*
* @returns {string} - The DID
*/
export const createNewDockDID = () => DockDid.random().toQualifiedEncodedString();

/**
* Temporary solution for the DID's backward compatibility.
*
* --------------------------------------------------------
*/
// eslint-disable-next-line no-extend-native
Object.defineProperty(String.prototype, 'asDid', {
get() {
if (this.isDid) {
return String(this);
} else {
throw new Error('Not a `Did`');
}
},
});
// eslint-disable-next-line no-extend-native
Object.defineProperty(String.prototype, 'isDid', {
get() {
return isHexWithGivenByteSize(String(this), DockDIDByteSize);
},
});
// eslint-disable-next-line no-extend-native
Object.defineProperty(String.prototype, 'toQualifiedEncodedString', {
value: function toQualifiedEncodedString() {
return new DockDid(this.asDid).toQualifiedEncodedString();
},
});

/**
* --------------------------------------------------------
*/
8 changes: 1 addition & 7 deletions src/utils/did/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,7 @@ export function createDidKey(publicKey, verRel) {
export function createDidSig(did, { keyId }, rawSig) {
const sig = rawSig.toJSON();

if (typeof did === 'string') {
return {
did,
keyId,
sig,
};
} else if (did.isDid) {
if (did.isDid) {
return {
DidSignature: {
did: did.asDid,
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/anoncreds/issuing.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { randomAsHex } from '@polkadot/util-crypto';
import { u8aToHex } from '@polkadot/util';
import { DefaultSchemaParsingOpts, CredentialBuilder, CredentialSchema, initializeWasm } from '@docknetwork/crypto-wasm-ts';
import {
DefaultSchemaParsingOpts, CredentialBuilder, CredentialSchema, initializeWasm,
} from '@docknetwork/crypto-wasm-ts';
import { DockAPI } from '../../../src';
import { createNewDockDID, DidKeypair } from '../../../src/utils/did';
import { DockResolver } from '../../../src/resolver';
Expand Down
Loading

0 comments on commit df35df3

Please sign in to comment.