Skip to content

Commit

Permalink
feat: add examples related to confidential assets
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantasdeveloper committed Feb 7, 2024
1 parent 41f7646 commit 22388b5
Show file tree
Hide file tree
Showing 14 changed files with 573 additions and 108 deletions.
21 changes: 20 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@typescript-eslint/eslint-plugin": "6.5.0",
"@typescript-eslint/parser": "6.5.0",
"cross-env": "^7.0.3",
"@zerollup/ts-transform-paths": "^1.7.11",
"cz-conventional-changelog": "^3.0.2",
"eslint": "^8.48.0",
"eslint-config-prettier": "^9.0.0",
Expand All @@ -53,10 +54,28 @@
},
"dependencies": {
"@polymeshassociation/local-signing-manager": "^3.1.0",
"@polymeshassociation/polymesh-sdk": "23.0.0",
"@polymeshassociation/polymesh-sdk": "24.0.0-confidential-assets.1",
"bluebird": "^3.7.2",
"dotenv": "^16.0.3"
},
"resolutions": {
"@polkadot/keyring": "12.4.2",
"@polkadot/api-augment": "10.9.1",
"@polkadot/api-base": "10.9.1",
"@polkadot/api-derive": "10.9.1",
"@polkadot/rpc-augment": "10.9.1",
"@polkadot/rpc-core": "10.9.1",
"@polkadot/rpc-provider": "10.9.1",
"@polkadot/types": "10.9.1",
"@polkadot/types-augment": "10.9.1",
"@polkadot/types-codec": "10.9.1",
"@polkadot/types-create": "10.9.1",
"@polkadot/types-known": "10.9.1",
"@polkadot/util": "12.4.2",
"@polkadot/networks": "12.4.2",
"@polkadot/util-crypto": "12.4.2",
"@polkadot/x-bigint": "12.4.2"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
Expand Down
10 changes: 5 additions & 5 deletions src/common/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export async function getClient(mnemonic?: string): Promise<Polymesh> {
api = await Polymesh.connect({
/* eslint-disable @typescript-eslint/no-non-null-assertion */
nodeUrl: process.env.POLYMESH_NODE_URL!,
middlewareV2: {
link: process.env.MIDDLEWARE_LINK!,
key: process.env.MIDDLEWARE_KEY!,
/* eslint-enable @typescript-eslint/no-non-null-assertion */
},
// middlewareV2: {
// link: process.env.MIDDLEWARE_LINK!,
// key: process.env.MIDDLEWARE_KEY!,
// /* eslint-enable @typescript-eslint/no-non-null-assertion */
// },
signingManager: localSigningManager,
});
}
Expand Down
59 changes: 59 additions & 0 deletions src/examples/addCAVenueFiltering.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { BigNumber } from '@polymeshassociation/polymesh-sdk';
import { ConfidentialAsset } from '@polymeshassociation/polymesh-sdk/types';

import { getClient } from '~/common/client';
import { toHumanObject } from '~/common/utils';

/**
* Retrieves all relevant info about a venue filtering for an asset
*/
export async function getVenueFiltering(asset: ConfidentialAsset) {
console.log('\n----------------------------------------------------------------------\n');
console.log('\n💡 Fetching venue filtering details : ');

const venueFilteringDetails = await asset.getVenueFilteringDetails();

console.log(
`\nℹ️ Venue Filtering details - ${JSON.stringify(toHumanObject(venueFilteringDetails))}`
);
}

/*
This script showcases how to get a confidential asset instance with its ID and get all the relevant info about it
*/
(async (): Promise<void> => {
console.log('Connecting to the node...\n');
const account = process.argv[2];
const assetId = process.argv[3];
const allowedVenue = process.argv[4];
if (!account || !process.env[account]) {
throw new Error('Please specify the account to be used to create the venue');
}
if (!assetId || !allowedVenue) {
throw new Error('Please specify asset ID and allowed Venue');
}
const api = await getClient(process.env[account]);

const signingIdentity = await api.getSigningIdentity();
if (!signingIdentity) {
throw new Error(`Account information not found for ${account}`);
}
console.log(
`\n💡 Enabling venue filtering for asset ID - ${assetId} and allowing venue - ${allowedVenue}`
);

const asset = await api.confidentialAssets.getConfidentialAsset({ id: assetId });

const setFilteringTx = await asset.setVenueFiltering({
enabled: true,
allowedVenues: [new BigNumber(allowedVenue)],
});

await setFilteringTx.run();

console.log(`\n✅ Venue filtering has been set for asset ID - ${asset.toHuman()}`);

await getVenueFiltering(asset);

await api.disconnect();
})();
58 changes: 58 additions & 0 deletions src/examples/addConfidentialTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { BigNumber } from '@polymeshassociation/polymesh-sdk';

import { getClient } from '~/common/client';
import { getTransactionDetails } from '~/examples/confidentialTransactionDetails';

/*
This script showcases how to get a confidential asset instance with its ID and get all the relevant info about it
*/
(async (): Promise<void> => {
console.log('Connecting to the node...\n');
const account = process.argv[2];
const sender = process.argv[3];
const receiver = process.argv[4];
const mediator = process.argv[5];
const venueId = process.argv[6];
const assetId = process.argv[7];
if (!account || !process.env[account]) {
throw new Error('Please specify the account to be used to create the venue');
}
if (!sender || !receiver || !mediator || !venueId) {
throw new Error('Please specify all the transaction details');
}

const api = await getClient(process.env[account]);

const signingIdentity = await api.getSigningIdentity();
if (!signingIdentity) {
throw new Error(`Account information not found for ${account}`);
}

console.log(
`\n💡 Creating a new confidential transaction using ${account} DID - ${signingIdentity.did}`
);

const venue = await api.confidentialSettlements.getVenue({ id: new BigNumber(venueId) });

const addTransactionTx = await venue.addTransaction({
legs: [
{
sender,
receiver,
auditors: [],
mediators: [mediator],
assets: [assetId],
},
],
});

const createdTransaction = await addTransactionTx.run();

console.log(
`\n✅ New confidential transaction created with ID - ${createdTransaction.toHuman()}`
);

await getTransactionDetails(createdTransaction);

await api.disconnect();
})();
63 changes: 63 additions & 0 deletions src/examples/affirmConfidentialTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { BigNumber } from '@polymeshassociation/polymesh-sdk';
import {
AffirmConfidentialTransactionParams,
ConfidentialAffirmParty,
} from '@polymeshassociation/polymesh-sdk/types';

import { getClient } from '~/common/client';

/*
This script showcases affirming of confidential transaction
*/
(async (): Promise<void> => {
console.log('Connecting to the node...\n');
const account = process.argv[2];
const transactionId = process.argv[3];
const legId = process.argv[4];
const party = process.argv[5] as ConfidentialAffirmParty;
const assetId = process.argv[6];
const proof = process.argv[7];

if (!account) {
throw new Error('Please specify both account and public key for creating confidential account');
}
const api = await getClient(process.env[account]);

const signingIdentity = await api.getSigningIdentity();
if (!signingIdentity) {
throw new Error(`Account information not found for ${account}`);
}

console.log(`\n💡 Affirming transaction ${transactionId} for ${party}`);

const transaction = await api.confidentialSettlements.getTransaction({
id: new BigNumber(transactionId),
});

let params: AffirmConfidentialTransactionParams;
if (party === ConfidentialAffirmParty.Sender) {
params = {
legId: new BigNumber(legId),
party,
proofs: [
{
asset: assetId,
proof,
},
],
};
} else {
params = {
legId: new BigNumber(legId),
party,
};
}
const affirmTx = await transaction.affirmLeg(params);

await affirmTx.run();

console.log(`\n✅ Transaction ${transactionId} is now affirmed by ${party}`);

// await getTransactionDetails(transaction);
await api.disconnect();
})();
41 changes: 41 additions & 0 deletions src/examples/confidentialAssetDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ConfidentialAsset } from '@polymeshassociation/polymesh-sdk/types';

