Skip to content

Commit

Permalink
Merge pull request #382 from docknetwork/trust-registry-did-key
Browse files Browse the repository at this point in the history
`Trust registry` and `did:key`
  • Loading branch information
lovesh authored Jan 17, 2024
2 parents 27a4bf8 + e8c615e commit 932af67
Show file tree
Hide file tree
Showing 68 changed files with 2,455 additions and 1,181 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/integrations-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
mainnet:
runs-on: ubuntu-latest
env:
DisableTrustRegistryTests: true
CONFIG_DOCK_NODE_IMAGE_TAG: 'mainnet'
steps:
- uses: actions/checkout@v2
Expand All @@ -39,6 +40,7 @@ jobs:
testnet:
runs-on: ubuntu-latest
env:
DisableTrustRegistryTests: true
CONFIG_DOCK_NODE_IMAGE_TAG: 'testnet'
steps:
- uses: actions/checkout@v2
Expand Down
9 changes: 4 additions & 5 deletions example/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { u8aToString } from '@polkadot/util';

import { DockAPI } from '../src/index';
import { DockBlobIdByteSize } from '../src/modules/blob';
import { createNewDockDID } from '../src/utils/did';
import { getPublicKeyFromKeyringPair } from '../src/utils/misc';
import { createNewDockDID, DidKeypair } from '../src/utils/did';

// The following can be tweaked depending on where the node is running and what
// account is to be used for sending the transaction.
Expand All @@ -20,7 +19,7 @@ async function writeAndReadBlob(dock, blobValue, dockDID, pair) {
blob: blobValue,
};

await dock.blob.new(blob, dockDID, pair, 1, { didModule: dock.did }, false);
await dock.blob.new(blob, dockDID, pair, { didModule: dock.did }, false);

console.log('Blob written, reading from chain...');

