Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo: Add a script to create a network member and chainspace #179

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions demo/src/welcome-developer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import * as Cord from "@cord.network/sdk";
import { createDid } from "../src/utils/generateDid";
import {
requestJudgement,
setIdentity,
setRegistrar,
provideJudgement,
} from "../src/utils/createRegistrar";
import { addNetworkMember } from "../src/utils/createAuthorities";
/**
Adding a Developer to Network and Creating a Chainspace.
This script facilitates adding a developer as a member of the network. Upon execution,
it provides essential information for developers to start building.

Command to Run the Script:
npx tsx welcome-developer.ts <networkAddress> <sudoAnchorUri> [<authorAnchorUri>] [<chainspaceLimit>]

Input:
networkAddress: Address of the network.
sudoAnchorUri: The sudo anchor URI.
authorAnchorUri (optional): The author anchor URI. (Default value is //Alice)
chainspaceLimit (optional): The chainspace limit. (Default value is 1000)

Output:
Issuer mnemonic
Issuer DID
Chainspace URI
Chainspace Authorization ID
*/

async function main() {

const networkAddress = process.argv[2];
const sudoAnchorUri = process.argv[3];
const authorAnchorUri = process.argv[4] ? process.argv[4] : "//Alice";
const chainspaceLimit = process.argv[5] ? parseInt(process.argv[5]) : 1000;

Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_IN_BLOCK });
await Cord.connect(networkAddress);

console.log(`\nCreating the credentials....`)
const authorityAuthorIdentity = Cord.Utils.Crypto.makeKeypairFromUri(
sudoAnchorUri,
"sr25519"
);

const authorIdentity = Cord.Utils.Crypto.makeKeypairFromUri(
authorAnchorUri,
"ed25519"
);

await addNetworkMember(authorityAuthorIdentity, authorIdentity.address);

const { mnemonic: issuerMnemonic, document: issuerDid } = await createDid(
authorIdentity
);
const issuerKeys = Cord.Utils.Keys.generateKeypairs(
issuerMnemonic,
"ed25519"
);

const { mnemonic: delegateTwoMnemonic, document: delegateTwoDid } =
await createDid(authorIdentity);

const spaceProperties = await Cord.ChainSpace.buildFromProperties(
issuerDid.uri
);

const space = await Cord.ChainSpace.dispatchToChain(
spaceProperties,
issuerDid.uri,
authorIdentity,
async ({ data }) => ({
signature: issuerKeys.authentication.sign(data),
keyType: issuerKeys.authentication.type,
})
);

await Cord.ChainSpace.sudoApproveChainSpace(
authorityAuthorIdentity,
space.uri,
chainspaceLimit
);
const permission: Cord.PermissionType = Cord.Permission.ASSERT;
const spaceAuthProperties =
await Cord.ChainSpace.buildFromAuthorizationProperties(
space.uri,
delegateTwoDid.uri,
permission,
issuerDid.uri
);

const delegateAuth = await Cord.ChainSpace.dispatchDelegateAuthorization(
spaceAuthProperties,
authorIdentity,
space.authorization,
async ({ data }) => ({
signature: issuerKeys.capabilityDelegation.sign(data),
keyType: issuerKeys.capabilityDelegation.type,
})
);

console.log(`\nThe Author URI:\n${authorAnchorUri}`);
console.log(`\nIssuer mnemonic:\n${issuerMnemonic}`);
console.log(`\nIssuer DID:\n${issuerDid.uri}`);
console.log(`\nThe Chanspace URI:\n${space.uri}`);
console.log(`\nThe chainspace Authorization ID:\n${space.authorization}`);
}

main()
.then(() => console.log("\nBye! 👋 👋 👋 "))
.finally(Cord.disconnect);

process.on("SIGINT", async () => {
console.log("\nBye! 👋 👋 👋 \n");
Cord.disconnect();
process.exit(0);
});