import { getClient } from '~/common/client';
import { toHumanObject } from '~/common/utils';

/**
* Retrieves all relevant info about a confidential asset
*/
export async function getConfidentialAssetDetails(asset: ConfidentialAsset) {
console.log('\n----------------------------------------------------------------------\n');
console.log('\n💡 Getting all the information about asset ID : ', asset.toHuman());

const [exists, details, auditorInfo, venueFilteringDetails] = await Promise.all([
asset.exists(),
asset.details(),
asset.getAuditors(),
asset.getVenueFilteringDetails(),
]);

console.log('\nℹ️ Exists - ', exists);
console.log('\nℹ️ Details - ', toHumanObject(details));
console.log('\nℹ️ Auditors - ', toHumanObject(auditorInfo));
console.log('\nℹ️ Venue filtering details - ', toHumanObject(venueFilteringDetails));
}
/*
This script showcases how to get a confidential asset instance with its ID and get all the relevant info about it
*/
(async (): Promise<void> => {
console.log('Connecting to the node...\n');
const assetId = process.argv[2];
if (!assetId) {
throw new Error('Please specify asset ID to fetch the details');
}
const api = await getClient();

const asset = await api.confidentialAssets.getConfidentialAsset({ id: assetId });

await getConfidentialAssetDetails(asset);

await api.disconnect();
})();
48 changes: 48 additions & 0 deletions src/examples/confidentialTransactionDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { BigNumber } from '@polymeshassociation/polymesh-sdk';
import { ConfidentialTransaction } from '@polymeshassociation/polymesh-sdk/types';