Expand All @@ -33,7 +32,7 @@ async function createAuthorDID(dock, pair) {
console.log('Creating new author DID', dockDID);

// Create an author DID to write with
const publicKey = getPublicKeyFromKeyringPair(pair);
const publicKey = pair.publicKey();
const didKey = new DidKey(publicKey, new VerificationRelationship());
await dock.did.new(dockDID, [didKey], [], false);
return dockDID;
Expand All @@ -57,7 +56,7 @@ async function main() {
const dock = await connectToNode();

// Generate keypair for DID
const pair = dock.keyring.addFromUri(randomAsHex(32));
const pair = DidKeypair.fromApi(dock);

// Generate a DID to be used as author
const dockDID = await createAuthorDID(dock, pair);
Expand Down
22 changes: 11 additions & 11 deletions example/dock-did.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import dock, {
} from '../src/index';
import {
createNewDockDID,
DidKeypair,
} from '../src/utils/did';
import { getPublicKeyFromKeyringPair } from '../src/utils/misc';

// The following can be tweaked depending on where the node is running and what
// account is to be used for sending the transaction.
Expand All @@ -31,17 +31,17 @@ async function removeDID() {
console.log('Removing DID now.');

// Sign the DID removal with this key pair as this is the current key of the DID
const pair = dock.keyring.addFromUri(firstKeySeed, null, 'sr25519');
const pair = new DidKeypair(dock.keyring.addFromUri(firstKeySeed, null, 'sr25519'), 1);

return dock.did.remove(dockDID, dockDID, pair, 1, undefined, false);
return dock.did.remove(dockDID, dockDID, pair, undefined, false);
}

// This function assumes the DID has been written.
async function addServiceEndpoint() {
console.log('Add new service endpoint now.');

// Sign key update with this key pair as this is the current key of the DID
const pair = dock.keyring.addFromUri(firstKeySeed, null, 'sr25519');
const pair = new DidKeypair(dock.keyring.addFromUri(firstKeySeed, null, 'sr25519'), 1);

const spType = new ServiceEndpointType();
spType.setLinkedDomains();
Expand All @@ -50,38 +50,38 @@ async function addServiceEndpoint() {
const spId = u8aToHex(encoder.encode(spIdText));
const originsText = ['https://foo.example.com'];
const origins = originsText.map((u) => u8aToHex(encoder.encode(u)));
return dock.did.addServiceEndpoint(spId, spType, origins, dockDID, dockDID, pair, 1, undefined, false);
return dock.did.addServiceEndpoint(spId, spType, origins, dockDID, dockDID, pair, undefined, false);
}

// This function assumes the DID has been written.
async function addController() {
console.log('Add new controller now.');

// Sign key update with this key pair as this is the current key of the DID
const pair = dock.keyring.addFromUri(firstKeySeed, null, 'sr25519');
const pair = new DidKeypair(dock.keyring.addFromUri(firstKeySeed, null, 'sr25519'), 1);

const newController = createNewDockDID();

return dock.did.addControllers([newController], dockDID, dockDID, pair, 1, undefined, false);
return dock.did.addControllers([newController], dockDID, dockDID, pair, undefined, false);
}

// This function assumes the DID has been written.
async function addKey() {
console.log('Add new key now.');

// Sign key update with this key pair as this is the current key of the DID
const pair = dock.keyring.addFromUri(firstKeySeed, null, 'sr25519');
const pair = new DidKeypair(dock.keyring.addFromUri(firstKeySeed, null, 'sr25519'), 1);

// Update DID key to the following
const newPair = dock.keyring.addFromUri(secondKeySeed, null, 'ed25519');
const newPair = new DidKeypair(dock.keyring.addFromUri(secondKeySeed, null, 'ed25519'), 1);
// the following function will figure out the correct PublicKey type from the `type` property of `newPair`
const newPk = getPublicKeyFromKeyringPair(newPair);
const newPk = newPair.publicKey();

const vr = new VerificationRelationship();
vr.setAuthentication();
const newDidKey = new DidKey(newPk, vr);

return dock.did.addKeys([newDidKey], dockDID, dockDID, pair, 1, undefined, false);
return dock.did.addKeys([newDidKey], dockDID, dockDID, pair, undefined, false);
}

async function getDIDDoc() {
Expand Down
4 changes: 2 additions & 2 deletions example/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { randomAsHex } from '@polkadot/util-crypto';
import ethr from 'ethr-did-resolver';
import { DockAPI } from '../src/index';
import { createNewDockDID, NoDIDError } from '../src/utils/did';
import { createNewDockDID, NoDIDError, DidKeypair } from '../src/utils/did';
import {
DIDResolver, DIDKeyResolver, UniversalResolver, WILDCARD, DockDIDResolver,
} from '../src/resolver';
Expand Down Expand Up @@ -54,7 +54,7 @@ async function createDockDID() {
dock.setAccount(account);

const dockDID = createNewDockDID();
const pair = dock.keyring.addFromUri(randomAsHex(32), null, 'sr25519');
const pair = new DidKeypair(dock.keyring.addFromUri(randomAsHex(32), null, 'sr25519'), 1);
await registerNewDIDUsingPair(dock, dockDID, pair);
return dockDID;
}
Expand Down
27 changes: 13 additions & 14 deletions example/revocation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { randomAsHex } from '@polkadot/util-crypto';

import dock from '../src/index';
import { createNewDockDID } from '../src/utils/did';
import { getPublicKeyFromKeyringPair } from '../src/utils/misc';
import { createNewDockDID, DidKeypair, typedHexDID } from '../src/utils/did';

import {
OneOfPolicy,
Expand All @@ -21,17 +20,17 @@ const registryId = createRandomRegistryId();
const controllerDID = createNewDockDID();
const controllerSeed = randomAsHex(32);

// Create a list of controllers
const controllers = new Set();
controllers.add(controllerDID);

// Create a registry policy
const policy = new OneOfPolicy(controllers);

// Create revoke IDs
const revokeId = randomAsHex(32);

async function createRegistry() {
// Create a list of controllers
const controllers = new Set();

controllers.add(typedHexDID(dock.api, controllerDID));

// Create a registry policy
const policy = new OneOfPolicy(controllers);
console.log(`Creating a registry with owner DID (${controllerDID}) with policy type:`, policy.constructor.name);
await dock.revocation.newRegistry(registryId, policy, false, false);
console.log('Created registry');
Expand All @@ -40,20 +39,20 @@ async function createRegistry() {
async function removeRegistry(pair) {
console.log('Removing registry...');

await dock.revocation.removeRegistryWithOneOfPolicy(registryId, controllerDID, pair, 1, { didModule: dock.did }, false);
await dock.revocation.removeRegistryWithOneOfPolicy(registryId, controllerDID, pair, { didModule: dock.did }, false);

console.log('Registry removed. All done.');
}

async function unrevoke(pair) {
console.log('Trying to undo the revocation (unrevoke) of id:', revokeId);
const extrinsic = await dock.revocation.unrevokeCredentialWithOneOfPolicy(registryId, revokeId, controllerDID, pair, 1, { didModule: dock.did }, false);
const extrinsic = await dock.revocation.unrevokeCredentialWithOneOfPolicy(registryId, revokeId, controllerDID, pair, { didModule: dock.did }, false);
await extrinsic;
}

async function revoke(pair) {
console.log('Trying to revoke id:', revokeId);
const extrinsic = await dock.revocation.revokeCredentialWithOneOfPolicy(registryId, revokeId, controllerDID, pair, 1, { didModule: dock.did }, false);
const extrinsic = await dock.revocation.revokeCredentialWithOneOfPolicy(registryId, revokeId, controllerDID, pair, { didModule: dock.did }, false);
await extrinsic;
}

Expand All @@ -65,13 +64,13 @@ async function main() {
dock.setAccount(account);

// The DID should be written before creating a registry
const pair = dock.keyring.addFromUri(controllerSeed, null, 'sr25519');
const pair = new DidKeypair(dock.keyring.addFromUri(controllerSeed, null, 'sr25519'), 1);

// Set our controller DID and associated keypair to be used for generating proof
console.log(`Creating controller DID (${controllerDID}) using sr25519 pair from seed (${controllerSeed})...`);

// The controller is same as the DID
const publicKey = getPublicKeyFromKeyringPair(pair);
const publicKey = pair.publicKey();
const didKey = new DidKey(publicKey, new VerificationRelationship());
await dock.did.new(controllerDID, [didKey], [], false);

Expand Down
11 changes: 5 additions & 6 deletions example/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { randomAsHex } from '@polkadot/util-crypto';
import Schema from '../src/modules/schema';

import { DockAPI } from '../src/index';
import { createNewDockDID } from '../src/utils/did';
import { getPublicKeyFromKeyringPair } from '../src/utils/misc';
import { createNewDockDID, DidKeypair } from '../src/utils/did';
import VerifiableCredential from '../src/verifiable-credential';
import { Ed25519VerKeyName } from '../src/utils/vc/crypto/constants';
import { getKeyDoc } from '../src/utils/vc/helpers';
Expand All @@ -27,7 +26,7 @@ async function createAuthorDID(dock, pair) {
console.log('Creating new author DID', dockDID);

// Create an author DID to write with
const publicKey = getPublicKeyFromKeyringPair(pair);
const publicKey = pair.publicKey();
const didKey = new DidKey(publicKey, new VerificationRelationship());
await dock.did.new(dockDID, [didKey], [], false);
return dockDID;
Expand All @@ -47,7 +46,7 @@ async function main() {
const subjectKeySeed = randomAsHex(32);

// Generate first key with this seed. The key type is Sr25519
const pair = dock.keyring.addFromUri(keySeed, null, 'ed25519');
const pair = new DidKeypair(dock.keyring.addFromUri(keySeed, null, 'ed25519'), 1);

// Generate a DID to be used as author
const dockDID = await createAuthorDID(dock, pair);
Expand All @@ -59,7 +58,7 @@ async function main() {
Ed25519VerKeyName,
);

const subjectPair = dock.keyring.addFromUri(subjectKeySeed);
const subjectPair = new DidKeypair(dock.keyring.addFromUri(subjectKeySeed), 1);
const subjectDID = createNewDockDID();
await registerNewDIDUsingPair(dock, subjectDID, subjectPair);

Expand Down Expand Up @@ -88,7 +87,7 @@ async function main() {
console.log('The schema is:', JSON.stringify(schema.toJSON(), null, 2));
console.log('Writing schema to the chain with blob id of', schema.id, '...');

await schema.writeToChain(dock, dockDID, pair, 1, undefined, false);
await schema.writeToChain(dock, dockDID, pair, undefined, false);

console.log(`Schema written, reading from chain (${schema.id})...`);

Expand Down
8 changes: 4 additions & 4 deletions example/vcdm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { randomAsHex } from '@polkadot/util-crypto';
import dock from '../src/index';
import VerifiableCredential from '../src/verifiable-credential';
import VerifiablePresentation from '../src/verifiable-presentation';
import { createNewDockDID } from '../src/utils/did';
import { createNewDockDID, DidKeypair, typedHexDID } from '../src/utils/did';
import { registerNewDIDUsingPair } from '../tests/integration/helpers';
import { createRandomRegistryId, OneOfPolicy, buildDockCredentialStatus } from '../src/utils/revocation';
import { FullNodeEndpoint, TestAccountURI } from '../tests/test-constants';
Expand Down Expand Up @@ -44,17 +44,17 @@ async function setup() {

// Register issuer DID
console.log('Registering issuer DID...');
const pair = dock.keyring.addFromUri(issuerSeed, null, 'ed25519');
const pair = new DidKeypair(dock.keyring.addFromUri(issuerSeed, null, 'ed25519'), 1);
await registerNewDIDUsingPair(dock, issuerDID, pair);

// Register holder DID
console.log('Registering holder DID...');
const pair1 = dock.keyring.addFromUri(holderSeed, null, 'ed25519');
const pair1 = new DidKeypair(dock.keyring.addFromUri(holderSeed, null, 'ed25519'), 1);
await registerNewDIDUsingPair(dock, holderDID, pair1);

// Create a new policy
const policy = new OneOfPolicy();
policy.addOwner(issuerDID);
policy.addOwner(typedHexDID(dock.api, issuerDID));

// Add a new revocation registry with above policy
console.log('Creating registry...');
Expand Down
23 changes: 12 additions & 11 deletions scripts/hit_1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { randomAsHex } from '@polkadot/util-crypto';
import dock, { DockAPI } from '../src/index';
import {
createNewDockDID,
createNewDockDID, DidKeypair,
} from '../src/utils/did';
import { getPublicKeyFromKeyringPair } from '../src/utils/misc';
import { sendBatch } from './helpers';
Expand Down Expand Up @@ -47,7 +47,7 @@ async function sendOnChainDIDTxns(count, waitForFinalization = true) {
const didKey = new DidKey(publicKey, new VerificationRelationship());
const tx = dock.did.createNewOnchainTx(did, [didKey], []);
txs.push(tx);
didPairs.push([did, pair]);
didPairs.push([did, new DidKeypair(pair, 1)]);
}

await sendBatch(dock, txs, account.address, waitForFinalization);
Expand Down Expand Up @@ -77,7 +77,7 @@ async function sendAddKeyTxns(count, didPairs) {
const newPair = dock.keyring.addFromUri(seed, null, 'sr25519');
const publicKey = getPublicKeyFromKeyringPair(newPair);
const didKey = new DidKey(publicKey, new VerificationRelationship());
const tx = await dock.did.createAddKeysTx([didKey], did, did, currentPair, 1);
const tx = await dock.did.createAddKeysTx([didKey], did, did, currentPair);
txs.push(tx);
j++;
}
Expand Down Expand Up @@ -105,7 +105,7 @@ async function sendAddControllerTxns(count, didPairs) {
while (txs.length < count) {
const did = didPairs[j][0];
const currentPair = didPairs[j][1];
const tx = await dock.did.createAddControllersTx([createNewDockDID()], did, did, currentPair, 1);
const tx = await dock.did.createAddControllersTx([createNewDockDID()], did, did, currentPair);
txs.push(tx);
j++;
}
Expand Down Expand Up @@ -133,7 +133,7 @@ async function sendRemoveTxns(count, didPairs, waitForFinalization = true) {
while (txs.length < count) {
const did = didPairs[j][0];
const currentPair = didPairs[j][1];
const tx = await dock.did.createRemoveTx(did, did, currentPair, 1);
const tx = await dock.did.createRemoveTx(did, did, currentPair);
txs.push(tx);
j++;
}
Expand Down Expand Up @@ -166,7 +166,7 @@ async function sendBlobTxns(count, didPairs) {
id: blobId,
blob: randomAsHex(995),
};
const tx = await dock.blob.createNewTx(blob, did, pair, 1, { didModule: dock.did });
const tx = await dock.blob.createNewTx(blob, did, pair, { didModule: dock.did });
txs.push(tx);
blobIds.push(blobId);
j++;
Expand Down Expand Up @@ -207,15 +207,16 @@ async function sendAnchorTxns(count) {
}

async function runOnce() {
let didPairs = await sendOnChainDIDTxns(1950);
const count = 1000;
let didPairs = await sendOnChainDIDTxns(count);
console.log('');
didPairs = await sendAddKeyTxns(1950, didPairs);
didPairs = await sendAddKeyTxns(count, didPairs);
console.log('');
await sendAddControllerTxns(1950, didPairs);
await sendAddControllerTxns(count, didPairs);
console.log('');
await sendBlobTxns(1500, didPairs);
await sendBlobTxns(800, didPairs);
console.log('');
await sendRemoveTxns(1950, didPairs);
await sendRemoveTxns(count, didPairs);
}

async function runInLoop(limit) {
Expand Down
Loading

0 comments on commit 932af67

Please sign in to comment.