import { getClient } from '~/common/client';
import { toHumanObject } from '~/common/utils';

/**
* Retrieves all relevant info about a confidential transaction
*/
export async function getTransactionDetails(transaction: ConfidentialTransaction) {
console.log('\n----------------------------------------------------------------------\n');
console.log('\n💡 Getting info about confidential transaction : ', transaction.toHuman());

const [exists, details, legs, involvedParties, legStates, pendingAffirmCount] = await Promise.all(
[
transaction.exists(),
transaction.details(),
transaction.getLegs(),
transaction.getInvolvedParties(),
transaction.getLegStates(),
transaction.getPendingAffirmsCount(),
]
);

console.log('\nℹ️ Exists - ', exists);
console.log('\nℹ️ Details - ', toHumanObject(details));
console.log('\nℹ️ Legs - ', toHumanObject(legs));
console.log('\nℹ️ Involved Parties - ', toHumanObject(involvedParties));
console.log('\nℹ️ Leg states - ', toHumanObject(legStates));
console.log('\nℹ️ Pending Affirmations - ', toHumanObject(pendingAffirmCount));
}
/*
This script showcases how to get a confidential asset instance with its ID and get all the relevant info about it
*/
(async (): Promise<void> => {
console.log('Connecting to the node...\n');
const id = process.argv[2];
if (!id) {
throw new Error('Please specify asset ID to fetch the details');
}
const api = await getClient();

const transaction = await api.confidentialSettlements.getTransaction({ id: new BigNumber(id) });

await getTransactionDetails(transaction);

await api.disconnect();
})();
59 changes: 59 additions & 0 deletions src/examples/createAndMintConfidentialAsset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { BigNumber } from '@polymeshassociation/polymesh-sdk';

import { getClient } from '~/common/client';
import { getConfidentialAssetDetails } from '~/examples/confidentialAssetDetails';

/*
This script showcases Confidential Asset related functionality. It:
- it will create a new asset
- mint into given account
*/
(async (): Promise<void> => {
console.log('Connecting to the node...\n');
const account = process.argv[2];
const ticker = process.argv[3];
const auditor = process.argv[4];
const owner = process.argv[5];
if (!account || !ticker || !auditor) {
throw new Error('Please specify the account, ticker, auditor for creating confidential asset');
}
const api = await getClient(process.env[account]);

const signingIdentity = await api.getSigningIdentity();
if (!signingIdentity) {
throw new Error(`Account information not found for ${account}`);
}
console.log(
`\n💡 Creating a new confidential asset with ticker ${ticker} for ${account} DID - ${signingIdentity.did}`
);

const createTx = await api.confidentialAssets.createConfidentialAsset({
ticker,
data: 'Some Random Data',
auditors: [auditor],
});

const createdConfidentialAsset = await createTx.run();

console.log(`\n✅ New confidential asset created with ID - ${createdConfidentialAsset.id}`);

await getConfidentialAssetDetails(createdConfidentialAsset);

console.log('\n----------------------------------------------------------------------\n');

console.log('\n💡 Minting some assets : ');

const amount = new BigNumber(10000);
const issueTx = await createdConfidentialAsset.issue({
account: owner,
amount,
});

await issueTx.run();

console.log(
`\n✅ Minted a total of ${amount.toString()} assets with ID - ${createdConfidentialAsset.id}`
);

await api.disconnect();
})();
Loading

0 comments on commit 22388b5

Please sign in to comment.